From a9f1e3fa8a5b3432256299de505f7d05c3a85c5a Mon Sep 17 00:00:00 2001
From: Blockitifluy <136175716+Blockitifluy@users.noreply.github.com>
Date: Sat, 3 Aug 2024 04:29:35 +0100
Subject: [PATCH] Pickup Dropping
---
.editorconfig | 10 +---
Generation/Props/GenProp.cs | 33 ++++++++++++
Generation/Props/Tree/TreeProp.cs | 28 +++-------
Items/Pickup/Pickup.cs | 63 +++++++++++++++-------
Items/Tools/Abstract/TerrainEditorTool.cs | 7 +--
Items/Tools/Shovel/Shovel.cs | 8 +++
Items/Tools/Shovel/Shovel.tscn | 3 +-
Environment.tres => Other/Environment.tres | 0
Extensions.cs => Other/Extensions.cs | 28 +++++-----
BaseTheme.tres => UI/BaseTheme.tres | 0
UI/backpack.tscn | 2 +-
main.tscn | 4 +-
12 files changed, 113 insertions(+), 73 deletions(-)
create mode 100644 Generation/Props/GenProp.cs
rename Environment.tres => Other/Environment.tres (100%)
rename Extensions.cs => Other/Extensions.cs (62%)
rename BaseTheme.tres => UI/BaseTheme.tres (100%)
diff --git a/.editorconfig b/.editorconfig
index 1aa2e62..f61900b 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -1,12 +1,4 @@
[*.cs]
# CA1050: Declare types in namespaces
-dotnet_diagnostic.CA1050.severity = none
-
-# IDE0270: Use coalesce expression
-dotnet_style_coalesce_expression = false
-
-dotnet_diagnostic.IDE0044.severity = none
-
-# IDE0025: Use expression body for property
-dotnet_diagnostic.IDE0025.severity = none
+dotnet_diagnostic.CA1050.severity = none
\ No newline at end of file
diff --git a/Generation/Props/GenProp.cs b/Generation/Props/GenProp.cs
new file mode 100644
index 0000000..508293d
--- /dev/null
+++ b/Generation/Props/GenProp.cs
@@ -0,0 +1,33 @@
+using Godot;
+
+[GlobalClass]
+public abstract partial class GenProp : StaticBody3D
+{
+ [Export]
+ public float MaxHealth = 35;
+
+ private float _health = 35;
+ [Export]
+ public float Health
+ {
+ get { return _health; }
+ set
+ {
+ if (value > MaxHealth) return;
+
+ if (_health <= 0) OnDeath();
+
+ if (_health > value) OnHit(value);
+
+ _health = value;
+ }
+ }
+
+ protected abstract void OnHit(float newHealth);
+
+ protected virtual void OnDeath()
+ {
+ if (_health <= 0)
+ QueueFree();
+ }
+}
\ No newline at end of file
diff --git a/Generation/Props/Tree/TreeProp.cs b/Generation/Props/Tree/TreeProp.cs
index 418a1d0..64cd517 100644
--- a/Generation/Props/Tree/TreeProp.cs
+++ b/Generation/Props/Tree/TreeProp.cs
@@ -1,31 +1,19 @@
using Godot;
[GlobalClass]
-public partial class TreeProp : StaticBody3D
+public partial class TreeProp : GenProp
{
[Export]
- public float MaxHealth = 35;
-
- private float _health = 35;
- [Export]
- public float Health
+ public int WoodDropAmount { get; set; } = 5;
+ protected override void OnHit(float newHealth)
{
- get { return _health; }
- set
- {
- if (value > MaxHealth) return;
-
- if (_health > value) OnTreeHit(value);
-
- _health = value;
- }
+ GD.Print(newHealth);
}
- private void OnTreeHit(float health)
+ protected override void OnDeath()
{
- if (health <= 0)
- {
- QueueFree();
- }
+ Pickup.DropItem(GlobalPosition, Items.ItemCode.Wood, WoodDropAmount, GetTree());
+
+ base.OnDeath();
}
}
\ No newline at end of file
diff --git a/Items/Pickup/Pickup.cs b/Items/Pickup/Pickup.cs
index 1674da4..ea0f238 100644
--- a/Items/Pickup/Pickup.cs
+++ b/Items/Pickup/Pickup.cs
@@ -1,21 +1,21 @@
-using System.Collections.Generic;
+using System;
using Godot;
[GlobalClass]
public partial class Pickup : RigidBody3D
{
- private Items.ItemCode itemCode;
+ private Items.ItemCode _ItemCode;
///
/// The item code of the pickup
///
[Export]
public Items.ItemCode ItemCode
{
- get { return itemCode; }
+ get { return _ItemCode; }
set
{
Item = Items.CodeToItem(value);
- itemCode = value;
+ _ItemCode = value;
}
}
@@ -26,11 +26,10 @@ public Items.ItemCode ItemCode
public Items.ItemData Item;
- private Label3D Label;
+ private Label3D _Label;
private int LastPickupInput;
- private MeshInstance3D meshInstance;
- private Mesh mesh;
-
+ private MeshInstance3D _MeshInstance;
+ private Mesh _Mesh;
///
/// Gets an item's mesh.
@@ -50,22 +49,48 @@ static public Mesh GetItemsMesh(Items.ItemCode code)
return GD.Load(basePath + expectedName);
}
- // Called when the node enters the scene tree for the first time.
- public override void _Ready()
+ static public readonly string PickupGroup = "Pickups";
+
+ static public Pickup DropItem(Vector3 pos, Items.ItemCode code, int amount, SceneTree tree)
{
- Label = GetNode("Label");
- meshInstance = GetNode("MeshInstance3D");
- mesh = GetItemsMesh(ItemCode);
+ Items.ItemData baseItem = Items.CodeToItem(code);
- meshInstance.Mesh = mesh;
+ if (amount > baseItem.MaxAmount || amount <= 0)
+ throw new ArgumentOutOfRangeException(nameof(amount));
- GD.PrintRich($"[b][color=PURPLE]Item[/color][/b] Made {Item.Name}");
+ const string pickupScenePath = "res://Items/Pickup/Pickup.tscn";
+
+ PackedScene scene = GD.Load(pickupScenePath);
+
+ Pickup pickup = scene.Instantiate();
+ pickup.ItemCode = code;
+ pickup.Amount = amount;
+ pickup.Position = pos;
+ tree.Root.AddChild(pickup);
+
+ pickup.AddToGroup(PickupGroup);
+
+ return pickup;
}
- // Called every frame. 'delta' is the elapsed time since the previous frame.
- public override void _Process(double delta)
+ // Called when the node enters the scene tree for the first time.
+ public override void _Ready()
{
- Items.ItemData Item = Items.CodeToItem(ItemCode);
- Label.Text = $"{Item} ({Amount})";
+ base._Ready();
+
+ _Label = GetNode("Label");
+ _MeshInstance = GetNode("MeshInstance3D");
+ _Mesh = GetItemsMesh(ItemCode);
+
+ Item = Items.CodeToItem(ItemCode);
+
+ if (_Mesh != null)
+ _MeshInstance.Mesh = _Mesh;
+
+ _Label.Text = $"{Item} ({Amount})";
+
+ GD.Print(_Mesh);
+
+ GD.PrintRich($"[b][color=PURPLE]Item[/color][/b] Made {Item.Name}");
}
}
diff --git a/Items/Tools/Abstract/TerrainEditorTool.cs b/Items/Tools/Abstract/TerrainEditorTool.cs
index eaf8e6a..a11a936 100644
--- a/Items/Tools/Abstract/TerrainEditorTool.cs
+++ b/Items/Tools/Abstract/TerrainEditorTool.cs
@@ -1,6 +1,4 @@
using System;
-using System.Collections.Generic;
-using System.Threading.Tasks;
using Godot;
[GlobalClass]
@@ -23,10 +21,7 @@ protected MeshGeneration TerrainGenerator
get
{
MeshGeneration meshGeneration = GetTree().Root
- .GetChildByType();
-
- if (meshGeneration == null) throw new NullReferenceException("MeshGeneration doesn't Exist");
-
+ .GetChildByType() ?? throw new NullReferenceException("MeshGeneration doesn't Exist");
return meshGeneration;
}
}
diff --git a/Items/Tools/Shovel/Shovel.cs b/Items/Tools/Shovel/Shovel.cs
index d271b19..e14cbe6 100644
--- a/Items/Tools/Shovel/Shovel.cs
+++ b/Items/Tools/Shovel/Shovel.cs
@@ -6,6 +6,12 @@ public partial class Shovel : TerrainEditorTool
[Export]
public float DigDepth { get; set; } = 1.0f;
+ [ExportGroup("Drops")]
+ [Export]
+ public Items.ItemCode ItemDrop { get; set; }
+ [Export]
+ public int DropAmount { get; set; } = 1;
+
private void EditChunk(Vector3 rayHit, ChunkComponents components)
{
MeshDataTool meshDataTool = new();
@@ -26,6 +32,8 @@ private void EditChunk(Vector3 rayHit, ChunkComponents components)
float newHeight = vertex.Y - DigDepth;
TerrainGenerator.EditVertexHeight(components.chunk, vrtPos, newHeight);
+
+ Pickup.DropItem(rayHit + Vector3.Up, ItemDrop, DropAmount, GetTree());
}
public override void Fire()
diff --git a/Items/Tools/Shovel/Shovel.tscn b/Items/Tools/Shovel/Shovel.tscn
index 2fed008..0dffe27 100644
--- a/Items/Tools/Shovel/Shovel.tscn
+++ b/Items/Tools/Shovel/Shovel.tscn
@@ -4,7 +4,8 @@
[node name="Shovel" type="Node3D"]
script = ExtResource("1_5evg7")
-DigDepth = 0.25
+DigDepth = 0.5
+ItemDrop = 3
Damage = 5.0
Range = 25.0
FiringMode = 1
diff --git a/Environment.tres b/Other/Environment.tres
similarity index 100%
rename from Environment.tres
rename to Other/Environment.tres
diff --git a/Extensions.cs b/Other/Extensions.cs
similarity index 62%
rename from Extensions.cs
rename to Other/Extensions.cs
index 179f72b..888967b 100644
--- a/Extensions.cs
+++ b/Other/Extensions.cs
@@ -1,9 +1,14 @@
-using System.Collections.Generic;
using Godot;
public static class GDExtensions
{
- //acts like Unity's GetComponent / GetComponentInChildren
+ ///
+ /// Gets a node child by type.
+ ///
+ /// Has to be a Node. he type to be searched
+ ///
+ /// Look into the children's children, ect.
+ /// The child node
public static T GetChildByType(this Node node, bool recursive = true)
where T : Node
{
@@ -26,19 +31,12 @@ public static T GetChildByType(this Node node, bool recursive = true)
return null;
}
- private struct PrintTypeData
- {
- public string Color;
- public string Text;
-
- public PrintTypeData(string color, string text)
- {
- Color = color;
- Text = text;
- }
- }
-
- //acts like Unity's GetComponentInParent
+ ///
+ /// Gets a node's parent by type.
+ ///
+ /// Has to be a Node. he type to be searched
+ ///
+ /// The child node
public static T GetParentByType(this Node node)
where T : Node
{
diff --git a/BaseTheme.tres b/UI/BaseTheme.tres
similarity index 100%
rename from BaseTheme.tres
rename to UI/BaseTheme.tres
diff --git a/UI/backpack.tscn b/UI/backpack.tscn
index e3fa1f0..67a7056 100644
--- a/UI/backpack.tscn
+++ b/UI/backpack.tscn
@@ -1,6 +1,6 @@
[gd_scene load_steps=5 format=3 uid="uid://bbe80tb3e3d0d"]
-[ext_resource type="Theme" uid="uid://bu43bgh6p57ya" path="res://BaseTheme.tres" id="1_a4pw8"]
+[ext_resource type="Theme" uid="uid://bu43bgh6p57ya" path="res://UI/BaseTheme.tres" id="1_a4pw8"]
[ext_resource type="Script" path="res://UI/Backpack.cs" id="2_neqql"]
[ext_resource type="PackedScene" uid="uid://cjk11qebpf1kw" path="res://UI/item_ui.tscn" id="3_ma0vb"]
diff --git a/main.tscn b/main.tscn
index aead843..7e1896c 100644
--- a/main.tscn
+++ b/main.tscn
@@ -1,7 +1,7 @@
[gd_scene load_steps=14 format=3 uid="uid://bwfly4emjkoa6"]
[ext_resource type="PackedScene" uid="uid://cv33llrox6khk" path="res://Characters/Player/player.tscn" id="1_2fumh"]
-[ext_resource type="Theme" uid="uid://bu43bgh6p57ya" path="res://BaseTheme.tres" id="1_rjc8t"]
+[ext_resource type="Theme" uid="uid://bu43bgh6p57ya" path="res://UI/BaseTheme.tres" id="1_rjc8t"]
[ext_resource type="Script" path="res://Characters/Player/CameraPivot.cs" id="2_wh28q"]
[ext_resource type="PackedScene" uid="uid://bbe80tb3e3d0d" path="res://UI/backpack.tscn" id="3_cbpee"]
[ext_resource type="PackedScene" uid="uid://w23cbmgs53r2" path="res://Items/Pickup/Pickup.tscn" id="3_vmgn5"]
@@ -10,7 +10,7 @@
[ext_resource type="Material" uid="uid://bmnw30ebxfv0t" path="res://Generation/Textures/Materials/Snow.tres" id="6_15161"]
[ext_resource type="Script" path="res://Generation/Terrain.cs" id="6_uka6n"]
[ext_resource type="Material" uid="uid://cbygtffo6wwrj" path="res://Items/Pickup/Meshs/Stone.tres" id="7_ujewj"]
-[ext_resource type="Environment" uid="uid://bf2jw56yi3qwe" path="res://Environment.tres" id="11_32728"]
+[ext_resource type="Environment" uid="uid://bf2jw56yi3qwe" path="res://Other/Environment.tres" id="11_32728"]
[ext_resource type="PackedScene" uid="uid://s8icoimu6cgo" path="res://Characters/Zombie/Zombie.tscn" id="12_pf40a"]
[sub_resource type="LabelSettings" id="LabelSettings_km65f"]