From 1597903d13f5beb0feb3a2d25299dc1815f97611 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Thu, 27 Apr 2023 23:56:06 +0200 Subject: [PATCH] Added ClickableComponent. Not sure what to do with it, though... --- components/ClickableComponent.cs | 55 ++++++++++++++++++++++++++++++++ entities/Player.cs | 14 -------- scenes/Game.tscn | 27 +++++++++++++++- 3 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 components/ClickableComponent.cs diff --git a/components/ClickableComponent.cs b/components/ClickableComponent.cs new file mode 100644 index 0000000..4d297db --- /dev/null +++ b/components/ClickableComponent.cs @@ -0,0 +1,55 @@ +using System; +using Godot; +using GoDotLog; + +public class ClickableComponent : Spatial +{ + [Export] public string ClickName = "ClickName"; + + [Signal] + delegate void Clicked(); + + public bool IsMouseOver = false; + + // private members + private CollisionObject _collisionObject; + + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + _collisionObject = (CollisionObject)FindNode("Area", false); + if (_collisionObject == null) + { + GD.PrintErr("Error: could not find Area for Clickable Component!"); + return; + } + + _collisionObject.Connect("input_event", this, nameof(OnAreaInputEvent)); + _collisionObject.Connect("mouse_entered", this, nameof(OnAreaMouseEntered)); + _collisionObject.Connect("mouse_exited", this, nameof(OnAreaMouseExited)); + } + + public void OnAreaInputEvent(Node camera, InputEvent inputEvent, Vector3 position, Vector3 normal, + int shapeIndex) + { + if (IsMouseOver && inputEvent is InputEventMouseButton) + { + InputEventMouseButton mouseButtonEvent = (InputEventMouseButton)inputEvent; + if (mouseButtonEvent.ButtonIndex == 1 && mouseButtonEvent.Pressed) + { + GD.Print("Clicked on Clickable Component!"); + EmitSignal("Clicked", this); + } + } + } + + public void OnAreaMouseEntered() + { + IsMouseOver = true; + } + + public void OnAreaMouseExited() + { + IsMouseOver = false; + } +} \ No newline at end of file diff --git a/entities/Player.cs b/entities/Player.cs index 3009f13..6564ae8 100644 --- a/entities/Player.cs +++ b/entities/Player.cs @@ -28,13 +28,6 @@ public class Player : Entity _navigationComponent = (NavigationComponent)FindNode("Navigation", false); _navigationComponent.TileWorld = _worldInfo.TileWorld; TaskQueueComponent = new TaskQueueComponent(); - - _movable = (MovableComponent)FindNode("Movable", false); - if (_movable != null) - { - _movable.Connect("OrientationUpdated", this, nameof(OnOrientationUpdated)); - } - _geometry = (Spatial)FindNode("Geometry", false); } @@ -74,12 +67,5 @@ public class Player : Entity { _navigationComponent.UpdateCurrentGoal(GlobalTransform); } - - - } - - private void OnOrientationUpdated(float newOrientation) - { - _geometry.Transform = new Transform(new Quat(Vector3.Up, newOrientation), Vector3.Zero); } } \ No newline at end of file diff --git a/scenes/Game.tscn b/scenes/Game.tscn index fd732c6..606eed2 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=22 format=2] +[gd_scene load_steps=27 format=2] [ext_resource path="res://scenes/StreamContainer.cs" type="Script" id=1] [ext_resource path="res://components/NavigationComponent.cs" type="Script" id=2] @@ -11,6 +11,8 @@ [ext_resource path="res://scenes/Game.cs" type="Script" id=9] [ext_resource path="res://scenes/DebugCamera.gd" type="Script" id=10] [ext_resource path="res://assets/CreatusPiratePack/characters/Pirate1final_0.01.glb" type="PackedScene" id=13] +[ext_resource path="res://components/ClickableComponent.cs" type="Script" id=14] +[ext_resource path="res://entities/Flower.cs" type="Script" id=15] [sub_resource type="OpenSimplexNoise" id=10] period = 39.6 @@ -279,8 +281,31 @@ script = ExtResource( 2 ) [node name="Chest" parent="Entities" instance=ExtResource( 7 )] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 2.01499, 0, -1.3224 ) +[node name="Flower" type="KinematicBody" parent="Entities"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 3.75737, 0, -3.73012 ) +input_ray_pickable = false +script = ExtResource( 15 ) + +[node name="MeshInstance" type="MeshInstance" parent="Entities/Flower"] +transform = Transform( 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0 ) +mesh = SubResource( 17 ) + +[node name="CollisionShape" type="CollisionShape" parent="Entities/Flower"] +shape = SubResource( 18 ) + +[node name="ClickableComponent" type="Spatial" parent="Entities/Flower"] +script = ExtResource( 14 ) + +[node name="Area" type="Area" parent="Entities/Flower/ClickableComponent"] + +[node name="CollisionShape" type="CollisionShape" parent="Entities/Flower/ClickableComponent/Area"] +transform = Transform( 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0 ) +shape = SubResource( 19 ) + [node name="Chest3" parent="Entities" instance=ExtResource( 7 )] transform = Transform( 0.550568, 0, -0.83479, 0, 1, 0, 0.83479, 0, 0.550568, 4.88275, 0, -1.70504 ) [node name="Chest2" parent="Entities" instance=ExtResource( 7 )] transform = Transform( 0.793576, 0, -0.608471, 0, 1, 0, 0.608471, 0, 0.793576, 2.79265, 0, -5.36551 ) + +[editable path="Entities/Chest"]