-
Notifications
You must be signed in to change notification settings - Fork 1
/
mediator.cpp
133 lines (106 loc) · 3.91 KB
/
mediator.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
#include "mediator.h"
// Global static pointer used to ensure a single instance of the class.
GameMediator* GameMediator::_instance = nullptr;
GameMediator::GameMediator()
{
currentStage = 0;
currentMap = 0;
player_w = 0;
player_h = 0;
}
GameMediator *GameMediator::get_instance()
{
if (!_instance) {
_instance = new GameMediator();
}
return _instance;
}
void GameMediator::load()
{
stage_list = file_io::load_from_disk<st_file_stage>("data/stage.dat");
if (stage_list.size() == 0) {
_instance->stage_list.push_back(st_file_stage());
}
char map_filename[512];
for (unsigned int i=0; i<stage_list.size(); i++) {
sprintf(map_filename, "data/stage_%d_map.dat", i);
std::cout << "Mediator::load, stage[" << i << "], stage_list.size[" << stage_list.size() << "]" << std::endl;
std::vector<st_file_map> map_list;
map_list_map.insert(std::pair<int, std::vector<st_file_map>>(currentStage, map_list));
map_list_map.at(currentStage) = file_io::load_from_disk<st_file_map>(std::string(map_filename));
/*
for (int j=0; j<map_list_map.at(currentStage).size(); j++) {
for (int x=0; x<MAP_W; x++) {
for (int y=0; y<MAP_H; y++) {
std::cout << "map[" << j << "].tile[" << x << "][" << y << "]: [" << map_list_map.at(currentStage).at(j).tile[x][y] << "]" << std::endl;
}
}
}
*/
if (map_list_map.at(currentStage).size() == 0) {
map_list_map.at(currentStage).push_back(st_file_map());
}
}
}
bool GameMediator::can_move_x(double x, double y, int w, int h, double xinc)
{
if (xinc == 0.0) {
return true;
}
// out of screen
if (xinc < 0 && x < 0) {
return true;
}
if (xinc > 0 && x > SCREEN_WIDTH) {
return true;
}
st_file_map current_map = map_list_map.at(currentStage).at(currentMap);
if (xinc > 0) {
x += w-1;
} else if (xinc < 0) {
x++;
}
for (int i=0; i<h; i++) {
int currentTile = current_map.tile[(int)x/TILESIZE][(int)(y+i)/TILESIZE];
int nextTile = current_map.tile[(int)(x+xinc)/TILESIZE][(int)(y+i)/TILESIZE];
if (currentTile == 0 && nextTile == 1) {
//std::cout << "Mediator::can_move_x - X-BLOCKED, xinc[" << xinc << "], pos[" << (int)x << "][" << (int)(y+i) << "][" << i << "], tile[" << (int)x/TILESIZE << "][" << (int)y/TILESIZE << "]" << std::endl;
return false;
}
}
//std::cout << "Mediator::can_move_x - xinc[" << xinc << "] - TRUE" << std::endl;
return true;
}
bool GameMediator::can_move_y(double x, double y, int w, int h, double yinc)
{
if (yinc == 0.0) {
return true;
}
// out of screen
if (yinc < 0 && y < 0) {
return true;
}
if (yinc > 0 && y > VISIBLE_HEIGHT) {
return true;
}
st_file_map current_map = map_list_map.at(currentStage).at(currentMap);
//std::cout << "currentStage[" << currentStage << "], currentMap[" << currentMap << "]" << std::endl;
if (yinc > 0) {
y += h;
} else if (yinc < 0) {
y++;
}
for (int i=0; i<w; i++) {
int tileX = (int)(x+i)/TILESIZE;
int currentTileY = (int)y/TILESIZE;
int nextTileY = (int)(y+yinc)/TILESIZE;
int currentTile = current_map.tile[tileX][currentTileY];
int nextTile = current_map.tile[tileX][nextTileY];
if (currentTile == 0 && nextTile == 1) {
//std::cout << "tileX[" << tileX << "], player.y[" << y << "], yinc[" << yinc << "], currentTileY[" << currentTileY << "], nextTileY[" << nextTileY << "] - BLOCKED" << std::endl;
//std::cout << "Mediator::can_move_x - Y-BLOCKED, pos[" << (int)x << "][" << (int)(y+i) << "][" << i << "], tile[" << (int)x/TILESIZE << "][" << (int)y/TILESIZE << "]" << std::endl;
return false;
}
}
return true;
}