From 39894f63a8019328c53db6c11a9b161728e533eb Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser <25856867+HumanoidSandvichDispenser@users.noreply.github.com> Date: Sun, 13 Nov 2022 19:52:09 -0800 Subject: [PATCH] inventory wip --- BoundingBoxes/Hitbox.cs | 41 ++++++++++++ BoundingBoxes/Hitbox.tscn | 14 ++++ BoundingBoxes/Hurtbox.cs | 26 ++++++++ BoundingBoxes/Hurtbox.tscn | 12 ++++ Characters/Character.cs | 32 ++++++++- Characters/ExampleEnemy.tscn | 15 ++++- Characters/Items/Inventory.cs | 61 ++++++++++++++++++ Characters/Items/Item.cs | 19 ++++++ Characters/Items/Weapon.cs | 54 ++++++++++++++++ Characters/Items/Weapons/Sword.cs | 20 ++++++ Characters/NPC.cs | 6 +- Characters/Player.tscn | 2 + Prototyping/ContextBasedSteeringTest.tscn | 9 --- Sprites/knife.ase | Bin 0 -> 696 bytes Sprites/knife.png | Bin 0 -> 310 bytes Sprites/knife.png.import | 34 ++++++++++ .../ContextBasedSteering.cs | 0 Tests/ContextBasedSteeringTest.tscn | 9 +++ Tests/HitboxTest.tscn | 37 +++++++++++ Tests/OscillatingBody.cs | 22 +++++++ Tests/StaticMovement.cs | 39 +++++++++++ 21 files changed, 436 insertions(+), 16 deletions(-) create mode 100644 BoundingBoxes/Hitbox.cs create mode 100644 BoundingBoxes/Hitbox.tscn create mode 100644 BoundingBoxes/Hurtbox.cs create mode 100644 BoundingBoxes/Hurtbox.tscn create mode 100644 Characters/Items/Inventory.cs create mode 100644 Characters/Items/Item.cs create mode 100644 Characters/Items/Weapon.cs create mode 100644 Characters/Items/Weapons/Sword.cs delete mode 100644 Prototyping/ContextBasedSteeringTest.tscn create mode 100644 Sprites/knife.ase create mode 100644 Sprites/knife.png create mode 100644 Sprites/knife.png.import rename {Prototyping => Tests}/ContextBasedSteering.cs (100%) create mode 100644 Tests/ContextBasedSteeringTest.tscn create mode 100644 Tests/HitboxTest.tscn create mode 100644 Tests/OscillatingBody.cs create mode 100644 Tests/StaticMovement.cs diff --git a/BoundingBoxes/Hitbox.cs b/BoundingBoxes/Hitbox.cs new file mode 100644 index 0000000..9ac8ea4 --- /dev/null +++ b/BoundingBoxes/Hitbox.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using Godot; +using SupaLidlGame.Characters; + +namespace SupaLidlGame.BoundingBoxes +{ + public partial class Hitbox : Area2D + { + private HashSet _ignoreList = new HashSet(); + + [Export] + public float Damage { get; set; } = 0; + + [Export] + public bool IsEnabled { get; set; } + + [Export] + public float Knockback { get; set; } + + public Character Inflictor { get; set; } + + public void _on_area_entered(Area2D area) + { + if (!IsEnabled) + { + return; + } + + if (area is Hurtbox hurtbox) + { + if (!_ignoreList.Contains(hurtbox)) + { + _ignoreList.Add(hurtbox); + hurtbox.InflictDamage(Damage, Inflictor, Knockback); + } + } + } + + public void ResetIgnoreList() => _ignoreList.Clear(); + } +} diff --git a/BoundingBoxes/Hitbox.tscn b/BoundingBoxes/Hitbox.tscn new file mode 100644 index 0000000..e9a60a2 --- /dev/null +++ b/BoundingBoxes/Hitbox.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=3 uid="uid://du5vhccg75nrq"] + +[ext_resource type="Script" path="res://BoundingBoxes/Hitbox.cs" id="1_44i8j"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_3w20g"] + +[node name="Hitbox" type="Area2D"] +script = ExtResource("1_44i8j") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_3w20g") +debug_color = Color(0.701961, 0.490196, 0, 0.419608) + +[connection signal="area_entered" from="." to="." method="_on_area_entered"] diff --git a/BoundingBoxes/Hurtbox.cs b/BoundingBoxes/Hurtbox.cs new file mode 100644 index 0000000..22fbcea --- /dev/null +++ b/BoundingBoxes/Hurtbox.cs @@ -0,0 +1,26 @@ +using Godot; +using SupaLidlGame.Characters; + +namespace SupaLidlGame.BoundingBoxes +{ + public partial class Hurtbox : Area2D + { + [Signal] + public delegate void ReceivedDamageEventHandler(float damage); + + public void InflictDamage( + float damage, + Character inflictor, + float knockback, + Vector2 knockbackOrigin = default, + Vector2 knockbackVector = default) + { + EmitSignal( + "ReceivedDamage", + damage, + inflictor, + knockback, + knockbackOrigin, knockbackVector); + } + } +} diff --git a/BoundingBoxes/Hurtbox.tscn b/BoundingBoxes/Hurtbox.tscn new file mode 100644 index 0000000..a8a2eae --- /dev/null +++ b/BoundingBoxes/Hurtbox.tscn @@ -0,0 +1,12 @@ +[gd_scene load_steps=3 format=3 uid="uid://cjgxyhgcyvsv7"] + +[ext_resource type="Script" path="res://BoundingBoxes/Hurtbox.cs" id="1_ov1ss"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_2rki1"] + +[node name="Hurtbox" type="Area2D"] +script = ExtResource("1_ov1ss") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_2rki1") +debug_color = Color(0.996078, 0, 0.129412, 0.419608) diff --git a/Characters/Character.cs b/Characters/Character.cs index 247c921..8cf725b 100644 --- a/Characters/Character.cs +++ b/Characters/Character.cs @@ -7,18 +7,31 @@ namespace SupaLidlGame.Characters [Export] public float Speed { get; protected set; } = 128.0f; + [Export] + public float Mass + { + get => _mass; + set + { + if (value > 0) + _mass = value; + } + } + + protected float _mass = 1.0f; + public float JumpVelocity { get; protected set; } = -400.0f; + public float AccelerationMagnitude { get; protected set; } = 256.0f; public Vector2 Acceleration => Direction * AccelerationMagnitude; - // Get the gravity from the project settings to be synced with RigidBody nodes. - public float Gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle(); - public Vector2 Direction { get; set; } = Vector2.Zero; public Vector2 Target { get; set; } = Vector2.Zero; + public float Health { get; set; } + [Export] public State.Machine StateMachine { get; set; } @@ -45,5 +58,18 @@ namespace SupaLidlGame.Characters StateMachine.PhysicsProcess(delta); } } + + public void ApplyImpulse(Vector2 impulse, bool resetVelocity = false) + { + // delta p = F delta t + if (resetVelocity) + Velocity = Vector2.Zero; + Velocity += impulse / Mass; + } + + public void _on_hurtbox_received_damage(float damage) + { + Health -= damage; + } } } diff --git a/Characters/ExampleEnemy.tscn b/Characters/ExampleEnemy.tscn index 2563266..31c160b 100644 --- a/Characters/ExampleEnemy.tscn +++ b/Characters/ExampleEnemy.tscn @@ -1,14 +1,18 @@ -[gd_scene load_steps=7 format=3 uid="uid://dymwd5ihpwyqm"] +[gd_scene load_steps=9 format=3 uid="uid://dymwd5ihpwyqm"] [ext_resource type="Script" path="res://Characters/NPC.cs" id="1_4x3dm"] [ext_resource type="Texture2D" uid="uid://bw052v8ikfget" path="res://icon.svg" id="2_ujqd7"] [ext_resource type="Script" path="res://Characters/States/Machine.cs" id="3_k4ypw"] [ext_resource type="Script" path="res://Characters/States/NPCIdleState.cs" id="4_8r2qn"] [ext_resource type="Script" path="res://Characters/States/NPCMoveState.cs" id="5_utogm"] +[ext_resource type="PackedScene" uid="uid://cjgxyhgcyvsv7" path="res://BoundingBoxes/Hurtbox.tscn" id="6_jo0cg"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_uict5"] size = Vector2(32, 16) +[sub_resource type="RectangleShape2D" id="RectangleShape2D_8lxmf"] +size = Vector2(32, 32) + [node name="ExampleEnemy" type="CharacterBody2D" node_paths=PackedStringArray("StateMachine")] script = ExtResource("1_4x3dm") Speed = 32.0 @@ -34,3 +38,12 @@ MoveState = NodePath("../Move") [node name="Move" type="Node" parent="StateMachine" node_paths=PackedStringArray("IdleState")] script = ExtResource("5_utogm") IdleState = NodePath("../Idle") + +[node name="Hurtbox" parent="." instance=ExtResource("6_jo0cg")] + +[node name="CollisionShape2D" parent="Hurtbox" index="0"] +shape = SubResource("RectangleShape2D_8lxmf") + +[connection signal="ReceivedDamage" from="Hurtbox" to="." method="_on_hurtbox_received_damage"] + +[editable path="Hurtbox"] diff --git a/Characters/Items/Inventory.cs b/Characters/Items/Inventory.cs new file mode 100644 index 0000000..b27ad25 --- /dev/null +++ b/Characters/Items/Inventory.cs @@ -0,0 +1,61 @@ +using System.Collections.Generic; +using Godot; +using SupaLidlGame.Characters; + +namespace SupaLidlGame.Items +{ + public partial class Inventory : Node2D + { + public Character Character { get; private set; } + + public List Items { get; private set; } = new List(); + + public const int MaxCapacity = 32; + + private Item _selectedItem; + + public Item SelectedItem + { + get => _selectedItem; + set + { + if (!Items.Contains(value)) + { + GD.PrintErr("Tried to equip an item not in the inventory."); + return; + } + + if (_selectedItem is not null) + { + _selectedItem.Unequip(Character); + } + + _selectedItem = value; + + _selectedItem.Equip(Character); + } + } + + public Item AddItem(Item item) + { + if (Items.Count >= MaxCapacity) + { + return null; + } + + Items.Add(item); + return item; + } + + public Item DropItem(Item item) + { + throw new System.NotImplementedException(); + } + + public override void _Ready() + { + Owner = GetParent(); + base._Ready(); + } + } +} diff --git a/Characters/Items/Item.cs b/Characters/Items/Item.cs new file mode 100644 index 0000000..af78ec6 --- /dev/null +++ b/Characters/Items/Item.cs @@ -0,0 +1,19 @@ +using Godot; +using SupaLidlGame.Characters; + +namespace SupaLidlGame.Items +{ + public abstract partial class Item : Node2D + { + [Export] + public string Description { get; set; } + + public abstract void Equip(Character character); + + public abstract void Unequip(Character character); + + public abstract void Use(); + + public abstract void Deuse(); + } +} diff --git a/Characters/Items/Weapon.cs b/Characters/Items/Weapon.cs new file mode 100644 index 0000000..62815ae --- /dev/null +++ b/Characters/Items/Weapon.cs @@ -0,0 +1,54 @@ +using Godot; +using SupaLidlGame.Characters; + +namespace SupaLidlGame.Items +{ + public abstract partial class Weapon : Item + { + public double RemainingUseTime { get; protected set; } = 0; + + public bool CanStartAttack => RemainingUseTime <= 0; + + /// + /// How much damage in HP that this weapon deals. + /// + [Export] + public float Damage { get; set; } = 0; + + /// + /// The time in seconds it takes for this weapon to become available + /// again after using. + /// + [Export] + public double UseTime { get; set; } = 0; + + /// + /// The magnitude of the knockback force of the weapon. + /// + [Export] + public float Knockback { get; set; } = 0; + + /// + /// The initial velocity of any projectile the weapon may spawn. + /// + [Export] + public float InitialVelocity { get; set; } = 0; + + public Character Character { get; set; } + + public override void Equip(Character character) + { + Character = character; + } + + public override void Unequip(Character character) + { + Character = null; + } + + public override void Deuse() + { + + } + } +} diff --git a/Characters/Items/Weapons/Sword.cs b/Characters/Items/Weapons/Sword.cs new file mode 100644 index 0000000..1725292 --- /dev/null +++ b/Characters/Items/Weapons/Sword.cs @@ -0,0 +1,20 @@ +using SupaLidlGame.Characters; + +namespace SupaLidlGame.Items.Weapons +{ + public partial class Sword : Weapon + { + //[Export] + //public Damagebox + + public override void Equip(Character character) + { + base.Equip(character); + } + + public override void Use() + { + //base.Use(); + } + } +} diff --git a/Characters/NPC.cs b/Characters/NPC.cs index 282fee4..970957d 100644 --- a/Characters/NPC.cs +++ b/Characters/NPC.cs @@ -12,9 +12,9 @@ namespace SupaLidlGame.Characters public float[] Weights => _weights; - float[] _weights = new float[16]; - Vector2[] _weightDirs = new Vector2[16]; - int _bestWeightIdx; + protected float[] _weights = new float[16]; + protected Vector2[] _weightDirs = new Vector2[16]; + protected int _bestWeightIdx; protected double _thinkTimeElapsed = 0; public override void _Ready() diff --git a/Characters/Player.tscn b/Characters/Player.tscn index 0754ceb..66c9aaa 100644 --- a/Characters/Player.tscn +++ b/Characters/Player.tscn @@ -62,3 +62,5 @@ label_settings = SubResource("LabelSettings_q5h1n") horizontal_alignment = 1 [node name="Node" type="Node" parent="."] + +[node name="Inventory" type="Node2D" parent="."] diff --git a/Prototyping/ContextBasedSteeringTest.tscn b/Prototyping/ContextBasedSteeringTest.tscn deleted file mode 100644 index 2622ca7..0000000 --- a/Prototyping/ContextBasedSteeringTest.tscn +++ /dev/null @@ -1,9 +0,0 @@ -[gd_scene load_steps=2 format=3 uid="uid://dmbgefwamg0u7"] - -[ext_resource type="Script" path="res://Prototyping/ContextBasedSteering.cs" id="1_t0k7y"] - -[node name="ContextBasedSteeringTest" type="Node2D"] - -[node name="ContextBasedSteering" type="Node2D" parent="."] -position = Vector2(410, 242) -script = ExtResource("1_t0k7y") diff --git a/Sprites/knife.ase b/Sprites/knife.ase new file mode 100644 index 0000000000000000000000000000000000000000..8d024a40a93f4abe0a194d27e64f952b12cb340e GIT binary patch literal 696 zcmcJN%`3xU0LP!1dD&@)Es^m%OjG99yo6~j+Pvf?+r^}43kNsRnsK6qqEKtQ$U&Bs zTxcalTqqGGDe`hda&UE-&o6oV13dM7dVX&`-_Q5?ZKnzG5LbyTp<}9q5dF-bA^$A@ zWmUue?{DefyUIk!mA-Z@{j+C|RFe{-sJ0i{duL(C{0LG=04-)yLKP~RB=xV8Bdo|2 zZT`@}uvFH>g+}^(n&R+3hw}T4F7^3*KDS?}4Lw$8!?dMa<60LXN$5Q)^+I?j!)44n L6;X{C+|^1SI=i~V literal 0 HcmV?d00001 diff --git a/Sprites/knife.png b/Sprites/knife.png new file mode 100644 index 0000000000000000000000000000000000000000..6dc136807f13fb1cf5b8b1929ac37cd4e5865555 GIT binary patch literal 310 zcmeAS@N?(olHy`uVBq!ia0vp^96&6^NLdu7j8}T)i$c~a9mp+eqc)R^{rE$U)^jXqn@Cn)8VbX zuUz}yJjWLY3sgka9c2wmRVX>R8>dfOv31k&^JiYaeJRZustt6NUP+K&FvEXRK+yCLAP?$#x;Tb# z%uG&TU=n5H2)Nj~A*4hjaYciKmVzBe!;z2!3_8o#F^2XWOkh~ez_8JaOTpe~E+