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