Changeset 4951 in orxonox.OLD for orxonox/trunk/src/world_entities/weapons
- Timestamp:
- Jul 24, 2005, 11:15:00 PM (19 years ago)
- Location:
- orxonox/trunk/src/world_entities/weapons
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/world_entities/weapons/weapon_manager.cc
r4949 r4951 13 13 main-programmer: Patrick Boenzli 14 14 co-programmer: Benjamin Grauer 15 16 2005-07-24: Benjamin Grauer: restructurate, so it can handle the new Weapons. 15 17 */ 16 18 … … 34 36 * @param number of weapon slots of the model/ship <= 8 (limitied) 35 37 */ 36 WeaponManager::WeaponManager( int slotCount)38 WeaponManager::WeaponManager(unsigned int slotCount) 37 39 { 38 40 this->init(); 39 this->s lotCount = slotCount;41 this->setSlotCount(slotCount); 40 42 } 41 43 … … 51 53 WeaponManager::~WeaponManager() 52 54 { 53 /*54 i dont have to delete the weapons itself, because they are55 worldentities and therefore in the entities list of the world.56 world will clean them up for me57 */58 for(int i = 0; i < WM_MAX_CONFIGS; ++i)59 {60 this->configs[i].bUsed = false;61 for(int j = 0; j < WM_MAX_SLOTS; ++j)62 this->configs[i].slots[j] = NULL;63 }64 65 55 // crosshair being a PNode it must not be deleted (this is because PNodes delete themselves.) 66 56 //delete this->crosshair; … … 74 64 this->setClassID(CL_WEAPON_MANAGER, "WeaponManager"); 75 65 76 for(int i = 0; i < WM_MAX_CONFIGS; ++i) 77 { 78 this->configs[i].bUsed = false; 79 for(int j = 0; j < WM_MAX_SLOTS; ++j) 80 this->configs[i].slots[j] = NULL; 81 } 82 this->currConfID = WM_CONFIG0; 83 84 85 86 66 this->parent = NULL; 67 68 for (int i = 0; i < WM_MAX_CONFIGS; i++) 69 for (int j = 0; j < WM_MAX_SLOTS; j++) 70 this->configs[i][j] = NULL; 71 72 for (int i = 0; i < WM_MAX_SLOTS; i++) 73 { 74 this->currentSlotConfig[i].capability = WM_SLOTC_ALL; 75 this->currentSlotConfig[i].currentWeapon = NULL; 76 this->currentSlotConfig[i].nextWeapon = NULL; 77 } 78 79 for (int i = 0; i < WM_MAX_LOADED_WEAPONS; i++) 80 this->availiableWeapons[i] = NULL; 81 82 83 this->currentConfigID = 0; 84 this->slotCount = 2; 85 this->weaponChange; 86 87 // CROSSHAIR INITIALISATION 87 88 this->crosshair = new Crosshair(); 88 89 … … 101 102 { 102 103 static_cast<BaseObject*>(this)->loadParams(root); 103 104 /* 104 105 LoadParam<WeaponManager>(root, "slot-count", this, &WeaponManager::setSlotCount) 105 106 .describe("how many slots(cannons) the WeaponManager can handle"); … … 111 112 // LoadParam<WeaponManager>(root, "Weapon", this, &WeaponManager::addWeapon); 112 113 113 LOAD_PARAM_END_CYCLE; 114 LOAD_PARAM_END_CYCLE;*/ 114 115 } 115 116 … … 134 135 * @param slotCount the number of slots 135 136 */ 136 void WeaponManager::setSlotCount(int slotCount) 137 { 138 this->slotCount = slotCount; 137 void WeaponManager::setSlotCount(unsigned int slotCount) 138 { 139 if (slotCount <= WM_MAX_SLOTS) 140 this->slotCount = slotCount; 141 else 142 this->slotCount = WM_MAX_SLOTS; 139 143 } 140 144 … … 152 156 void WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID) 153 157 { 154 if( slotID == WM_FREE_SLOT) 155 { 156 int freeSlot = this->getNextFreeSlot( configID); 157 if( freeSlot < 0 || freeSlot >= this->slotCount) 158 if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount)) 159 { 160 PRINTF(2)("Slot %d of config %d is not availiabe\n", slotID, configID); 161 return; 162 } 163 164 if (this->configs[configID][slotID] != NULL && configID > 0 && slotID > 0) 165 PRINTF(3)("Weapon-slot %d/%d of %s already poulated, overwriting\n", configID, slotID, this->getName()); 166 167 if (slotID == -1) // WM_FREE_SLOT 168 { 169 slotID = this->getNextFreeSlot(configID); 170 if( slotID < 0 || slotID >= this->slotCount) 158 171 { 159 172 PRINTF(0)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n"); 160 173 return; 161 174 } 162 PRINTF(3)("Added new Weapon to Config:%i/Slot:%i\n", configID, freeSlot); 163 this->configs[configID].bUsed = true; 164 this->configs[configID].slots[freeSlot] = weapon; 165 return; 166 } 167 this->configs[configID].bUsed = true; 168 this->configs[configID].slots[slotID] = weapon; 175 } 176 this->configs[configID][slotID] = weapon; 169 177 PRINTF(3)("Added a new Weapon to the WeaponManager: config %i/ slot %i\n", configID, slotID); 170 178 } … … 187 195 void WeaponManager::nextWeaponConf() 188 196 { 189 PRINTF(4)("Changing weapon configuration: from %i to next\n", this->currConfID); 190 191 int i, lastConfID; 192 lastConfID = this->currConfID; 193 for(i = this->currConfID + 1; i < WM_MAX_CONFIGS && !this->configs[i].bUsed; ++i); 194 if( i == WM_MAX_CONFIGS) this->currConfID = WM_CONFIG0; 195 else this->currConfID = i; 196 197 198 Weapon *w1, *w2; 199 for(int j = 0; j < WM_MAX_SLOTS; ++j) 200 { 201 w1 = this->configs[lastConfID].slots[j]; 202 w2 = this->configs[this->currConfID].slots[j]; 203 204 if( w1 == w2) 197 PRINTF(4)("Changing weapon configuration: from %i to next\n", this->currentConfigID); 198 199 ++this->currentConfigID; 200 if (this->currentConfigID >= WM_MAX_CONFIGS) 201 this->currentConfigID = 0; 202 203 for (int i = 0; i < WM_MAX_SLOTS; i++) 204 { 205 this->currentSlotConfig[i].nextWeapon = this->configs[currentConfigID][i]; 206 if (this->currentSlotConfig[i].currentWeapon != this->currentSlotConfig[i].nextWeapon && this->currentSlotConfig[i].currentWeapon != NULL) 207 (this->currentSlotConfig[i].currentWeapon->requestAction(WA_DEACTIVATE)); 208 } 209 210 this->debug(); 211 } 212 213 214 /** 215 * triggers fire of all weapons in the current weaponconfig 216 */ 217 void WeaponManager::fire() 218 { 219 Weapon* firingWeapon; 220 for(int i = 0; i < this->slotCount; i++) 221 { 222 firingWeapon = this->currentSlotConfig[i].currentWeapon; 223 if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT); 224 } 225 this->crosshair->setRotationSpeed(500); 226 this->crossHairSizeAnim->replay(); 227 } 228 229 230 /** 231 * triggers tick of all weapons in the current weaponconfig 232 * @param second passed since last tick 233 */ 234 void WeaponManager::tick(float dt) 235 { 236 Weapon* tickWeapon; 237 238 // all weapons 239 for(int i = 0; i < this->slotCount; i++) 240 { 241 242 tickWeapon = this->currentSlotConfig[i].currentWeapon; 243 if (tickWeapon != this->currentSlotConfig[i].nextWeapon) // if no change occures 205 244 { 206 printf("no need for change\n"); 207 } 208 else 209 { 210 if( w1 != NULL ) 245 if (tickWeapon != NULL && tickWeapon->isActive()) 211 246 { 212 w1->requestAction(WA_DEACTIVATE); 213 printf("deactivating %i,%i\n", j,lastConfID); 247 tickWeapon->requestAction(WA_DEACTIVATE); 214 248 } 215 if( w2 != NULL)249 else 216 250 { 217 w2->requestAction(WA_ACTIVATE); 218 printf("activating %i,%i\n", j, this->currConfID); 251 tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon; 252 if (tickWeapon != NULL) 253 tickWeapon->requestAction(WA_ACTIVATE); 219 254 } 220 255 } 221 } 222 } 223 224 225 /** 226 * triggers fire of all weapons in the current weaponconfig 227 */ 228 void WeaponManager::fire() 229 { 230 Weapon* firingWeapon; 231 for(int i = 0; i < WM_MAX_SLOTS; ++i) 232 { 233 firingWeapon = this->configs[this->currConfID].slots[i]; 234 if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT); 235 } 236 this->crosshair->setRotationSpeed(500); 237 this->crossHairSizeAnim->replay(); 238 } 239 240 241 /** 242 * triggers tick of all weapons in the current weaponconfig 243 * @param second passed since last tick 244 */ 245 void WeaponManager::tick(float dt) 246 { 247 Weapon* w; 248 for(int i = 0; i < WM_MAX_SLOTS; ++i) 249 { 250 w = this->configs[this->currConfID].slots[i]; 251 if( w != NULL && w->isActive()) 252 w->tickW(dt); 256 257 if( tickWeapon != NULL && tickWeapon->isActive()) 258 tickWeapon->tickW(dt); 253 259 } 254 260 … … 260 266 * triggers draw of all weapons in the current weaponconfig 261 267 */ 262 void WeaponManager::draw() 263 { 264 Weapon* w; 265 for (int j = 0; j < 4; ++j ) 266 for(int i = 0; i < WM_MAX_SLOTS; ++i) 267 { 268 w = this->configs[j].slots[i]; 269 if( w != NULL && w->isVisible()) 270 w->draw(); 268 void WeaponManager::draw() const 269 { 270 Weapon* drawWeapon; 271 for (int i = 0; i < this->slotCount; i++) 272 { 273 drawWeapon = this->currentSlotConfig[i].currentWeapon; 274 if( drawWeapon != NULL && drawWeapon->isVisible()) 275 drawWeapon->draw(); 271 276 } 272 277 } … … 279 284 int WeaponManager::getNextFreeSlot(int configID) 280 285 { 281 for( int i = 0; i < WM_MAX_SLOTS; ++i)282 { 283 if( this->configs[configID] .slots[i] == NULL)286 for( int i = 0; i < this->slotCount; ++i) 287 { 288 if( this->configs[configID][i] == NULL) 284 289 return i; 285 290 } … … 287 292 } 288 293 294 295 296 void WeaponManager::debug() const 297 { 298 PRINT(3)("WeaponManager Debug Information\n"); 299 PRINT(3)("-------------------------------\n"); 300 PRINT(3)("current Config is %d\n", this->currentConfigID); 301 for (int i = 0; i < WM_MAX_CONFIGS; i++) 302 { 303 PRINT(3)("Listing Weapons in Configuration %d\n", i); 304 for (int j = 0; j < WM_MAX_SLOTS; j++) 305 { 306 if (this->configs[i][j] != NULL) 307 PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassName()); 308 } 309 } 310 } -
orxonox/trunk/src/world_entities/weapons/weapon_manager.h
r4950 r4951 29 29 #include "base_object.h" 30 30 31 #include "vector.h" 31 32 32 33 // FORWARD DECLARATION 34 class PNode; 33 35 class Weapon; 34 36 class Crosshair; 35 37 template <class T> class tAnimation; 36 38 37 #define WM_MAX_SLOTS 8 //!< How many slots the WeaponManager has at its max38 #define WM_MAX_CONFIGS 4 //!< The maximum number of predefined Configurations39 #define WM_MAX_LOADED_WEAPONS 20 //!< The40 39 41 //! this is an identifier for the weapon config 40 #define WM_MAX_SLOTS 8 //!< How many slots the WeaponManager has at its max 41 #define WM_MAX_CONFIGS 4 //!< The maximum number of predefined Configurations 42 #define WM_MAX_LOADED_WEAPONS 20 //!< The 43 44 //! an enumerator defining capabilities of a WeaponSlot 42 45 typedef enum 43 46 { 44 WM_CONFIG0 = 0, 45 WM_CONFIG1 = 1, 46 WM_CONFIG2 = 2, 47 WM_CONFIG3 = 3, 47 WM_SLOTC_DIRECTIONAL = 0x00000001, //!< if the Slot is able to carry directional weapons 48 WM_SLOTC_TURRET = 0x00000002, //!< if the Slot is able to carry turrets 49 WM_SLOTC_ALLKINDS = 0x0000000f, //!< if the Slot is able to carry all kinds of weapons 48 50 49 WM_CONFIGCOUNT = 4 50 } WM_CONFIG; 51 WM_SLOTC_FORWARD = 0x00000010, //!< if the Slot is able to carry weapons firing forward 52 WM_SLOTC_BACKWARD = 0x00000020, //!< if the Slot is able to carry weapons firing backwards 53 WM_SLOTC_LEFT = 0x00000040, //!< if the Slot is able to carry weapons firing to the left 54 WM_SLOTC_RIGHT = 0x00000080, //!< if the Slot is able to carry weapons firing to the right 55 WM_SLOTC_ALLDIRS = 0x000000f0, //!< this slot can fire into all directions 51 56 52 //! this is an identifier for the slot. there are up to 8 weapon slots -> this means there can't be more than 8 weapons at the same time 53 typedef enum 54 { 55 WM_SLOT0 = 0, 56 WM_SLOT1 = 1, 57 WM_SLOT2 = 2, 58 WM_SLOT3 = 3, 59 WM_SLOT4 = 4, 60 WM_SLOT5 = 5, 61 WM_SLOT6 = 6, 62 WM_SLOT7 = 7, 63 64 WM_SLOT_COUNT = 8, 65 66 WM_FREE_SLOT = -1 67 } WM_SLOT; 68 69 typedef enum 70 { 71 WM_SLOTC_DIRECTIONAL = 1, 72 WM_SLOTC_TURRET = 2, 73 WM_SLOTC_ALLKINDS = 3, 74 75 WM_SLOTC_FORWARD = 4, 76 WM_SLOTC_BACKWARD = 8, 77 WM_SLOTC_LEFT = 16, 78 WM_SLOTC_RIGHT = 32, 79 WM_SLOTC_ALLDIRS = 60, 80 81 WM_SLOTC_ALL = 63, 57 WM_SLOTC_ALL = 0x000000ff, //!< everything allowed in this slot 82 58 } WM_SlotCapability; 83 59 84 typedef struct WM_Slot 85 { 86 int number; 87 long capability; 88 89 Weapon* currentWeapon; 90 Weapon* nextWeapon; 91 }; 92 93 //! this is a weapon Configuration: it has up to 8 slots 60 //! an enumerator defining a Slot, where a Weapon can be stored inside. 94 61 typedef struct 95 62 { 96 bool bUsed; //!< is set to true, if this configuration is 97 Weapon* slots[WM_MAX_SLOTS]; 98 } weaponConfig; 63 Vector position; //!< the relative Position to the position of the carrying entity. (const PNode* parent; of WeaponManager) 64 long capability; //!< the capabilities of the Slot @see WM_SlotCapability. 99 65 66 Weapon* currentWeapon; //!< The current weapon this slot is carrying. 67 Weapon* nextWeapon; //!< either NULL or the next weapon that will be set (require currentWeapon to deactivate) 68 } WM_Slot; 100 69 101 70 class WeaponManager : public BaseObject { 102 71 public: 103 WeaponManager( int slotCount);72 WeaponManager(unsigned int slotCount); 104 73 WeaponManager(const TiXmlElement* root); 105 74 ~WeaponManager(); … … 109 78 void loadWeapons(const TiXmlElement* root); 110 79 111 void setSlotCount(int slotCount); 80 // setting up the WeaponManager with the following functions 81 void setSlotPosition(int slot, const Vector& position); 82 void setSlotCapability(long slotCapability); 112 83 113 void addWeapon(Weapon* weapon, int configID = WM_CONFIG0, int slotID = WM_FREE_SLOT); 114 void removeWeapon(Weapon* weapon, int configID = WM_CONFIG0); 84 void addWeapon(Weapon* weapon, int configID = -1, int slotID = -1); 85 void removeWeapon(Weapon* weapon, int configID = -1); 86 115 87 void nextWeaponConf(); 116 88 117 89 void fire(); 90 118 91 void tick(float dt); 119 void draw(); 92 void draw() const; 93 94 void debug() const; 120 95 121 96 private: 97 void setSlotCount(unsigned int slotCount); 98 122 99 int getNextFreeSlot(int configID); 123 100 124 101 private: 125 Crosshair* crosshair; //!< an aim. 126 tAnimation<Crosshair>* crossHairSizeAnim; //!< An animation for the crosshair (scaling) 102 PNode* parent; //!< The parent, this WeaponManager is connected to. 127 103 128 int slotCount; //!< number of weapon slots a ship has 129 int currConfID; //!< the currently selected config 130 weaponConfig configs[4]; //!< a list of four configurations 104 int slotCount; //!< number of weapon slots the ship has. 105 int currentConfigID; //!< the currently selected config. 106 Weapon* configs[WM_MAX_CONFIGS][WM_MAX_SLOTS]; //!< An array of predefined configurations and assigned weapon. 107 WM_Slot currentSlotConfig[WM_MAX_SLOTS]; //!< The currentConfigureation. 131 108 132 109 Weapon* availiableWeapons[WM_MAX_LOADED_WEAPONS]; 110 111 bool weaponChange; 112 113 Crosshair* crosshair; //!< an aim. 114 tAnimation<Crosshair>* crossHairSizeAnim; //!< An animation for the crosshair (scaling) 133 115 };
Note: See TracChangeset
for help on using the changeset viewer.