[4592] | 1 | /* |
---|
[3471] | 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: |
---|
[5915] | 12 | main-programmer: Silvan Nellen |
---|
| 13 | co-programmer: Benjamin Knecht |
---|
[3471] | 14 | */ |
---|
| 15 | |
---|
| 16 | #include "player.h" |
---|
[5915] | 17 | #include "playable.h" |
---|
[3596] | 18 | |
---|
[4404] | 19 | #include "event_handler.h" |
---|
[4389] | 20 | |
---|
[6222] | 21 | #include "state.h" |
---|
[6438] | 22 | #include "util/hud.h" |
---|
[6222] | 23 | |
---|
[8362] | 24 | #include "debug.h" |
---|
[3471] | 25 | |
---|
[9869] | 26 | ObjectListDefinition(Player); |
---|
[3471] | 27 | /** |
---|
[4885] | 28 | * creates a new Player |
---|
[3471] | 29 | */ |
---|
[4762] | 30 | Player::Player() |
---|
[3471] | 31 | { |
---|
[9061] | 32 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
[9869] | 33 | this->registerObject(this, Player::_objectList); |
---|
[6222] | 34 | |
---|
[6440] | 35 | PRINTF(4)("PLAYER INIT\n"); |
---|
| 36 | |
---|
[6985] | 37 | this->playable = NULL; |
---|
[9019] | 38 | this->_hud.setVisibility(true); |
---|
[6440] | 39 | |
---|
[7868] | 40 | this->subscribeEvent(ES_GAME, KeyMapper::PEV_CHANGE_SHIP); |
---|
[3471] | 41 | } |
---|
| 42 | |
---|
[4975] | 43 | |
---|
[4010] | 44 | /** |
---|
[4975] | 45 | * destructs the player, deletes alocated memory |
---|
| 46 | */ |
---|
| 47 | Player::~Player () |
---|
| 48 | { |
---|
[6986] | 49 | this->setPlayable(NULL); |
---|
[4780] | 50 | } |
---|
| 51 | |
---|
| 52 | |
---|
[6985] | 53 | bool Player::setPlayable(Playable* playable) |
---|
[4780] | 54 | { |
---|
[6986] | 55 | if (this->playable == playable) |
---|
| 56 | return false; |
---|
| 57 | |
---|
| 58 | // get out of the current Playable |
---|
| 59 | if (this->playable != NULL) |
---|
[5435] | 60 | { |
---|
[6987] | 61 | PRINTF(4)("Player gets ejected from Playable\n"); |
---|
[9019] | 62 | this->_hud.setEnergyWidget(NULL); |
---|
| 63 | this->_hud.setWeaponManager(NULL); |
---|
[6986] | 64 | |
---|
| 65 | Playable* ejectedPlayable = this->playable; |
---|
| 66 | |
---|
| 67 | this->playable = NULL; |
---|
| 68 | ejectedPlayable->setPlayer(NULL); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | if (playable != NULL) |
---|
| 72 | { |
---|
[6987] | 73 | PRINTF(4)("Enter new Playable\n"); |
---|
[9061] | 74 | this->playable = playable; |
---|
[9062] | 75 | this->_hud.setEnergyWidget(this->playable->getHealthWidget()); |
---|
| 76 | this->_hud.setWeaponManager(&this->playable->getWeaponManager()); |
---|
[6986] | 77 | |
---|
[9061] | 78 | this->playable->setPlayer(this); |
---|
| 79 | return true; |
---|
[5751] | 80 | } |
---|
[8316] | 81 | |
---|
[8147] | 82 | if ( playable == NULL ) |
---|
| 83 | this->playable = NULL; |
---|
[6986] | 84 | |
---|
| 85 | return true; |
---|
[3471] | 86 | } |
---|
[3584] | 87 | |
---|
[6986] | 88 | bool Player::eject() |
---|
[9061] | 89 | { |
---|
| 90 | return this->setPlayable(NULL); |
---|
| 91 | } |
---|
[3584] | 92 | |
---|
[6986] | 93 | |
---|
[9869] | 94 | void Player::weaponConfigChanged() |
---|
| 95 | { |
---|
| 96 | this->_hud.updateWeaponManager(); |
---|
| 97 | } |
---|
[6443] | 98 | |
---|
[6438] | 99 | |
---|
[9061] | 100 | void Player::enterNewPlayable() |
---|
| 101 | { |
---|
| 102 | /// FIXME this should be in the ObjectManager |
---|
[9869] | 103 | for (ObjectList<Playable>::const_iterator node = Playable::objectList().begin(); |
---|
| 104 | node != Playable::objectList().end(); |
---|
| 105 | ++node) |
---|
[9061] | 106 | { |
---|
[9869] | 107 | if (this->playable != (*node) && |
---|
| 108 | ((*node)->getAbsCoor() - this->playable->getAbsCoor()).len() < ((*node)->getEnterRadius())) |
---|
| 109 | { |
---|
[6222] | 110 | |
---|
[9869] | 111 | this->setPlayable(*(node)); |
---|
[6222] | 112 | |
---|
[9869] | 113 | break; |
---|
| 114 | } |
---|
[9061] | 115 | } |
---|
| 116 | } |
---|
[5751] | 117 | |
---|
[9061] | 118 | |
---|
| 119 | void Player::process(const Event &event) |
---|
| 120 | { |
---|
| 121 | if (event.type == KeyMapper::PEV_CHANGE_SHIP && event.bPressed) |
---|
| 122 | { |
---|
| 123 | this->enterNewPlayable(); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | if (likely(this->playable != NULL)) |
---|
| 127 | this->playable->process(event); |
---|
| 128 | } |
---|
| 129 | |
---|