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