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: Manuel Leuenberger |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
17 | |
---|
18 | |
---|
19 | #include "power_up.h" |
---|
20 | #include "extendable.h" |
---|
21 | #include "primitive_model.h" |
---|
22 | |
---|
23 | #include "util/loading/resource_manager.h" |
---|
24 | #include "util/loading/load_param.h" |
---|
25 | |
---|
26 | using namespace std; |
---|
27 | |
---|
28 | PowerUp::PowerUp(float r, float g, float b) |
---|
29 | { |
---|
30 | this->setClassID(CL_POWER_UP, "PowerUp"); |
---|
31 | |
---|
32 | this->respawnType = RESPAWN_TIME; |
---|
33 | this->respawnStart = 10; |
---|
34 | this->model = NULL; |
---|
35 | /* if(!PowerUp::sphereModel) {*/ |
---|
36 | |
---|
37 | Model* sphereModel = new PrimitiveModel(PRIM_SPHERE, 7, 5); |
---|
38 | |
---|
39 | this->setModel(sphereModel); |
---|
40 | this->buildObbTree( 4); |
---|
41 | this->sphereMaterial = new Material; |
---|
42 | this->sphereMaterial->setTransparency(.8); |
---|
43 | this->sphereMaterial->setDiffuse(r, g, b); |
---|
44 | this->toList(OM_COMMON); |
---|
45 | |
---|
46 | this->soundSource.setSourceNode(this); |
---|
47 | this->pickupBuffer = NULL; |
---|
48 | this->respawnBuffer = NULL; |
---|
49 | |
---|
50 | this->collider = NULL; |
---|
51 | } |
---|
52 | |
---|
53 | PowerUp::~PowerUp () |
---|
54 | { |
---|
55 | delete this->sphereMaterial; |
---|
56 | if (this->pickupBuffer != NULL) |
---|
57 | ResourceManager::getInstance()->unload(this->pickupBuffer); |
---|
58 | if (this->respawnBuffer != NULL) |
---|
59 | ResourceManager::getInstance()->unload(this->respawnBuffer); |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | void PowerUp::loadParams(const TiXmlElement* root) |
---|
64 | { |
---|
65 | WorldEntity::loadParams(root); |
---|
66 | |
---|
67 | LoadParam(root, "respawnType", this, PowerUp, setRespawnType); |
---|
68 | |
---|
69 | LoadParam(root, "respawnTime", this, PowerUp, setRespawnTime); |
---|
70 | |
---|
71 | LoadParam(root, "pickup-sound", this, PowerUp, loadPickupSound); |
---|
72 | |
---|
73 | LoadParam(root, "respawn-sound", this, PowerUp, loadRespawnSound); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | void PowerUp::loadPickupSound(const char* pickupSound) |
---|
78 | { |
---|
79 | if (this->pickupBuffer != NULL) |
---|
80 | ResourceManager::getInstance()->unload(this->pickupBuffer); |
---|
81 | |
---|
82 | else if (pickupSound != NULL) |
---|
83 | { |
---|
84 | this->pickupBuffer = (SoundBuffer*)ResourceManager::getInstance()->load(pickupSound, WAV); |
---|
85 | if (this->pickupBuffer != NULL) |
---|
86 | { |
---|
87 | PRINTF(4)("Loaded sound %s to Pickup: %s.\n", pickupSound, this->getName()); |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | PRINTF(2)("Failed to load sound %s to pickup %s.\n.", pickupSound, this->getName()); |
---|
92 | } |
---|
93 | } |
---|
94 | else |
---|
95 | this->pickupBuffer = NULL; |
---|
96 | } |
---|
97 | |
---|
98 | void PowerUp::loadRespawnSound(const char* respawnSound) |
---|
99 | { |
---|
100 | if (this->respawnBuffer != NULL) |
---|
101 | ResourceManager::getInstance()->unload(this->respawnBuffer); |
---|
102 | |
---|
103 | else if (respawnSound != NULL) |
---|
104 | { |
---|
105 | this->respawnBuffer = (SoundBuffer*)ResourceManager::getInstance()->load(respawnSound, WAV); |
---|
106 | if (this->respawnBuffer != NULL) |
---|
107 | { |
---|
108 | PRINTF(4)("Loaded sound %s to Pickup: %s.\n", respawnSound, this->getName()); |
---|
109 | } |
---|
110 | else |
---|
111 | { |
---|
112 | PRINTF(2)("Failed to load sound %s to respawn %s.\n.", respawnSound, this->getName()); |
---|
113 | } |
---|
114 | } |
---|
115 | else |
---|
116 | this->respawnBuffer = NULL; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | void PowerUp::collidesWith (WorldEntity* entity, const Vector& location) |
---|
121 | { |
---|
122 | if(this->collider != entity && entity->isA(CL_EXTENDABLE)) |
---|
123 | { |
---|
124 | this->collider = entity; |
---|
125 | if(dynamic_cast<Extendable*>(entity)->pickup(this)) |
---|
126 | { |
---|
127 | if(pickupBuffer != NULL) |
---|
128 | this->soundSource.play(this->pickupBuffer); |
---|
129 | |
---|
130 | switch(respawnType) { |
---|
131 | case RESPAWN_NONE: |
---|
132 | this->toList(OM_DEAD); |
---|
133 | break; |
---|
134 | case RESPAWN_TIME: |
---|
135 | this->toList(OM_DEAD_TICK); |
---|
136 | this->respawnTime = this->respawnStart; |
---|
137 | break; |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | void PowerUp::tick(float dt) { |
---|
144 | if(this->getOMListNumber() != OM_COMMON) { |
---|
145 | this->respawnTime -= dt; |
---|
146 | if(this->respawnTime <= 0) |
---|
147 | { |
---|
148 | this->toList(OM_COMMON); |
---|
149 | this->collider = NULL; |
---|
150 | if (likely(this->respawnBuffer != NULL)) |
---|
151 | this->soundSource.play(this->respawnBuffer); |
---|
152 | |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | void PowerUp::draw() const |
---|
158 | { |
---|
159 | if(this->model != NULL) { |
---|
160 | glMatrixMode(GL_MODELVIEW); |
---|
161 | glPushMatrix(); |
---|
162 | glTranslatef (this->getAbsCoor ().x, |
---|
163 | this->getAbsCoor ().y, |
---|
164 | this->getAbsCoor ().z); |
---|
165 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
166 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
167 | this->model->draw(); |
---|
168 | glPopMatrix(); |
---|
169 | } |
---|
170 | this->sphereMaterial->select(); |
---|
171 | WorldEntity::draw(); |
---|
172 | } |
---|
173 | |
---|
174 | const char* PowerUp::respawnTypes[] = { |
---|
175 | "none", |
---|
176 | "time" |
---|
177 | }; |
---|
178 | |
---|
179 | void PowerUp::setRespawnType(const char* type) |
---|
180 | { |
---|
181 | for(int i = 0; i < RESPAWN_size; ++i) { |
---|
182 | if(!strcmp(type, respawnTypes[i])) { |
---|
183 | this->respawnType = (PowerUpRespawn)i; |
---|
184 | break; |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | void PowerUp::setRespawnTime(const float respawnTime) |
---|
190 | { |
---|
191 | this->respawnStart = respawnTime; |
---|
192 | } |
---|
193 | |
---|
194 | |
---|
195 | /******************************************************************************************** |
---|
196 | NETWORK STUFF |
---|
197 | ********************************************************************************************/ |
---|
198 | |
---|
199 | |
---|
200 | /** |
---|
201 | * data copied in data will bee sent to another host |
---|
202 | * @param data pointer to data |
---|
203 | * @param maxLength max length of data |
---|
204 | * @return the number of bytes writen |
---|
205 | */ |
---|
206 | int PowerUp::readState( byte * data, int maxLength ) |
---|
207 | { |
---|
208 | SYNCHELP_WRITE_BEGIN(); |
---|
209 | SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_PU_WE_STATE ); |
---|
210 | return SYNCHELP_WRITE_N; |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | /** |
---|
215 | * Writes data from network containing information about the state |
---|
216 | * @param data pointer to data |
---|
217 | * @param length length of data |
---|
218 | * @param sender hostID of sender |
---|
219 | */ |
---|
220 | int PowerUp::writeState( const byte * data, int length, int sender ) |
---|
221 | { |
---|
222 | SYNCHELP_READ_BEGIN(); |
---|
223 | SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_PU_WE_STATE ); |
---|
224 | return SYNCHELP_READ_N; |
---|
225 | } |
---|
226 | |
---|