-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
153 lines (125 loc) · 2.99 KB
/
player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "player.hpp"
#include <iostream>
#include <cstdlib>
Vect3D Player::_gravity(0.0f, 0.0f, 0.02f);
Player::Player(World * w):Luciole(w), _world(w), _fly(false), _v(0.0f,0.0f,0.0f), _jump(false), _espace_presse(false)
{
spawn();
}
Player::~Player(){}
void Player::spawn()
{
int x = rand() % WORLDSIZEX, y = rand() % WORLDSIZEY, z = _world->hauteur(x, y) + 3.0f;
_eye(x, y, z);
}
void Player::draw()
{
Luciole::_phi = -Camera::_phi;
Luciole::_theta = Camera::_theta + 180.0f;
_pos = _center + Vect3D(0.0f, -LUCIOLE_WIDTH/2.0f, -0.2f);
Luciole::draw();
}
void Player::toucheJoueur(int key, int action)
{
switch (key) {
case GLFW_KEY_LEFT:
case GLFW_KEY_A:
_gauche_presse = (action == GLFW_PRESS) || (action == GLFW_REPEAT);
break;
case GLFW_KEY_UP:
case GLFW_KEY_W:
_avant_presse = (action == GLFW_PRESS) || (action == GLFW_REPEAT);
break;
case GLFW_KEY_RIGHT:
case GLFW_KEY_D:
_droite_presse = (action == GLFW_PRESS) || (action == GLFW_REPEAT);
break;
case GLFW_KEY_DOWN:
case GLFW_KEY_S:
_arriere_presse = (action == GLFW_PRESS) || (action == GLFW_REPEAT);
break;
case GLFW_KEY_PAGE_UP:
case GLFW_KEY_Q:
_haut_presse = (action == GLFW_PRESS) || (action == GLFW_REPEAT);
break;
case GLFW_KEY_PAGE_DOWN:
case GLFW_KEY_Z:
_bas_presse = (action == GLFW_PRESS) || (action == GLFW_REPEAT);
break;
case GLFW_KEY_SPACE:
if (!_jump)
{
_v(0.0f, 0.0f, 0.4f);
_jump = true;
}
break;
case GLFW_KEY_R:
if (action == GLFW_PRESS)
{
_fly = !_fly;
};
break;
}
}
void Player::move()
{
if (_fly)
{
Camera::move();
}
else
{
Vect3D av(cos(Camera::_theta * M_PI/180), sin(Camera::_theta * M_PI/180), 0.0f);
av.normalize();
Vect3D s = _haut * av;
s.normalize();
Vect3D pied = _eye - Vect3D(0.0f, 0.0f, 1.8f);
if (!_v)
{
if (_world->empty(pied - _gravity))
{
_v -= _gravity;
}
}
else
{
if (_world->empty(pied + _v))
{
pied += _v;
_v -= _gravity;
}
else
{
_v(0.0f, 0.0f, 0.0f);
_jump = false;
pied[3] = _world->hauteur(static_cast<unsigned int>(pied[0]), static_cast<unsigned int>(pied[1]));
}
}
_eye = pied + Vect3D(0.0f, 0.0f, 1.8f);
if (_avant_presse && _world->empty(pied + av * _vitesse))
{
_eye += av * _vitesse;
}
if (_gauche_presse && _world->empty(pied + s * _vitesse))
{
_eye += s * _vitesse;
}
if (_arriere_presse && _world->empty(pied - av * _vitesse))
{
_eye -= av * _vitesse;
}
if (_droite_presse && _world->empty(pied - s * _vitesse))
{
_eye -= s * _vitesse;
}
if (_haut_presse && _world->empty(pied + _haut * _vitesse))
{
_eye += _haut * _vitesse;
}
if (_bas_presse && _world->empty(pied - _haut * _vitesse))
{
_eye -= _haut * _vitesse;
}
_center = _eye + _avant;
}
}