-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
111 lines (86 loc) · 2.63 KB
/
game.cpp
File metadata and controls
111 lines (86 loc) · 2.63 KB
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
#include "game.h"
#include "spriteWrapper.h"
#include "collider.h"
Game::~Game()
{
this->objCollection.clean();
// this->player gets deleted via this->components in the dtor
}
void Game::pollEvents()
{
this->window.update(this->ev);
this->input.update();
}
void Game::startFrame()
{
this->lastTime = this->clock.getElapsedTime().asSeconds();
}
void Game::awake()
{
// Some INIT stuff
this->renderWindow = this->window.window;
this->objCollection.setCollisionSystem(&this->collisionSystem);
// Player
this->player = new GameObject();
SpriteWrapper* playerSpriteWrapper = new SpriteWrapper(this->player, "assets/sprites/player.png");
this->player->addComponent(playerSpriteWrapper);
Collider* playerCollider = new Collider(this->player);
playerCollider->setRect(sf::FloatRect(0.0f, 0.0f, 32.0f, 64.0f));
this->player->addComponent(playerCollider);
this->objCollection.addObject(this->player);
// Square
GameObject* square = new GameObject();
square->transform->setPosition(sf::Vector2f(300, 300));
Collider* squareCollider = new Collider(square);
square->transform->setStatic(true);
squareCollider->setRect(sf::FloatRect(0.0f, 0.0f, 32.0f, 32.0f));
square->addComponent(squareCollider);
square->addComponent(new SpriteWrapper(square, "assets/tiles/1.png"));
this->objCollection.addObject(square);
// End Stuff
objCollection.processNewObjects();
}
void Game::earlyUpdate()
{
this->objCollection.earlyUpdate();
}
void Game::update()
{
this->objCollection.update();
this->collisionSystem.update();
// Player Movement
int moveSpeed = 400;
float vertical = static_cast<float>(this->input.isKeyPressed(sf::Keyboard::S) - this->input.isKeyPressed(sf::Keyboard::W));
float horizontal = static_cast<float>(this->input.isKeyPressed(sf::Keyboard::D) - this->input.isKeyPressed(sf::Keyboard::A));
if (horizontal && vertical)
{
horizontal *= 0.707f;
vertical *= 0.707f;
}
if (this->player)
{
this->player->transform->move(sf::Vector2f(horizontal * moveSpeed * this->deltaTime, vertical * moveSpeed * deltaTime));
}
}
void Game::lateUpdate()
{
this->objCollection.lateUpdate();
}
void Game::render()
{
this->window.prepare();
this->objCollection.render(this->renderWindow);
this->window.render();
}
void Game::calculateDt()
{
this->deltaTime = this->clock.getElapsedTime().asSeconds() - this->lastTime;
}
bool Game::isRunning()
{
return !(this->window.windowClosed or this->shouldClose);
}
float Game::getElapsedTime()
{
return this->clock.getElapsedTime().asSeconds();
}