-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayState.h
92 lines (82 loc) · 2.26 KB
/
PlayState.h
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
#pragma once
#include "GameState.h"
#include "Game.h"
#include "EntityManager.h"
#include "Ball.h"
#include "Block.h"
#include "Paddle.h"
#include "Collision.h"
#include "SFML\Graphics.hpp"
class PlayState : public GameState
{
public:
void init(Game* game)
{
auto windowWidth = game->getWindow().getSize().x;
auto windowHeight = game->getWindow().getSize().y;
sf::Vector2f blockSize{ 60.f, 20.f };
auto countBlocksX = 11, countBlocksY = 4;
auto margin = 3;
auto padding = ((windowWidth - ((blockSize.x + margin) * countBlocksX)) / 2.f) - blockSize.x / 2.f;
m_entityManager.clear();
for (int i = 1; i <= countBlocksX; ++i)
{
for (int j = 1; j <= countBlocksY; ++j)
{
auto& block(m_entityManager.create<Block>(sf::Vector2f(i * (blockSize.x + margin) + padding, j * (blockSize.y + margin)), blockSize));
block.setRequiredHits(1 + (i + j) % 3);
}
}
m_entityManager.create<Paddle>(sf::Vector2f(windowWidth / 2.f, windowHeight - 30.f));
m_entityManager.create<Ball>(sf::Vector2f(windowWidth / 2.f, windowHeight / 2.f));
}
void cleanUp() { }
void pause() { }
void resume() { }
void handleEvent(Game* game)
{
sf::Event event;
while (game->getWindow().pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
game->setStatus(Status::Ending);
game->getWindow().close();
break;
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Key::Escape)
game->setStatus(Status::Ending);
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Key::R)
init(game);
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Key::Space)
{
m_entityManager.forEach<Ball>([](Ball& ball)
{
ball.setVelocity({ 0.6f, -0.6f });
});
}
}
}
void update(Game* game, float deltaTime)
{
m_entityManager.update(deltaTime);
m_entityManager.forEach<Ball>([this](Ball& ball)
{
m_entityManager.forEach<Block>([&ball](Block& block)
{
checkBlockBallCollision(block, ball);
});
m_entityManager.forEach<Paddle>([&ball](Paddle& paddle)
{
checkPaddleBallCollision(paddle, ball);
});
});
m_entityManager.refresh();
}
void draw(Game* game)
{
m_entityManager.draw(game->getWindow());
}
private:
EntityManager m_entityManager;
};