From dc3639ad6e5d6a5c9c3a4ae5c76beafa487d3dd9 Mon Sep 17 00:00:00 2001 From: Zenny Date: Sun, 1 Sep 2024 16:34:33 -0700 Subject: [PATCH] Sync weapon equip to hitbox damage --- project.godot | 1 - src/enemy/Enemy.cs | 4 +-- src/enemy/state/EnemyLogic.Input.cs | 2 +- .../state/states/EnemyLogic.State.Alive.cs | 4 +-- src/hitbox/Hitbox.cs | 12 +++++++ src/player/Player.cs | 34 ++++++++++++------- src/player/Player.tscn | 14 ++++---- src/player/PlayerData.cs | 5 +-- 8 files changed, 49 insertions(+), 27 deletions(-) create mode 100644 src/hitbox/Hitbox.cs diff --git a/project.godot b/project.godot index e6eab508..6db64d2c 100644 --- a/project.godot +++ b/project.godot @@ -18,7 +18,6 @@ config/features=PackedStringArray("4.4", "C#", "GL Compatibility") window/size/viewport_width=1920 window/size/viewport_height=1080 -window/size/mode=3 [dotnet] diff --git a/src/enemy/Enemy.cs b/src/enemy/Enemy.cs index aab7ec62..0c50fe74 100644 --- a/src/enemy/Enemy.cs +++ b/src/enemy/Enemy.cs @@ -106,12 +106,12 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide public void OnPlayerHitboxEntered(Area3D body) { - if (body.GetParent() is IPlayer) + if (body is IHitbox hitBox) { if (CurrentHP > 0) { GD.Print("Enemy Hit"); - EnemyLogic.Input(new EnemyLogic.Input.HitByPlayer()); + EnemyLogic.Input(new EnemyLogic.Input.HitByPlayer(hitBox.Damage)); } } } diff --git a/src/enemy/state/EnemyLogic.Input.cs b/src/enemy/state/EnemyLogic.Input.cs index fb9a7c5f..2b473f13 100644 --- a/src/enemy/state/EnemyLogic.Input.cs +++ b/src/enemy/state/EnemyLogic.Input.cs @@ -10,7 +10,7 @@ public readonly record struct PhysicsTick(double Delta); - public readonly record struct HitByPlayer(); + public readonly record struct HitByPlayer(int Damage); } } } diff --git a/src/enemy/state/states/EnemyLogic.State.Alive.cs b/src/enemy/state/states/EnemyLogic.State.Alive.cs index d0751098..44c94ff3 100644 --- a/src/enemy/state/states/EnemyLogic.State.Alive.cs +++ b/src/enemy/state/states/EnemyLogic.State.Alive.cs @@ -10,11 +10,11 @@ namespace GameJamDungeon [Meta, Id("enemy_logic_state_alive")] public abstract partial record Alive : State, IGet { - public Transition On(in Input.HitByPlayer _) + public Transition On(in Input.HitByPlayer input) { var enemy = Get(); var gameRepo = Get(); - enemy.CurrentHP -= gameRepo.EquippedWeapon.InventoryInfo.Damage; + enemy.CurrentHP -= input.Damage; GD.Print("Current HP: " + enemy.CurrentHP); GD.Print($"Hit by {gameRepo.EquippedWeapon.Name}"); Output(new Output.HitByPlayer()); diff --git a/src/hitbox/Hitbox.cs b/src/hitbox/Hitbox.cs new file mode 100644 index 00000000..b7990cc3 --- /dev/null +++ b/src/hitbox/Hitbox.cs @@ -0,0 +1,12 @@ +using Chickensoft.GodotNodeInterfaces; +using Godot; + +public interface IHitbox : IArea3D +{ + public int Damage { get; set; } +} + +public partial class Hitbox : Area3D, IHitbox +{ + public int Damage { get; set; } +} diff --git a/src/player/Player.cs b/src/player/Player.cs index 4ffc0e29..f7b390c4 100644 --- a/src/player/Player.cs +++ b/src/player/Player.cs @@ -1,9 +1,11 @@ using Chickensoft.AutoInject; +using Chickensoft.Collections; using Chickensoft.GodotNodeInterfaces; using Chickensoft.Introspection; using Chickensoft.SaveFileBuilder; using GameJamDungeon; using Godot; +using System; namespace GameJamDungeon { @@ -52,16 +54,17 @@ namespace GameJamDungeon public PlayerLogic.IBinding PlayerBinding { get; set; } = default!; - [Node] - public IAnimationPlayer AnimationPlayer { get; set; } = default!; + [Node] public IAnimationPlayer AnimationPlayer { get; set; } = default!; - [Node] - public IAnimatedSprite2D SwordSlashAnimation { get; set; } = default!; + [Node] public AnimatedSprite2D SwordSlashAnimation { get; set; } = default!; + + [Node] public IHitbox Hitbox { get; set; } = default!; + + private IAutoProp EquippedWeapon { get; set; } = default!; public void Initialize() { AnimationPlayer.AnimationFinished += OnAnimationFinished; - SwordSlashAnimation.Position = GetViewport().GetVisibleRect().Size / 2; } public void Setup() @@ -77,20 +80,15 @@ namespace GameJamDungeon PlayerLogic.Set(GameRepo); PlayerLogic.Set(PlayerData); + GameRepo.SetPlayerGlobalPosition(GlobalPosition); + EquippedWeapon = new AutoProp(WeaponInfo.Default); GameRepo.PlayerGlobalPosition.Sync += OnPlayerPositionUpdated; } - private void OnPlayerPositionUpdated(Vector3 globalPosition) - { - GlobalPosition = globalPosition; - } - public void OnResolved() { PlayerBinding = PlayerLogic.Bind(); - GameRepo.SetPlayerGlobalPosition(GlobalPosition); - PlayerBinding .Handle((in PlayerLogic.Output.MovementComputed output) => { @@ -104,9 +102,15 @@ namespace GameJamDungeon this.Provide(); PlayerLogic.Start(); + + EquippedWeapon.Sync += OnEquippedWeaponChanged; + SwordSlashAnimation.Position = GetViewport().GetVisibleRect().Size / 2; } - public void OnReady() => SetPhysicsProcess(true); + public void OnReady() + { + SetPhysicsProcess(true); + } public void OnPhysicsProcess(double delta) { @@ -145,5 +149,9 @@ namespace GameJamDungeon PlayerBinding.Dispose(); AnimationPlayer.AnimationFinished -= OnAnimationFinished; } + + private void OnEquippedWeaponChanged(WeaponInfo info) => Hitbox.Damage = info.Damage; + + private void OnPlayerPositionUpdated(Vector3 globalPosition) => GlobalPosition = globalPosition; } } diff --git a/src/player/Player.tscn b/src/player/Player.tscn index a71046e6..9c74632a 100644 --- a/src/player/Player.tscn +++ b/src/player/Player.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=15 format=3 uid="uid://cfecvvav8kkp6"] +[gd_scene load_steps=16 format=3 uid="uid://cfecvvav8kkp6"] [ext_resource type="Script" path="res://src/player/Player.cs" id="1_xcol5"] [ext_resource type="Texture2D" uid="uid://bokx3h8kfdo5i" path="res://src/player/slash_0000_Classic_30.png" id="2_la11l"] +[ext_resource type="Script" path="res://src/hitbox/Hitbox.cs" id="2_lb3qc"] [ext_resource type="Texture2D" uid="uid://byosr5gk51237" path="res://src/player/slash_0001_Classic_29.png" id="3_ux3f1"] [ext_resource type="Texture2D" uid="uid://nh071o6ii03j" path="res://src/player/slash_0002_Classic_28.png" id="4_gqnq0"] [ext_resource type="Texture2D" uid="uid://bodfblud4kea3" path="res://src/player/slash_0003_Classic_27.png" id="5_eebal"] @@ -29,7 +30,7 @@ tracks/0/keys = { tracks/1/type = "value" tracks/1/imported = false tracks/1/enabled = true -tracks/1/path = NodePath("Area3D/Hitbox:disabled") +tracks/1/path = NodePath("Hitbox/HitboxCollision:disabled") tracks/1/interp = 1 tracks/1/loop_wrap = true tracks/1/keys = { @@ -57,7 +58,7 @@ tracks/0/keys = { tracks/1/type = "value" tracks/1/imported = false tracks/1/enabled = true -tracks/1/path = NodePath("Area3D/Hitbox:disabled") +tracks/1/path = NodePath("Hitbox/HitboxCollision:disabled") tracks/1/interp = 1 tracks/1/loop_wrap = true tracks/1/keys = { @@ -137,13 +138,14 @@ libraries = { "": SubResource("AnimationLibrary_w8l8m") } -[node name="Area3D" type="Area3D" parent="."] +[node name="Hitbox" type="Area3D" parent="."] +unique_name_in_owner = true transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, -1) collision_layer = 16 collision_mask = 16 +script = ExtResource("2_lb3qc") -[node name="Hitbox" type="CollisionShape3D" parent="Area3D"] -unique_name_in_owner = true +[node name="HitboxCollision" type="CollisionShape3D" parent="Hitbox"] shape = SubResource("BoxShape3D_wedu3") disabled = true diff --git a/src/player/PlayerData.cs b/src/player/PlayerData.cs index fc77bf6a..5bcb1a9e 100644 --- a/src/player/PlayerData.cs +++ b/src/player/PlayerData.cs @@ -1,4 +1,5 @@ -using Chickensoft.Serialization; +using Chickensoft.Collections; +using Chickensoft.Serialization; using Godot; using System.Collections.Generic; @@ -13,7 +14,7 @@ namespace GameJamDungeon public required PlayerLogic StateMachine { get; init; } [Save("PlayerEquippedSword")] - public required InventoryItemInfo EquippedWeapon { get; set; } + public required IAutoProp EquippedWeapon { get; set; } [Save("PlayerInventory")] public required IEnumerable Inventory { get; set; }