37 lines
962 B
GDScript3
37 lines
962 B
GDScript3
|
extends CharacterBody2D
|
||
|
|
||
|
@onready var tile_map: TileMap = get_parent()
|
||
|
@onready var animation_player = $AnimationPlayer
|
||
|
@export var speed = 80
|
||
|
var y_spawn_offset = -8
|
||
|
var is_moving = false
|
||
|
var destination: Vector2 = Vector2.ZERO
|
||
|
|
||
|
|
||
|
func _ready():
|
||
|
var spawn_cell: Vector2i = tile_map.get_top_spawn_cell()
|
||
|
position = tile_map.map_to_local(spawn_cell)
|
||
|
position.y += y_spawn_offset
|
||
|
|
||
|
func _process(_delta):
|
||
|
if is_moving:
|
||
|
animation_player.play("walk")
|
||
|
else:
|
||
|
animation_player.stop()
|
||
|
|
||
|
func _physics_process(delta):
|
||
|
move_to_rand_cell(delta)
|
||
|
|
||
|
func move_to_rand_cell(delta):
|
||
|
if !is_moving:
|
||
|
var rand_cell: Vector2i = tile_map.get_random_top_cell()
|
||
|
tile_map.set_cell(0, Vector2i(rand_cell.x, rand_cell.y), 1, Vector2i(0, 0), 0) # debug purpose
|
||
|
destination = tile_map.map_to_local(rand_cell)
|
||
|
destination.y += y_spawn_offset
|
||
|
|
||
|
is_moving = true
|
||
|
position = position.move_toward(destination, delta * speed)
|
||
|
|
||
|
if position == destination:
|
||
|
is_moving = false
|