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: Silvan Nellen |
---|
13 | co-programmer: Benjamin Knecht |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | #include "playable.h" |
---|
18 | |
---|
19 | #include "weapons/weapon_manager.h" |
---|
20 | #include "event_handler.h" |
---|
21 | #include "player.h" |
---|
22 | #include "state.h" |
---|
23 | |
---|
24 | #include "world_entities/projectiles/projectile.h" |
---|
25 | |
---|
26 | #include "power_ups/weapon_power_up.h" |
---|
27 | #include "power_ups/param_power_up.h" |
---|
28 | |
---|
29 | |
---|
30 | Playable::Playable() |
---|
31 | { |
---|
32 | this->setClassID(CL_PLAYABLE, "Playable"); |
---|
33 | PRINTF(4)("PLAYABLE INIT\n"); |
---|
34 | |
---|
35 | this->toList(OM_GROUP_01); |
---|
36 | this->weaponMan = new WeaponManager(this); |
---|
37 | |
---|
38 | // the reference to the Current Player is NULL, because we dont have one at the beginning. |
---|
39 | this->currentPlayer = NULL; |
---|
40 | |
---|
41 | this->bFire = false; |
---|
42 | |
---|
43 | this->setSynchronized(true); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | Playable::~Playable() |
---|
49 | { |
---|
50 | delete this->weaponMan; |
---|
51 | |
---|
52 | if (this->currentPlayer) |
---|
53 | { |
---|
54 | PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName()); |
---|
55 | |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | void Playable::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
61 | { |
---|
62 | this->weaponMan->addWeapon(weapon, configID, slotID); |
---|
63 | |
---|
64 | this->weaponConfigChanged(); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | void Playable::removeWeapon(Weapon* weapon) |
---|
69 | { |
---|
70 | this->weaponMan->removeWeapon(weapon); |
---|
71 | |
---|
72 | this->weaponConfigChanged(); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | void Playable::nextWeaponConfig() |
---|
77 | { |
---|
78 | this->weaponMan->nextWeaponConfig(); |
---|
79 | this->weaponConfigChanged(); |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | void Playable::previousWeaponConfig() |
---|
84 | { |
---|
85 | this->weaponMan->previousWeaponConfig(); |
---|
86 | this->weaponConfigChanged(); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | void Playable::weaponConfigChanged() |
---|
91 | { |
---|
92 | if (this->currentPlayer != NULL) |
---|
93 | this->currentPlayer->weaponConfigChanged(); |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | /** |
---|
98 | * @brief helps us colliding Playables |
---|
99 | */ |
---|
100 | void Playable::collidesWith(WorldEntity* entity, const Vector& location) |
---|
101 | { |
---|
102 | if (entity->isA(CL_PROJECTILE)) |
---|
103 | this->decreaseHealth(entity->getHealth()); |
---|
104 | |
---|
105 | // EXTREME HACK |
---|
106 | if (this->getHealth() == 0.0f) |
---|
107 | this->deactivateNode(); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * subscribe to all events the controllable needs |
---|
112 | * @param player the player that shall controll this Playable |
---|
113 | */ |
---|
114 | bool Playable::subscribePlayer(Player* player) |
---|
115 | { |
---|
116 | if (this->currentPlayer != NULL) |
---|
117 | { |
---|
118 | PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); |
---|
119 | return false; |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | this->currentPlayer = player; |
---|
124 | /*EventHandler*/ |
---|
125 | EventHandler* evh = EventHandler::getInstance(); |
---|
126 | std::list<int>::iterator ev; |
---|
127 | for (ev = this->events.begin(); ev != events.end(); ev++) |
---|
128 | evh->subscribe(player, ES_GAME, (*ev)); |
---|
129 | this->enter(); |
---|
130 | return true; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * unsubscribe from all events the controllable needs |
---|
136 | * @param player the Player, that controlled this Ship. |
---|
137 | */ |
---|
138 | bool Playable::unsubscribePlayer(Player* player) |
---|
139 | { |
---|
140 | if (this->currentPlayer != player) |
---|
141 | { |
---|
142 | PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); |
---|
143 | return false; |
---|
144 | } |
---|
145 | |
---|
146 | else |
---|
147 | { |
---|
148 | /*EventHandler*/ |
---|
149 | EventHandler* evh = EventHandler::getInstance(); |
---|
150 | std::list<int>::iterator ev; |
---|
151 | for (ev = this->events.begin(); ev != events.end(); ev++) |
---|
152 | evh->unsubscribe( ES_GAME, (*ev)); |
---|
153 | |
---|
154 | this->leave(); |
---|
155 | this->currentPlayer = NULL; |
---|
156 | return true; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | bool Playable::pickup(PowerUp* powerUp) |
---|
161 | { |
---|
162 | if(powerUp->isA(CL_WEAPON_POWER_UP)) { |
---|
163 | Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon(); |
---|
164 | WeaponManager* manager = this->getWeaponManager(); |
---|
165 | return manager->addWeapon(weapon); |
---|
166 | } |
---|
167 | else if(powerUp->isA(CL_PARAM_POWER_UP)) { |
---|
168 | ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp); |
---|
169 | switch(ppu->getType()) { |
---|
170 | case POWERUP_PARAM_HEALTH: |
---|
171 | this->increaseHealth(ppu->getValue()); |
---|
172 | return true; |
---|
173 | case POWERUP_PARAM_MAX_HEALTH: |
---|
174 | this->increaseHealthMax(ppu->getValue()); |
---|
175 | return true; |
---|
176 | } |
---|
177 | } |
---|
178 | return false; |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * add an event to the event list of events this Playable can capture |
---|
183 | * @param eventType the Type of event to add |
---|
184 | */ |
---|
185 | void Playable::registerEvent(int eventType) |
---|
186 | { |
---|
187 | this->events.push_back(eventType); |
---|
188 | |
---|
189 | if (this->currentPlayer != NULL) |
---|
190 | EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * remove an event to the event list this Playable can capture. |
---|
195 | * @param event the event to unregister. |
---|
196 | */ |
---|
197 | void Playable::unregisterEvent(int eventType) |
---|
198 | { |
---|
199 | this->events.remove(eventType); |
---|
200 | |
---|
201 | if (this->currentPlayer != NULL) |
---|
202 | EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * @brief ticks a Playable |
---|
207 | * @param dt: the passed time since the last Tick |
---|
208 | */ |
---|
209 | void Playable::tick(float dt) |
---|
210 | { |
---|
211 | this->weaponMan->tick(dt); |
---|
212 | if (this->bFire) |
---|
213 | weaponMan->fire(); |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | /** |
---|
218 | * @brief processes Playable events. |
---|
219 | * @param event the Captured Event. |
---|
220 | */ |
---|
221 | void Playable::process(const Event &event) |
---|
222 | { |
---|
223 | if( event.type == KeyMapper::PEV_FIRE1) |
---|
224 | this->bFire = event.bPressed; |
---|
225 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
226 | { |
---|
227 | this->nextWeaponConfig(); |
---|
228 | } |
---|
229 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
230 | this->previousWeaponConfig(); |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | |
---|
235 | void Playable::attachCamera() |
---|
236 | { |
---|
237 | State::getCamera()->setParentSoft(this); |
---|
238 | State::getCameraTarget()->setParentSoft(this); |
---|
239 | |
---|
240 | } |
---|
241 | |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | void Playable::detachCamera() |
---|
246 | { |
---|
247 | } |
---|