-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.cpp
135 lines (116 loc) · 3.26 KB
/
Game.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
#include "Game.hpp"
#include "RectangleFunctions.hpp"
#include "Renderer.hpp"
namespace TappyPlane {
using namespace SDLW::Video;
using namespace SDLW::Events;
constexpr auto backgroundSpritesheetHandle = "background";
constexpr auto backgroundBounds = canvasBounds;
constexpr auto backgroundScrollSpeed = 0.5f;
constexpr auto groundSpritesheetHandle = "ground";
constexpr Rectangle groundBounds{0, canvasBounds.height() - 270,
canvasBounds.width(), 270};
constexpr auto groundScrollSpeed = 2.0f;
constexpr int spikePairDistance{420};
constexpr int spikesScrollSpeed{5};
constexpr auto getReadySpritesheetHandle = "getReady";
constexpr Rectangle getReadyBounds{canvasBounds.width() / 2 - 907 / 2,
canvasBounds.height() / 2 - 163 / 2, 907, 163};
constexpr auto gameOverSpritesheetHandle = "gameOver";
constexpr Rectangle gameOverBounds{canvasBounds.width() / 2 - 934 / 2,
canvasBounds.height() / 2 - 176 / 2, 934, 176};
Game::Game()
: mGameState(State::GetReady)
, mBackground(backgroundSpritesheetHandle, backgroundBounds,
backgroundScrollSpeed)
, mGround(groundSpritesheetHandle, groundBounds, groundScrollSpeed)
, mSpikes(spikePairDistance, spikesScrollSpeed)
, mGetReadySprite(getReadySpritesheetHandle, getReadyBounds)
, mGameOverSprite(gameOverSpritesheetHandle, gameOverBounds)
{
}
void Game::handleEvent(const Event& event) noexcept
{
if (event.type() == Event::Type::MouseButtonDown) {
switch (mGameState) {
case State::GetReady:
start();
break;
case State::InProgress:
mPlane.jump();
break;
case State::GameOver:
reset();
break;
default:
break;
}
}
}
void Game::update() noexcept
{
if (mGameState != State::InProgress) {
return;
}
mBackground.update();
mGround.update();
mSpikes.update();
mPlane.update();
if (hasPlaneCrashed()) {
mBackground.pause();
mGround.pause();
mSpikes.pause();
mPlane.pause();
mGameState = State::GameOver;
}
}
void Game::draw() const
{
mBackground.draw();
mPlane.draw();
mSpikes.draw();
mGround.draw();
if (mGameState == State::GetReady) {
mGetReadySprite.draw();
} else if (mGameState == State::GameOver) {
mGameOverSprite.draw();
}
}
void Game::start()
{
mBackground.play();
mSpikes.play();
mGround.play();
mPlane.play();
mGameState = State::InProgress;
}
void Game::reset()
{
mPlane.reset();
mSpikes.reset();
mGameState = State::GetReady;
}
static bool areColliding(const Plane& plane, const Spikes& spikes)
{
for (auto[first, last] = spikes.bounds(); first < last; ++first) {
if (areIntersecting(plane.bounds(), first->topSpikeBounds)
|| areIntersecting(plane.bounds(), first->bottomSpikeBounds)) {
return true;
}
}
return false;
}
bool Game::hasPlaneCrashed()
{
return areColliding(mPlane, mSpikes) || didPlaneHitTheGround()
|| didPlaneHitTheCeiling();
}
bool Game::didPlaneHitTheGround()
{
return areIntersecting(mPlane.bounds(), mGround.bounds());
}
bool Game::didPlaneHitTheCeiling()
{
return topOf(mPlane.bounds()) < topOf(canvasBounds);
}
} // namespace TappyPlane