1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | */ |
---|
14 | |
---|
15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION |
---|
16 | |
---|
17 | #include "collision_handle.h" |
---|
18 | |
---|
19 | |
---|
20 | #include "collision.h" |
---|
21 | #include "collision_event.h" |
---|
22 | |
---|
23 | using namespace std; |
---|
24 | |
---|
25 | |
---|
26 | /** |
---|
27 | * standard constructor |
---|
28 | * @todo this constructor is not jet implemented - do it |
---|
29 | */ |
---|
30 | CollisionHandle::CollisionHandle (WorldEntity* owner, CREngine::CRType type) |
---|
31 | { |
---|
32 | this->setClassID(CL_COLLISION_HANDLE, "CollisionHandle"); |
---|
33 | |
---|
34 | this->owner = owner; |
---|
35 | this->type = type; |
---|
36 | |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | /** |
---|
41 | * standard deconstructor |
---|
42 | */ |
---|
43 | CollisionHandle::~CollisionHandle () |
---|
44 | { |
---|
45 | // delete what has to be deleted here |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * restores the CollisionHandle to its initial state |
---|
50 | */ |
---|
51 | void CollisionHandle::reset() |
---|
52 | { |
---|
53 | this->flushCollisions(); |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | /** |
---|
58 | * add more filter targets to this collision handle |
---|
59 | * @param classID the classid to look for |
---|
60 | */ |
---|
61 | void CollisionHandle::addTarget(long classID) |
---|
62 | { |
---|
63 | // make sure there is no dublicate |
---|
64 | std::vector<long>::iterator it; |
---|
65 | for( it = this->targetList.begin(); it != this->targetList.end(); it++) |
---|
66 | if(*it == classID) |
---|
67 | return; |
---|
68 | |
---|
69 | // add element |
---|
70 | this->targetList.push_back(classID); |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | /** |
---|
75 | * registers a new Collision Object |
---|
76 | * if a there is already a collision object with the same stats |
---|
77 | * registration will be skipped and the last collision object is returned |
---|
78 | */ |
---|
79 | Collision* CollisionHandle::registerCollision(WorldEntity* entityA, WorldEntity* entityB) |
---|
80 | { |
---|
81 | //first get the collision object, multiple sources |
---|
82 | Collision* c; |
---|
83 | if( this->collisionList.empty() || |
---|
84 | ((this->collisionList.back())->getEntityA() != entityA && (this->collisionList.back())->getEntityB() != entityB )) |
---|
85 | c = CREngine::getInstance()->popCollisionObject(); |
---|
86 | else |
---|
87 | c = this->collisionList.back(); |
---|
88 | |
---|
89 | c->collide(entityA, entityB); |
---|
90 | this->collisionList.push_back(c); |
---|
91 | |
---|
92 | return c; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | /** |
---|
97 | * this is the function to be called on a collision event for this handle |
---|
98 | * @param collision the collision objects containing all collision informations |
---|
99 | */ |
---|
100 | void CollisionHandle::registerCollisionEvent(CollisionEvent* collisionEvent) |
---|
101 | { |
---|
102 | // first element only |
---|
103 | Collision* c = this->registerCollision(collisionEvent->getEntityA(), collisionEvent->getEntityB()); |
---|
104 | c->registerCollisionEvent(collisionEvent); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /** |
---|
109 | * flushes the collision list |
---|
110 | */ |
---|
111 | void CollisionHandle::flushCollisions() |
---|
112 | { |
---|
113 | this->collisionList.clear(); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | /** |
---|
118 | * handles the collisions and react according to algorithm |
---|
119 | */ |
---|
120 | void CollisionHandle::handleCollisions() |
---|
121 | { |
---|
122 | |
---|
123 | // collision reaction calculations |
---|
124 | |
---|
125 | // now set state to dispatched |
---|
126 | this->bDispatched = true; |
---|
127 | this->bCollided = false; |
---|
128 | this->flushCollisions(); |
---|
129 | } |
---|