diff --git a/autoloads/audio_manager.gd b/autoloads/audio_manager.gd index e69de29..61510e1 100644 --- a/autoloads/audio_manager.gd +++ b/autoloads/audio_manager.gd @@ -0,0 +1 @@ +extends Node diff --git a/autoloads/game_state.gd b/autoloads/game_state.gd index e69de29..f5e14f3 100644 --- a/autoloads/game_state.gd +++ b/autoloads/game_state.gd @@ -0,0 +1,19 @@ +extends Node + +var inventory := {} + +func add_item(id: String) -> void: + if inventory.has(id): + inventory[id] += 1 + else: + inventory[id] = 1 + print("Inventaire : ", inventory) + +func has_item(id: String) -> bool: + return inventory.get(id, 0) > 0 + +func remove_item(id: String) -> void: + if has_item(id): + inventory[id] -= 1 + if inventory[id] <= 0: + inventory.erase(id) diff --git a/autoloads/scene_manager.gd b/autoloads/scene_manager.gd index e69de29..61510e1 100644 --- a/autoloads/scene_manager.gd +++ b/autoloads/scene_manager.gd @@ -0,0 +1 @@ +extends Node diff --git a/entities/items/item.gd b/entities/items/item.gd index e69de29..640a474 100644 --- a/entities/items/item.gd +++ b/entities/items/item.gd @@ -0,0 +1,9 @@ +extends Area2D + +@export var item_id : String = "engrenage" +@export var item_name : String = "Engrenage" + +func interact() -> void: + GameState.add_item(item_id) + print("Ramassé : ", item_name) + queue_free() # supprime l'item de la scène diff --git a/entities/items/item.tscn b/entities/items/item.tscn index 04748d0..5fdc0e9 100644 --- a/entities/items/item.tscn +++ b/entities/items/item.tscn @@ -1,3 +1,22 @@ [gd_scene format=3 uid="uid://grs5ypwkxv3x"] +[ext_resource type="Script" uid="uid://ps4bl4k552ia" path="res://entities/items/item.gd" id="1_m0k53"] +[ext_resource type="Texture2D" uid="uid://cl6rwoc5egg4x" path="res://tests/assets/sprites/objects/rpgItems.png" id="2_h1ip2"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_l3fek"] +radius = 6.0827627 + [node name="Item" type="Node2D" unique_id=1691516870] + +[node name="Area2D" type="Area2D" parent="." unique_id=260230698] +collision_layer = 8 +collision_mask = 0 +script = ExtResource("1_m0k53") + +[node name="Sprite2D" type="Sprite2D" parent="Area2D" unique_id=1835094462] +texture = ExtResource("2_h1ip2") +region_enabled = true +region_rect = Rect2(34, 82, 12, 12) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D" unique_id=1351590810] +shape = SubResource("CircleShape2D_l3fek") diff --git a/entities/player/player.gd b/entities/player/player.gd index 9aa374d..4d05c5d 100644 --- a/entities/player/player.gd +++ b/entities/player/player.gd @@ -3,21 +3,24 @@ extends CharacterBody2D const SPEED := 120 @onready var sprite := $AnimatedSprite2D +@onready var interaction_area := $InteractionArea var last_direction := Vector2.DOWN var interactable : Node = null +func _ready() -> void: + interaction_area.area_entered.connect(_on_interaction_area_area_entered) + interaction_area.area_exited.connect(_on_interaction_area_area_exited) + func _physics_process(_delta: float) -> void: var direction := Input.get_vector( "move_left", "move_right", "move_up", "move_down" ) - velocity = direction * SPEED move_and_slide() - - position.round() + position = position.round() _update_animation(direction) - + func _unhandled_input(event: InputEvent) -> void: if event.is_action_pressed("interact") and interactable: interactable.interact() @@ -35,3 +38,13 @@ func _dir_name(dir: Vector2) -> String: return "right" if dir.x > 0 else "left" else: return "down" if dir.y > 0 else "up" + +# --- Interaction --- +func _on_interaction_area_area_entered(area: Area2D) -> void: + print("Area détectée : ", area.name) + if area.has_method("interact"): + interactable = area + +func _on_interaction_area_area_exited(area: Area2D) -> void: + if area == interactable: + interactable = null diff --git a/entities/player/player.tscn b/entities/player/player.tscn index 3f22cec..3c9bd68 100644 --- a/entities/player/player.tscn +++ b/entities/player/player.tscn @@ -506,11 +506,12 @@ animations = [{ }] [sub_resource type="RectangleShape2D" id="RectangleShape2D_bectd"] -size = Vector2(45.375, 49.6875) +size = Vector2(45.375, 42.84375) [node name="Player" type="Node2D" unique_id=628518902] [node name="CharacterBody2D" type="CharacterBody2D" parent="." unique_id=92363508] +collision_layer = 3 script = ExtResource("1_fkugw") [node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D" unique_id=865652003] @@ -526,9 +527,12 @@ sprite_frames = SubResource("SpriteFrames_ix1gt") animation = &"walk_right" autoplay = "idle_down" -[node name="InteractionArea" type="Area2D" parent="CharacterBody2D/AnimatedSprite2D" unique_id=22124935] +[node name="InteractionArea" type="Area2D" parent="CharacterBody2D" unique_id=22124935] +position = Vector2(-1, -12) +collision_layer = 16 +collision_mask = 12 -[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D/AnimatedSprite2D/InteractionArea" unique_id=51125783] -position = Vector2(2, 6) +[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D/InteractionArea" unique_id=51125783] +position = Vector2(2, 7) shape = SubResource("RectangleShape2D_bectd") debug_color = Color(0.9411765, 0, 0.29411766, 0.19607843) diff --git a/project.godot b/project.godot index d62ee21..71fa27f 100644 --- a/project.godot +++ b/project.godot @@ -16,6 +16,12 @@ config/features=PackedStringArray("4.6", "GL Compatibility") run/max_fps=60 config/icon="res://icon.svg" +[autoload] + +GameState="*uid://dj5xpo55gtvr7" +AudioManager="uid://dsilww05kuakm" +SceneManager="uid://cqmks3a5dsxuy" + [display] window/size/viewport_width=320 diff --git a/tests/main.tscn b/tests/main.tscn index e3fbe3d..edf808d 100644 --- a/tests/main.tscn +++ b/tests/main.tscn @@ -3,6 +3,7 @@ [ext_resource type="Script" uid="uid://dgjqqygp6oydl" path="res://tests/main_test.gd" id="1_ctdtd"] [ext_resource type="Texture2D" uid="uid://b2j1csoujc7gc" path="res://tests/assets/tileset/Dungeon_Tileset.png" id="2_pi5dy"] [ext_resource type="PackedScene" uid="uid://mh3msmluve7p" path="res://entities/player/player.tscn" id="3_pi5dy"] +[ext_resource type="PackedScene" uid="uid://grs5ypwkxv3x" path="res://entities/items/item.tscn" id="4_gn22r"] [sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_gn22r"] texture = ExtResource("2_pi5dy") @@ -154,3 +155,12 @@ label_settings = SubResource("LabelSettings_wsebr") [node name="Player" parent="Node2D" unique_id=628518902 instance=ExtResource("3_pi5dy")] position = Vector2(158, 121) + +[node name="Item" parent="Node2D" unique_id=1691516870 instance=ExtResource("4_gn22r")] +position = Vector2(237, 94) + +[node name="Item2" parent="Node2D" unique_id=738262451 instance=ExtResource("4_gn22r")] +position = Vector2(288, 162) + +[node name="Item3" parent="Node2D" unique_id=589131931 instance=ExtResource("4_gn22r")] +position = Vector2(186, 210)