Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/playable.cc @ 6685

Last change on this file since 6685 was 6658, checked in by patrick, 19 years ago

network: debug function for NetworkStream and connection

File size: 4.8 KB
Line 
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 "power_ups/weapon_power_up.h"
25#include "power_ups/param_power_up.h"
26
27
28Playable::Playable()
29{
30  this->setClassID(CL_PLAYABLE, "Playable");
31  PRINTF(4)("PLAYABLE INIT\n");
32
33  this->toList(OM_GROUP_01);
34  this->weaponMan = new WeaponManager(this);
35
36  // the reference to the Current Player is NULL, because we dont have one at the beginning.
37  this->currentPlayer = NULL;
38
39  this->setSynchronized(true);
40}
41
42
43
44Playable::~Playable()
45{
46  delete this->weaponMan;
47
48  if (this->currentPlayer)
49  {
50    PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName());
51
52  }
53}
54
55
56void Playable::addWeapon(Weapon* weapon, int configID, int slotID)
57{
58  this->weaponMan->addWeapon(weapon, configID, slotID);
59
60  this->weaponConfigChanged();
61}
62
63
64void Playable::removeWeapon(Weapon* weapon)
65{
66  this->weaponMan->removeWeapon(weapon);
67
68    this->weaponConfigChanged();
69}
70
71
72void Playable::nextWeaponConfig()
73{
74  this->weaponMan->nextWeaponConfig();
75    this->weaponConfigChanged();
76}
77
78
79void Playable::previousWeaponConfig()
80{
81  this->weaponMan->previousWeaponConfig();
82    this->weaponConfigChanged();
83}
84
85
86void Playable::weaponConfigChanged()
87{
88  if (this->currentPlayer != NULL)
89    this->currentPlayer->weaponConfigChanged();
90}
91
92
93/**
94 * @brief helps us colliding Playables
95 */
96void Playable::collidesWith(WorldEntity* entity, const Vector& location)
97{
98  if (entity->isA(CL_PROJECTILE))
99    this->removeEnergy(entity->getEnergy());
100
101  // EXTREME HACK
102  if (this->getEnergy() == 0.0f)
103    this->deactivateNode();
104}
105
106/**
107 * subscribe to all events the controllable needs
108 * @param player the player that shall controll this Playable
109 */
110bool Playable::subscribePlayer(Player* player)
111{
112  if (this->currentPlayer != NULL)
113  {
114    PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
115    return false;
116  }
117  else
118  {
119    this->currentPlayer = player;
120    /*EventHandler*/
121    EventHandler* evh = EventHandler::getInstance();
122    std::list<int>::iterator ev;
123    for (ev = this->events.begin(); ev != events.end(); ev++)
124      evh->subscribe(player, ES_GAME, (*ev));
125    this->enter();
126    return true;
127  }
128}
129
130/**
131 * unsubscribe from all events the controllable needs
132 * @param player the Player, that controlled this Ship.
133 */
134bool Playable::unsubscribePlayer(Player* player)
135{
136  if (this->currentPlayer != player)
137  {
138    PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName());
139    return false;
140  }
141
142  else
143  {
144    /*EventHandler*/
145    EventHandler* evh = EventHandler::getInstance();
146    std::list<int>::iterator ev;
147    for (ev = this->events.begin(); ev != events.end(); ev++)
148      evh->unsubscribe( ES_GAME, (*ev));
149
150    this->leave();
151    this->currentPlayer = NULL;
152    return true;
153  }
154}
155
156bool Playable::pickup(PowerUp* powerUp)
157{
158  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
159    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
160    WeaponManager* manager = this->getWeaponManager();
161    manager->addWeapon(weapon, 2, -1);
162    return true;
163  }
164  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
165    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
166    switch(ppu->getType()) {
167      case POWERUP_PARAM_HEALTH:
168        this->addEnergy(ppu->getValue());
169        return true;
170      case POWERUP_PARAM_MAX_HEALTH:
171        this->setMaxEnergy(this->getMaxEnergy() + ppu->getValue());
172        return true;
173    }
174  }
175  return false;
176}
177
178/**
179 * add an event to the event list of events this Playable can capture
180 * @param eventType the Type of event to add
181 */
182void Playable::registerEvent(int eventType)
183{
184  this->events.push_back(eventType);
185
186  if (this->currentPlayer != NULL)
187    EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType);
188}
189
190/**
191 * remove an event to the event list this Playable can capture.
192 * @param event the event to unregister.
193 */
194void Playable::unregisterEvent(int eventType)
195{
196  this->events.remove(eventType);
197
198  if (this->currentPlayer != NULL)
199    EventHandler::getInstance()->unsubscribe(ES_GAME, eventType);
200}
201
202
203void  Playable::attachCamera()
204{
205  State::getCamera()->setParentSoft(this);
206  State::getCameraTarget()->setParentSoft(this);
207
208}
209
210
211void  Playable::detachCamera()
212{
213}
Note: See TracBrowser for help on using the repository browser.