This repository has been archived by the owner on Apr 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
159 lines (131 loc) · 3.48 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
154
155
156
157
158
159
#include "Player.h"
Player::Player() : Entity("./gfx/ghostie.bmp")
{
velocity = 4;
lives = 3;
bombs = 3;
// Init the player at this position
SurfaceRectangle.x = 400;
SurfaceRectangle.y = 300;
// Set collision rectangle
SurfaceRectangle.h = 40;
SurfaceRectangle.w = 40;
// A map for checking if key is pressed down, and move if it is
isPressed["SDLK_w"] = false;
isPressed["SDLK_a"] = false;
isPressed["SDLK_s"] = false;
isPressed["SDLK_d"] = false;
isPressed["mouse1"] = false;
ShieldUp = false;
}
Player::~Player()
{
}
void Player::move() {
// Move the player accordingly
if (isPressed["SDLK_w"] && SurfaceRectangle.y > 0)
SurfaceRectangle.y -= velocity;
else if (isPressed["SDLK_s"] && SurfaceRectangle.y < (600 - SurfaceRectangle.h))
SurfaceRectangle.y += velocity;
if (isPressed["SDLK_a"] && SurfaceRectangle.x > 0)
SurfaceRectangle.x -= velocity;
else if (isPressed["SDLK_d"] && SurfaceRectangle.x < (800 - SurfaceRectangle.w))
SurfaceRectangle.x += velocity;
if (isPressed["mouse1"]) {
}
}
void Player::check_events(SDL_Event &event)
{
// Get keyinput
// Check if it's pressed or released
if (event.type == SDL_KEYDOWN) {
// Check up-down movement
if (event.key.keysym.sym == SDLK_w)
isPressed["SDLK_w"] = true;
else if (event.key.keysym.sym == SDLK_s)
isPressed["SDLK_s"] = true;
// Check left-right movement
if (event.key.keysym.sym == SDLK_a)
isPressed["SDLK_a"] = true;
else if (event.key.keysym.sym == SDLK_d)
isPressed["SDLK_d"] = true;
}
else if (event.type == SDL_KEYUP) {
// Check up-down movement
if (event.key.keysym.sym == SDLK_w)
isPressed["SDLK_w"] = false;
else if (event.key.keysym.sym == SDLK_s)
isPressed["SDLK_s"] = false;
// Check left-right movement
if (event.key.keysym.sym == SDLK_a)
isPressed["SDLK_a"] = false;
else if (event.key.keysym.sym == SDLK_d)
isPressed["SDLK_d"] = false;
}
// If you click one of the mouse-buttons
if (event.type == SDL_MOUSEBUTTONUP) {
// If the player clicks mouse1, shoot
if (event.button.button == SDL_BUTTON_LEFT) {
isPressed["mouse1"] = false;
new Projectile(SurfaceRectangle.x + (SurfaceRectangle.w / 2),
SurfaceRectangle.y + (SurfaceRectangle.h / 2),
event.button.x, event.button.y);
}
if (event.button.button == SDL_BUTTON_RIGHT && bombs > 0)
{
// Find and delete the enemy from the Entity- and Enemylist
std::list<Enemy*>::iterator eit = Enemy::EnemyList.begin();
std::list<Entity*>::iterator it2 = Entity::EntityList.begin();
for (; eit != Enemy::EnemyList.end(); eit++)
{
for (; it2 != Entity::EntityList.end(); it2++) {
if (*it2 == *eit)
{
Entity *del2 = *it2;
eit = Enemy::EnemyList.erase(eit);
Entity::EntityList.erase(it2);
delete del2;
it2 = Entity::EntityList.begin();
}
}
}
bombs--;
}
}
/* new Projectile(SurfaceRectangle.x + (SurfaceRectangle.w / 2),
SurfaceRectangle.y + (SurfaceRectangle.h / 2),
event.button.x, event.button.y);
*/
// If player clicks mouse2, check if he has bombs left, and remove all entites except player
}
std::string Player::get_type()
{
return "Player";
}
int Player::get_lives() const
{
return lives;
}
void Player::set_lives(int change)
{
lives += change;
}
bool Player::get_shield_up()
{
return ShieldUp;
}
void Player::set_shield_up(bool value)
{
if (value)
ShieldUp = true;
else
ShieldUp = false;
}
void Player::add_bombs()
{
bombs++;
}
int Player::get_bombs() const
{
return bombs;
}