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