Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

health

File size: 3.2 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(const TiXmlElement* root) : PowerUp(1.0, 1.0, 0.0)
34{
35  this->init();
36  if( root != NULL)
37    this->loadParams(root);
38}
39
40
41WeaponPowerUp::~WeaponPowerUp ()
42{
43}
44
45
46void WeaponPowerUp::init()
47{
48  this->setClassID(CL_WEAPON_POWER_UP, "WeaponPowerUp");
49  this->loadPickupSound("sound/powerups/whats this2.wav");
50
51  this->weaponXML = NULL;
52  this->weapon = NULL;
53}
54
55
56void WeaponPowerUp::loadParams(const TiXmlElement* root)
57{
58  PowerUp::loadParams(root);
59  const TiXmlElement* elem = root->FirstChildElement("weapon");
60  if(elem != NULL && (elem = elem->FirstChildElement()) != NULL)
61  {
62    this->weaponXML = elem;
63    newWeapon();
64  }
65  else
66  {
67    LoadParam(root, "weaponID", this, WeaponPowerUp, setWeaponClass);
68  }
69}
70
71Weapon* WeaponPowerUp::getWeapon()
72{
73  return this->weapon;
74}
75
76bool WeaponPowerUp::process(WeaponManager* manager)
77{
78  if(manager->addWeapon(this->weapon)) {
79    newWeapon();
80  }
81  else {
82    manager->increaseAmmunition(this->weapon->getProjectileType(), this->weapon->getEnergy());
83  }
84  return true;
85}
86
87void WeaponPowerUp::newWeapon()
88{
89  this->weapon = dynamic_cast<Weapon*>((weaponXML == NULL)
90      ? Factory::fabricate(static_cast<ClassID>(this->weapon->getLeafClassID()))
91      : Factory::fabricate((TiXmlElement*)this->getXmlElem()->FirstChildElement("weapon")));
92  this->model = this->weapon->getModel(0);
93}
94
95void WeaponPowerUp::setWeaponClass(const char* name)
96{
97  this->weapon = dynamic_cast<Weapon*>(Factory::fabricate(name));
98  if (this->weapon == NULL)
99  {
100    PRINTF(1)("Unable to load Weapon. %s\n", name);
101    this->weapon = dynamic_cast<Weapon*>(Factory::fabricate("Turret"));
102  }
103  this->model = this->weapon->getModel(0);
104}
105
106int WeaponPowerUp::writeBytes( const byte * data, int length, int sender )
107{
108  setRequestedSync( false );
109  setIsOutOfSync( false );
110
111  SYNCHELP_READ_BEGIN();
112
113  SYNCHELP_READ_FKT( PowerUp::writeState, NWT_WPU_WE_STATE );
114
115  //TODO: sync weapon class ( see loadParams )
116
117  return SYNCHELP_READ_N;
118}
119
120
121
122int WeaponPowerUp::readBytes( byte * data, int maxLength, int * reciever )
123{
124  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
125  {
126    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
127    setRequestedSync( true );
128  }
129
130  int rec = this->getRequestSync();
131  if ( rec > 0 )
132  {
133    *reciever = rec;
134
135    SYNCHELP_WRITE_BEGIN();
136
137    SYNCHELP_WRITE_FKT( PowerUp::readState, NWT_WPU_WE_STATE );
138
139    //TODO: sync weapon class ( see loadParams )
140
141    return SYNCHELP_WRITE_N;
142  }
143
144  *reciever = 0;
145  return 0;
146}
Note: See TracBrowser for help on using the repository browser.