Обновлено перемещение npc
This commit is contained in:
29
npc/npc.gd
29
npc/npc.gd
@@ -2,6 +2,9 @@ class_name NPC
|
||||
extends Node2D
|
||||
|
||||
var id_path: Array[Vector2i]
|
||||
var target: Vector2i
|
||||
var speed: float = 1
|
||||
|
||||
|
||||
func is_walking() -> bool:
|
||||
return not id_path.is_empty()
|
||||
@@ -10,7 +13,6 @@ func is_walking() -> bool:
|
||||
func _ready() -> void:
|
||||
randomize()
|
||||
$Sprite2D.set_nation(randi()%$Sprite2D.Nations.max)
|
||||
set_spacesuit(false)
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
@@ -20,11 +22,34 @@ func _physics_process(delta: float) -> void:
|
||||
|
||||
var target_pos = Vector2(id_path.front()*World.tile_size)
|
||||
|
||||
global_position = global_position.move_toward(target_pos, 1)
|
||||
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
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
[ext_resource type="Script" path="res://npc/sprite_2d.gd" id="2_jtl2q"]
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_2v0ml"]
|
||||
resource_local_to_scene = true
|
||||
atlas = ExtResource("1_vgeae")
|
||||
region = Rect2(0, 0, 16, 16)
|
||||
|
||||
@@ -14,6 +15,7 @@ z_as_relative = false
|
||||
script = ExtResource("1_m82ir")
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
texture_filter = 1
|
||||
texture = SubResource("AtlasTexture_2v0ml")
|
||||
centered = false
|
||||
script = ExtResource("2_jtl2q")
|
||||
|
||||
Reference in New Issue
Block a user