From 72a1ace2f1829f83fd74de1d0c7d98748b835a7c Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Sat, 25 Mar 2023 10:21:24 -0700 Subject: [PATCH] basic connecting areas --- BoundingBoxes/ConnectorBox.cs | 52 +++++++++++++++++++++++++++++++++++ Scenes/Level.tscn | 16 ++++++++++- Utils/World.cs | 9 +++++- project.godot | 5 ++++ 4 files changed, 80 insertions(+), 2 deletions(-) diff --git a/BoundingBoxes/ConnectorBox.cs b/BoundingBoxes/ConnectorBox.cs index ebdd868..040bb0e 100644 --- a/BoundingBoxes/ConnectorBox.cs +++ b/BoundingBoxes/ConnectorBox.cs @@ -1,14 +1,66 @@ using Godot; using System; +using SupaLidlGame.Characters; namespace SupaLidlGame.BoundingBoxes { public partial class ConnectorBox : Area2D { + [Signal] + public delegate void RequestedEnterEventHandler( + ConnectorBox box, + Player player); + [Export] public string ToArea { get; set; } [Export] public string ToConnector { get; set; } + + /// + /// Determines if the connector requires the user to interact to enter + /// the connector + /// + [Export] + public bool RequiresInteraction { get; set; } = true; + + [Export] + public CollisionShape2D Collision { get; set; } + + private Player _player = null; + + public override void _Ready() + { + if (Collision is null) + { + throw new NullReferenceException("Collision not specified"); + } + + BodyEntered += (Node2D body) => + { + if (body is Player player) + { + _player = player; + } + }; + + BodyExited += (Node2D body) => + { + if (body is Player) + { + _player = null; + } + }; + } + + public override void _Process(double delta) + { + if (Input.IsActionJustPressed("interact")) + { + EmitSignal(SignalName.RequestedEnter, this, _player); + } + + base._Process(delta); + } } } diff --git a/Scenes/Level.tscn b/Scenes/Level.tscn index 5984242..65ea681 100644 --- a/Scenes/Level.tscn +++ b/Scenes/Level.tscn @@ -1,10 +1,11 @@ -[gd_scene load_steps=8 format=3 uid="uid://dd6xy1y0m8smm"] +[gd_scene load_steps=10 format=3 uid="uid://dd6xy1y0m8smm"] [ext_resource type="Script" path="res://Utils/World.cs" id="1_1k6ew"] [ext_resource type="Texture2D" uid="uid://gm2pcnfg7h8j" path="res://Assets/Sprites/tileset.png" id="1_k6myx"] [ext_resource type="PackedScene" uid="uid://b2254pup8k161" path="res://Characters/Player.tscn" id="1_m35hr"] [ext_resource type="PackedScene" uid="uid://ddcf6bfv212wj" path="res://Characters/ExampleEnemy.tscn" id="2_uti3y"] [ext_resource type="PackedScene" uid="uid://dhl071rj5wyvx" path="res://Entities/Campfire.tscn" id="4_6awvs"] +[ext_resource type="Script" path="res://BoundingBoxes/ConnectorBox.cs" id="6_eol7a"] [sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_1pa1q"] texture = ExtResource("1_k6myx") @@ -1110,6 +1111,9 @@ texture = ExtResource("1_k6myx") physics_layer_0/collision_layer = 1 sources/0 = SubResource("TileSetAtlasSource_1pa1q") +[sub_resource type="RectangleShape2D" id="RectangleShape2D_drg0k"] +size = Vector2(34, 14) + [node name="World" type="Node2D"] script = ExtResource("1_1k6ew") @@ -1177,3 +1181,13 @@ scale = Vector2(1.00571, 1) Faction = 1 [node name="Spawners" type="Node2D" parent="TileMap/Entities"] + +[node name="Areas" type="Node2D" parent="TileMap"] + +[node name="Area2D" type="Area2D" parent="TileMap/Areas" node_paths=PackedStringArray("Collision")] +script = ExtResource("6_eol7a") +Collision = NodePath("CollisionShape2D") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="TileMap/Areas/Area2D"] +position = Vector2(158, -24) +shape = SubResource("RectangleShape2D_drg0k") diff --git a/Utils/World.cs b/Utils/World.cs index 8e370b7..9a8a784 100644 --- a/Utils/World.cs +++ b/Utils/World.cs @@ -34,7 +34,7 @@ namespace SupaLidlGame.Utils base._Process(delta); } - public void MoveToArea(string area, string connector) + public void MoveToArea(string area, string connector, Player player) { if (area != CurrentArea) { @@ -45,5 +45,12 @@ namespace SupaLidlGame.Utils _currentConnector = connector; } + + public void _on_area_2d_requested_enter( + BoundingBoxes.ConnectorBox box, + Player player) + { + MoveToArea(box.ToArea, box.ToConnector, player); + } } } diff --git a/project.godot b/project.godot index d44123f..6febc80 100644 --- a/project.godot +++ b/project.godot @@ -61,6 +61,11 @@ equip={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":69,"key_label":0,"unicode":0,"echo":false,"script":null) ] } +interact={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":70,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} [layer_names]