1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific: |
---|
14 | main-programmer: Patrick Boenzli |
---|
15 | co-programmer: Christian Meyer |
---|
16 | */ |
---|
17 | |
---|
18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PLAYER |
---|
19 | |
---|
20 | #include "player.h" |
---|
21 | |
---|
22 | #include "stdincl.h" |
---|
23 | //#include "collision.h" |
---|
24 | #include "objModel.h" |
---|
25 | #include "list.h" |
---|
26 | #include "weapon.h" |
---|
27 | #include "track_manager.h" |
---|
28 | |
---|
29 | using namespace std; |
---|
30 | |
---|
31 | /** |
---|
32 | \brief creates a new Player |
---|
33 | \param isFree if the player is free |
---|
34 | */ |
---|
35 | Player::Player(bool isFree) : WorldEntity(isFree) |
---|
36 | { |
---|
37 | this->model = new OBJModel("../data/models/reaplow.obj"); |
---|
38 | this->weapons = new tList<Weapon>(); |
---|
39 | this->activeWeapon = NULL; |
---|
40 | |
---|
41 | travelSpeed = 15.0; |
---|
42 | velocity = Vector(); |
---|
43 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
---|
44 | bFire = false; |
---|
45 | acceleration = 10.0; |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | \brief destructs the player, deletes alocated memory |
---|
50 | */ |
---|
51 | Player::~Player () |
---|
52 | { |
---|
53 | Weapon* w = this->weapons->enumerate(); |
---|
54 | while( w != NULL) |
---|
55 | { |
---|
56 | delete w; |
---|
57 | w = this->weapons->nextElement(); |
---|
58 | } |
---|
59 | delete this->weapons; |
---|
60 | |
---|
61 | //delete this->velocity; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | /** |
---|
66 | \brief adds a weapon to the weapon list of player |
---|
67 | \param weapon to add |
---|
68 | */ |
---|
69 | void Player::addWeapon(Weapon* weapon) |
---|
70 | { |
---|
71 | this->weapons->add(weapon); |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | /** |
---|
76 | \brief removes a weapon from the player |
---|
77 | \param weapon to remove |
---|
78 | */ |
---|
79 | void Player::removeWeapon(Weapon* weapon) |
---|
80 | { |
---|
81 | this->weapons->remove(weapon); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | /** |
---|
86 | \brief effect that occurs after the player is spawned |
---|
87 | */ |
---|
88 | void Player::postSpawn () |
---|
89 | { |
---|
90 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | /** |
---|
95 | \brief the action occuring if the player left the game |
---|
96 | */ |
---|
97 | void Player::leftWorld () |
---|
98 | {} |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | /** |
---|
103 | \brief if the player is hit, call this function |
---|
104 | \param weapon hit by this weapon |
---|
105 | \param loc ?? |
---|
106 | */ |
---|
107 | void Player::hit (WorldEntity* weapon, Vector* loc) |
---|
108 | { |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | /** |
---|
113 | \brief Collision with another Entity has this effect |
---|
114 | \param other the other colider |
---|
115 | \param ownhitflags ?? |
---|
116 | \param otherhitflags ?? |
---|
117 | */ |
---|
118 | void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) |
---|
119 | { |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | /** |
---|
124 | \brief draws the player after transforming him. |
---|
125 | */ |
---|
126 | void Player::draw () |
---|
127 | { |
---|
128 | glMatrixMode(GL_MODELVIEW); |
---|
129 | glPushMatrix(); |
---|
130 | float matrix[4][4]; |
---|
131 | |
---|
132 | /* translate */ |
---|
133 | glTranslatef (this->getAbsCoor ().x, |
---|
134 | this->getAbsCoor ().y, |
---|
135 | this->getAbsCoor ().z); |
---|
136 | /* rotate */ |
---|
137 | this->getAbsDir ().matrix (matrix); |
---|
138 | glMultMatrixf((float*)matrix); |
---|
139 | |
---|
140 | this->model->draw(); |
---|
141 | glPopMatrix(); |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | /** |
---|
146 | \brief the function called for each passing timeSnap |
---|
147 | \param time The timespan passed since last update |
---|
148 | */ |
---|
149 | void Player::tick (float time) |
---|
150 | { |
---|
151 | // player controlled movement |
---|
152 | this->move (time); |
---|
153 | // weapon system manipulation |
---|
154 | this->fire(); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | /** |
---|
159 | \brief action if player moves |
---|
160 | \param time the timeslice since the last frame |
---|
161 | */ |
---|
162 | void Player::move (float time) |
---|
163 | { |
---|
164 | Vector accel(0.0, 0.0, 0.0); |
---|
165 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
166 | |
---|
167 | /* calculate the direction in which the craft is heading */ |
---|
168 | Vector direction (1.0, 0.0, 0.0); |
---|
169 | //direction = this->absDirection.apply (direction); |
---|
170 | Vector orthDirection (0.0, 0.0, 1.0); |
---|
171 | //orthDirection = orthDirection.cross (direction); |
---|
172 | |
---|
173 | if( this->bUp && this->getRelCoor().x < 20) |
---|
174 | accel = accel+(direction*acceleration); |
---|
175 | if( this->bDown && this->getRelCoor().x > -5) |
---|
176 | accel = accel-(direction*acceleration); |
---|
177 | if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2) |
---|
178 | accel = accel - (orthDirection*acceleration); |
---|
179 | if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2) |
---|
180 | accel = accel + (orthDirection*acceleration); |
---|
181 | if( this->bAscend ) |
---|
182 | if( this->bDescend) {/* FIXME */} /* \todo up and down player movement */ |
---|
183 | |
---|
184 | Vector move = accel * time; |
---|
185 | this->shiftCoor (&move); |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | /** |
---|
190 | \brief weapon manipulation by the player |
---|
191 | */ |
---|
192 | void Player::fire() |
---|
193 | { |
---|
194 | if(this->bFire) |
---|
195 | { |
---|
196 | if(this->activeWeapon != NULL) |
---|
197 | this->activeWeapon->fire(); |
---|
198 | } |
---|
199 | if(this->bWeaponChange) |
---|
200 | { |
---|
201 | Weapon* w = this->weapons->enumerate(); |
---|
202 | this->activeWeapon = this->weapons->nextElement(this->activeWeapon); |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | /** |
---|
208 | \brief The connection to the command node |
---|
209 | \param cmd the Command unit from witch to map |
---|
210 | |
---|
211 | here the commands are mapped to the players movement/weaponary |
---|
212 | */ |
---|
213 | void Player::command (Command* cmd) |
---|
214 | { |
---|
215 | PRINTF(3)("recieved command [%s]\n", cmd->cmd); |
---|
216 | if( !strcmp( cmd->cmd, "up")) this->bUp = !cmd->bUp; |
---|
217 | else if( !strcmp( cmd->cmd, "down")) this->bDown = !cmd->bUp; |
---|
218 | else if( !strcmp( cmd->cmd, "left")) this->bLeft = !cmd->bUp; |
---|
219 | else if( !strcmp( cmd->cmd, "right")) this->bRight = !cmd->bUp; |
---|
220 | else if( !strcmp( cmd->cmd, "fire")) this->bFire = !cmd->bUp; |
---|
221 | else if( !strcmp( cmd->cmd, "mode")) this->bWeaponChange = !cmd->bUp; |
---|
222 | } |
---|