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