bovido/scripts/tile_map/tile_map.gd

79 lines
2.4 KiB
GDScript3
Raw Normal View History

extends TileMap
@export var ground_tile_source_id = 0
2023-06-27 22:14:14 +02:00
@export var wall_tile_source_id = 1
@export var destination_tile_source_id = 2
@export var target_tile_source_id = 3
@export var map_width = 13 # keep to a odd value
@export var map_height = 19 # keep to a odd value
2023-06-27 22:14:14 +02:00
# Debug variables
var current_destination_cell: Vector2i
var current_target_cell: Vector2i
2023-06-27 22:14:14 +02:00
# The tilemap is divided in two areas separated by a wall
# One for the player (bottom area)
# One for the enemy (top area)
func _ready():
draw_map()
2023-06-27 22:14:14 +02:00
func draw_map():
draw_ground()
2023-06-27 22:14:14 +02:00
draw_wall()
func draw_ground():
for x in range(map_width):
for y in range(map_height):
2023-06-27 22:14:14 +02:00
set_cell(0, Vector2i(x, y), ground_tile_source_id, Vector2i(0, 0), 0)
2023-06-27 22:14:14 +02:00
func draw_wall():
var middle_height = floor(map_height / 2.0)
for x in range(map_width):
2023-06-27 22:14:14 +02:00
set_cell(1, Vector2i(x - 1, middle_height - 1), wall_tile_source_id, Vector2i(0, 0), 0) # why have we to add -1?
func get_top_spawn_cell() -> Vector2i:
return Vector2i(floor(map_width / 2.0), 1)
2023-06-27 22:14:14 +02:00
func get_bottom_spawn_cell() -> Vector2i:
return Vector2i(floor(map_width / 2.0), map_height - 2)
func get_random_top_cell() -> Vector2i:
2023-06-27 22:14:14 +02:00
var w = get_area_width()
var h = get_top_area_height()
2023-06-27 22:14:14 +02:00
return Vector2i(randi_range(w.x, w.y), randi_range(h.x, h.y))
func get_random_bottom_cell() -> Vector2i:
2023-06-27 22:14:14 +02:00
var w = get_area_width()
var h = get_bottom_area_height()
2023-06-27 22:14:14 +02:00
return Vector2i(randi_range(w.x, w.y), randi_range(h.x, h.y))
func is_in_bottom_area(cell: Vector2i) -> bool:
2023-06-27 22:14:14 +02:00
var height = get_bottom_area_height()
return cell.y >= height.x && cell.y <= height.y
2023-06-27 22:14:14 +02:00
func get_area_width() -> Vector2i:
2023-06-27 22:14:14 +02:00
return Vector2i(0, map_width - 1)
func get_top_area_height() -> Vector2i:
var middle_height = floor(map_height / 2.0)
2023-06-27 22:14:14 +02:00
return Vector2i(0, middle_height - 1)
func get_bottom_area_height() -> Vector2i:
2023-06-27 22:14:14 +02:00
var middle_height = floor(map_height / 2.0)
return Vector2i(middle_height + 1, map_height - 1)
2023-06-27 22:14:14 +02:00
# Debug 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