Обновлено перемещение npc

This commit is contained in:
DIvan2000 2024-12-08 15:05:33 +04:00
parent 62cf438b0d
commit d9aff4fdc4
8 changed files with 54 additions and 9 deletions

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 398 B

View File

@ -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

View File

@ -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")

View File

@ -22,13 +22,7 @@ func _ready() -> void:
func set_npc_target(npc: NPC, target: Vector2i):
var id_path = $"../TileMap".astar_grid.get_id_path(
$"../TileMap/ground".local_to_map(npc.global_position+$"../TileMap/ground".global_position),
target
).slice(1)
if not id_path.is_empty():
npc.id_path = id_path
npc.set_target(target)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:

View File

@ -244,3 +244,9 @@ func weight(pos: Vector2i) -> int:
func update_astar_tile(pos: Vector2i):
astar_grid.set_point_solid(pos, not is_walkable(pos))
astar_grid.set_point_weight_scale(pos, weight(pos))
func check_indoors(pos: Vector2i) -> bool:
var layer = get_toplayer(pos)
var tiledata: TileData = layer.get_cell_tile_data(pos)
if tiledata != null: return tiledata.get_custom_data("indoors")
else: return false

View File

@ -20,6 +20,7 @@ var lmb: bool
var rmb: bool
func _ready():
Engine.physics_ticks_per_second = 60
selection = SELECTION_SCENE.instantiate()
selection2 = SELECTION_SCENE.instantiate()
add_child(selection)

View File

@ -852,14 +852,17 @@ texture = ExtResource("4_xeinu")
1:1/0/custom_data_3 = Vector2i(1, 1)
1:1/0/custom_data_4 = true
1:1/0/custom_data_5 = 1
1:1/0/custom_data_6 = true
2:1/0 = 0
2:1/0/custom_data_3 = Vector2i(0, 1)
2:1/0/custom_data_4 = true
2:1/0/custom_data_5 = 1
2:1/0/custom_data_6 = true
3:1/0 = 0
3:1/0/custom_data_3 = Vector2i(-1, 1)
3:1/0/custom_data_4 = true
3:1/0/custom_data_5 = 1
3:1/0/custom_data_6 = true
4:1/0 = 0
4:1/0/custom_data_3 = Vector2i(-1, 1)
4:1/0/custom_data_5 = 255
@ -871,16 +874,19 @@ texture = ExtResource("4_xeinu")
1:2/0/custom_data_3 = Vector2i(1, 0)
1:2/0/custom_data_4 = true
1:2/0/custom_data_5 = 1
1:2/0/custom_data_6 = true
2:2/0 = 0
2:2/0/custom_data_0 = true
2:2/0/custom_data_1 = Vector2i(5, 5)
2:2/0/custom_data_2 = "EMPTY_ROOM"
2:2/0/custom_data_4 = true
2:2/0/custom_data_5 = 1
2:2/0/custom_data_6 = true
3:2/0 = 0
3:2/0/custom_data_3 = Vector2i(-1, 0)
3:2/0/custom_data_4 = true
3:2/0/custom_data_5 = 1
3:2/0/custom_data_6 = true
4:2/0 = 0
4:2/0/custom_data_3 = Vector2i(-1, 0)
4:2/0/custom_data_4 = true
@ -892,14 +898,17 @@ texture = ExtResource("4_xeinu")
1:3/0/custom_data_3 = Vector2i(1, -1)
1:3/0/custom_data_4 = true
1:3/0/custom_data_5 = 1
1:3/0/custom_data_6 = true
2:3/0 = 0
2:3/0/custom_data_3 = Vector2i(0, -1)
2:3/0/custom_data_4 = true
2:3/0/custom_data_5 = 1
2:3/0/custom_data_6 = true
3:3/0 = 0
3:3/0/custom_data_3 = Vector2i(-1, -1)
3:3/0/custom_data_4 = true
3:3/0/custom_data_5 = 1
3:3/0/custom_data_6 = true
4:3/0 = 0
4:3/0/custom_data_3 = Vector2i(-1, -1)
4:3/0/custom_data_5 = 255
@ -925,6 +934,7 @@ texture = ExtResource("4_xeinu")
1:5/0/custom_data_3 = Vector2i(0, 1)
1:5/0/custom_data_4 = true
1:5/0/custom_data_5 = 1
1:5/0/custom_data_6 = true
2:5/0 = 0
2:5/0/custom_data_3 = Vector2i(-1, 1)
2:5/0/custom_data_5 = 255
@ -937,6 +947,7 @@ texture = ExtResource("4_xeinu")
1:6/0/custom_data_2 = "V_WAY"
1:6/0/custom_data_4 = true
1:6/0/custom_data_5 = 1
1:6/0/custom_data_6 = true
2:6/0 = 0
2:6/0/custom_data_3 = Vector2i(-1, 0)
2:6/0/custom_data_5 = 255
@ -946,12 +957,14 @@ texture = ExtResource("4_xeinu")
3:6/0/custom_data_3 = Vector2i(1, 0)
3:6/0/custom_data_4 = true
3:6/0/custom_data_5 = 1
3:6/0/custom_data_6 = true
1:7/0 = 0
1:7/0/custom_data_0 = true
1:7/0/custom_data_1 = Vector2i(3, 1)
1:7/0/custom_data_3 = Vector2i(0, -1)
1:7/0/custom_data_4 = true
1:7/0/custom_data_5 = 1
1:7/0/custom_data_6 = true
2:7/0 = 0
2:7/0/custom_data_3 = Vector2i(-1, -1)
2:7/0/custom_data_5 = 255
@ -976,6 +989,7 @@ texture = ExtResource("4_xeinu")
4:6/0/custom_data_2 = "H_WAY"
4:6/0/custom_data_4 = true
4:6/0/custom_data_5 = 1
4:6/0/custom_data_6 = true
4:5/0 = 0
4:5/0/custom_data_3 = Vector2i(0, 1)
4:5/0/custom_data_5 = 255
@ -988,6 +1002,7 @@ texture = ExtResource("4_xeinu")
5:6/0/custom_data_3 = Vector2i(-1, 0)
5:6/0/custom_data_4 = true
5:6/0/custom_data_5 = 1
5:6/0/custom_data_6 = true
5:7/0 = 0
5:7/0/custom_data_3 = Vector2i(-1, -1)
5:7/0/custom_data_5 = 255
@ -1012,6 +1027,8 @@ custom_data_layer_4/name = "walkable"
custom_data_layer_4/type = 1
custom_data_layer_5/name = "astar_weight"
custom_data_layer_5/type = 2
custom_data_layer_6/name = "indoors"
custom_data_layer_6/type = 1
sources/0 = SubResource("TileSetAtlasSource_fyh4j")
sources/1 = SubResource("TileSetAtlasSource_mv7cw")