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 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION |
---|
17 | |
---|
18 | |
---|
19 | #include "cr_object_damage.h" |
---|
20 | #include "cr_physics_full_walk.h" |
---|
21 | #include "cr_physics_ground_walk.h" |
---|
22 | |
---|
23 | #include "collision.h" |
---|
24 | #include "collision_event.h" |
---|
25 | #include "collision_filter.h" |
---|
26 | #include "collision_tube.h" |
---|
27 | #include "cr_defs.h" |
---|
28 | |
---|
29 | #include "cr_engine.h" |
---|
30 | |
---|
31 | #include "debug.h" |
---|
32 | |
---|
33 | namespace CoRe |
---|
34 | { |
---|
35 | |
---|
36 | ObjectListDefinition(CREngine); |
---|
37 | |
---|
38 | |
---|
39 | /** |
---|
40 | * standard constructor |
---|
41 | */ |
---|
42 | CREngine::CREngine () |
---|
43 | : BaseObject() |
---|
44 | { |
---|
45 | this->registerObject(this, CREngine::_objectList); |
---|
46 | this->setName("CREngine"); |
---|
47 | |
---|
48 | this->init(); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * the singleton reference to this class |
---|
53 | */ |
---|
54 | CREngine* CREngine::singletonRef = NULL; |
---|
55 | |
---|
56 | /** |
---|
57 | @brief standard deconstructor |
---|
58 | */ |
---|
59 | CREngine::~CREngine () |
---|
60 | { |
---|
61 | CREngine::singletonRef = NULL; |
---|
62 | |
---|
63 | if( this->collisionsUnused.size() != CR_MAX_COLLISIONS) |
---|
64 | PRINTF(0)("CollisionReaction Error: Collision cache size missmatch: %i of %i\n", this->collisionsUnused.size(), CR_MAX_COLLISIONS); |
---|
65 | if( this->collisionEventsUnused.size() != CR_MAX_COLLISION_EVENTS) |
---|
66 | PRINTF(0)("CollisionReaction Error: CollisionEvent cache size missmatch: %i of %i\n", this->collisionEventsUnused.size(), CR_MAX_COLLISION_EVENTS); |
---|
67 | |
---|
68 | this->flushCollisions(); |
---|
69 | |
---|
70 | CollisionIterator it1 = this->collisionsUnused.begin(); |
---|
71 | for(; it1 < this->collisionsUnused.end(); it1++) |
---|
72 | delete *it1; |
---|
73 | CollisionEventIterator it2 = this->collisionEventsUnused.begin(); |
---|
74 | for(; it2 < this->collisionEventsUnused.end(); it2++) |
---|
75 | delete *it2; |
---|
76 | |
---|
77 | this->collisionsUnused.clear(); |
---|
78 | this->collisionEventsUnused.clear(); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | /** |
---|
83 | * inits the CREngine to a working state |
---|
84 | */ |
---|
85 | void CREngine::init() |
---|
86 | { |
---|
87 | // precaching: |
---|
88 | // create a list of Collisions and CollisionEvents for fast object recycling purposes |
---|
89 | for( int i = 0; i < CR_MAX_COLLISIONS; i++) |
---|
90 | this->collisionsUnused.push_back(new Collision()); |
---|
91 | for( int i = 0; i < CR_MAX_COLLISION_EVENTS; i++) |
---|
92 | this->collisionEventsUnused.push_back(new CollisionEvent()); |
---|
93 | |
---|
94 | |
---|
95 | // push the collision reaction object on the list in the right order |
---|
96 | |
---|
97 | // physical reactions |
---|
98 | this->_reactionList[CREngine::CR_PHYSICS_MOMENTUM] = NULL; |
---|
99 | this->_reactionList[CREngine::CR_PHYSICS_STEP_BACK] = NULL; |
---|
100 | this->_reactionList[CREngine::CR_PHYSICS_GROUND_WALK] = new CRPhysicsGroundWalk(); |
---|
101 | this->_reactionList[CREngine::CR_PHYSICS_FULL_WALK] = new CRPhysicsFullWalk(); |
---|
102 | this->_reactionList[CREngine::CR_PHYSICS_DAMAGE] = NULL; |
---|
103 | // object based reactions |
---|
104 | this->_reactionList[CREngine::CR_OBJECT_DAMAGE] = new CRObjectDamage(); |
---|
105 | this->_reactionList[CREngine::CR_OBJECT_PICKUP] = NULL; |
---|
106 | // misc reactions |
---|
107 | this->_reactionList[CREngine::CR_VERTEX_TRAFO] = NULL; |
---|
108 | this->_reactionList[CREngine::CR_SPECIAL_CALLBACK] = NULL; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | /** |
---|
113 | * @returns an instance to a collision object. instead of creating new object this ones can be resycled |
---|
114 | */ |
---|
115 | Collision* CREngine::popCollisionObject() |
---|
116 | { |
---|
117 | if( !this->collisionsUnused.empty()) |
---|
118 | { |
---|
119 | this->collisionsUsed.push_back(this->collisionsUnused.back()); |
---|
120 | this->collisionsUnused.pop_back(); |
---|
121 | return this->collisionsUsed.back(); |
---|
122 | } |
---|
123 | else return NULL; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | /** |
---|
128 | * @return an instanco of a CollisionEvent object. instead of creating a new object this ones can be used and resycled |
---|
129 | */ |
---|
130 | CollisionEvent* CREngine::popCollisionEventObject() |
---|
131 | { |
---|
132 | if( !this->collisionEventsUnused.empty()) |
---|
133 | { |
---|
134 | this->collisionEventsUsed.push_back(this->collisionEventsUnused.back()); |
---|
135 | this->collisionEventsUnused.pop_back(); |
---|
136 | return this->collisionEventsUsed.back(); |
---|
137 | } |
---|
138 | else return NULL; |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | /** |
---|
143 | * handles all collisions in registered in this tube |
---|
144 | */ |
---|
145 | void CREngine::handleCollisions() |
---|
146 | { |
---|
147 | // for all collisions: |
---|
148 | CollisionIterator ci = CollisionTube::getInstance()->begin(); |
---|
149 | for(; ci < CollisionTube::getInstance()->end(); ++ci) |
---|
150 | { |
---|
151 | for( int i = CREngine::CR_PHYSICS_MOMENTUM; i < CREngine::CR_NUMBER; i++) |
---|
152 | { |
---|
153 | if( _reactionList[i] == NULL) |
---|
154 | continue; |
---|
155 | |
---|
156 | // check if entity A or B is subscibed for this event |
---|
157 | if( (*ci)->getEntityA()->isReactive(*(*ci)->getEntityB(), (CREngine::ReactionType)i) || |
---|
158 | (*ci)->getEntityB()->isReactive(*(*ci)->getEntityA(), (CREngine::ReactionType)i)) |
---|
159 | this->_reactionList[i]->reactToCollision(*ci); |
---|
160 | |
---|
161 | (*ci)->reset(); |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | /** |
---|
168 | * flushes all the collision lists and puts them to their initial state |
---|
169 | */ |
---|
170 | void CREngine::flushCollisions() |
---|
171 | { |
---|
172 | CollisionIterator it1 = this->collisionsUsed.begin(); |
---|
173 | for(; it1 < this->collisionsUsed.end(); it1++) |
---|
174 | this->collisionsUnused.push_back(*it1); |
---|
175 | |
---|
176 | CollisionEventIterator it2 = this->collisionEventsUsed.begin(); |
---|
177 | for(; it2 < this->collisionEventsUsed.end(); it2++) |
---|
178 | this->collisionEventsUnused.push_back(*it2); |
---|
179 | |
---|
180 | this->collisionsUsed.clear(); |
---|
181 | this->collisionEventsUsed.clear(); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | void CREngine::debug() |
---|
186 | { |
---|
187 | } |
---|
188 | |
---|
189 | } |
---|