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