-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpackman.cpp
68 lines (62 loc) · 1.86 KB
/
packman.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
#include "packman.h"
#include "field.h"
static const sf::Color PACKMAN_COLOR = sf::Color(255, 216, 0);
static const float PACKMAN_SPEED = 120.f; // pixels per second.
static const float PACKMAN_RADIUS = 12.5f; // pixels
static void updatePackmanDirection(Packman &packman)
{
packman.direction = Direction::NONE;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
packman.direction = Direction::UP;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
packman.direction = Direction::DOWN;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
packman.direction = Direction::LEFT;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
packman.direction = Direction::RIGHT;
}
}
void initializePackman(Packman &packman)
{
packman.direction = Direction::NONE;
packman.shape.setRadius(PACKMAN_RADIUS);
packman.shape.setFillColor(PACKMAN_COLOR);
packman.shape.setPosition(getPackmanStartPosition());
}
void updatePackman(Packman &packman, float elapsedTime, const Field &field)
{
const float step = PACKMAN_SPEED * elapsedTime;
updatePackmanDirection(packman);
sf::Vector2f movement(0.f, 0.f);
switch (packman.direction)
{
case Direction::UP:
movement.y -= step;
break;
case Direction::DOWN:
movement.y += step;
break;
case Direction::LEFT:
movement.x -= step;
break;
case Direction::RIGHT:
movement.x += step;
break;
case Direction::NONE:
break;
}
const sf::FloatRect packmanBounds = packman.shape.getGlobalBounds();
if (checkFieldWallsCollision(field, packmanBounds, movement))
{
// Останавливаем пакмана при столкновении
packman.direction = Direction::NONE;
}
packman.shape.move(movement);
}