le player se pilote à la souris ou avec le doigt

reste à faire: intérargir avec bob
This commit is contained in:
Thomas
2025-03-28 19:08:24 +01:00
parent 7a58999034
commit be1ef5d71c
12 changed files with 207 additions and 67 deletions

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=17 format=3 uid="uid://bleadp4yrdgj"] [gd_scene load_steps=18 format=3 uid="uid://bleadp4yrdgj"]
[ext_resource type="Script" path="res://caracters/human.gd" id="1_x3vfc"] [ext_resource type="Script" path="res://caracters/human.gd" id="1_x3vfc"]
[ext_resource type="AnimationNodeStateMachine" uid="uid://ddr1ltkievtku" path="res://animations/human/human_state_machine.tres" id="2_86nrf"] [ext_resource type="AnimationNodeStateMachine" uid="uid://ddr1ltkievtku" path="res://animations/human/human_state_machine.tres" id="2_86nrf"]
@@ -10,6 +10,7 @@
[ext_resource type="PackedScene" uid="uid://07byq4mh8uwv" path="res://caracters/npc_controler.tscn" id="8_uueub"] [ext_resource type="PackedScene" uid="uid://07byq4mh8uwv" path="res://caracters/npc_controler.tscn" id="8_uueub"]
[ext_resource type="PackedScene" uid="uid://x2asns3kiqwg" path="res://interactable/interaction_zone.tscn" id="9_35f01"] [ext_resource type="PackedScene" uid="uid://x2asns3kiqwg" path="res://interactable/interaction_zone.tscn" id="9_35f01"]
[ext_resource type="PackedScene" uid="uid://dn10ervwv15oo" path="res://UI/clues/bubble_clue.tscn" id="10_rm4iv"] [ext_resource type="PackedScene" uid="uid://dn10ervwv15oo" path="res://UI/clues/bubble_clue.tscn" id="10_rm4iv"]
[ext_resource type="PackedScene" uid="uid://cjm4k0fv7eger" path="res://caracters/human_pathfinder.tscn" id="11_ubech"]
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_a4vmx"] [sub_resource type="CapsuleShape2D" id="CapsuleShape2D_a4vmx"]
radius = 5.0 radius = 5.0
@@ -94,6 +95,8 @@ visible = false
z_index = 1000 z_index = 1000
position = Vector2(44, -38) position = Vector2(44, -38)
[node name="Node2D" parent="." instance=ExtResource("11_ubech")]
[connection signal="start_intracting" from="." to="npcControler" method="_on_character_body_2d_start_intracting"] [connection signal="start_intracting" from="." to="npcControler" method="_on_character_body_2d_start_intracting"]
[connection signal="area_entered" from="detector" to="." method="_on_area_2d_area_entered"] [connection signal="area_entered" from="detector" to="." method="_on_area_2d_area_entered"]
[connection signal="body_entered" from="detector" to="." method="_on_area_2d_body_entered"] [connection signal="body_entered" from="detector" to="." method="_on_area_2d_body_entered"]

View File

@@ -0,0 +1,72 @@
extends Node2D
var astar_grid: AStarGrid2D
var toFollow: Array[Vector2i]
@onready var world: World = get_parent().get_parent();
@onready var controled:Human = get_parent()
@export var can_walk_on_roads = false
var destination:Vector2 = Vector2.INF
var HumanLayer = 0
var car_layer = 1
func _ready() -> void:
astar_grid = AStarGrid2D.new()
astar_grid.region = world.get_used_rect()
astar_grid.cell_size = Vector2(48, 48)
astar_grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
astar_grid.default_compute_heuristic = AStarGrid2D.HEURISTIC_EUCLIDEAN
astar_grid.default_estimate_heuristic = AStarGrid2D.HEURISTIC_EUCLIDEAN
astar_grid.update()
# only take into account the tiles that are marked with a navigation layer for cars
for x in world.get_used_rect().size.x:
for y in world.get_used_rect().size.y:
var tile_position = Vector2(
x + world.get_used_rect().position.x,
y + world.get_used_rect().position.y,
)
var tile_data = world.get_cell_tile_data(tile_position)
if tile_data == null or tile_data.get_navigation_polygon(HumanLayer) == null:
if can_walk_on_roads:
if tile_data == null or tile_data.get_navigation_polygon(car_layer) == null:
astar_grid.set_point_solid(tile_position)
else:
astar_grid.set_point_solid(tile_position)
for obstacles in world.obstacles:
if obstacles.get_cell_tile_data(tile_position) != null and obstacles.get_cell_tile_data(tile_position).get_collision_polygons_count(0):
astar_grid.set_point_solid(tile_position)
func _process(delta: float) -> void:
if !controled:
return
if destination != Vector2.INF:
var my_global_position = controled.get_feet_global_position()
var target_global_position = destination
# make the wanted position on the track move ahead by a certain amount
#if toFollow == null or toFollow.is_empty():
# compute the new navigation points the car should follow
var points = astar_grid.get_id_path(
world.local_to_map(world.to_local(my_global_position)),
world.local_to_map(world.to_local(target_global_position))
).slice(1, -1)
if !points.is_empty():
toFollow = points
if $Label.visible:
$Label.text = (
"position "+str(world.local_to_map(world.to_local(my_global_position)))+" , "+str(my_global_position)+
"\ntarget position "+ str(world.local_to_map(world.to_local(target_global_position)))+" , "+str(target_global_position)+
"\npoint to follow "+str(toFollow)
)
if !toFollow.is_empty():
if world.local_to_map(world.to_local(my_global_position)) == toFollow.front():
toFollow.pop_front()
if !toFollow.is_empty():
controled.moveFeetTo(world.to_global(world.map_to_local(toFollow.front())));
else:
controled.face(target_global_position)
destination = Vector2.INF

View File

@@ -0,0 +1,11 @@
[gd_scene load_steps=2 format=3 uid="uid://cjm4k0fv7eger"]
[ext_resource type="Script" path="res://caracters/human_pathfinder.gd" id="1_247nc"]
[node name="PathFinder" type="Node2D"]
script = ExtResource("1_247nc")
[node name="Label" type="Label" parent="."]
visible = false
offset_right = 40.0
offset_bottom = 23.0

View File

@@ -1,69 +1,11 @@
extends Node extends Node
var astar_grid: AStarGrid2D
var toFollow: Array[Vector2i]
@onready var world: TileMapLayer = get_parent().get_parent();
@onready var obstacles: TileMapLayer = world.get_children()[2]
@onready var controled:Human = get_parent() @onready var controled:Human = get_parent()
var destination:Node2D
var HumanLayer = 0
func _ready() -> void:
astar_grid = AStarGrid2D.new()
astar_grid.region = world.get_used_rect()
astar_grid.cell_size = Vector2(48, 48)
astar_grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
astar_grid.default_compute_heuristic = AStarGrid2D.HEURISTIC_EUCLIDEAN
astar_grid.default_estimate_heuristic = AStarGrid2D.HEURISTIC_EUCLIDEAN
astar_grid.update()
# only take into account the tiles that are marked with a navigation layer for cars
for x in world.get_used_rect().size.x:
for y in world.get_used_rect().size.y:
var tile_position = Vector2(
x + world.get_used_rect().position.x,
y + world.get_used_rect().position.y,
)
var tile_data = world.get_cell_tile_data(tile_position)
if tile_data == null or tile_data.get_navigation_polygon(HumanLayer) == null:
astar_grid.set_point_solid(tile_position)
if obstacles.get_cell_tile_data(tile_position) != null and obstacles.get_cell_tile_data(tile_position).get_collision_polygons_count(0):
astar_grid.set_point_solid(tile_position)
func _process(delta: float) -> void:
if !controled:
return
if destination:
var my_global_position = controled.get_feet_global_position()
var target_global_position = destination.get_feet_global_position()
# make the wanted position on the track move ahead by a certain amount
#if toFollow == null or toFollow.is_empty():
# compute the new navigation points the car should follow
var points = astar_grid.get_id_path(
world.local_to_map(world.to_local(my_global_position)),
world.local_to_map(world.to_local(target_global_position))
).slice(1, -1)
if !points.is_empty():
toFollow = points
if $Label.visible:
$Label.text = (
"position "+str(world.local_to_map(world.to_local(my_global_position)))+" , "+str(my_global_position)+
"\ntarget position "+ str(world.local_to_map(world.to_local(target_global_position)))+" , "+str(target_global_position)+
"\npoint to follow "+str(toFollow)
)
if !toFollow.is_empty():
if world.local_to_map(world.to_local(my_global_position)) == toFollow.front():
toFollow.pop_front()
if !toFollow.is_empty():
controled.moveFeetTo(world.to_global(world.map_to_local(toFollow.front())));
else:
controled.face(target_global_position)
func _on_character_body_2d_start_intracting(interactingWith: Human) -> void: func _on_character_body_2d_start_intracting(interactingWith: Human) -> void:
controled.face(interactingWith.global_position) controled.face(interactingWith.global_position)

View File

@@ -0,0 +1,95 @@
[gd_scene load_steps=13 format=3 uid="uid://vclpg4e4ql54"]
[ext_resource type="Script" path="res://caracters/human.gd" id="1_l1sti"]
[ext_resource type="Script" path="res://caracters/player/player_controler.gd" id="1_oapm5"]
[ext_resource type="AnimationNodeStateMachine" uid="uid://ddr1ltkievtku" path="res://animations/human/human_state_machine.tres" id="3_1y7fn"]
[ext_resource type="PackedScene" uid="uid://bvsendl25xjju" path="res://animations/human/human_animation_player.tscn" id="3_c286j"]
[ext_resource type="PackedScene" uid="uid://cg4dhp7qe68pt" path="res://animations/human/human.tscn" id="4_rsj36"]
[ext_resource type="PackedScene" uid="uid://brh7cqaxc13ie" path="res://zindex/ZIndexControler.tscn" id="5_pb07x"]
[ext_resource type="PackedScene" uid="uid://cjm4k0fv7eger" path="res://caracters/human_pathfinder.tscn" id="7_g6pgs"]
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_a4vmx"]
radius = 5.0
height = 48.0
[sub_resource type="AnimationNodeTimeScale" id="AnimationNodeTimeScale_85jde"]
[sub_resource type="AnimationNodeBlendTree" id="AnimationNodeBlendTree_iwsa7"]
nodes/HumanState/node = ExtResource("3_1y7fn")
nodes/HumanState/position = Vector2(133.333, 120)
nodes/TimeScale/node = SubResource("AnimationNodeTimeScale_85jde")
nodes/TimeScale/position = Vector2(453.333, 53.3333)
nodes/output/position = Vector2(640, 146.667)
node_connections = [&"TimeScale", 0, &"HumanState", &"output", 0, &"TimeScale"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_1kv0e"]
size = Vector2(40, 10)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_11ib5"]
size = Vector2(20, 150)
[node name="CharacterBody2D" type="CharacterBody2D"]
z_index = 100
motion_mode = 1
script = ExtResource("1_l1sti")
metadata/_edit_vertical_guides_ = [-20.0]
metadata/_edit_horizontal_guides_ = [48.0]
[node name="Camera2D" type="Camera2D" parent="."]
zoom = Vector2(1.5, 1.5)
position_smoothing_enabled = true
drag_horizontal_enabled = true
drag_vertical_enabled = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(0, 43)
rotation = 1.5708
shape = SubResource("CapsuleShape2D_a4vmx")
[node name="AnimationTree" type="AnimationTree" parent="."]
tree_root = SubResource("AnimationNodeBlendTree_iwsa7")
advance_expression_base_node = NodePath("..")
anim_player = NodePath("../AnimationPlayer")
parameters/HumanState/grabing/blend_position = Vector2(0, 0)
parameters/HumanState/idling/blend_position = Vector2(0.000657439, 1.0284)
parameters/HumanState/walking/blend_position = Vector2(0, 0)
parameters/TimeScale/scale = 1.0
[node name="AnimationPlayer" parent="." instance=ExtResource("3_c286j")]
[node name="Sprite2D" parent="." instance=ExtResource("4_rsj36")]
frame = 1
[node name="ZIndexControler" parent="." instance=ExtResource("5_pb07x")]
position = Vector2(-1, 37)
[node name="ShapeCast2D" type="ShapeCast2D" parent="ZIndexControler"]
position = Vector2(1, -13)
shape = SubResource("RectangleShape2D_1kv0e")
target_position = Vector2(0, -48)
[node name="Area2D" type="Area2D" parent="."]
collision_layer = 4
collision_mask = 4
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
position = Vector2(0, 43)
rotation = 1.5708
shape = SubResource("CapsuleShape2D_a4vmx")
[node name="controleur" type="Node2D" parent="." node_paths=PackedStringArray("human", "ray")]
script = ExtResource("1_oapm5")
human = NodePath("..")
ray = NodePath("../ShapeCast2D")
[node name="ShapeCast2D" type="ShapeCast2D" parent="."]
shape = SubResource("RectangleShape2D_11ib5")
target_position = Vector2(1, 90)
collision_mask = 8
collide_with_areas = true
collide_with_bodies = false
[node name="PathFinder" parent="." instance=ExtResource("7_g6pgs")]
[connection signal="area_entered" from="Area2D" to="." method="_on_area_2d_area_entered"]
[connection signal="body_entered" from="Area2D" to="." method="_on_area_2d_body_entered"]

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=12 format=3 uid="uid://vclpg4e4ql54"] [gd_scene load_steps=13 format=3 uid="uid://vclpg4e4ql54"]
[ext_resource type="Script" path="res://caracters/human.gd" id="1_l1sti"] [ext_resource type="Script" path="res://caracters/human.gd" id="1_l1sti"]
[ext_resource type="Script" path="res://caracters/player/player_controler.gd" id="1_oapm5"] [ext_resource type="Script" path="res://caracters/player/player_controler.gd" id="1_oapm5"]
@@ -6,6 +6,7 @@
[ext_resource type="PackedScene" uid="uid://bvsendl25xjju" path="res://animations/human/human_animation_player.tscn" id="3_c286j"] [ext_resource type="PackedScene" uid="uid://bvsendl25xjju" path="res://animations/human/human_animation_player.tscn" id="3_c286j"]
[ext_resource type="PackedScene" uid="uid://cg4dhp7qe68pt" path="res://animations/human/human.tscn" id="4_rsj36"] [ext_resource type="PackedScene" uid="uid://cg4dhp7qe68pt" path="res://animations/human/human.tscn" id="4_rsj36"]
[ext_resource type="PackedScene" uid="uid://brh7cqaxc13ie" path="res://zindex/ZIndexControler.tscn" id="5_pb07x"] [ext_resource type="PackedScene" uid="uid://brh7cqaxc13ie" path="res://zindex/ZIndexControler.tscn" id="5_pb07x"]
[ext_resource type="PackedScene" uid="uid://cjm4k0fv7eger" path="res://caracters/human_pathfinder.tscn" id="7_g6pgs"]
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_a4vmx"] [sub_resource type="CapsuleShape2D" id="CapsuleShape2D_a4vmx"]
radius = 5.0 radius = 5.0
@@ -88,5 +89,8 @@ collision_mask = 8
collide_with_areas = true collide_with_areas = true
collide_with_bodies = false collide_with_bodies = false
[node name="PathFinder" parent="." instance=ExtResource("7_g6pgs")]
can_walk_on_roads = true
[connection signal="area_entered" from="Area2D" to="." method="_on_area_2d_area_entered"] [connection signal="area_entered" from="Area2D" to="." method="_on_area_2d_area_entered"]
[connection signal="body_entered" from="Area2D" to="." method="_on_area_2d_body_entered"] [connection signal="body_entered" from="Area2D" to="." method="_on_area_2d_body_entered"]

View File

@@ -1,8 +1,9 @@
extends Node extends Node2D
@export var human: Human @export var human: Human
@export var ray : ShapeCast2D @export var ray : ShapeCast2D
var can_interact_with : Node2D var can_interact_with : Node2D
@onready var world: World = get_parent().get_parent();
var possible_interactables : Array[Node2D] var possible_interactables : Array[Node2D]
@@ -18,6 +19,11 @@ func _unhandled_input(event: InputEvent) -> void:
human.wants_to_interact_with = can_interact_with human.wants_to_interact_with = can_interact_with
else: else:
human.wants_to_grab = true human.wants_to_grab = true
if event is InputEventMouseButton or event is InputEventScreenTouch:
var tile_pos = world.local_to_map(world.to_local(get_global_mouse_position()))
if event.pressed:
print(event.position, tile_pos)
$"../PathFinder".destination = get_global_mouse_position()
func _process(delta) -> void: func _process(delta) -> void:
ray.target_position = human.last_facing_direction * 48 ray.target_position = human.last_facing_direction * 48

4
maps/world.gd Normal file
View File

@@ -0,0 +1,4 @@
extends TileMapLayer
class_name World
@export var obstacles: Array[TileMapLayer]

File diff suppressed because one or more lines are too long

View File

@@ -25,10 +25,10 @@ point_count = 20
[node name="bob" parent="world" instance=ExtResource("5_n64eb")] [node name="bob" parent="world" instance=ExtResource("5_n64eb")]
position = Vector2(-333, -262) position = Vector2(-333, -262)
[node name="movibles" type="Node2D" parent="."] [node name="player" parent="world" instance=ExtResource("2_5x6b5")]
position = Vector2(-171, -253)
[node name="player" parent="movibles" instance=ExtResource("2_5x6b5")] [node name="movibles" type="Node2D" parent="."]
position = Vector2(-107, -161)
[node name="cars" type="Node" parent="movibles"] [node name="cars" type="Node" parent="movibles"]

View File

@@ -1,6 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://cl201baro5y5"] [gd_scene load_steps=2 format=3 uid="uid://cl201baro5y5"]
[ext_resource type="Script" path="res://scenes/pathFollow.gd" id="1_fbnn2"] [ext_resource type="Script" path="res://vehicules/car_pathfinder.gd" id="1_fbnn2"]
[node name="voiture_pnj" type="PathFollow2D"] [node name="voiture_pnj" type="PathFollow2D"]
position = Vector2(-411, 81) position = Vector2(-411, 81)