Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/power_ups/weapon_power_up.cc @ 6988

Last change on this file since 6988 was 6973, checked in by bensch, 18 years ago

orxonox/trunk: added powerups

File size: 3.1 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: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17
18#include "weapon_power_up.h"
19#include "factory.h"
20#include "state.h"
21#include "list.h"
22#include "network_game_manager.h"
23
24#include "primitive_model.h"
25
26#include "factory.h"
27#include "load_param.h"
28
29using namespace std;
30
31CREATE_FACTORY(WeaponPowerUp, CL_WEAPON_POWER_UP);
32
33WeaponPowerUp::WeaponPowerUp () : PowerUp(1.0, 1.0, 0.0)
34{
35  this->init();
36}
37
38WeaponPowerUp::WeaponPowerUp(const TiXmlElement* root) : PowerUp(1.0, 1.0, 0.0)
39{
40  this->init();
41  if( root != NULL)
42    this->loadParams(root);
43}
44
45
46WeaponPowerUp::~WeaponPowerUp ()
47{
48}
49
50
51void WeaponPowerUp::init()
52{
53  this->setClassID(CL_WEAPON_POWER_UP, "WeaponPowerUp");
54  this->weaponXML = NULL;
55  this->weapon = NULL;
56}
57
58
59void WeaponPowerUp::loadParams(const TiXmlElement* root)
60{
61  PowerUp::loadParams(root);
62  const TiXmlElement* elem = root->FirstChildElement("weapon");
63  if(elem != NULL && (elem = elem->FirstChildElement()) != NULL)
64  {
65    this->weaponXML = elem;
66    newWeapon();
67  }
68  else
69  {
70    LoadParam(root, "weaponID", this, WeaponPowerUp, setWeaponClass);
71  }
72}
73
74Weapon* WeaponPowerUp::getWeapon()
75{
76  return this->weapon;
77}
78
79bool WeaponPowerUp::process(WeaponManager* manager)
80{
81  if(manager->addWeapon(this->weapon)) {
82    newWeapon();
83  }
84  else {
85    manager->increaseAmmunition(this->weapon->getProjectileType(), this->weapon->getEnergy());
86  }
87  return true;
88}
89
90void WeaponPowerUp::newWeapon()
91{
92  this->weapon = dynamic_cast<Weapon*>((weaponXML == NULL)
93      ? Factory::fabricate(static_cast<ClassID>(this->weapon->getLeafClassID()))
94      : Factory::fabricate((TiXmlElement*)this->getXmlElem()->FirstChildElement("weapon")));
95  this->model = this->weapon->getModel(0);
96}
97
98void WeaponPowerUp::setWeaponClass(const char* name)
99{
100  this->weapon = dynamic_cast<Weapon*>(Factory::fabricate(name));
101  this->model = this->weapon->getModel(0);
102}
103
104int WeaponPowerUp::writeBytes( const byte * data, int length, int sender )
105{
106  setRequestedSync( false );
107  setIsOutOfSync( false );
108
109  SYNCHELP_READ_BEGIN();
110
111  SYNCHELP_READ_FKT( PowerUp::writeState, NWT_WPU_WE_STATE );
112
113  //TODO: sync weapon class ( see loadParams )
114
115  return SYNCHELP_READ_N;
116}
117
118
119
120int WeaponPowerUp::readBytes( byte * data, int maxLength, int * reciever )
121{
122  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
123  {
124    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
125    setRequestedSync( true );
126  }
127
128  int rec = this->getRequestSync();
129  if ( rec > 0 )
130  {
131    *reciever = rec;
132
133    SYNCHELP_WRITE_BEGIN();
134
135    SYNCHELP_WRITE_FKT( PowerUp::readState, NWT_WPU_WE_STATE );
136
137    //TODO: sync weapon class ( see loadParams )
138
139    return SYNCHELP_WRITE_N;
140  }
141
142  *reciever = 0;
143  return 0;
144}
Note: See TracBrowser for help on using the repository browser.