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 <string.h> |
---|
22 | |
---|
23 | using namespace std; |
---|
24 | |
---|
25 | // initialize the pointers used for is_a identification |
---|
26 | bool (*Player::basefunc)(char*) = WorldEntity::is_a; |
---|
27 | char *Player::is = "Player"; |
---|
28 | |
---|
29 | Player::Player(bool isFree) : WorldEntity(isFree) |
---|
30 | { |
---|
31 | } |
---|
32 | |
---|
33 | Player::~Player () |
---|
34 | { |
---|
35 | } |
---|
36 | |
---|
37 | void Player::post_spawn () |
---|
38 | { |
---|
39 | travel_speed = 10.0; |
---|
40 | velocity = Vector(); |
---|
41 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
---|
42 | bFire = false; |
---|
43 | acceleration = 10.0; |
---|
44 | set_collision (new CollisionCluster (1.0, Vector(0,0,0))); |
---|
45 | } |
---|
46 | |
---|
47 | void Player::tick (float time) |
---|
48 | { |
---|
49 | // movement |
---|
50 | move (time); |
---|
51 | } |
---|
52 | |
---|
53 | void Player::hit (Damage* dmg, WorldEntity* instigator, Uint32 hitflags) |
---|
54 | { |
---|
55 | } |
---|
56 | |
---|
57 | void Player::destroy () |
---|
58 | { |
---|
59 | } |
---|
60 | |
---|
61 | void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) |
---|
62 | { |
---|
63 | } |
---|
64 | |
---|
65 | void Player::command (Command* cmd) |
---|
66 | { |
---|
67 | printf("Player|recieved command [%s]\n", cmd->cmd); |
---|
68 | if( !strcmp( cmd->cmd, "up")) bUp = !cmd->bUp; |
---|
69 | else if( !strcmp( cmd->cmd, "down")) bDown = !cmd->bUp; |
---|
70 | else if( !strcmp( cmd->cmd, "left")) bLeft = !cmd->bUp; |
---|
71 | else if( !strcmp( cmd->cmd, "right")) bRight = !cmd->bUp; |
---|
72 | else if( !strcmp( cmd->cmd, "fire")) bFire = !cmd->bUp; |
---|
73 | } |
---|
74 | |
---|
75 | void Player::draw () |
---|
76 | { |
---|
77 | glMatrixMode(GL_MODELVIEW); |
---|
78 | glLoadIdentity(); |
---|
79 | float matrix[4][4]; |
---|
80 | |
---|
81 | glTranslatef(get_placement()->r.x,get_placement()->r.y,get_placement()->r.z); |
---|
82 | get_placement()->w.matrix (matrix); |
---|
83 | glMultMatrixf ((float*)matrix); |
---|
84 | |
---|
85 | glBegin(GL_TRIANGLES); |
---|
86 | glColor3f(1,0,0); |
---|
87 | glVertex3f(0,0,0.5); |
---|
88 | glColor3f(0,1,0); |
---|
89 | glVertex3f(-0.5,0,-1); |
---|
90 | glColor3f(0,0,1); |
---|
91 | glVertex3f(0.5,0,-1); |
---|
92 | glEnd(); |
---|
93 | |
---|
94 | printf("Player is a Player: %d\n", is_a("Player")); |
---|
95 | printf("Player is a WorldEntity: %d\n", is_a("WorldEntity")); |
---|
96 | printf("Player is a Banana: %d\n", is_a("Banana")); |
---|
97 | printf("Player@%f/%f/%f\n", get_placement()->r.x, get_placement()->r.y, get_placement()->r.z); |
---|
98 | } |
---|
99 | |
---|
100 | void Player::get_lookat (Location* locbuf) |
---|
101 | { |
---|
102 | *locbuf = *get_location(); |
---|
103 | //locbuf->dist += 5.0; |
---|
104 | } |
---|
105 | |
---|
106 | void Player::left_world () |
---|
107 | { |
---|
108 | } |
---|
109 | |
---|
110 | void Player::move (float time) |
---|
111 | { |
---|
112 | float xAccel, yAccel, zAccel; |
---|
113 | xAccel = yAccel = zAccel = 0; |
---|
114 | if( bUp) xAccel += acceleration; |
---|
115 | if( bDown) xAccel -= acceleration; |
---|
116 | if( bLeft) yAccel += acceleration; |
---|
117 | if( bRight) yAccel -= acceleration; |
---|
118 | if( bAscend) zAccel += acceleration; |
---|
119 | if( bDescend) zAccel -= acceleration; |
---|
120 | |
---|
121 | Vector accel( xAccel, yAccel, zAccel); |
---|
122 | |
---|
123 | Location* l = get_location(); |
---|
124 | |
---|
125 | // r(t) = r(0) + v(0)*t + 1/2*a*t^2 |
---|
126 | // r = position |
---|
127 | // v = velocity |
---|
128 | // a = acceleration |
---|
129 | |
---|
130 | l->pos = l->pos + velocity*time + (accel*(0.5*time*time)); |
---|
131 | l->dist += travel_speed * time; |
---|
132 | |
---|
133 | velocity = velocity + (accel * time); |
---|
134 | printf("Player|velocity %f/%f/%f\n", velocity.x, velocity.y, velocity.z); |
---|
135 | } |
---|
136 | |
---|
137 | bool Player::is_a(char* name) |
---|
138 | { |
---|
139 | if( !strcmp( name, is)) return true; |
---|
140 | else return basefunc (name); |
---|
141 | } |
---|