From b6b11228a5e5828ed96e600d49de1378a225cc82 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Mon, 1 Jan 2024 15:56:56 +0100 Subject: [PATCH] Added Wood entity, trees drop 1-2 Wood logs when chopped. --- entities/Player.cs | 17 +++++++++ entities/Player.tscn | 3 -- entities/Tree.cs | 23 +++++++++++- entities/Wood.cs | 49 +++++++++++++++++++++++++ entities/Wood.tscn | 22 +++++++++++ scenes/Game.cs | 19 ++++++++-- scenes/Game.tscn | 87 ++++++++++++++++++++++++++++++++++++++++---- 7 files changed, 205 insertions(+), 15 deletions(-) create mode 100644 entities/Wood.cs create mode 100644 entities/Wood.tscn diff --git a/entities/Player.cs b/entities/Player.cs index 4672ca7..811f816 100644 --- a/entities/Player.cs +++ b/entities/Player.cs @@ -9,11 +9,15 @@ public class Player : Entity, IInteractionInterface { // public members [Export] public NodePath WorldNode; + public int WoodCount; public int GoldCount; public TaskQueueComponent TaskQueueComponent; public NavigationComponent NavigationComponent { get; private set; } public InteractionComponent InteractionComponent { get; set; } + [Signal] + private delegate void WoodCountChanged(int woodCount); + [Signal] private delegate void GoldCountChanged(int goldCount); @@ -111,6 +115,11 @@ public class Player : Entity, IInteractionInterface { GoldBar bar = (GoldBar)node; bar.SetTarget(GlobalTransform.origin); } + + if (node is Wood) { + Wood wood = (Wood)node; + wood.SetTarget(GlobalTransform.origin); + } } UpdateDebugGeometry(); @@ -139,6 +148,11 @@ public class Player : Entity, IInteractionInterface { bar.UnsetTarget(); } + if (node is Wood) { + Wood wood = (Wood)node; + wood.UnsetTarget(); + } + _attractedItemList.Remove(node); } @@ -152,6 +166,9 @@ public class Player : Entity, IInteractionInterface { if (body is GoldBar) { GoldCount++; EmitSignal("GoldCountChanged", GoldCount); + } else if (body is Wood) { + WoodCount++; + EmitSignal("WoodCountChanged", WoodCount); } body.QueueFree(); diff --git a/entities/Player.tscn b/entities/Player.tscn index ff5daf5..75ce71c 100644 --- a/entities/Player.tscn +++ b/entities/Player.tscn @@ -397,9 +397,6 @@ generate_lightmap = false [node name="PirateAsset" parent="Geometry" instance=ExtResource( 8 )] transform = Transform( 0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, 0 ) -[node name="Skeleton" parent="Geometry/PirateAsset/Armature" index="0"] -bones/4/bound_children = [ NodePath("ToolAttachement") ] - [node name="ToolAttachement" type="BoneAttachment" parent="Geometry/PirateAsset/Armature/Skeleton" index="5"] transform = Transform( 1, 7.13626e-08, -4.47035e-08, 1.64262e-07, -1, -1.00583e-07, 1.19209e-07, 1.18278e-07, -1, -0.72, 0.45, 1.78362e-08 ) visible = false diff --git a/entities/Tree.cs b/entities/Tree.cs index 955c6fa..ad9627b 100644 --- a/entities/Tree.cs +++ b/entities/Tree.cs @@ -1,10 +1,13 @@ using System; using System.Diagnostics; +using System.Linq; using Godot; using GodotComponentTest.components; using GodotComponentTest.entities; public class Tree : StaticBody, IInteractionInterface { + private readonly PackedScene _woodScene = GD.Load("res://entities/Wood.tscn"); + [Export] public float ChopDuration = 2; public bool IsMouseOver; public InteractionComponent InteractionComponent { get; set; } @@ -13,6 +16,7 @@ public class Tree : StaticBody, IInteractionInterface { private AnimationPlayer _animationPlayer; private float _health = 100; private bool _isBeingChopped; + private bool _isDead; [Signal] public delegate void EntityClicked(Entity entity); @@ -41,10 +45,27 @@ public class Tree : StaticBody, IInteractionInterface { _health = Math.Max(0, _health - delta * 100 / ChopDuration); } - if (_health == 0) { + if (!_isDead && _health == 0) { InteractionComponent.EndInteraction(); InteractionComponent.TargetEntity = null; GD.Print("Tree chopped!"); + + int numWoodLogs = (int)(GD.Randi() % 2) + 1; + + foreach (int i in Enumerable.Range(1, numWoodLogs)) { + 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( + (GD.Randf() * 2f - 1f) * 1.2f, + 5 + GD.Randf() * 0.3f, + (GD.Randf() * 2f - 1f) * 1.2f + ); + GetParent().AddChild(wood); + } + + _isDead = true; + EmitSignal("TreeChopped", this); } } diff --git a/entities/Wood.cs b/entities/Wood.cs new file mode 100644 index 0000000..d515148 --- /dev/null +++ b/entities/Wood.cs @@ -0,0 +1,49 @@ +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 diff --git a/entities/Wood.tscn b/entities/Wood.tscn new file mode 100644 index 0000000..a63bd46 --- /dev/null +++ b/entities/Wood.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=4 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] + +[sub_resource type="BoxShape" id=21] +extents = Vector3( 0.344943, 0.0817164, 0.173406 ) + +[node name="Wood" type="KinematicBody"] +collision_layer = 9 +collision_mask = 0 +input_ray_pickable = false +script = ExtResource( 1 ) + +[node name="CollisionShape" type="CollisionShape" parent="."] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.084728, 0 ) +shape = SubResource( 21 ) + +[node name="Spatial" 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 )] diff --git a/scenes/Game.cs b/scenes/Game.cs index 1e69d07..65baf52 100644 --- a/scenes/Game.cs +++ b/scenes/Game.cs @@ -12,6 +12,7 @@ public class Game : Spatial { // ui elements private Label _framesPerSecondLabel; private Control _gameUi; + private Label _woodCountLabel; private Label _goldCountLabel; private TextureRect _heightTextureRect; @@ -52,6 +53,8 @@ public class Game : Spatial { _worldTextureRect = worldGeneratorWidget.GetNode("WorldTextureRect"); _heightTextureRect = worldGeneratorWidget.GetNode("HeightTextureRect"); _gameUi = (Control)FindNode("GameUI"); + _woodCountLabel = _gameUi.GetNode