mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2025-12-16 13:30:07 +01:00
37 lines
1.0 KiB
GDScript
37 lines
1.0 KiB
GDScript
extends TileMap
|
|
|
|
enum CellType { EMPTY = -1, ACTOR, OBSTACLE, OBJECT }
|
|
|
|
func _ready():
|
|
for child in get_children():
|
|
set_cellv(world_to_map(child.position), child.type)
|
|
|
|
|
|
func get_cell_pawn(coordinates):
|
|
for node in get_children():
|
|
if world_to_map(node.position) == coordinates:
|
|
return(node)
|
|
|
|
|
|
func request_move(pawn, direction):
|
|
var cell_start = world_to_map(pawn.position)
|
|
var cell_target = cell_start + direction
|
|
|
|
var cell_target_type = get_cellv(cell_target)
|
|
match cell_target_type:
|
|
CellType.EMPTY:
|
|
return update_pawn_position(pawn, cell_start, cell_target)
|
|
CellType.OBJECT:
|
|
var object_pawn = get_cell_pawn(cell_target)
|
|
object_pawn.queue_free()
|
|
return update_pawn_position(pawn, cell_start, cell_target)
|
|
CellType.ACTOR:
|
|
var pawn_name = get_cell_pawn(cell_target).name
|
|
print("Cell %s contains %s" % [cell_target, pawn_name])
|
|
|
|
|
|
func update_pawn_position(pawn, cell_start, cell_target):
|
|
set_cellv(cell_target, pawn.type)
|
|
set_cellv(cell_start, CellType.EMPTY)
|
|
return map_to_world(cell_target) + cell_size / 2
|