testproject/npc/npc.gd
2024-12-08 15:37:58 +04:00

57 lines
1.4 KiB
GDScript

class_name NPC
extends Node2D
var id_path: Array[Vector2i]
var target: Vector2i
var speed: float = 1
var oxygen: float = 100
func is_walking() -> bool:
return not id_path.is_empty()
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
randomize()
$Sprite2D.set_nation(randi()%$Sprite2D.Nations.max)
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
if id_path.is_empty(): return
var target_pos = Vector2(id_path.front()*World.tile_size)
global_position = global_position.move_toward(target_pos, speed)
if global_position.x < target_pos.x: $Sprite2D.flip_h = true
elif global_position.x > target_pos.x: $Sprite2D.flip_h = false
if global_position == target_pos:
id_path.pop_front()
set_target(target)
if $"../../TileMap".check_indoors(global_position/16):
set_spacesuit(false)
else:
set_spacesuit(true)
func set_spacesuit(suit: bool) -> void:
$Sprite2D.set_spacesuit(suit)
if suit: speed = 0.2
else: speed = 1
func set_target(pos: Vector2i):
var new_id_path = $"../../TileMap".astar_grid.get_id_path(
$"../../TileMap/ground".local_to_map(global_position+$"../../TileMap/ground".global_position),
target
).slice(1)
if not new_id_path.is_empty():
id_path = new_id_path
target = pos
else:
id_path = []
target = pos