[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 | |
---|
| 22 | #include "class_list.h" |
---|
| 23 | #include "state.h" |
---|
[6438] | 24 | #include "util/hud.h" |
---|
[6222] | 25 | |
---|
[3471] | 26 | using namespace std; |
---|
| 27 | |
---|
[4010] | 28 | |
---|
[3471] | 29 | /** |
---|
[4885] | 30 | * creates a new Player |
---|
[3471] | 31 | */ |
---|
[4762] | 32 | Player::Player() |
---|
[3471] | 33 | { |
---|
[6440] | 34 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
| 35 | this->setClassID(CL_PLAYER, "Player"); |
---|
[6222] | 36 | |
---|
[6440] | 37 | PRINTF(4)("PLAYER INIT\n"); |
---|
| 38 | |
---|
[6985] | 39 | this->playable = NULL; |
---|
[6441] | 40 | this->hud.show(); |
---|
[6440] | 41 | |
---|
[6998] | 42 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_CHANGE_SHIP); |
---|
[3471] | 43 | } |
---|
| 44 | |
---|
[4975] | 45 | |
---|
[4010] | 46 | /** |
---|
[4975] | 47 | * destructs the player, deletes alocated memory |
---|
| 48 | */ |
---|
| 49 | Player::~Player () |
---|
| 50 | { |
---|
[6986] | 51 | this->setPlayable(NULL); |
---|
[4780] | 52 | } |
---|
| 53 | |
---|
| 54 | |
---|
[6985] | 55 | bool Player::setPlayable(Playable* playable) |
---|
[4780] | 56 | { |
---|
[6986] | 57 | if (this->playable == playable) |
---|
| 58 | return false; |
---|
| 59 | |
---|
| 60 | // get out of the current Playable |
---|
| 61 | if (this->playable != NULL) |
---|
[5435] | 62 | { |
---|
[6987] | 63 | PRINTF(4)("Player gets ejected from Playable\n"); |
---|
[6986] | 64 | this->hud.setEnergyWidget(NULL); |
---|
| 65 | this->hud.setWeaponManager(NULL); |
---|
| 66 | |
---|
| 67 | Playable* ejectedPlayable = this->playable; |
---|
| 68 | |
---|
| 69 | this->playable = NULL; |
---|
| 70 | ejectedPlayable->setPlayer(NULL); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | if (playable != NULL) |
---|
| 74 | { |
---|
[6987] | 75 | PRINTF(4)("Enter new Playable\n"); |
---|
[6985] | 76 | this->playable = playable; |
---|
| 77 | this->hud.setEnergyWidget(this->playable->getHealthWidget()); |
---|
| 78 | this->hud.setWeaponManager(this->playable->getWeaponManager()); |
---|
[6986] | 79 | |
---|
| 80 | this->playable->setPlayer(this); |
---|
[5915] | 81 | return true; |
---|
[5751] | 82 | } |
---|
[6986] | 83 | |
---|
| 84 | return true; |
---|
[3471] | 85 | } |
---|
[3584] | 86 | |
---|
[6986] | 87 | bool Player::eject() |
---|
[5915] | 88 | { |
---|
[6986] | 89 | this->setPlayable(NULL); |
---|
[5915] | 90 | } |
---|
[3584] | 91 | |
---|
[6986] | 92 | |
---|
[6443] | 93 | void Player::weaponConfigChanged() |
---|
| 94 | { |
---|
| 95 | this->hud.updateWeaponManager(); |
---|
| 96 | } |
---|
| 97 | |
---|
[5915] | 98 | void Player::process(const Event &event) |
---|
| 99 | { |
---|
[6998] | 100 | if (event.type == KeyMapper::PEV_CHANGE_SHIP && event.bPressed) |
---|
[6222] | 101 | { |
---|
| 102 | /// FIXME this should be in the ObjectManager |
---|
| 103 | const std::list<BaseObject*>* objectList = ClassList::getList(CL_PLAYABLE); |
---|
| 104 | if (objectList != NULL) |
---|
| 105 | { |
---|
| 106 | list<BaseObject*>::const_iterator node; |
---|
| 107 | for (node = objectList->begin(); node != objectList->end(); node++) |
---|
[6985] | 108 | if (this->playable != (*node) && (dynamic_cast<PNode*>(*node)->getAbsCoor() - this->playable->getAbsCoor()).len() < 10.0) |
---|
[6222] | 109 | { |
---|
[6438] | 110 | |
---|
[6985] | 111 | this->setPlayable(dynamic_cast<Playable*>(*node)); |
---|
[6222] | 112 | |
---|
| 113 | break; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
[6985] | 118 | if (likely(this->playable != NULL)) |
---|
| 119 | this->playable->process(event); |
---|
[5915] | 120 | } |
---|
[5751] | 121 | |
---|