-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamestate.cpp
161 lines (144 loc) · 4.84 KB
/
gamestate.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
#include<iostream>
#include<sstream>
#include "definitions.h"
#include "gamestate.h"
#include "gameoverstate.h"
namespace Game {
GameState::GameState(GameDataRef data)
: _data(data) {}
void GameState::Init()
{
if (!_hitSoundBuffer.loadFromFile(HIT_SOUND_FILEPATH))
{
std::cerr << "Error loading hit sound effect." << std::endl;
}
if (!_pointSoundBuffer.loadFromFile(POINT_SOUND_FILEPATH))
{
std::cerr << "Error loading point sound effect." << std::endl;
}
if (!_wingSoundBuffer.loadFromFile(WING_SOUND_FILEPATH))
{
std::cerr << "Error loading wing sound effect." << std::endl;
}
_hitSound.setBuffer(_hitSoundBuffer);
_pointSound.setBuffer(_pointSoundBuffer);
_wingSound.setBuffer(_wingSoundBuffer);
_data->assets.LoadTexture("Game Background", GAME_BACKGROUND_FILEPATH);
_data->assets.LoadTexture("Pipe Up", PIPE_UP_BACKGROUND_FILEPATH);
_data->assets.LoadTexture("Pipe Down", PIPE_DOWN_BACKGROUND_FILEPATH);
_data->assets.LoadTexture("Scoring Pipe", SCORING_PIPE_FILEPATH);
_data->assets.LoadTexture("Bird Frame 1", BIRD_FRAME_1_FILEPATH);
_data->assets.LoadTexture("Bird Frame 2", BIRD_FRAME_2_FILEPATH);
_data->assets.LoadTexture("Bird Frame 3", BIRD_FRAME_3_FILEPATH);
_data->assets.LoadTexture("Bird Frame 4", BIRD_FRAME_4_FILEPATH);
_data->assets.LoadTexture("Land", LAND_FILEPATH);
_data->assets.LoadFont("Flappy Font", FLAPPY_FONT_FILEPATH);
pipe = new Pipe(_data);
land = new Land(_data);
bird = new Bird(_data);
flash = new Flash(_data);
hud = new Hud(_data);
_background.setTexture(this->_data->assets.GetTexture("Game Background"));
// _background.setTexture(this->_data->assets.GetTexture("Pipe Up Background"));
//_background.setTexture(this->_data->assets.GetTexture("Pipe Down Background"));
_gameState = GameStates::eReady;
_score = 0;
hud->UpdateScore(_score);
}
void GameState::HandleInput()
{
sf::Event event;
while (_data->window.pollEvent(event))
{
if (sf::Event::Closed == event.type)
{
_data->window.close();
//_data->machine.AddState(StateRef(new GameOverState(_data)),true);
}
if (_data->input.IsSpriteClicked(_background, sf::Mouse::Left, _data->window))
{
if (GameStates::eGameOver != _gameState)
{
_gameState = GameStates::ePlaying;
bird->Tap();
_wingSound.play();
}
}
}
}
void GameState::Update(float dt)
{
if (GameStates::eGameOver != _gameState)
{
bird->Animate(dt);
land ->MoveLand(dt);
}
if (GameStates::ePlaying == _gameState)
{
pipe ->MovePipes(dt);
if (_clock.getElapsedTime().asSeconds() > PIPE_SPAWN_FREQUENCY)
{
pipe->RandomisePipeOffset();
pipe->SpawnInvisiblePipe();
pipe->SpawnBottomPipe();
pipe->SpawnTopPipe();
pipe->SpawnScoringPipe();
_clock.restart();
}
bird->Update(dt);
std::vector<sf::Sprite> landSprites = land->GetSprites();
for (int i = 0; i < landSprites.size(); i++)
{
if (collision.CheckSpriteCollision(bird->GetSprite(), 0.7f, landSprites.at(i), 1.0f))
{
_gameState = GameStates::eGameOver;
_clock.restart();
_hitSound.play();
}
}
std::vector<sf::Sprite> pipeSprites = pipe->GetSprites();
for (int i = 0; i < pipeSprites.size(); i++)
{
if (collision.CheckSpriteCollision(bird->GetSprite(), 0.625f, pipeSprites.at(i), 1.0f))
{
_gameState = GameStates::eGameOver;
_clock.restart();
_hitSound.play();
}
}
if (GameStates::ePlaying == _gameState)
{
std::vector<sf::Sprite> &scoringPipes = pipe->GetScoringPipes();
for (int i = 0; i < scoringPipes.size(); i++)
{
if (collision.CheckSpriteCollision(bird->GetSprite(), 0.625f, scoringPipes.at(i), 1.0f))
{
_score++;
hud->UpdateScore(_score);
scoringPipes.erase(scoringPipes.begin() + i);
_pointSound.play();
}
}
}
}
if (GameStates::eGameOver == _gameState)
{
flash->Show(dt);
if (_clock.getElapsedTime().asSeconds() > TIME_BEFORE_GAME_OVER_APPEARS)
{
_data->machine.AddState(StateRef(new GameOverState(_data, _score)), true);
}
}
}
void GameState::Draw(float dt)
{
_data->window.clear();
_data->window.draw(_background);
pipe->DrawPipes();
land->DrawLand();
bird->Draw();
flash->Draw();
hud->Draw();
_data->window.display();
}
}