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 | |
---|
20 | #include "collision.h" |
---|
21 | #include "collision_event.h" |
---|
22 | #include "collision_handle.h" |
---|
23 | #include "cr_defs.h" |
---|
24 | |
---|
25 | #include "cr_engine.h" |
---|
26 | |
---|
27 | using namespace std; |
---|
28 | |
---|
29 | |
---|
30 | /** |
---|
31 | * standard constructor |
---|
32 | */ |
---|
33 | CREngine::CREngine () |
---|
34 | : BaseObject() |
---|
35 | { |
---|
36 | this->setClassID(CL_CR_ENGINE, "CREngine"); |
---|
37 | this->setName("CREngine"); |
---|
38 | |
---|
39 | this->init(); |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | * the singleton reference to this class |
---|
44 | */ |
---|
45 | CREngine* CREngine::singletonRef = NULL; |
---|
46 | |
---|
47 | /** |
---|
48 | @brief standard deconstructor |
---|
49 | */ |
---|
50 | CREngine::~CREngine () |
---|
51 | { |
---|
52 | CREngine::singletonRef = NULL; |
---|
53 | |
---|
54 | if( this->collisionsUnused.size() != CR_MAX_COLLISIONS) |
---|
55 | PRINTF(0)("CollisionReaction Error: Collision cache size missmatch: %i of %i\n", this->collisionsUnused.size(), CR_MAX_COLLISIONS); |
---|
56 | if( this->collisionEventsUnused.size() != CR_MAX_COLLISION_EVENTS) |
---|
57 | PRINTF(0)("CollisionReaction Error: CollisionEvent cache size missmatch: %i of %i\n", this->collisionEventsUnused.size(), CR_MAX_COLLISION_EVENTS); |
---|
58 | |
---|
59 | this->reset(); |
---|
60 | |
---|
61 | vector<Collision*>::iterator it1 = this->collisionsUnused.begin(); |
---|
62 | for(; it1 < this->collisionsUnused.end(); it1++) |
---|
63 | delete *it1; |
---|
64 | vector<CollisionEvent*>::iterator it2 = this->collisionEventsUnused.begin(); |
---|
65 | for(; it2 < this->collisionEventsUnused.end(); it2++) |
---|
66 | delete *it2; |
---|
67 | |
---|
68 | this->collisionsUnused.clear(); |
---|
69 | this->collisionEventsUnused.clear(); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * inits the CREngine to a working state |
---|
74 | */ |
---|
75 | void CREngine::init() |
---|
76 | { |
---|
77 | // create a list of Collision events (precaching) |
---|
78 | for( int i = 0; i < CR_MAX_COLLISIONS; i++) |
---|
79 | this->collisionsUnused.push_back(new Collision()); |
---|
80 | for( int i = 0; i < CR_MAX_COLLISION_EVENTS; i++) |
---|
81 | this->collisionEventsUnused.push_back(new CollisionEvent()); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | /** |
---|
86 | * flushes the CollisionHandles and restores the CREngine to the initial state |
---|
87 | */ |
---|
88 | void CREngine::reset() |
---|
89 | { |
---|
90 | // first clear all CollisionHandles |
---|
91 | |
---|
92 | vector<CollisionHandle*>::iterator it = this->collisionHandles.begin(); |
---|
93 | for(; it < this->collisionHandles.end(); it++) |
---|
94 | { |
---|
95 | (*it)->reset(); |
---|
96 | delete *it; |
---|
97 | } |
---|
98 | |
---|
99 | this->collisionHandles.clear(); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | /** |
---|
104 | * subscribes a WorldEntity for a CollisionReaction |
---|
105 | * @param owner: the WE to subscribe |
---|
106 | * @param type: the type of collision reaction to perform |
---|
107 | * @return the newly created CollisionHandle |
---|
108 | */ |
---|
109 | CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, CRType type) |
---|
110 | { |
---|
111 | CollisionHandle* ch = new CollisionHandle(owner, type); |
---|
112 | this->collisionHandles.push_back(ch); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | /** |
---|
117 | * unsubscribe reaction from the reaction list |
---|
118 | * @param collisionHandle the CollisionHandle to remove |
---|
119 | * @param returns true if worked collrectly |
---|
120 | */ |
---|
121 | bool CREngine::unsubscribeReaction(CollisionHandle* collisionHandle) |
---|
122 | { |
---|
123 | std::vector<CollisionHandle*>::iterator it; |
---|
124 | for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++) |
---|
125 | { |
---|
126 | if( *it == collisionHandle) |
---|
127 | { |
---|
128 | this->collisionHandles.erase(it); |
---|
129 | delete collisionHandle; |
---|
130 | return true; |
---|
131 | } |
---|
132 | } |
---|
133 | return false; |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | /** |
---|
138 | * processes the collisions by calling the EventHandlers |
---|
139 | */ |
---|
140 | void CREngine::handleCollisions() |
---|
141 | { |
---|
142 | std::vector<CollisionHandle*>::iterator it; |
---|
143 | for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++) |
---|
144 | { |
---|
145 | if( (*it)->isCollided()) |
---|
146 | { |
---|
147 | (*it)->handleCollisions(); |
---|
148 | } |
---|
149 | } |
---|
150 | this->flushCollisions(); |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | /** |
---|
155 | * flushes all the collision lists and puts them to their initial state |
---|
156 | */ |
---|
157 | void CREngine::flushCollisions() |
---|
158 | { |
---|
159 | vector<Collision*>::iterator it = this->collisionsUsed.begin(); |
---|
160 | for(; it < this->collisionsUsed.end(); it++) |
---|
161 | this->collisionsUnused.push_back(*it); |
---|
162 | |
---|
163 | this->collisionsUsed.clear(); |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | void CREngine::debug() |
---|
168 | { |
---|
169 | |
---|
170 | } |
---|
171 | |
---|