-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
174 lines (134 loc) · 3.37 KB
/
game.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <chrono>
#include <thread>
#include <iostream>
#include "game.hpp"
#include "exception.hpp"
namespace cprogame{
// desired frame rate
typedef std::chrono::duration<int, std::ratio<1, 60>> frame_duration;
Game::Game():player_colored(false),
items(std::vector<std::pair<Position, std::shared_ptr<Item> > >()),
map(Map(this)), player(Position()){};
void Game::update(){
input();
// Update actors, and other updateables
}
void Game::render(){
//print map
move(0,0);
map.print();
// Print player
move(player(Position::Y_POS), player(Position::X_POS));
if(player_colored){
attron(COLOR_PAIR(1));
}
printw(player_colored?"*":"@");
attroff(COLOR_PAIR(1));
for(auto i = items.begin(); i != items.end(); ++i){
move(i->first(Position::Y_POS),i->first(Position::X_POS));
printw(i->second->get_name().c_str());
}
/**
* TODO PRINT ACTORS HERE LATER.
*/
//refresh
refresh(); /* Print it on to the real screen */
}
/**
* Loads a map, sadly it is hardcoded atm.
*/
void Game::init(){
game_over = false;
map.load("resources/map1.cg");
player = map.get_player_pos(); //Not hardcoded, very nice.
//Very ful hack
// MapItem item;
//
// item.p = Position(4,4);
//
// std::shared_ptr<Item> p(Key(this));
// std::shared_ptr<Key>(this);
// std::make_shared<Item>();
// item.i =
// items.push_back(item);
// std::pair<Position, std::shared_ptr<Item> >;
/* INITIALIZE NCURSES */
initscr();
start_color();
init_pair(1, COLOR_RED,COLOR_BLACK);
curs_set(0);
keypad(stdscr, TRUE);
/* END OF NCURSES INITIALIZATION*/
}
void Game::clean(){
endwin(); /* End ncurses session */
}
void Game::player_move(const Position & new_pos){
auto new_tile = map.get_tile(new_pos(Position::X_POS), new_pos(Position::Y_POS));
if(!new_tile->is_blocking()){
player = new_pos;
for(auto i = items.begin(); i != items.end(); ++i){
if(i->first == new_pos){
i->second->use();
items.erase(i);
break;
}
}
if(new_tile->get_type() == Tile::DOOR){
auto door = map.get_door(new_pos(Position::X_POS), new_pos(Position::Y_POS));
erase();
map.load(door.get_path());
player = map.get_player_pos(); //Not hardcoded, very nice.
}
}else if(new_tile->get_type() == Tile::DOOR){
//TODO check if I have the key then....
//inventory[at_some_index]->use(Tile::DOOR);
}
}
void Game::input(){
int key = getch();
switch(key){
case KEY_DOWN:
player_move(Position(player(Position::X_POS), player(Position::Y_POS)+1));
break;
case KEY_UP:
player_move(Position(player(Position::X_POS), player(Position::Y_POS)-1));
break;
case KEY_LEFT:
player_move(Position(player(Position::X_POS)-1, player(Position::Y_POS)));
break;
case KEY_RIGHT:
player_move(Position(player(Position::X_POS)+1, player(Position::Y_POS)));
break;
case 'Q': case 'q':
game_over = true;
break;
default:
break;
}
}
using std::chrono::system_clock;
Map & Game::get_map(){
return map;
}
void Game::run(){
init();
render();
//while game is not over
while(!game_over){
// calculate frame time
// auto start_time = std::chrono::steady_clock::now();
// auto end_time = start_time + frame_duration(1);
// UPDATE:
// execute one step of actions
update();
// RENDER
// re-render the state so that the actions executed above is visible
render();
// ask all actors for actions
// get end time
// std::this_thread::sleep_until(end_time);
}
clean();
}
}