2023-06-11 23:38:54 +02:00
|
|
|
extends TileMap
|
|
|
|
|
2023-06-18 20:30:25 +02:00
|
|
|
@export var ground_tile_source_id = 0
|
|
|
|
@export var destination_tile_source_id = 2
|
|
|
|
@export var target_tile_source_id = 3
|
2023-06-11 23:38:54 +02:00
|
|
|
@export var map_width = 13 # keep to a odd value
|
|
|
|
@export var map_height = 19 # keep to a odd value
|
2023-06-18 20:30:25 +02:00
|
|
|
var current_destination_cell: Vector2i
|
|
|
|
var current_target_cell: Vector2i
|
2023-06-11 23:38:54 +02:00
|
|
|
|
|
|
|
func _ready():
|
|
|
|
draw_map()
|
|
|
|
|
|
|
|
func draw_map():
|
|
|
|
draw_ground()
|
|
|
|
draw_separation()
|
|
|
|
get_random_top_cell()
|
|
|
|
|
|
|
|
func draw_ground():
|
|
|
|
for x in range(map_width):
|
|
|
|
for y in range(map_height):
|
|
|
|
set_cell(0, Vector2i(x, y), 0, Vector2i(0, 0), 0)
|
|
|
|
|
|
|
|
# The tilemap is initially divided in two section
|
|
|
|
# One for the player (bottom section)
|
|
|
|
# One for the enemy (top section)
|
|
|
|
func draw_separation():
|
|
|
|
var middle_height = floor(map_height / 2.0)
|
|
|
|
for x in range(map_width):
|
|
|
|
set_cell(1, Vector2i(x - 1, middle_height - 1), 1, Vector2i(0, 0), 0) # why have we to add -1?
|
|
|
|
|
|
|
|
func get_top_spawn_cell() -> Vector2i:
|
|
|
|
return Vector2i(floor(map_width / 2.0), 1)
|
|
|
|
|
|
|
|
func get_bottom_spawn_cell() -> Vector2i:
|
|
|
|
return Vector2i(floor(map_width / 2.0), map_height - 2)
|
|
|
|
|
|
|
|
func get_random_top_cell() -> Vector2i:
|
|
|
|
var middle_height = floor(map_height / 2.0)
|
|
|
|
var rand_width = randi_range(0, map_width - 1)
|
|
|
|
var rand_height = randi_range(0, middle_height - 1)
|
2023-06-18 20:30:25 +02:00
|
|
|
|
2023-06-11 23:38:54 +02:00
|
|
|
return Vector2i(rand_width, rand_height)
|
2023-06-18 20:30:25 +02:00
|
|
|
|
|
|
|
func get_random_bottom_cell() -> Vector2i:
|
|
|
|
var middle_height = floor(map_height / 2.0)
|
|
|
|
var rand_width = randi_range(0, map_width - 1)
|
|
|
|
var rand_height = randi_range(middle_height + 1, map_height - 1)
|
|
|
|
|
|
|
|
return Vector2i(rand_width, rand_height)
|
|
|
|
|
2023-06-27 21:25:32 +02:00
|
|
|
func is_in_bottom_area(local_position: Vector2) -> bool:
|
|
|
|
var map_position = local_to_map(local_position)
|
|
|
|
|
|
|
|
var middle_height = floor(map_height / 2.0)
|
|
|
|
var bottom_min_height = middle_height + 1
|
|
|
|
var bottom_max_height = map_height - 1
|
|
|
|
|
|
|
|
return map_position.y >= bottom_min_height && map_position.y <= map_height
|
|
|
|
|
|
|
|
|
2023-06-18 20:30:25 +02:00
|
|
|
# Debug helper functions
|
|
|
|
func reset_and_set_target_cell(cell: Vector2i):
|
|
|
|
reset_and_set_cell(current_target_cell, cell, target_tile_source_id)
|
|
|
|
|
|
|
|
func reset_and_set_destination_cell(cell: Vector2i):
|
|
|
|
reset_and_set_cell(current_destination_cell, cell, destination_tile_source_id)
|
|
|
|
|
|
|
|
func reset_and_set_cell(current_cell: Vector2i, cell: Vector2i, tile_source_id: int):
|
|
|
|
if current_cell != null:
|
|
|
|
set_cell(0, current_cell, ground_tile_source_id, Vector2i(0, 0), 0)
|
|
|
|
set_cell(0, cell, tile_source_id, Vector2i(0, 0), 0)
|
|
|
|
current_cell = cell
|
2023-06-27 21:25:32 +02:00
|
|
|
|