Clean tile map script

This commit is contained in:
Mathilde Grapin 2023-06-27 22:14:14 +02:00
parent e0633efcbe
commit 97f9ec7fa1
2 changed files with 35 additions and 30 deletions

View file

@ -8,7 +8,6 @@ func tick(actor, blackboard):
if !actor.is_throw_animation_finished: if !actor.is_throw_animation_finished:
return RUNNING return RUNNING
else: else:
print("enemy return ball!")
var target = blackboard.get_value("target") var target = blackboard.get_value("target")
actor.return_ball(target) actor.return_ball(target)
return SUCCESS return SUCCESS

View file

@ -1,65 +1,72 @@
extends TileMap extends TileMap
@export var ground_tile_source_id = 0 @export var ground_tile_source_id = 0
@export var wall_tile_source_id = 1
@export var destination_tile_source_id = 2 @export var destination_tile_source_id = 2
@export var target_tile_source_id = 3 @export var target_tile_source_id = 3
@export var map_width = 13 # keep to a odd value @export var map_width = 13 # keep to a odd value
@export var map_height = 19 # keep to a odd value @export var map_height = 19 # keep to a odd value
# Debug variables
var current_destination_cell: Vector2i var current_destination_cell: Vector2i
var current_target_cell: Vector2i var current_target_cell: Vector2i
# 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(): func _ready():
draw_map() draw_map()
func draw_map(): func draw_map():
draw_ground() draw_ground()
draw_separation() draw_wall()
get_random_top_cell()
func draw_ground(): func draw_ground():
for x in range(map_width): for x in range(map_width):
for y in range(map_height): for y in range(map_height):
set_cell(0, Vector2i(x, y), 0, Vector2i(0, 0), 0) set_cell(0, Vector2i(x, y), ground_tile_source_id, Vector2i(0, 0), 0)
# The tilemap is initially divided in two section func draw_wall():
# One for the player (bottom section)
# One for the enemy (top section)
func draw_separation():
var middle_height = floor(map_height / 2.0) var middle_height = floor(map_height / 2.0)
for x in range(map_width): 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? 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: func get_top_spawn_cell():
return Vector2i(floor(map_width / 2.0), 1) return Vector2i(floor(map_width / 2.0), 1)
func get_bottom_spawn_cell() -> Vector2i: func get_bottom_spawn_cell():
return Vector2i(floor(map_width / 2.0), map_height - 2) return Vector2i(floor(map_width / 2.0), map_height - 2)
func get_random_top_cell() -> Vector2i: func get_random_top_cell():
var middle_height = floor(map_height / 2.0) var w = get_area_width()
var rand_width = randi_range(0, map_width - 1) var h = get_top_area_height()
var rand_height = randi_range(0, middle_height - 1)
return Vector2i(rand_width, rand_height) return Vector2i(randi_range(w.x, w.y), randi_range(h.x, h.y))
func get_random_bottom_cell() -> Vector2i: func get_random_bottom_cell():
var middle_height = floor(map_height / 2.0) var w = get_area_width()
var rand_width = randi_range(0, map_width - 1) var h = get_bottom_area_height()
var rand_height = randi_range(middle_height + 1, map_height - 1)
return Vector2i(rand_width, rand_height) return Vector2i(randi_range(w.x, w.y), randi_range(h.x, h.y))
func is_in_bottom_area(local_position: Vector2) -> bool: func is_in_bottom_area(local_position: Vector2):
var map_position = local_to_map(local_position) var map_position = local_to_map(local_position)
var height = get_bottom_area_height()
return map_position.y >= height.x && map_position.y <= height.y
func get_area_width():
return Vector2i(0, map_width - 1)
func get_top_area_height():
var middle_height = floor(map_height / 2.0) var middle_height = floor(map_height / 2.0)
var bottom_min_height = middle_height + 1 return Vector2i(0, middle_height - 1)
var bottom_max_height = map_height - 1
return map_position.y >= bottom_min_height && map_position.y <= map_height
func get_bottom_area_height():
var middle_height = floor(map_height / 2.0)
return Vector2i(middle_height + 1, map_height - 1)
# Debug helper functions # Debug functions
func reset_and_set_target_cell(cell: Vector2i): func reset_and_set_target_cell(cell: Vector2i):
reset_and_set_cell(current_target_cell, cell, target_tile_source_id) reset_and_set_cell(current_target_cell, cell, target_tile_source_id)
@ -71,4 +78,3 @@ func reset_and_set_cell(current_cell: Vector2i, cell: Vector2i, tile_source_id:
set_cell(0, current_cell, ground_tile_source_id, Vector2i(0, 0), 0) set_cell(0, current_cell, ground_tile_source_id, Vector2i(0, 0), 0)
set_cell(0, cell, tile_source_id, Vector2i(0, 0), 0) set_cell(0, cell, tile_source_id, Vector2i(0, 0), 0)
current_cell = cell current_cell = cell