Skip to content

Commit

Permalink
Pickup Dropping
Browse files Browse the repository at this point in the history
  • Loading branch information
Blockitifluy committed Aug 3, 2024
1 parent 76b0291 commit a9f1e3f
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 73 deletions.
10 changes: 1 addition & 9 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions Generation/Props/GenProp.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
28 changes: 8 additions & 20 deletions Generation/Props/Tree/TreeProp.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
63 changes: 44 additions & 19 deletions Items/Pickup/Pickup.cs
Original file line number Diff line number Diff line change
@@ -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;
/// <summary>
/// The item code of the pickup
/// </summary>
[Export]
public Items.ItemCode ItemCode
{
get { return itemCode; }
get { return _ItemCode; }
set
{
Item = Items.CodeToItem(value);
itemCode = value;
_ItemCode = value;
}
}

Expand All @@ -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;

/// <summary>
/// Gets an item's mesh.
Expand All @@ -50,22 +49,48 @@ static public Mesh GetItemsMesh(Items.ItemCode code)
return GD.Load<Mesh>(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<Label3D>("Label");
meshInstance = GetNode<MeshInstance3D>("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<PackedScene>(pickupScenePath);

Pickup pickup = scene.Instantiate<Pickup>();
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<Label3D>("Label");
_MeshInstance = GetNode<MeshInstance3D>("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}");
}
}
7 changes: 1 addition & 6 deletions Items/Tools/Abstract/TerrainEditorTool.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Godot;

[GlobalClass]
Expand All @@ -23,10 +21,7 @@ protected MeshGeneration TerrainGenerator
get
{
MeshGeneration meshGeneration = GetTree().Root
.GetChildByType<MeshGeneration>();

if (meshGeneration == null) throw new NullReferenceException("MeshGeneration doesn't Exist");

.GetChildByType<MeshGeneration>() ?? throw new NullReferenceException("MeshGeneration doesn't Exist");
return meshGeneration;
}
}
Expand Down
8 changes: 8 additions & 0 deletions Items/Tools/Shovel/Shovel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion Items/Tools/Shovel/Shovel.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
28 changes: 13 additions & 15 deletions Extensions.cs → Other/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System.Collections.Generic;
using Godot;

public static class GDExtensions
{
//acts like Unity's GetComponent<T> / GetComponentInChildren<T>
/// <summary>
/// Gets a node child by type.
/// </summary>
/// <typeparam name="T">Has to be a Node. he type to be searched</typeparam>
/// <param name="node"></param>
/// <param name="recursive">Look into the children's children, ect.</param>
/// <returns>The child node</returns>
public static T GetChildByType<T>(this Node node, bool recursive = true)
where T : Node
{
Expand All @@ -26,19 +31,12 @@ public static T GetChildByType<T>(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<T>
/// <summary>
/// Gets a node's parent by type.
/// </summary>
/// <typeparam name="T">Has to be a Node. he type to be searched</typeparam>
/// <param name="node"></param>
/// <returns>The child node</returns>
public static T GetParentByType<T>(this Node node)
where T : Node
{
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion UI/backpack.tscn
Original file line number Diff line number Diff line change
@@ -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"]

Expand Down
4 changes: 2 additions & 2 deletions main.tscn
Original file line number Diff line number Diff line change
@@ -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"]
Expand All @@ -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"]
Expand Down

0 comments on commit a9f1e3f

Please sign in to comment.