| 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 | #include "player.h" |
|---|
| 19 | #include "stdincl.h" |
|---|
| 20 | #include "collision.h" |
|---|
| 21 | #include "importer/objModel.h" |
|---|
| 22 | |
|---|
| 23 | using namespace std; |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | Player::Player(bool isFree) : WorldEntity(isFree) |
|---|
| 27 | { |
|---|
| 28 | |
|---|
| 29 | this->model = new OBJModel("../data/models/reaplow.obj"); |
|---|
| 30 | /* |
|---|
| 31 | objectList = glGenLists(1); |
|---|
| 32 | glNewList (objectList, GL_COMPILE); |
|---|
| 33 | |
|---|
| 34 | glBegin(GL_TRIANGLES); |
|---|
| 35 | glColor3f(1,1,1); |
|---|
| 36 | glVertex3f(0,0,0.5); |
|---|
| 37 | glVertex3f(-0.5,0,-1); |
|---|
| 38 | glVertex3f(0.5,0,-1); |
|---|
| 39 | |
|---|
| 40 | glVertex3f(0,0,0.5); |
|---|
| 41 | glVertex3f(0,0.5,-1); |
|---|
| 42 | glVertex3f(0,-0.5,-1); |
|---|
| 43 | glEnd(); |
|---|
| 44 | |
|---|
| 45 | glBegin(GL_QUADS); |
|---|
| 46 | glColor3f(0,0,1); |
|---|
| 47 | glVertex3f(0.5,0.5,-1); |
|---|
| 48 | glVertex3f(0.5,-0.5,-1); |
|---|
| 49 | glVertex3f(-0.5,-0.5,-1); |
|---|
| 50 | glVertex3f(-0.5,0.5,-1); |
|---|
| 51 | glEnd(); |
|---|
| 52 | |
|---|
| 53 | glEndList (); |
|---|
| 54 | */ |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | Player::~Player () |
|---|
| 58 | { |
|---|
| 59 | delete this->model; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | void Player::postSpawn () |
|---|
| 63 | { |
|---|
| 64 | travelSpeed = 15.0; |
|---|
| 65 | velocity = Vector(); |
|---|
| 66 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
|---|
| 67 | bFire = false; |
|---|
| 68 | acceleration = 20.0; |
|---|
| 69 | setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void Player::tick (float time) |
|---|
| 73 | { |
|---|
| 74 | // movement |
|---|
| 75 | this->move (time); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void Player::hit (WorldEntity* weapon, Vector loc) |
|---|
| 79 | { |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | void Player::destroy () |
|---|
| 83 | { |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) |
|---|
| 87 | { |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | void Player::command (Command* cmd) |
|---|
| 91 | { |
|---|
| 92 | //printf("Player|recieved command [%s]\n", cmd->cmd); |
|---|
| 93 | if( !strcmp( cmd->cmd, "up")) bUp = !cmd->bUp; |
|---|
| 94 | else if( !strcmp( cmd->cmd, "down")) bDown = !cmd->bUp; |
|---|
| 95 | else if( !strcmp( cmd->cmd, "left")) bLeft = !cmd->bUp; |
|---|
| 96 | else if( !strcmp( cmd->cmd, "right")) bRight = !cmd->bUp; |
|---|
| 97 | else if( !strcmp( cmd->cmd, "fire")) bFire = !cmd->bUp; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | void Player::draw () |
|---|
| 101 | { |
|---|
| 102 | glMatrixMode(GL_MODELVIEW); |
|---|
| 103 | glLoadIdentity(); |
|---|
| 104 | float matrix[4][4]; |
|---|
| 105 | //glRotatef(90.0f,1.0f,0.0f,0.0f); |
|---|
| 106 | /* translate */ |
|---|
| 107 | glTranslatef (this->getAbsCoor ().x, |
|---|
| 108 | this->getAbsCoor ().y, |
|---|
| 109 | this->getAbsCoor ().z); |
|---|
| 110 | /* rotate */ |
|---|
| 111 | this->getAbsDir ().matrix (matrix); |
|---|
| 112 | glMultMatrixf((float*)matrix); |
|---|
| 113 | glRotatef(angle,1.0f,0.0f,0.0f); |
|---|
| 114 | glMatrixMode(GL_MODELVIEW); |
|---|
| 115 | this->model->draw(); |
|---|
| 116 | // glCallList(objectList); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | /*PN |
|---|
| 121 | void Player::getLookat(Location* locbuf) |
|---|
| 122 | { |
|---|
| 123 | *locbuf = *getLocation(); |
|---|
| 124 | //locbuf->dist += 5.0; |
|---|
| 125 | } |
|---|
| 126 | */ |
|---|
| 127 | |
|---|
| 128 | void Player::leftWorld () |
|---|
| 129 | { |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | void Player::move (float time) |
|---|
| 133 | { |
|---|
| 134 | |
|---|
| 135 | Vector accel(0.0, 0.0, 0.0); |
|---|
| 136 | |
|---|
| 137 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
|---|
| 138 | //Placement *pos = getPlacement(); |
|---|
| 139 | |
|---|
| 140 | /* calculate the direction in which the craft is heading */ |
|---|
| 141 | Vector direction (1.0, 0.0, 0.0); |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | //direction = this->absDirection.apply (direction); |
|---|
| 146 | Vector orthDirection (0.0, 0.0, 1.0); |
|---|
| 147 | |
|---|
| 148 | //orthDirection = orthDirection.cross (direction); |
|---|
| 149 | |
|---|
| 150 | if( bUp) { accel = accel+(direction*acceleration);accelSlipDirection=accel; } |
|---|
| 151 | if( bDown) { accel = accel-(direction*acceleration);accelSlipDirection=accel; } |
|---|
| 152 | if( bLeft ) {if(angle<0) {accel = accel-(orthDirection*acceleration);}rotate(time);accelSlip=accel; } |
|---|
| 153 | if( bRight ) { if(angle>0){accel = accel+(orthDirection*acceleration);}rotate(time);accelSlip=accel;} |
|---|
| 154 | if( bAscend ) { /* not yet implemented but just: (0,0,1)*acceleration */} |
|---|
| 155 | if( bDescend) {/* FIXME */} /* \todo up and down player movement */ |
|---|
| 156 | if((int)angle!=0&&!bLeft&&!bRight){backRotate(time);accelSlip=accelSlip-slip(time,accelSlip);} |
|---|
| 157 | if(!bUp&&!bDown){accelSlipDirection=accelSlipDirection-slip(time,accelSlipDirection);} |
|---|
| 158 | |
|---|
| 159 | //Location* l = getLocation(); |
|---|
| 160 | |
|---|
| 161 | // r(t) = r(0) + v(0)*t + 1/2*a*t^2 |
|---|
| 162 | // r = position |
|---|
| 163 | // v = velocity |
|---|
| 164 | // a = acceleration |
|---|
| 165 | |
|---|
| 166 | /* this the base-speed of the player: determines how fast and how the player follows the track*/ |
|---|
| 167 | //l->dist = l->dist + travelSpeed * time; |
|---|
| 168 | |
|---|
| 169 | //Vector* shift = new Vector (this->travelSpeed * time, 0, 0); |
|---|
| 170 | Vector shift(this->travelSpeed*time,0,0); |
|---|
| 171 | |
|---|
| 172 | this->shiftCoor (&shift); |
|---|
| 173 | |
|---|
| 174 | /* this updates the player position on the track - user interaction */ |
|---|
| 175 | //l->pos = l->pos + accel*time; |
|---|
| 176 | |
|---|
| 177 | Vector move = accel * time; |
|---|
| 178 | this->shiftCoor (&move); |
|---|
| 179 | |
|---|
| 180 | } |
|---|
| 181 | void Player::rotate(float time){ |
|---|
| 182 | if(bLeft&&angle>-40.0f){ |
|---|
| 183 | angle=angle-time*200.0f; |
|---|
| 184 | |
|---|
| 185 | } |
|---|
| 186 | if(bRight&&angle<40.0f){ |
|---|
| 187 | angle=angle+time*200.0f; |
|---|
| 188 | |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | void Player::backRotate(float time){ |
|---|
| 193 | if(angle<0){ |
|---|
| 194 | angle=angle+3.0f*(exp(angle*.0015f*time)); |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | } |
|---|
| 198 | if(angle>0){ |
|---|
| 199 | angle=angle-3.0f*(exp(-angle*.0015f*time)); |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | } |
|---|
| 205 | //When the sidewords movement is finished, the ship will still slip untill it stands still |
|---|
| 206 | Vector Player::slip(float time,Vector accel){ |
|---|
| 207 | Vector move= accel*time; |
|---|
| 208 | this->shiftCoor(&move); |
|---|
| 209 | return accel*time*3.0f; |
|---|
| 210 | |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|