-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
84 lines (62 loc) · 1.85 KB
/
Player.cpp
File metadata and controls
84 lines (62 loc) · 1.85 KB
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
#include "Player.h"
#include "HoverCraft.h"
#include "RaceTrack.h"
#include "Game.h"
Player::Player(RaceTrack* raceTrack) :
raceTrack(raceTrack) {
}
const vec3& Player::getCraftPosition() {
return craftPos;
}
const vec3& Player::getCraftDirection() {
return craftDir;
}
const vec3& Player::getCraftUp() {
return craftUp;
}
const vec2& Player::getCraftTrackOffset() {
return carTrackPos;
}
const vec2& Player::getCraftTrackDirection() {
return carTrackDirection;
}
void Player::init() {
hoverCraft = new HoverCraft();
hoverCraft->init();
carTrackPos = vec2(0.0, 0.0);
carTrackDirection = vec2(0.0, 1.0);
carVelocity = 0.0;
mSteeringRotation = 0.0;
update();
}
void Player::setCarVelocity(float velocity) {
carVelocity = velocity;
}
void Player::setSteeringRotation(float steer) {
mSteeringRotation = steer;
if (mSteeringRotation < -M_PI * 3 / 4)
mSteeringRotation = -M_PI * 3 / 4;
if (mSteeringRotation > M_PI * 3 / 4)
mSteeringRotation = M_PI * 3 / 4;
carTrackDirection = vec2(0, 1);
carTrackDirection.rotate(-mSteeringRotation * 0.25);
}
void Player::update() {
carTrackPos += (carTrackDirection * carVelocity) * Game::getSecondsPerFrame();
vec3 craftSize = hoverCraft->getSize();
float trackWidth = raceTrack->getWidth();
if (carTrackPos.x < -trackWidth / 2.0 + craftSize.x / 2.0)
carTrackPos.x = -trackWidth / 2.0 + craftSize.x / 2.0;
if (carTrackPos.x > trackWidth / 2.0 - craftSize.x / 2.0)
carTrackPos.x = trackWidth / 2.0 - craftSize.x / 2.0;
raceTrack->getCameraVectors(carTrackPos.y, craftPos, craftDir, craftUp);
craftPos += craftUp.cross(craftDir) * carTrackPos.x;
craftDir.rotateAroundAxis(craftUp, -mSteeringRotation);
craftUp.rotateAroundAxis(craftDir, mSteeringRotation);
}
void Player::render() {
hoverCraft->setPosition(craftPos);
hoverCraft->setDirection(craftDir);
hoverCraft->setUp(craftUp);
hoverCraft->render();
}