[1853] | 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. |
---|
[1872] | 12 | |
---|
| 13 | ### File Specific: |
---|
| 14 | main-programmer: Patrick Boenzli |
---|
[2190] | 15 | co-programmer: Christian Meyer |
---|
[1853] | 16 | */ |
---|
| 17 | |
---|
[2036] | 18 | #include "player.h" |
---|
[2190] | 19 | #include "stdincl.h" |
---|
| 20 | #include "collision.h" |
---|
[3155] | 21 | #include "command_node.h" |
---|
[2036] | 22 | |
---|
[1856] | 23 | using namespace std; |
---|
[1853] | 24 | |
---|
| 25 | |
---|
[2190] | 26 | Player::Player(bool isFree) : WorldEntity(isFree) |
---|
| 27 | { |
---|
[2860] | 28 | |
---|
[3151] | 29 | obj = new Object ("reaplow.obj"); |
---|
[3155] | 30 | tottime=0.0f; |
---|
[3172] | 31 | movfinishright=movfinishleft=movfinishup=movfinishdown=false; |
---|
| 32 | angleturn=false; |
---|
[3155] | 33 | |
---|
[2860] | 34 | /* |
---|
[2730] | 35 | objectList = glGenLists(1); |
---|
| 36 | glNewList (objectList, GL_COMPILE); |
---|
| 37 | |
---|
| 38 | glBegin(GL_TRIANGLES); |
---|
| 39 | glColor3f(1,1,1); |
---|
| 40 | glVertex3f(0,0,0.5); |
---|
| 41 | glVertex3f(-0.5,0,-1); |
---|
| 42 | glVertex3f(0.5,0,-1); |
---|
| 43 | |
---|
| 44 | glVertex3f(0,0,0.5); |
---|
| 45 | glVertex3f(0,0.5,-1); |
---|
| 46 | glVertex3f(0,-0.5,-1); |
---|
| 47 | glEnd(); |
---|
| 48 | |
---|
| 49 | glBegin(GL_QUADS); |
---|
| 50 | glColor3f(0,0,1); |
---|
| 51 | glVertex3f(0.5,0.5,-1); |
---|
| 52 | glVertex3f(0.5,-0.5,-1); |
---|
| 53 | glVertex3f(-0.5,-0.5,-1); |
---|
| 54 | glVertex3f(-0.5,0.5,-1); |
---|
| 55 | glEnd(); |
---|
| 56 | |
---|
| 57 | glEndList (); |
---|
[2860] | 58 | */ |
---|
[1872] | 59 | } |
---|
[1853] | 60 | |
---|
[1896] | 61 | Player::~Player () |
---|
[2551] | 62 | |
---|
[1896] | 63 | { |
---|
| 64 | } |
---|
[1853] | 65 | |
---|
[2190] | 66 | void Player::post_spawn () |
---|
[1858] | 67 | { |
---|
[2860] | 68 | travel_speed = 15.0; |
---|
| 69 | velocity = Vector(); |
---|
| 70 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
---|
| 71 | bFire = false; |
---|
| 72 | acceleration = 10.0; |
---|
| 73 | set_collision (new CollisionCluster (1.0, Vector(0,0,0))); |
---|
[1858] | 74 | } |
---|
| 75 | |
---|
[2190] | 76 | void Player::tick (float time) |
---|
[1872] | 77 | { |
---|
[2190] | 78 | // movement |
---|
| 79 | move (time); |
---|
[1872] | 80 | } |
---|
[1858] | 81 | |
---|
[2190] | 82 | void Player::hit (WorldEntity* weapon, Vector loc) |
---|
[1900] | 83 | { |
---|
| 84 | } |
---|
| 85 | |
---|
[2190] | 86 | void Player::destroy () |
---|
[1872] | 87 | { |
---|
| 88 | } |
---|
| 89 | |
---|
[2190] | 90 | void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) |
---|
[1872] | 91 | { |
---|
| 92 | } |
---|
| 93 | |
---|
[2190] | 94 | void Player::command (Command* cmd) |
---|
[1872] | 95 | { |
---|
[2636] | 96 | //printf("Player|recieved command [%s]\n", cmd->cmd); |
---|
[3172] | 97 | |
---|
[3155] | 98 | tottime=cmd->tottime; |
---|
[3172] | 99 | rottime=cmd->rottime; |
---|
| 100 | angleturn=cmd->angleturn; |
---|
| 101 | |
---|
| 102 | |
---|
[2636] | 103 | if( !strcmp( cmd->cmd, "up")) bUp = !cmd->bUp; |
---|
| 104 | else if( !strcmp( cmd->cmd, "down")) bDown = !cmd->bUp; |
---|
| 105 | else if( !strcmp( cmd->cmd, "left")) bLeft = !cmd->bUp; |
---|
| 106 | else if( !strcmp( cmd->cmd, "right")) bRight = !cmd->bUp; |
---|
| 107 | else if( !strcmp( cmd->cmd, "fire")) bFire = !cmd->bUp; |
---|
[1872] | 108 | } |
---|
| 109 | |
---|
[2190] | 110 | void Player::draw () |
---|
[2640] | 111 | { |
---|
[2551] | 112 | glMatrixMode(GL_MODELVIEW); |
---|
| 113 | glLoadIdentity(); |
---|
| 114 | float matrix[4][4]; |
---|
[3172] | 115 | float a[3]; |
---|
| 116 | a[0]=.8f; |
---|
| 117 | a[1]=.8f; |
---|
| 118 | a[2]=.8f; |
---|
| 119 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
[2551] | 120 | glTranslatef(get_placement()->r.x,get_placement()->r.y,get_placement()->r.z); |
---|
[2190] | 121 | get_placement()->w.matrix (matrix); |
---|
| 122 | glMultMatrixf ((float*)matrix); |
---|
[3172] | 123 | |
---|
[2860] | 124 | glMatrixMode (GL_MODELVIEW); |
---|
[3172] | 125 | glRotatef (angle,0,0,1); |
---|
[2860] | 126 | glRotatef (-90, 0,1,0); |
---|
[3172] | 127 | |
---|
[2860] | 128 | obj->draw(); |
---|
| 129 | // glCallList (objectList); |
---|
[2551] | 130 | |
---|
| 131 | |
---|
[2860] | 132 | |
---|
[2551] | 133 | //printf("Player@%f/%f/%f\n", get_placement()->r.x, get_placement()->r.y, get_placement()->r.z); |
---|
[2190] | 134 | } |
---|
[2036] | 135 | |
---|
[2190] | 136 | void Player::get_lookat (Location* locbuf) |
---|
[1896] | 137 | { |
---|
[2190] | 138 | *locbuf = *get_location(); |
---|
| 139 | //locbuf->dist += 5.0; |
---|
| 140 | } |
---|
[2036] | 141 | |
---|
[2190] | 142 | void Player::left_world () |
---|
| 143 | { |
---|
[1872] | 144 | } |
---|
| 145 | |
---|
[2190] | 146 | void Player::move (float time) |
---|
[1858] | 147 | { |
---|
[2551] | 148 | Vector accel(0.0, 0.0, 0.0); |
---|
| 149 | /* FIXME: calculating the direction and orthDirection every time_slice is redundant! save it somewhere */ |
---|
| 150 | Placement *pos = get_placement(); |
---|
| 151 | /* calculate the direction in which the craft is heading */ |
---|
| 152 | Vector direction(0.0, 0.0, 1.0); |
---|
| 153 | direction = pos->w.apply(direction); |
---|
| 154 | Vector orthDirection(0.0, 0.0, 1.0); |
---|
| 155 | orthDirection = orthDirection.cross(direction); |
---|
[3172] | 156 | |
---|
| 157 | if(rottime<1.0f) |
---|
| 158 | rottime=rottime+2.5f*time; |
---|
| 159 | else |
---|
| 160 | rottime=1.0f; |
---|
[3155] | 161 | if(tottime<1.5f) |
---|
| 162 | tottime=tottime+4.0f*time; |
---|
| 163 | else |
---|
| 164 | tottime=1.5f; |
---|
[3172] | 165 | |
---|
| 166 | //Langsam zurückdrehen ->anlgeturn ist für das Zurückdrehen verantwortlich! |
---|
| 167 | if((angleturn||bLeft&&bRight)&&angle<=0.0f){ |
---|
| 168 | if(!bLeft) |
---|
| 169 | angle=angle+rottime*10.0f; |
---|
| 170 | if(angle>0.0f){ |
---|
| 171 | |
---|
| 172 | angleturn=false; |
---|
| 173 | angle=0.0f;} |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | if((angleturn||bLeft&&bRight)&&angle>=0.0f){ |
---|
| 177 | if(!bRight) |
---|
| 178 | angle=angle-rottime*10.0f; |
---|
| 179 | if(angle<0.0f){ |
---|
| 180 | angleturn=false; |
---|
| 181 | angle=0.0f;} |
---|
| 182 | } |
---|
| 183 | //Bewegung lansam abbremsen ************** |
---|
| 184 | if(movfinishleft&&!bLeft){ |
---|
| 185 | accel=accel+(orthDirection*acceleration*(tottimecopy-pow(tottime,1.5f)+.5f)); |
---|
| 186 | if(tottime>tottimecopy) |
---|
| 187 | movfinishleft=false; |
---|
| 188 | } |
---|
| 189 | if(movfinishright&&!bRight){ |
---|
| 190 | accel=accel-(orthDirection*acceleration*(tottimecopy-pow(tottime,1.5f)+.5f)); |
---|
| 191 | if(tottime>tottimecopy) |
---|
| 192 | movfinishright=false; |
---|
| 193 | } |
---|
| 194 | if(movfinishup&&!bUp){ |
---|
| 195 | accel=accel+(direction*acceleration*(tottimecopy-pow(tottime,1.5f)+.5f)); |
---|
| 196 | if(tottime>tottimecopy) |
---|
| 197 | movfinishup=false; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | if(movfinishdown&&!bDown){ |
---|
| 201 | accel=accel-(direction*acceleration*(tottimecopy-pow(tottime,1.5f)+.5f)); |
---|
| 202 | if(tottime>tottimecopy) |
---|
| 203 | movfinishdown=false; |
---|
| 204 | } |
---|
| 205 | // ************** |
---|
| 206 | |
---|
| 207 | if( bUp) { accel = accel+(direction*acceleration*pow(tottime,2.0f));movfinishup=true;tottimecopy=tottime;} |
---|
| 208 | if( bDown) { accel = accel-(direction*acceleration*pow(tottime,2.0f));movfinishdown=true;tottimecopy=tottime; } |
---|
| 209 | if( bLeft ) { accel = accel + (orthDirection*acceleration*pow(tottime,2.0f)); |
---|
| 210 | if(angle>-40.0f){ |
---|
| 211 | angle=angle-rottime*40.0f;} |
---|
| 212 | movfinishleft=true; |
---|
| 213 | tottimecopy=tottime;} |
---|
| 214 | if( bRight ) { accel = accel - (orthDirection*acceleration*pow(tottime,2.0f)); |
---|
| 215 | if(angle<40.0f){ |
---|
| 216 | angle=angle+rottime*40.0f;} |
---|
| 217 | movfinishright=true; |
---|
| 218 | tottimecopy=tottime; |
---|
| 219 | } |
---|
[2551] | 220 | if( bAscend ) { /* not yet implemented but just: (0,0,1)*acceleration */} |
---|
| 221 | if( bDescend) {/* FIXME */} |
---|
| 222 | |
---|
| 223 | Location* l = get_location(); |
---|
| 224 | |
---|
| 225 | // r(t) = r(0) + v(0)*t + 1/2*a*t^2 |
---|
| 226 | // r = position |
---|
| 227 | // v = velocity |
---|
| 228 | // a = acceleration |
---|
| 229 | |
---|
| 230 | /* this the base-speed of the player: determines how fast and how the player follows the track*/ |
---|
| 231 | l->dist = l->dist + travel_speed * time; |
---|
| 232 | |
---|
| 233 | /* this updates the player position on the track - user interaction */ |
---|
[3155] | 234 | l->pos = l->pos + (accel*time); |
---|
[1896] | 235 | } |
---|
[1879] | 236 | |
---|
| 237 | |
---|
[1896] | 238 | |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | |
---|
| 243 | |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | |
---|