-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandleInput.cpp
173 lines (163 loc) · 4.2 KB
/
HandleInput.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "ZombieArena.hpp"
//#include "Player.hpp"
//#include "TextureHolder.hpp"
//#include "Bullet.hpp"
//#include "Pickup.hpp"
void handleEvents(Event& event, State& state, Clock& clock, Player* player, t_sound& sounds, t_stat& stat)
{
if (event.type == Event::KeyPressed)
{
// Pause a game while playing
if (event.key.code == Keyboard::Return &&
state == State::PLAYING)
{
state = State::PAUSED;
}
// Restart while paused
else if (event.key.code == Keyboard::Return &&
state == State::PAUSED)
{
state = State::PLAYING;
// Reset the clock so there isn't a frame jump
clock.restart();
}
// Start a new game while in GAME_OVER state
else if (event.key.code == Keyboard::Return &&
state == State::GAME_OVER)
{
state = State::LEVELING_UP;
stat.wave = 0;
stat.score = 0;
// Prepare the gun and ammo for next game
stat.currentBullet = 0;
stat.bulletsSpare = 24;
stat.bulletsInClip = 6;
stat.clipSize = 6;
stat.fireRate = 1;
// Reset the player's stats
player->resetPlayerStats();
}
}
}
void handleMove(Player* player, t_stat& stat, t_sound& sounds, Bullet bullets[], t_time& time, Vector2f mouseWorldPosition)
{
static bool isReloading = false;
// Handle the pressing and releasing of WASD keys
if (Keyboard::isKeyPressed(Keyboard::W))
player->moveUp();
else
player->stopUp();
if (Keyboard::isKeyPressed(Keyboard::S))
player->moveDown();
else
player->stopDown();
if (Keyboard::isKeyPressed(Keyboard::A))
player->moveLeft();
else
player->stopLeft();
if (Keyboard::isKeyPressed(Keyboard::D))
player->moveRight();
else
player->stopRight();
// Fire a bullet
if (Mouse::isButtonPressed(sf::Mouse::Left))
{
if (time.gameTimeTotal.asMilliseconds()
- time.lastPressed.asMilliseconds()
> 1000 / stat.fireRate && stat.bulletsInClip > 0)
{
bullets[stat.currentBullet].shoot(
player->getCenter().x, player->getCenter().y,
mouseWorldPosition.x, mouseWorldPosition.y);
stat.currentBullet++;
if (stat.currentBullet > 99)
stat.currentBullet = 0;
time.lastPressed = time.gameTimeTotal;
sounds.shoot.play();
stat.bulletsInClip--;
}
}
// Reloading
if (Mouse::isButtonPressed(sf::Mouse::Right) && !isReloading)
{
isReloading = true;
if (stat.bulletsSpare >= stat.clipSize)
{
stat.bulletsInClip = stat.clipSize;
stat.bulletsSpare -= stat.clipSize;
sounds.reload.play();
}
else if (stat.bulletsSpare > 0)
{
stat.bulletsInClip = stat.bulletsSpare;
stat.bulletsSpare = 0;
sounds.reload.play();
}
else
{
sounds.reloadFailed.play();
}
}
else if (!Mouse::isButtonPressed(sf::Mouse::Right))
isReloading = false;
}
State handleLevelUp(Event event, t_stat& stat, Player* player, State& state, Pickup* healthPickup, Pickup* ammoPickup)
{
// Handle the player LEVELING up
if (event.key.code == Keyboard::Num1)
{
stat.fireRate++;
state = State::PLAYING;
}
else if (event.key.code == Keyboard::Num2)
{
stat.clipSize += stat.clipSize;
state = State::PLAYING;
}
else if (event.key.code == Keyboard::Num3)
{
player->upgradeHealth();
state = State::PLAYING;
}
else if (event.key.code == Keyboard::Num4)
{
player->upgradeSpeed();
state = State::PLAYING;
}
else if (event.key.code == Keyboard::Num5)
{
healthPickup->upgrade();
state = State::PLAYING;
}
else if (event.key.code == Keyboard::Num6)
{
ammoPickup->upgrade();
state = State::PLAYING;
}
return state;
}
void setNewLevel(State state, t_stat& stat, IntRect& arena, Player* player, VertexArray& background,
Vector2f resolution, Pickup* healthPickup, Pickup* ammoPickup, Sound powerup)
{
if (state == State::PLAYING)
{
stat.wave++;
arena.width = 500 * stat.wave;
arena.height = 500 * stat.wave;
arena.left = 0;
arena.top = 0;
int tileSize = createBackground(background, arena);
// Spawn the player in middle of the arena
player->spawn(arena, resolution, tileSize);
// Create a horde of zombies
// Configure the pick-ups
healthPickup->setArena(arena);
ammoPickup->setArena(arena);
stat.numZombies = 5 * stat.wave;
//// Delete the previously allocated memory (if it exists)
//delete[] zombies;
//zombies = createHorde(numZombies, arena);
stat.numZombiesAlive = stat.numZombies;
powerup.play();
}
}