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

Skip to content

Commit 97da7ad

Browse files
committed
Factor out creating a room from deciding where rooms should go. =
Factor out creating a room.
1 parent 99f88d0 commit 97da7ad

File tree

1 file changed

+63
-51
lines changed

1 file changed

+63
-51
lines changed

game.cc

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -88,68 +88,80 @@ tile_t get_tile(world_t* world, uint tile_x, uint tile_y) {
8888
return tile;
8989
}
9090

91+
void create_room(
92+
world_t* world,
93+
uint room_index_x,
94+
uint room_index_y,
95+
bool door_left,
96+
bool door_right,
97+
bool door_top,
98+
bool door_bottom,
99+
uint pattern) {
100+
uint top_left_y = room_index_y * TILES_PER_SCREEN_Y;
101+
uint top_left_x = room_index_x * TILES_PER_SCREEN_X;
102+
103+
// Top wall
104+
uint vertical_door_x = TILES_PER_SCREEN_X / 2;
105+
for (int i = 0; i < TILES_PER_SCREEN_X; i++) {
106+
if (door_top && (i == vertical_door_x || i == vertical_door_x - 1)) {
107+
// Leave a door
108+
} else {
109+
set_tile(world, top_left_x + i, top_left_y, 1);
110+
}
111+
}
112+
113+
// Bottom wall
114+
for (int i = 0; i < TILES_PER_SCREEN_X; i++) {
115+
if (door_bottom && (i == vertical_door_x || i == vertical_door_x - 1)) {
116+
// Leave a door
117+
} else {
118+
set_tile(world, top_left_x + i, top_left_y + TILES_PER_SCREEN_Y - 1, 1);
119+
}
120+
}
121+
122+
// Left wall
123+
uint horizontal_door_y = TILES_PER_SCREEN_Y / 2;
124+
for (int i = 0; i < TILES_PER_SCREEN_Y; i++) {
125+
if (door_left && i == horizontal_door_y) {
126+
// Leave a door
127+
} else {
128+
set_tile(world, top_left_x, top_left_y + i, 1);
129+
}
130+
}
131+
132+
// Right wall
133+
for (int i = 0; i < TILES_PER_SCREEN_Y; i++) {
134+
if (door_right && i == horizontal_door_y) {
135+
// Leave a door
136+
} else {
137+
set_tile(world, top_left_x + TILES_PER_SCREEN_X - 1, top_left_y + i, 1);
138+
}
139+
}
140+
141+
// A pattern in the center
142+
for (int i = 0; i < 2; i++) {
143+
for (int j = 0; j < 3; j++) {
144+
uint hash = pattern + i + 10 * j;
145+
if (hash % 3 == 0 || hash % 4 == 0 || hash % 5 == 0) {
146+
set_tile(world, top_left_x + vertical_door_x - 1 + i, top_left_y + horizontal_door_y - 1 + j, 1);
147+
}
148+
}
149+
}
150+
}
151+
91152
void fill_world_with_rooms(world_t* world) {
92153
uint room_count_y = 5;
93154
uint room_count_x = 5;
94155

95156
for (uint row = 0; row < room_count_x; row++) {
96-
uint top_left_y = row * TILES_PER_SCREEN_Y;
97-
98157
for (uint col = 0; col < room_count_y; col++) {
99-
uint top_left_x = col * TILES_PER_SCREEN_X;
100-
101158
bool has_top_door = row > 0;
102159
bool has_bottom_door = row < room_count_y - 1;
103160
bool has_left_door = col > 0;
104161
bool has_right_door = col < room_count_x - 1;
162+
uint pattern = row * 3 + col * 7;
105163

106-
// Top wall
107-
uint vertical_door_x = TILES_PER_SCREEN_X / 2;
108-
for (int i = 0; i < TILES_PER_SCREEN_X; i++) {
109-
if (has_top_door && (i == vertical_door_x || i == vertical_door_x - 1)) {
110-
// Leave a door
111-
} else {
112-
set_tile(world, top_left_x + i, top_left_y, 1);
113-
}
114-
}
115-
116-
// Bottom wall
117-
for (int i = 0; i < TILES_PER_SCREEN_X; i++) {
118-
if (has_bottom_door && (i == vertical_door_x || i == vertical_door_x - 1)) {
119-
// Leave a door
120-
} else {
121-
set_tile(world, top_left_x + i, top_left_y + TILES_PER_SCREEN_Y - 1, 1);
122-
}
123-
}
124-
125-
// Left wall
126-
uint horizontal_door_y = TILES_PER_SCREEN_Y / 2;
127-
for (int i = 0; i < TILES_PER_SCREEN_Y; i++) {
128-
if (has_left_door && i == horizontal_door_y) {
129-
// Leave a door
130-
} else {
131-
set_tile(world, top_left_x, top_left_y + i, 1);
132-
}
133-
}
134-
135-
// Right wall
136-
for (int i = 0; i < TILES_PER_SCREEN_Y; i++) {
137-
if (has_right_door && i == horizontal_door_y) {
138-
// Leave a door
139-
} else {
140-
set_tile(world, top_left_x + TILES_PER_SCREEN_X - 1, top_left_y + i, 1);
141-
}
142-
}
143-
144-
// A pattern in the center
145-
for (int i = 0; i < 2; i++) {
146-
for (int j = 0; j < 3; j++) {
147-
uint hash = i+j+row*room_count_x+col;
148-
if (hash % 3 == 0 || hash % 4 == 0 || hash % 5 == 0) {
149-
set_tile(world, top_left_x + vertical_door_x - 1 + i, top_left_y + horizontal_door_y - 1 + j, 1);
150-
}
151-
}
152-
}
164+
create_room(world, col, row, has_left_door, has_right_door, has_top_door, has_bottom_door, pattern);
153165
}
154166
}
155167
}

0 commit comments

Comments
 (0)