-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.cpp
148 lines (117 loc) · 4.01 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
#include "Engine.h"
#include "Player.h"
#include "BombCreator.h"
Player::Player(size_t id, Position initial_position, PT::PlayerType type, PlayerControllerPtr controller)
: m_id(id),
m_position(initial_position),
m_direction(0,0),
m_type(type),
m_current_action_start_time(SDL_GetTicks()),
m_is_dying(false),
m_all_bomb_count(g_bomb_player_has_at_start),
m_used_bomb_count(0),
m_flame_range(g_flame_range_player_has_at_start) {
SetController(controller);
}
void Player::Draw() {
TexCoords tc = Engine::Get().Scripts()->GetPlayerSprite(GetType(),
GetDirection(),
IsDying(),
SDL_GetTicks() - m_current_action_start_time);
// std::cerr << "type = " << GetType() << ", direction = " << GetDirection().x() << ", " << GetDirection().y()
// << ", is dying = " << IsDying() << "dt = " << SDL_GetTicks() - m_current_action_start_time << "\n";
// std::cerr << "tc = " << tc.left << ", " << tc.bottom << ", " << tc.width << ", " << tc.height << "\n";
Engine::Get().Renderer()->DrawSprite(tc, GetPosition());
if (g_render_aabbs)
Engine::Get().Renderer()->DrawAABB(GetAABB());
}
void Player::Update(const DataForController& /* data */, double dt) {
m_position = GetNextPosition(dt); // (1)
PerformAction(GetController()->GetNextAction()); // (2)
GetController()->ResetNextAction();
// (1) & (2) must be in that order. Otherwise it is possible to walk
// through the walls by pressing keys quickly
}
void Player::GivePowerup(PowerupPtr powerup) {
powerup->Affect(*this);
}
void Player::Burnt() {
#warning TODO: implement what happens when player is in fire
}
Position Player::GetNextPosition(double dt) const {
const double speed = 7;
const Position pos = GetPosition();
const Direction dir = GetDirection();
return Position(pos.x + dir.x() * dt * speed,
pos.y + dir.y() * dt * speed);
}
AABB Player::GetAABB() const {
// bounding box is a bit less then player sprite
const Position pos = GetCenterPosition();
const Position min = pos - Position( .35, .4);
const Position max = pos + Position( .35, .25);
return AABB(min,max);
}
AABB Player::GetNextAABB(double dt) const {
// bounding box is a bit less then player sprite
const Position pos = GetNextCenterPosition(dt);
const Position min = pos - Position( .37, .42);
const Position max = pos + Position( .37, .25);
return AABB(min,max);
}
void Player::PerformAction(PA::PlayerAction action) {
if (action != PA::None) {
m_current_action_start_time = SDL_GetTicks();
}
const double epsilon = 0.01; // close to 0 but not 0. Just in case :)
// "Go" actions
if (action == PA::GoLeft) {
m_direction.SetX(-1);
}
else if (action == PA::GoRight) {
m_direction.SetX(1);
}
else if (action == PA::GoUp) {
m_direction.SetY(1);
}
else if (action == PA::GoDown) {
m_direction.SetY(-1);
}
else if (action == PA::GoNowhere) {
m_direction.Set(0,0);
}
// "Stop" Actions
else if (action == PA::StopGoLeft && m_direction.x()<epsilon) {
m_direction.SetX(0);
}
else if (action == PA::StopGoRight && m_direction.x()>epsilon) {
m_direction.SetX(0);
}
else if (action == PA::StopGoUp && m_direction.y()>epsilon) {
m_direction.SetY(0);
}
else if (action == PA::StopGoDown && m_direction.y()<epsilon) {
m_direction.SetY(0);
}
// "Bomb" actions
else if (action == PA::PlaceBomb) {
PlaceBomb();
}
// Other Actions
else if (action == PA::None) {
}
else {
std::cerr << "Got unknown action! [" << action << "]\n";
}
}
void Player::PlaceBomb() {
PlaceBomb(GetPosition(), GetFlameRange());
}
void Player::PlaceBomb(const Position& position, size_t range) {
if (GetAvailableBombCount()>0) { // can place?
AddCreator(CreatorPtr(new BombCreator(GetId(), position, range)));
}
}
bool Player::HandleInput(const SDL_Event& event) {
return GetController()->HandleInput(event);
}