Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61c6022

Browse files
committed
Convert location system to use only tile index and location in tile.
1 parent 706b23e commit 61c6022

File tree

2 files changed

+113
-110
lines changed

2 files changed

+113
-110
lines changed

game.cc

Lines changed: 87 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ const color_t PLAYER_COLOR = rgb(150.3, 250.5, 20.6);
1818
const double PLAYER_SPEED_METERS_PER_SECOND = 3.0;
1919
const double PLAYER_SPEED = PLAYER_SPEED_METERS_PER_SECOND * METERS_TO_PIXELS;
2020

21+
// Define world layout
22+
tile_t tiles[WORLD_HEIGHT][WORLD_WIDTH] = {
23+
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
24+
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
25+
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1},
26+
{1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1},
27+
{1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
28+
{1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
29+
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 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40+
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
41+
};
42+
2143
void put_pixel(pixel_buffer_t* pixel_buffer, uint x, uint y, color_t color) {
2244
uint offset = pixel_buffer->width * y + x;
2345
pixel_buffer->data[offset] = color;
@@ -37,11 +59,14 @@ void draw_box(pixel_buffer_t* pixel_buffer, double x, double y, double width, do
3759
}
3860

3961
void initialize_game_state(game_state_t &game_state) {
40-
game_state.player_location.x = TILE_WIDTH*2;
41-
game_state.player_location.y = TILE_HEIGHT*5;
62+
game_state.player_location.tile_x = 2;
63+
game_state.player_location.tile_y = 5;
64+
game_state.player_location.x = 0;
65+
game_state.player_location.y = 0;
4266

43-
game_state.player_location.tile_map_x = 0;
44-
game_state.player_location.tile_map_y = 0;
67+
game_state.world.width = WORLD_WIDTH;
68+
game_state.world.height = WORLD_HEIGHT;
69+
game_state.world.tiles = (tile_t*) tiles;
4570

4671
game_state.initialized = true;
4772
}
@@ -50,47 +75,65 @@ void clear_screen(pixel_buffer_t* pixel_buffer) {
5075
draw_box(pixel_buffer, 0, 0, pixel_buffer->width, pixel_buffer->height, BLACK);
5176
}
5277

53-
tile_map_t* get_tile_map(world_t* world, location_t location) {
54-
return world->tile_maps + (location.tile_map_y*WORLD_WIDTH + location.tile_map_x);
78+
#ifdef DEBUG
79+
void print_location(location_t location) {
80+
std::cout << "location(" << location.tile_x + location.x << ", " << location.tile_y + location.y << ")" << std::endl;
81+
}
82+
83+
void print_screen_location(screen_location_t location) {
84+
std::cout << "screen_location(" << location.x << ", " << location.y << ")" << std::endl;
85+
}
86+
#endif
87+
88+
screen_location_t get_screen_location(location_t location) {
89+
screen_location_t screen_location;
90+
screen_location.x = (location.tile_x % TILES_PER_SCREEN_X + location.x) * METERS_TO_PIXELS;
91+
screen_location.y = (location.tile_y % TILES_PER_SCREEN_Y + location.y) * METERS_TO_PIXELS;
92+
return screen_location;
93+
}
94+
95+
tile_t get_tile(world_t* world, uint tile_x, uint tile_y) {
96+
return world->tiles[tile_y*world->width + tile_x];
5597
}
5698

57-
tile_t get_tile(tile_map_t* tile_map, location_t location) {
58-
uint row = location.y / TILE_HEIGHT;
59-
uint col = location.x / TILE_WIDTH;
60-
return tile_map->tiles[row*TILE_MAP_WIDTH + col];
99+
tile_t get_tile(world_t* world, location_t location) {
100+
return get_tile(world, location.tile_x, location.tile_y);
101+
}
102+
103+
bool location_occupied(world_t* world, uint tile_x, uint tile_y) {
104+
return get_tile(world, tile_x, tile_y);
61105
}
62106

63107
bool location_occupied(world_t* world, location_t location) {
64-
tile_map_t* tile_map = get_tile_map(world, location);
65-
return get_tile(tile_map, location);
108+
return get_tile(world, location.tile_x, location.tile_y);
66109
}
67110

68111
location_t update_location(location_t location, double dx, double dy) {
69112
// Should only change locations using this method so x and y stay within bounds of a tile map
70113
location.x += dx;
71114
location.y += dy;
72115
while (location.x < 0) {
73-
location.x += SCREEN_WIDTH;
74-
location.tile_map_x -= 1;
116+
location.x += 1;
117+
location.tile_x -= 1;
75118
}
76-
while (location.x > SCREEN_WIDTH) {
77-
location.x -= SCREEN_WIDTH;
78-
location.tile_map_x += 1;
119+
while (location.x >= 1) {
120+
location.x -= 1;
121+
location.tile_x += 1;
79122
}
80123
while (location.y < 0) {
81-
location.y += SCREEN_HEIGHT;
82-
location.tile_map_y -= 1;
124+
location.y += 1;
125+
location.tile_y -= 1;
83126
}
84-
while (location.y > SCREEN_HEIGHT) {
85-
location.y -= SCREEN_HEIGHT;
86-
location.tile_map_y += 1;
127+
while (location.y >= 1) {
128+
location.y -= 1;
129+
location.tile_y += 1;
87130
}
88131
return location;
89132
}
90133

91134
bool valid_player_location(world_t* world, location_t location) {
92-
location_t bottom_left = update_location(location, PLAYER_WIDTH / -2, 0);
93-
location_t bottom_right = update_location(location, PLAYER_WIDTH / 2, 0);
135+
location_t bottom_left = update_location(location, PLAYER_WIDTH_METERS / -2, 0);
136+
location_t bottom_right = update_location(location, PLAYER_WIDTH_METERS / 2, 0);
94137
return !location_occupied(world, bottom_left) &&
95138
!location_occupied(world, bottom_right);
96139
}
@@ -100,96 +143,45 @@ void update(double dt, pixel_buffer_t* pixel_buffer, controller_t &controller) {
100143
initialize_game_state(game_state);
101144
}
102145

103-
// Define world layout
104-
tile_t tiles_00[TILE_MAP_HEIGHT][TILE_MAP_WIDTH] = {
105-
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
106-
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
107-
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
108-
{1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1},
109-
{1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
110-
{1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1},
111-
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
112-
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
113-
{1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1},
114-
};
115-
tile_t tiles_01[TILE_MAP_HEIGHT][TILE_MAP_WIDTH] = {
116-
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
117-
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
118-
{1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1},
119-
{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1},
120-
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
121-
{1, 0, 0, 1, 0, 1, 0, 0, 0,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 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1},
144-
{1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1},
145-
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
146-
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
147-
};
148-
149-
tile_map_t tile_maps[WORLD_HEIGHT][WORLD_WIDTH];
150-
tile_maps[0][0].tiles = (tile_t*) tiles_00;
151-
tile_maps[0][1].tiles = (tile_t*) tiles_01;
152-
tile_maps[1][0].tiles = (tile_t*) tiles_10;
153-
tile_maps[1][1].tiles = (tile_t*) tiles_11;
154-
155-
world_t world;
156-
world.width = WORLD_WIDTH;
157-
world.height = WORLD_HEIGHT;
158-
world.tile_maps = (tile_map_t*) tile_maps;
159-
160146
location_t player_location = game_state.player_location;
161147

162148
clear_screen(pixel_buffer);
163149

164150
// Move player
165-
double dx = 0, dy = 0;
151+
meters dx = 0, dy = 0;
166152
if (controller.right_pressed) {
167-
dx += PLAYER_SPEED * dt;
153+
dx += PLAYER_SPEED_METERS_PER_SECOND * dt;
168154
}
169155
if (controller.left_pressed) {
170-
dx -= PLAYER_SPEED * dt;
156+
dx -= PLAYER_SPEED_METERS_PER_SECOND * dt;
171157
}
172158
location_t new_location = update_location(player_location, dx, 0);
173-
if (valid_player_location(&world, new_location)) {
159+
if (valid_player_location(&(game_state.world), new_location)) {
174160
player_location = new_location;
175161
}
176162
if (controller.down_pressed) {
177-
dy += PLAYER_SPEED * dt;
163+
dy += PLAYER_SPEED_METERS_PER_SECOND * dt;
178164
}
179165
if (controller.up_pressed) {
180-
dy -= PLAYER_SPEED * dt;
166+
dy -= PLAYER_SPEED_METERS_PER_SECOND * dt;
181167
}
182168
new_location = update_location(player_location, 0, dy);
183-
if (valid_player_location(&world, new_location)) {
169+
if (valid_player_location(&(game_state.world), new_location)) {
184170
player_location = new_location;
185171
}
186172
game_state.player_location = player_location;
187173

188174
// Render tile map
189-
tile_map_t* tile_map = get_tile_map(&world, player_location);
190-
for (int row=0; row < TILE_MAP_HEIGHT; row++) {
191-
for (int col=0; col < TILE_MAP_WIDTH; col++) {
192-
if (tile_map->tiles[row*TILE_MAP_WIDTH + col]) {
175+
uint top_left_x = game_state.player_location.tile_x / TILES_PER_SCREEN_X * TILES_PER_SCREEN_X;
176+
uint top_left_y = game_state.player_location.tile_y / TILES_PER_SCREEN_Y * TILES_PER_SCREEN_Y;
177+
for (uint row=0; row < TILES_PER_SCREEN_Y; row++) {
178+
for (int col=0; col < TILES_PER_SCREEN_X; col++) {
179+
if (location_occupied(
180+
&(game_state.world),
181+
top_left_x + col,
182+
top_left_y + row
183+
)
184+
) {
193185
draw_box(
194186
pixel_buffer,
195187
col*TILE_WIDTH,
@@ -203,10 +195,11 @@ void update(double dt, pixel_buffer_t* pixel_buffer, controller_t &controller) {
203195
}
204196

205197
// Render Player
198+
screen_location_t screen_location = get_screen_location(player_location);
206199
draw_box(
207200
pixel_buffer,
208-
game_state.player_location.x - PLAYER_WIDTH/2,
209-
game_state.player_location.y - PLAYER_HEIGHT,
201+
screen_location.x - PLAYER_WIDTH/2,
202+
screen_location.y - PLAYER_HEIGHT,
210203
PLAYER_WIDTH,
211204
PLAYER_HEIGHT,
212205
PLAYER_COLOR

game.h

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
const uint SCREEN_WIDTH = 1024;
99
const uint SCREEN_HEIGHT = 576;
1010

11-
const uint WORLD_WIDTH = 2;
12-
const uint WORLD_HEIGHT = 2;
13-
const uint TILE_MAP_WIDTH = 16;
14-
const uint TILE_MAP_HEIGHT = 9;
11+
const uint WORLD_WIDTH_IN_SCREENS = 2;
12+
const uint WORLD_HEIGHT_IN_SCREENS = 2;
13+
const uint TILES_PER_SCREEN_X = 16;
14+
const uint TILES_PER_SCREEN_Y = 9;
15+
const uint WORLD_WIDTH = WORLD_HEIGHT_IN_SCREENS*TILES_PER_SCREEN_X;
16+
const uint WORLD_HEIGHT = WORLD_HEIGHT_IN_SCREENS*TILES_PER_SCREEN_Y;
1517

16-
const double METERS_TO_PIXELS = SCREEN_WIDTH / TILE_MAP_WIDTH; // Each tile is a meter
18+
const double METERS_TO_PIXELS = SCREEN_WIDTH / TILES_PER_SCREEN_X; // Each tile is a meter
1719

18-
const uint TILE_WIDTH = SCREEN_WIDTH / TILE_MAP_WIDTH;
19-
const uint TILE_HEIGHT = SCREEN_HEIGHT / TILE_MAP_HEIGHT;
20+
const uint TILE_WIDTH = SCREEN_WIDTH / TILES_PER_SCREEN_X;
21+
const uint TILE_HEIGHT = SCREEN_HEIGHT / TILES_PER_SCREEN_Y;
2022
const color_t TILE_COLOR = rgb(100, 100, 100);
2123

2224
typedef bool tile_t;
@@ -26,27 +28,35 @@ struct tile_map_t {
2628
};
2729

2830
struct world_t {
29-
tile_map_t* tile_maps;
31+
tile_t* tiles;
3032
uint width;
3133
uint height;
3234
};
3335

36+
typedef double meters;
37+
typedef double pixels;
38+
3439
struct location_t {
35-
// Which tile map?
36-
int tile_map_x;
37-
int tile_map_y;
38-
39-
// Where in the tile map?
40-
// These values are assumed to be within the bounds of the tile map
41-
double x;
42-
double y;
40+
// Which tile
41+
int tile_x;
42+
int tile_y;
43+
44+
// Offset in tile
45+
meters x;
46+
meters y;
47+
};
48+
49+
struct screen_location_t {
50+
pixels x;
51+
pixels y;
4352
};
4453

4554
struct game_state_t {
4655
game_state_t() : initialized(false) {}
4756
bool initialized;
4857

4958
location_t player_location;
59+
world_t world;
5060
};
5161

5262
#endif

0 commit comments

Comments
 (0)