@@ -18,6 +18,28 @@ const color_t PLAYER_COLOR = rgb(150.3, 250.5, 20.6);
18
18
const double PLAYER_SPEED_METERS_PER_SECOND = 3.0 ;
19
19
const double PLAYER_SPEED = PLAYER_SPEED_METERS_PER_SECOND * METERS_TO_PIXELS;
20
20
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 ,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0 , 1 },
37
+ {1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 },
38
+ {1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 1 },
39
+ {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 },
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
+
21
43
void put_pixel (pixel_buffer_t * pixel_buffer, uint x, uint y, color_t color) {
22
44
uint offset = pixel_buffer->width * y + x;
23
45
pixel_buffer->data [offset] = color;
@@ -37,11 +59,14 @@ void draw_box(pixel_buffer_t* pixel_buffer, double x, double y, double width, do
37
59
}
38
60
39
61
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 ;
42
66
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;
45
70
46
71
game_state.initialized = true ;
47
72
}
@@ -50,47 +75,65 @@ void clear_screen(pixel_buffer_t* pixel_buffer) {
50
75
draw_box (pixel_buffer, 0 , 0 , pixel_buffer->width , pixel_buffer->height , BLACK);
51
76
}
52
77
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];
55
97
}
56
98
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);
61
105
}
62
106
63
107
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 );
66
109
}
67
110
68
111
location_t update_location (location_t location, double dx, double dy) {
69
112
// Should only change locations using this method so x and y stay within bounds of a tile map
70
113
location.x += dx;
71
114
location.y += dy;
72
115
while (location.x < 0 ) {
73
- location.x += SCREEN_WIDTH ;
74
- location.tile_map_x -= 1 ;
116
+ location.x += 1 ;
117
+ location.tile_x -= 1 ;
75
118
}
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 ;
79
122
}
80
123
while (location.y < 0 ) {
81
- location.y += SCREEN_HEIGHT ;
82
- location.tile_map_y -= 1 ;
124
+ location.y += 1 ;
125
+ location.tile_y -= 1 ;
83
126
}
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 ;
87
130
}
88
131
return location;
89
132
}
90
133
91
134
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 );
94
137
return !location_occupied (world, bottom_left) &&
95
138
!location_occupied (world, bottom_right);
96
139
}
@@ -100,96 +143,45 @@ void update(double dt, pixel_buffer_t* pixel_buffer, controller_t &controller) {
100
143
initialize_game_state (game_state);
101
144
}
102
145
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 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0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 },
143
- {1 , 0 , 0 , 0 , 0 , 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
-
160
146
location_t player_location = game_state.player_location ;
161
147
162
148
clear_screen (pixel_buffer);
163
149
164
150
// Move player
165
- double dx = 0 , dy = 0 ;
151
+ meters dx = 0 , dy = 0 ;
166
152
if (controller.right_pressed ) {
167
- dx += PLAYER_SPEED * dt;
153
+ dx += PLAYER_SPEED_METERS_PER_SECOND * dt;
168
154
}
169
155
if (controller.left_pressed ) {
170
- dx -= PLAYER_SPEED * dt;
156
+ dx -= PLAYER_SPEED_METERS_PER_SECOND * dt;
171
157
}
172
158
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)) {
174
160
player_location = new_location;
175
161
}
176
162
if (controller.down_pressed ) {
177
- dy += PLAYER_SPEED * dt;
163
+ dy += PLAYER_SPEED_METERS_PER_SECOND * dt;
178
164
}
179
165
if (controller.up_pressed ) {
180
- dy -= PLAYER_SPEED * dt;
166
+ dy -= PLAYER_SPEED_METERS_PER_SECOND * dt;
181
167
}
182
168
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)) {
184
170
player_location = new_location;
185
171
}
186
172
game_state.player_location = player_location;
187
173
188
174
// 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
+ ) {
193
185
draw_box (
194
186
pixel_buffer,
195
187
col*TILE_WIDTH,
@@ -203,10 +195,11 @@ void update(double dt, pixel_buffer_t* pixel_buffer, controller_t &controller) {
203
195
}
204
196
205
197
// Render Player
198
+ screen_location_t screen_location = get_screen_location (player_location);
206
199
draw_box (
207
200
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,
210
203
PLAYER_WIDTH,
211
204
PLAYER_HEIGHT,
212
205
PLAYER_COLOR
0 commit comments