-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGame.cpp
More file actions
139 lines (115 loc) · 3.04 KB
/
Game.cpp
File metadata and controls
139 lines (115 loc) · 3.04 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
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
/*
* File: Game.cpp
* Author: cyril
*
* Created on January 2, 2012, 12:21 PM
*/
#include <iostream>
#include <sstream>
#include "Game.h"
#include "entity/Block.h"
#include "entity/World.h"
Game::Game(sf::RenderWindow *app) {
running = false;
ticker = new Ticker();
ticker->setRate(50);
world = new World();
mapIndex = 0;
this->app = app;
loadMap("res/map/map10");
}
Game::~Game() {
delete(ticker);
delete(world);
}
int Game::run() {
running = true;
sf::Event event;
while(running) {
//Manage event
while(app->pollEvent(event)) {
handleEvent(event);
}
if(ticker->tick())
update(ticker->getElapsedTime());
//Draw screen
draw(app);
app->display();
}
return Screen::EXIT;
}
void Game::update(sf::Time frametime) {
Input input;
input.Left = sf::Keyboard::isKeyPressed(sf::Keyboard::Left);
input.Right = sf::Keyboard::isKeyPressed(sf::Keyboard::Right);
input.Up = sf::Keyboard::isKeyPressed(sf::Keyboard::Up);
input.Down = sf::Keyboard::isKeyPressed(sf::Keyboard::Down);
input.LeftCarve = sf::Keyboard::isKeyPressed(sf::Keyboard::A);
input.RightCarve = sf::Keyboard::isKeyPressed(sf::Keyboard::Z);
int status = world->update(frametime, input);
switch(status) {
case World::WIN:
nextMap();
break;
default:
break;
}
}
void Game::handleEvent(sf::Event event) {
switch(event.type) {
case sf::Event::Closed:
onClose();
break;
case sf::Event::KeyPressed:
onKeyPressed(event);
break;
default:
break;
}
}
void Game::onClose() {
running = false;
}
void Game::onKeyPressed(sf::Event event) {
switch(event.key.code) {
case sf::Keyboard::Escape:
onClose();
break;
case sf::Keyboard::N:
nextMap();
break;
case sf::Keyboard::P:
prevMap();
break;
default:
break;
}
}
void Game::draw(sf::RenderTarget *rt) {
rt->clear(sf::Color(150,150,150));
world->draw(rt);
}
void Game::loadMap(std::string file) {
std::cout << "Loading :" << file << std::endl;
map = file;
world->loadFromFile(file);
app->setSize(sf::Vector2u(world->getSize().x, world->getSize().y));
sf::View v = sf::View(sf::FloatRect(Block::WIDTH, Block::HEIGHT, world->getSize().x - 2*Block::WIDTH, world->getSize().y - 2*Block::HEIGHT));
app->setView(v);
}
void Game::nextMap() {
mapIndex++;
std::cout << mapIndex << std::endl;
std::ostringstream nextMap;
nextMap << "res/map/map" << mapIndex;
loadMap(nextMap.str());
}
void Game::prevMap() {
if(mapIndex > 0) {
mapIndex--;
std::cout << mapIndex << std::endl;
std::ostringstream nextMap;
nextMap << "res/map/map" << mapIndex;
loadMap(nextMap.str());
}
}