-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatformerGame.cpp
89 lines (65 loc) · 1.41 KB
/
PlatformerGame.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
#include "PlatformerGame.h"
using namespace sf;
const float DELAY = 0.1f;
const int TIME_SPEED = 700; // bigger is slower
const int TIME_MAX_DELAY = 20;
int BLOCK_SIZE = 16;
PlatformerGame::PlatformerGame(const std::string& title, int width, int height)
: player(&world)
, Game(title, width, height)
{
Reset();
}
PlatformerGame::~PlatformerGame()
{
}
void PlatformerGame::Reset()
{
time = 0;
}
void PlatformerGame::OnInit()
{
texture1.loadFromFile("images/mario_tileset.png");
player.SetTexture(texture1);
world.SetTexture(texture1);
level.Load("Level1.tmx");
}
void PlatformerGame::OnUpdate()
{
time = clock.getElapsedTime().asMicroseconds();
clock.restart();
time /= TIME_SPEED;
if (time > TIME_MAX_DELAY)
time = TIME_MAX_DELAY;
player.Update(time);
if (player.GetRect().left > windowWidth / 2)
{
offsetX = player.GetRect().left - windowWidth / 2;
}
offsetY = 0;
world.SetOffset(offsetX, offsetY);
player.SetOffset(offsetX, offsetY);
}
void PlatformerGame::OnDraw(::Window& window)
{
world.Draw(window);
player.Draw(window);
}
void PlatformerGame::OnHandleInput()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
player.MoveRight();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
player.MoveLeft();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
if (player.IsOnGround())
{
player.Jump();
}
}
}