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