Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/power_ups/param_power_up.cc @ 6498

Last change on this file since 6498 was 6498, checked in by patrick, 18 years ago

trunk: merged the network branche into the trunk

File size: 3.5 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 "param_power_up.h"
19#include "factory.h"
20#include "state.h"
21#include "list.h"
22
23#include "primitive_model.h"
24
25#include "factory.h"
26#include "load_param.h"
27#include "network_game_manager.h"
28
29using namespace std;
30
31CREATE_FACTORY(ParamPowerUp, CL_PARAM_POWER_UP);
32
33const char* ParamPowerUp::paramTypes[] = {
34  "shield"
35};
36
37ParamPowerUp::ParamPowerUp () : PowerUp(0.0, 1.0, 0.0)
38{
39  this->init();
40}
41
42ParamPowerUp::ParamPowerUp(const TiXmlElement* root) : PowerUp(0.0, 1.0, 0.0)
43{
44  this->init();
45  this->loadParams(root);
46}
47
48
49ParamPowerUp::~ParamPowerUp ()
50{
51}
52
53
54void ParamPowerUp::init()
55{
56  this->setClassID(CL_PARAM_POWER_UP, "ParamPowerUp");
57  this->value = 0;
58  this->max_value = 0;
59  this->min_value = 0;
60}
61
62
63void ParamPowerUp::loadParams(const TiXmlElement* root)
64{
65  static_cast<PowerUp*>(this)->loadParams(root);
66  LoadParam(root, "type", this, ParamPowerUp, setType);
67
68  if( root != NULL && root->FirstChildElement("value") != NULL) {
69
70    LoadParam(root, "value", this, ParamPowerUp, setValue);
71  }
72  else {
73    LoadParam(root, "max-value", this, ParamPowerUp, setMaxValue);
74    LoadParam(root, "min-value", this, ParamPowerUp, setMinValue);
75    respawn();
76  }
77}
78
79void ParamPowerUp::setValue(int value)
80{
81  this->value = value;
82}
83
84void ParamPowerUp::setType(const char* type)
85{
86  for(int i = 0; i < PARAM_size; ++i) {
87    if(strcmp(type, paramTypes[i]) == 0) {
88      this->type = (EnumParamPowerUpType)i;
89      break;
90    }
91  }
92}
93
94void ParamPowerUp::setMaxValue(int value)
95{
96  this->max_value = value;
97}
98
99void ParamPowerUp::setMinValue(int value)
100{
101  this->min_value = value;
102}
103
104int ParamPowerUp::getValue()
105{
106  return this->value;
107}
108
109EnumParamPowerUpType ParamPowerUp::getType()
110{
111  return this->type;
112}
113
114void ParamPowerUp::respawn()
115{
116  if(this->min_value != this->max_value)
117  {
118    value = this->min_value + (int)(rand() * (this->max_value - this->min_value));
119  }
120}
121
122int ParamPowerUp::writeBytes( const byte * data, int length, int sender )
123{
124  setRequestedSync( false );
125  setIsOutOfSync( false );
126
127  SYNCHELP_READ_BEGIN();
128
129  SYNCHELP_READ_FKT( PowerUp::writeState );
130
131  int i;
132  SYNCHELP_READ_INT( i );
133  this->type = (EnumParamPowerUpType)i;
134  SYNCHELP_READ_INT( this->value );
135
136  if ( this->value != 0 )
137  {
138    SYNCHELP_READ_INT( this->min_value );
139    SYNCHELP_READ_INT( this->max_value );
140    respawn();
141  }
142
143  return SYNCHELP_READ_N;
144}
145
146
147
148int ParamPowerUp::readBytes( byte * data, int maxLength, int * reciever )
149{
150  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
151  {
152    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
153    setRequestedSync( true );
154  }
155
156  int rec = this->getRequestSync();
157  if ( rec > 0 )
158  {
159    *reciever = rec;
160
161    SYNCHELP_WRITE_BEGIN();
162
163    SYNCHELP_WRITE_FKT( PowerUp::readState );
164
165    int i = (int)this->type;
166    SYNCHELP_WRITE_INT( i );
167    SYNCHELP_WRITE_INT( this->value );
168
169    if ( this->value != 0 )
170    {
171      SYNCHELP_WRITE_INT( this->min_value );
172      SYNCHELP_WRITE_INT( this->max_value );
173    }
174
175    return SYNCHELP_WRITE_N;
176  }
177
178  *reciever = 0;
179  return 0;
180}
181
Note: See TracBrowser for help on using the repository browser.