From ee859886f000ce772877a42037b3a44e41083552 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Thu, 4 Jan 2024 15:45:26 +0100 Subject: [PATCH] Added CollectibleComponent and refactored GoldBar, Wood and Axe. --- components/CollectibleComponent.cs | 46 +++++++++++++++++++++++++ components/CollectibleComponent.tscn | 6 ++++ entities/Axe.cs | 7 +--- entities/Axe.tscn | 7 ++-- entities/Chest.cs | 2 +- entities/Entity.cs | 26 +++++++++++++++ entities/GoldBar.cs | 50 +--------------------------- entities/GoldBar.tscn | 9 +++-- entities/Player.cs | 30 +++++++---------- entities/Tree.cs | 2 +- entities/Wood.cs | 50 +--------------------------- entities/Wood.tscn | 9 +++-- scenes/Game.tscn | 8 ++++- 13 files changed, 119 insertions(+), 133 deletions(-) create mode 100644 components/CollectibleComponent.cs create mode 100644 components/CollectibleComponent.tscn diff --git a/components/CollectibleComponent.cs b/components/CollectibleComponent.cs new file mode 100644 index 0000000..e60baa2 --- /dev/null +++ b/components/CollectibleComponent.cs @@ -0,0 +1,46 @@ +using Godot; + +public class CollectibleComponent : Component { + private Vector3 targetPosition; + private bool hasTarget; + + // Called when the node enters the scene tree for the first time. + public override void _Ready() { } + + public void SetTarget(Vector3 target) { + targetPosition = target; + hasTarget = true; + } + + public void UnsetTarget() { + hasTarget = false; + } + + public void PhysicsProcess(float delta, Entity entity) { + if (hasTarget) { + if (targetPosition.IsEqualApprox(entity.GlobalTransform.origin)) { + targetPosition = entity.GlobalTransform.origin; + entity.Velocity = Vector3.Zero; + } + + + Vector3 targetDirection = (targetPosition - entity.GlobalTransform.origin).Normalized(); + entity.Velocity = targetDirection * (entity.Velocity.Length() + 10 * delta); + entity.Transform = new Transform(entity.Transform.basis.Rotated(Vector3.Up, delta * 2.0f), + entity.Transform.origin); + } else { + entity.Velocity = entity.Velocity - 9.81f * delta * Vector3.Up; + } + + entity.Velocity = entity.MoveAndSlide(entity.Velocity, Vector3.Up); + + if (entity.IsOnFloor() || Mathf.Abs(entity.Transform.origin.y) < 0.01) { + // apply damping when on ground + entity.Velocity = entity.Velocity - entity.Velocity.Normalized() * 0.9f * delta; + } + + if (entity.Velocity.LengthSquared() < 0.01) { + entity.Velocity = Vector3.Zero; + } + } +} \ No newline at end of file diff --git a/components/CollectibleComponent.tscn b/components/CollectibleComponent.tscn new file mode 100644 index 0000000..0f472e9 --- /dev/null +++ b/components/CollectibleComponent.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://components/CollectibleComponent.cs" type="Script" id=1] + +[node name="CollectibleComponent" type="Node"] +script = ExtResource( 1 ) diff --git a/entities/Axe.cs b/entities/Axe.cs index f3269d6..e513fdc 100644 --- a/entities/Axe.cs +++ b/entities/Axe.cs @@ -1,6 +1 @@ -using Godot; - -public class Axe : StaticBody { - // Called when the node enters the scene tree for the first time. - public override void _Ready() { } -} \ No newline at end of file +public class Axe : Entity { } \ No newline at end of file diff --git a/entities/Axe.tscn b/entities/Axe.tscn index 3147c81..e149f63 100644 --- a/entities/Axe.tscn +++ b/entities/Axe.tscn @@ -1,13 +1,14 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://assets/Objects/toolAxe.tscn" type="PackedScene" id=1] [ext_resource path="res://entities/Axe.cs" type="Script" id=2] +[ext_resource path="res://components/CollectibleComponent.tscn" type="PackedScene" id=3] [sub_resource type="CylinderShape" id=1] height = 0.846435 radius = 0.687167 -[node name="Axe" type="StaticBody"] +[node name="Axe" type="KinematicBody"] collision_layer = 9 collision_mask = 0 script = ExtResource( 2 ) @@ -18,3 +19,5 @@ shape = SubResource( 1 ) [node name="toolAxe" parent="." instance=ExtResource( 1 )] transform = Transform( 0.707107, 0.707107, -3.09086e-08, 4.37114e-08, 1.91069e-15, 1, 0.707107, -0.707107, -3.09086e-08, -0.323064, 0.0760467, 0.348457 ) + +[node name="CollectibleComponent" parent="." instance=ExtResource( 3 )] diff --git a/entities/Chest.cs b/entities/Chest.cs index dbdd4a1..5de19a2 100644 --- a/entities/Chest.cs +++ b/entities/Chest.cs @@ -84,7 +84,7 @@ public class Chest : Entity, IInteractionInterface { GoldBar bar = (GoldBar)_goldBarScene.Instance(); bar.Transform = new Transform(Transform.basis.Rotated(Vector3.Up, GD.Randf() * 2 * Mathf.Pi), Transform.origin + Vector3.Up * 0.8f); - bar.velocity = new Vector3( + bar.Velocity = new Vector3( (GD.Randf() * 2f - 1f) * 2, 5 + GD.Randf() * 0.3f, (GD.Randf() * 2f - 1f) * 2 diff --git a/entities/Entity.cs b/entities/Entity.cs index 4ce6e37..1e19ada 100644 --- a/entities/Entity.cs +++ b/entities/Entity.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using Godot; public class Entity : KinematicBody { @@ -15,6 +16,31 @@ public class Entity : KinematicBody { public Vector3 Velocity { get; set; } = Vector3.Zero; public float RotationalVelocity { get; set; } = 0; + private CollectibleComponent _collectibleComponent; + + public override void _Ready() { + base._Ready(); + + foreach (Node node in GetChildren()) { + if (node is CollectibleComponent) { + Debug.Assert(_collectibleComponent == null); + _collectibleComponent = node as CollectibleComponent; + } + } + } + + public override void _PhysicsProcess(float delta) { + base._PhysicsProcess(delta); + + if (_collectibleComponent != null) { + _collectibleComponent.PhysicsProcess(delta, this); + } + } + + public CollectibleComponent GetCollectibleComponent() { + return _collectibleComponent; + } + /** * Defines the angle in plane coordinates, 0 => pointing to the right/east, pi/2 pointing up/north, range [-pi,pi]. */ diff --git a/entities/GoldBar.cs b/entities/GoldBar.cs index 0fde53f..4fbcd3a 100644 --- a/entities/GoldBar.cs +++ b/entities/GoldBar.cs @@ -1,49 +1 @@ -using Godot; - -public class GoldBar : KinematicBody { - private Vector3 targetPosition; - private bool hasTarget; - public Vector3 velocity; - - // Called when the node enters the scene tree for the first time. - public override void _Ready() { - targetPosition = GlobalTransform.origin; - } - - public void SetTarget(Vector3 target) { - targetPosition = target; - hasTarget = true; - } - - public void UnsetTarget() { - hasTarget = false; - } - - // Called every frame. 'delta' is the elapsed time since the previous frame. - public override void _PhysicsProcess(float delta) { - if (hasTarget) { - if (targetPosition.IsEqualApprox(GlobalTransform.origin)) { - targetPosition = GlobalTransform.origin; - velocity = Vector3.Zero; - } - - - Vector3 targetDirection = (targetPosition - GlobalTransform.origin).Normalized(); - velocity = targetDirection * (velocity.Length() + 10 * delta); - Transform = new Transform(Transform.basis.Rotated(Vector3.Up, delta * 2.0f), Transform.origin); - } else { - velocity.y = velocity.y - 9.81f * delta; - } - - velocity = MoveAndSlide(velocity, Vector3.Up); - - if (IsOnFloor() || Mathf.Abs(Transform.origin.y) < 0.01) { - // apply damping when on ground - velocity = velocity - velocity.Normalized() * 0.9f * delta; - } - - if (velocity.LengthSquared() < 0.01) { - velocity = Vector3.Zero; - } - } -} \ No newline at end of file +public class GoldBar : Entity { } \ No newline at end of file diff --git a/entities/GoldBar.tscn b/entities/GoldBar.tscn index 273cfa1..d265351 100644 --- a/entities/GoldBar.tscn +++ b/entities/GoldBar.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://entities/GoldBar.cs" type="Script" id=1] [ext_resource path="res://assets/CreatusPiratePack/Models/Items/gltf/Gold_Bar.glb" type="PackedScene" id=2] +[ext_resource path="res://components/CollectibleComponent.tscn" type="PackedScene" id=3] [sub_resource type="BoxShape" id=21] extents = Vector3( 0.354271, 0.0817164, 0.173406 ) @@ -16,8 +17,10 @@ script = ExtResource( 1 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.084728, 0 ) shape = SubResource( 21 ) -[node name="Spatial" type="Spatial" parent="."] +[node name="Geometry" type="Spatial" parent="."] transform = Transform( 0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, 0 ) -[node name="Gold_Bar" parent="Spatial" instance=ExtResource( 2 )] +[node name="Gold_Bar" parent="Geometry" instance=ExtResource( 2 )] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 21.335, -0.022, -6.533 ) + +[node name="CollectibleComponent" parent="." instance=ExtResource( 3 )] diff --git a/entities/Player.cs b/entities/Player.cs index ebae1e6..b24249e 100644 --- a/entities/Player.cs +++ b/entities/Player.cs @@ -57,8 +57,6 @@ public class Player : Entity, IInteractionInterface { _itemPickupArea.Connect("body_entered", this, nameof(OnItemPickupAreaBodyEntered)); } - _playerAnimationPlayer = GetNode("Geometry/AnimationPlayer"); - Debug.Assert(_playerAnimationPlayer != null); _animationTree = GetNode("Geometry/AnimationTree"); Debug.Assert(_animationTree != null); AnimationNodeStateMachinePlayback stateMachine = @@ -113,14 +111,12 @@ public class Player : Entity, IInteractionInterface { } foreach (Node node in _attractedItemList) { - if (node is GoldBar) { - GoldBar bar = (GoldBar)node; - bar.SetTarget(GlobalTransform.origin); - } - - if (node is Wood) { - Wood wood = (Wood)node; - wood.SetTarget(GlobalTransform.origin); + Entity entity = node as Entity; + if (entity != null) { + CollectibleComponent collectibleComponent = entity.GetCollectibleComponent(); + if (collectibleComponent != null) { + collectibleComponent.SetTarget(GlobalTransform.origin); + } } } @@ -145,14 +141,12 @@ public class Player : Entity, IInteractionInterface { } public void OnItemAttractorBodyExited(Node node) { - if (node is GoldBar) { - GoldBar bar = (GoldBar)node; - bar.UnsetTarget(); - } - - if (node is Wood) { - Wood wood = (Wood)node; - wood.UnsetTarget(); + Entity entity = node as Entity; + if (entity != null) { + CollectibleComponent collectibleComponent = entity.GetCollectibleComponent(); + if (collectibleComponent != null) { + collectibleComponent.UnsetTarget(); + } } _attractedItemList.Remove(node); diff --git a/entities/Tree.cs b/entities/Tree.cs index ad9627b..67dc0d8 100644 --- a/entities/Tree.cs +++ b/entities/Tree.cs @@ -56,7 +56,7 @@ public class Tree : StaticBody, IInteractionInterface { Wood wood = (Wood)_woodScene.Instance(); wood.Transform = new Transform(Transform.basis.Rotated(Vector3.Up, GD.Randf() * 2 * Mathf.Pi), Transform.origin + Vector3.Up * 0.8f); - wood.velocity = new Vector3( + wood.Velocity = new Vector3( (GD.Randf() * 2f - 1f) * 1.2f, 5 + GD.Randf() * 0.3f, (GD.Randf() * 2f - 1f) * 1.2f diff --git a/entities/Wood.cs b/entities/Wood.cs index d515148..cee12bc 100644 --- a/entities/Wood.cs +++ b/entities/Wood.cs @@ -1,49 +1 @@ -using Godot; - -public class Wood : KinematicBody { - private Vector3 targetPosition; - private bool hasTarget; - public Vector3 velocity; - - // Called when the node enters the scene tree for the first time. - public override void _Ready() { - targetPosition = GlobalTransform.origin; - } - - public void SetTarget(Vector3 target) { - targetPosition = target; - hasTarget = true; - } - - public void UnsetTarget() { - hasTarget = false; - } - - // Called every frame. 'delta' is the elapsed time since the previous frame. - public override void _PhysicsProcess(float delta) { - if (hasTarget) { - if (targetPosition.IsEqualApprox(GlobalTransform.origin)) { - targetPosition = GlobalTransform.origin; - velocity = Vector3.Zero; - } - - - Vector3 targetDirection = (targetPosition - GlobalTransform.origin).Normalized(); - velocity = targetDirection * (velocity.Length() + 10 * delta); - Transform = new Transform(Transform.basis.Rotated(Vector3.Up, delta * 2.0f), Transform.origin); - } else { - velocity.y = velocity.y - 9.81f * delta; - } - - velocity = MoveAndSlide(velocity, Vector3.Up); - - if (IsOnFloor() || Mathf.Abs(Transform.origin.y) < 0.01) { - // apply damping when on ground - velocity = velocity - velocity.Normalized() * 0.9f * delta; - } - - if (velocity.LengthSquared() < 0.01) { - velocity = Vector3.Zero; - } - } -} \ No newline at end of file +public class Wood : Entity { } \ No newline at end of file diff --git a/entities/Wood.tscn b/entities/Wood.tscn index a63bd46..165f335 100644 --- a/entities/Wood.tscn +++ b/entities/Wood.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://entities/Wood.cs" type="Script" id=1] [ext_resource path="res://assets/KenneySurvivalKit/Models/resourceWood.glb" type="PackedScene" id=2] +[ext_resource path="res://components/CollectibleComponent.tscn" type="PackedScene" id=3] [sub_resource type="BoxShape" id=21] extents = Vector3( 0.344943, 0.0817164, 0.173406 ) @@ -16,7 +17,9 @@ script = ExtResource( 1 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.084728, 0 ) shape = SubResource( 21 ) -[node name="Spatial" type="Spatial" parent="."] +[node name="Geometry" type="Spatial" parent="."] transform = Transform( 2.12132, 0, -2.12132, 0, 3, 0, 2.12132, 0, 2.12132, 0, 0, 0 ) -[node name="resourceWood" parent="Spatial" instance=ExtResource( 2 )] +[node name="resourceWood" parent="Geometry" instance=ExtResource( 2 )] + +[node name="CollectibleComponent" parent="." instance=ExtResource( 3 )] diff --git a/scenes/Game.tscn b/scenes/Game.tscn index e9b1f44..3c5aef5 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -415,13 +415,19 @@ WorldNode = NodePath("../World") [node name="WorldInfo" parent="Player" index="2"] WorldPath = NodePath("../../World") +[node name="Skeleton" parent="Player/Geometry/PirateAsset/Armature" index="0"] +bones/4/bound_children = [ NodePath("ToolAttachement") ] + +[node name="ToolAttachement" parent="Player/Geometry/PirateAsset/Armature/Skeleton" index="5"] +transform = Transform( 1, 8.68458e-08, -1.04308e-07, 1.74623e-07, -1, -1.30385e-07, 1.41561e-07, 1.50874e-07, -1, -0.72, 0.45, 3.28113e-08 ) + [node name="AnimationTree" parent="Player/Geometry" index="1"] parameters/playback = SubResource( 26 ) [node name="Entities" type="Spatial" parent="."] [node name="Axe" parent="Entities" instance=ExtResource( 14 )] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 1.79762, 0, 0 ) +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -1.03292, -2.38419e-07, -4.33215 ) input_ray_pickable = false [node name="Chest" parent="Entities" instance=ExtResource( 11 )]