Created
September 28, 2020 10:36
-
-
Save smks/85e68a3af0197b441f625f40718e4f9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node2D | |
var Room = preload("res://src/Room.tscn") | |
enum DIRECTIONS { | |
UP, | |
RIGHT, | |
LEFT, | |
DOWN | |
} | |
const MIN_ROOMS_TO_CREATE = 15 | |
const MAX_ROOMS_TO_CREATE = 30 | |
const ROOM_SIZE = Vector2(1280, 720) | |
var rooms: Array | |
var rooms_total_to_create: int | |
var last_room_direction | |
func _ready(): | |
generate() | |
func generate(_theme: String = ''): | |
rooms_total_to_create = rand_range(MIN_ROOMS_TO_CREATE, MAX_ROOMS_TO_CREATE) | |
print('creating ' + str(rooms_total_to_create) + ' rooms') | |
create_first_room() | |
create_middle_rooms() | |
create_last_room() | |
func create_first_room(): | |
var room = Room.instance() | |
add_child(room) | |
rooms.push_back(room) | |
func create_middle_rooms(): | |
var remaining_rooms = rooms_total_to_create - 2 | |
for room_index in remaining_rooms: | |
var position = get_next_room_position() | |
var room = Room.instance() | |
room.position = position | |
add_child(room) | |
rooms.push_back(room) | |
func create_last_room(): | |
pass | |
func get_random_direction(): | |
var directions_available = [] | |
if last_room_direction == DIRECTIONS.UP: | |
directions_available = [DIRECTIONS.UP, DIRECTIONS.RIGHT, DIRECTIONS.LEFT] | |
elif last_room_direction == DIRECTIONS.DOWN: | |
directions_available = [DIRECTIONS.DOWN, DIRECTIONS.RIGHT, DIRECTIONS.LEFT] | |
elif last_room_direction == DIRECTIONS.LEFT: | |
directions_available = [DIRECTIONS.UP, DIRECTIONS.LEFT, DIRECTIONS.DOWN] | |
elif last_room_direction == DIRECTIONS.RIGHT: | |
directions_available = [DIRECTIONS.UP, DIRECTIONS.RIGHT, DIRECTIONS.DOWN] | |
else: | |
directions_available = DIRECTIONS.values() | |
return directions_available[randi() % directions_available.size()] | |
func get_next_room_position(): | |
var next_position: Vector2 | |
var direction:int = get_random_direction() | |
var last_room_added = rooms[-1] | |
var last_room_position = last_room_added.position | |
if direction == DIRECTIONS.UP: | |
next_position = last_room_position - Vector2(0, -ROOM_SIZE.y) | |
elif direction == DIRECTIONS.DOWN: | |
next_position = last_room_position - Vector2(0, ROOM_SIZE.y) | |
elif direction == DIRECTIONS.LEFT: | |
next_position = last_room_position - Vector2(-ROOM_SIZE.x, 0) | |
elif direction == DIRECTIONS.RIGHT: | |
next_position = last_room_position - Vector2(ROOM_SIZE.x, 0) | |
return next_position | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment