Item Effects system

This commit is contained in:
Marco 2025-03-17 16:20:22 +01:00
commit dae952af8b
15 changed files with 255 additions and 84 deletions

View file

@ -1,7 +1,14 @@
[gd_resource type="Resource" script_class="LootItem" load_steps=3 format=3 uid="uid://dodwpect0ldjf"] [gd_resource type="Resource" script_class="LootItem" load_steps=5 format=3 uid="uid://dodwpect0ldjf"]
[ext_resource type="Script" uid="uid://epnwjptvks3t" path="res://Scripts/Resources/LootItem.cs" id="1_hyh2l"] [ext_resource type="Script" uid="uid://epnwjptvks3t" path="res://Scripts/Resources/LootItem.cs" id="1_hyh2l"]
[ext_resource type="Texture2D" uid="uid://dlkqy5jgchwy" path="res://Sprites/Items/Heart_Pickup.png" id="1_xg75n"] [ext_resource type="Texture2D" uid="uid://dlkqy5jgchwy" path="res://Sprites/Items/Heart_Pickup.png" id="1_xg75n"]
[ext_resource type="Script" uid="uid://bxgjw8wis5l0t" path="res://Scripts/Resources/ItemEffects/HealPercentEffectResource.cs" id="2_yj7ej"]
[sub_resource type="Resource" id="Resource_vsvqq"]
script = ExtResource("2_yj7ej")
HealPercentage = 25.0
ResourceType = 0
metadata/_custom_type_script = "uid://bxgjw8wis5l0t"
[resource] [resource]
script = ExtResource("1_hyh2l") script = ExtResource("1_hyh2l")
@ -9,6 +16,7 @@ ItemName = &"Health"
ItemDescription = &"Gives back 25% of Max Health on use" ItemDescription = &"Gives back 25% of Max Health on use"
ItemKey = &"HEALTH" ItemKey = &"HEALTH"
Item = 4 Item = 4
ItemEffect = SubResource("Resource_vsvqq")
Amount = 1 Amount = 1
Max = 10 Max = 10
PickupIfMaxed = false PickupIfMaxed = false

View file

@ -1,14 +1,22 @@
[gd_resource type="Resource" script_class="LootItem" load_steps=3 format=3 uid="uid://bhbufxodybsw4"] [gd_resource type="Resource" script_class="LootItem" load_steps=5 format=3 uid="uid://bhbufxodybsw4"]
[ext_resource type="Texture2D" uid="uid://8n23xie4kore" path="res://Sprites/Items/Shield_Pickup.png" id="1_0hq6q"] [ext_resource type="Texture2D" uid="uid://8n23xie4kore" path="res://Sprites/Items/Shield_Pickup.png" id="1_0hq6q"]
[ext_resource type="Script" uid="uid://bxgjw8wis5l0t" path="res://Scripts/Resources/ItemEffects/HealPercentEffectResource.cs" id="2_mvfwl"]
[ext_resource type="Script" uid="uid://epnwjptvks3t" path="res://Scripts/Resources/LootItem.cs" id="2_xsuq5"] [ext_resource type="Script" uid="uid://epnwjptvks3t" path="res://Scripts/Resources/LootItem.cs" id="2_xsuq5"]
[sub_resource type="Resource" id="Resource_qh6j2"]
script = ExtResource("2_mvfwl")
HealPercentage = 25.0
ResourceType = 1
metadata/_custom_type_script = "uid://bxgjw8wis5l0t"
[resource] [resource]
script = ExtResource("2_xsuq5") script = ExtResource("2_xsuq5")
ItemName = &"Shield Charge" ItemName = &"Shield Charge"
ItemDescription = &"Recharges the shield by 25%" ItemDescription = &"Recharges the shield by 25%"
ItemKey = &"SHIELD" ItemKey = &"SHIELD"
Item = 8 Item = 8
ItemEffect = SubResource("Resource_qh6j2")
Amount = 1 Amount = 1
Max = 10 Max = 10
PickupIfMaxed = false PickupIfMaxed = false

View file

@ -1,14 +1,19 @@
[gd_resource type="Resource" script_class="LootItem" load_steps=7 format=3 uid="uid://dhbltvgsa3g88"] [gd_resource type="Resource" script_class="LootItem" load_steps=9 format=3 uid="uid://dhbltvgsa3g88"]
[ext_resource type="Texture2D" uid="uid://r2h77ktff10m" path="res://Sprites/Bullets/Snowball.png" id="1_gpot4"] [ext_resource type="Texture2D" uid="uid://r2h77ktff10m" path="res://Sprites/Bullets/Snowball.png" id="1_gpot4"]
[ext_resource type="Script" uid="uid://epnwjptvks3t" path="res://Scripts/Resources/LootItem.cs" id="1_lus3u"] [ext_resource type="Script" uid="uid://epnwjptvks3t" path="res://Scripts/Resources/LootItem.cs" id="1_lus3u"]
[ext_resource type="Script" uid="uid://b6fmrnipv88bk" path="res://Scripts/Resources/WeaponResource.cs" id="1_qd764"] [ext_resource type="Script" uid="uid://b6fmrnipv88bk" path="res://Scripts/Resources/WeaponResource.cs" id="1_qd764"]
[ext_resource type="Script" uid="uid://cyipg3seetrxd" path="res://Scripts/Resources/ItemEffects/SpiderbombEffectResource.cs" id="2_6b2qd"]
[ext_resource type="Resource" uid="uid://dwxn7f1ue0m4s" path="res://Resources/Bullets/Spider_Bomb_Bullet.tres" id="2_gpot4"] [ext_resource type="Resource" uid="uid://dwxn7f1ue0m4s" path="res://Resources/Bullets/Spider_Bomb_Bullet.tres" id="2_gpot4"]
[sub_resource type="AtlasTexture" id="AtlasTexture_gpot4"] [sub_resource type="AtlasTexture" id="AtlasTexture_gpot4"]
atlas = ExtResource("1_gpot4") atlas = ExtResource("1_gpot4")
region = Rect2(0, 0, 16, 16) region = Rect2(0, 0, 16, 16)
[sub_resource type="Resource" id="Resource_2pgyg"]
script = ExtResource("2_6b2qd")
metadata/_custom_type_script = "uid://cyipg3seetrxd"
[sub_resource type="Resource" id="Resource_v5a4k"] [sub_resource type="Resource" id="Resource_v5a4k"]
script = ExtResource("1_qd764") script = ExtResource("1_qd764")
Name = null Name = null
@ -32,6 +37,7 @@ ItemName = &"SnowBall Bomb"
ItemDescription = &"A snowball packed of explosive, explodes on contact" ItemDescription = &"A snowball packed of explosive, explodes on contact"
ItemKey = &"SPIDER_BOMB" ItemKey = &"SPIDER_BOMB"
Item = 5 Item = 5
ItemEffect = SubResource("Resource_2pgyg")
WeaponData = SubResource("Resource_v5a4k") WeaponData = SubResource("Resource_v5a4k")
Amount = 1 Amount = 1
Max = 10 Max = 10

View file

@ -36,6 +36,7 @@ public partial class PlayerFSMItemUseModule : ModuleBase<PlayerState, CharacterB
} }
private IStateMachine<PlayerState, CharacterBody2D> _machine; private IStateMachine<PlayerState, CharacterBody2D> _machine;
public IStateMachine<PlayerState, CharacterBody2D> Machine => _machine;
public override void Init(IStateMachine<PlayerState, CharacterBody2D> machine) public override void Init(IStateMachine<PlayerState, CharacterBody2D> machine)
{ {
@ -46,91 +47,96 @@ public partial class PlayerFSMItemUseModule : ModuleBase<PlayerState, CharacterB
private void UseItem(LootItem item, int totalcount) private void UseItem(LootItem item, int totalcount)
{ {
if (!Enabled) return; if (!Enabled) return;
GD.Print("Used spiderbomb in player"); GD.Print($"Used {item.ItemName} in player");
switch (item.Item)
{ item.ItemEffect?.Execute(this, item);
case ItemTypes.KeycardRed:
break; return;
case ItemTypes.KeycardBlue:
break; // switch (item.Item)
case ItemTypes.KeycardGreen: // {
break; // case ItemTypes.KeycardRed:
case ItemTypes.Ammo: // break;
break; // case ItemTypes.KeycardBlue:
case ItemTypes.Medkit: // break;
Heal(item); // case ItemTypes.KeycardGreen:
break; // break;
case ItemTypes.FrogBomb: // case ItemTypes.Ammo:
SpawnSpiderBomb(item); // break;
break; // case ItemTypes.Medkit:
case ItemTypes.Bomb: // Heal(item);
SpawnBomb(item); // break;
break; // case ItemTypes.FrogBomb:
case ItemTypes.Mine: // SpawnSpiderBomb(item);
break; // break;
case ItemTypes.Battery: // case ItemTypes.Bomb:
RechargeShield(item); // SpawnBomb(item);
break; // break;
case ItemTypes.Weapon: // case ItemTypes.Mine:
break; // break;
case ItemTypes.Power: // case ItemTypes.Battery:
break; // RechargeShield(item);
case ItemTypes.Points: // break;
break; // case ItemTypes.Weapon:
case ItemTypes.Credits: // break;
break; // case ItemTypes.Power:
case ItemTypes.KeyItem: // break;
break; // case ItemTypes.Points:
} // break;
// case ItemTypes.Credits:
// break;
// case ItemTypes.KeyItem:
// break;
// }
} }
private void SpawnBomb(LootItem item) // private void SpawnBomb(LootItem item)
{ // {
GD.Print("Spawned bomb"); // GD.Print("Spawned bomb");
} // }
//
private void Heal(LootItem item) // private void Heal(LootItem item)
{ // {
Health.CurrentResource += Mathf.CeilToInt(Health.MaxResource * 25 / 100); // Health.CurrentResource += Mathf.CeilToInt(Health.MaxResource * 25 / 100);
} // }
//
private void RechargeShield(LootItem item) // private void RechargeShield(LootItem item)
{ // {
Shield.CurrentResource += Mathf.CeilToInt(Health.MaxResource * 25 / 100); // Shield.CurrentResource += Mathf.CeilToInt(Health.MaxResource * 25 / 100);
} // }
//
private void SpawnSpiderBomb(LootItem item) // private void SpawnSpiderBomb(LootItem item)
{ // {
//var bullet = item.WeaponData.BulletData.BulletScene.Instantiate<Bullet>(); // //var bullet = item.WeaponData.BulletData.BulletScene.Instantiate<Bullet>();
//bullet.Initialize(item.WeaponData.BulletData.MakeBullet(this.GlobalPosition, 1,0, 0), GameManager.Instance); // //bullet.Initialize(item.WeaponData.BulletData.MakeBullet(this.GlobalPosition, 1,0, 0), GameManager.Instance);
//
//
//InventoryManager.Instance.RemoveItem(item.ItemKey, 1); // //InventoryManager.Instance.RemoveItem(item.ItemKey, 1);
// emit projectile // // emit projectile
var bullet = this.CreateChildOf<Bullet>(GameManager.Instance.BulletsContainer, item.WeaponData.BulletData.BulletScene, this.GlobalPosition); // var bullet = this.CreateChildOf<Bullet>(GameManager.Instance.BulletsContainer, item.WeaponData.BulletData.BulletScene, this.GlobalPosition);
//
var bulletData = item.WeaponData.MakeBullet(this.GlobalPosition); // var bulletData = item.WeaponData.MakeBullet(this.GlobalPosition);
//
bullet.Initialize(bulletData, GameManager.Instance); // bullet.Initialize(bulletData, GameManager.Instance);
bullet.SetDirection(FacingDirection); // bullet.SetDirection(FacingDirection);
bullet.RotateSpriteDegrees(-90); // bullet.RotateSpriteDegrees(-90);
//bullet.SetDirection(_facingDirection); // //bullet.SetDirection(_facingDirection);
bullet.Speed = item.WeaponData.BulletData.BulletSpeed; // bullet.Speed = item.WeaponData.BulletData.BulletSpeed;
//
_machine.SetState(PlayerState.Controlling); // _machine.SetState(PlayerState.Controlling);
//
//RequestMovementDisable(true); // //RequestMovementDisable(true);
// set camera // // set camera
GameManager.Instance.CameraTargetObject(bullet); // GameManager.Instance.CameraTargetObject(bullet);
// set event destroy // // set event destroy
bullet.OnDestroy += () => // bullet.OnDestroy += () =>
{ // {
GameManager.Instance.CameraTargetPlayer(); // GameManager.Instance.CameraTargetPlayer();
_machine.SetState(PlayerState.Active); // _machine.SetState(PlayerState.Active);
//RequestMovementDisable(false); // //RequestMovementDisable(false);
}; // };
} // }
public override void Process(double delta) public override void Process(double delta)
{ {

View file

@ -0,0 +1,41 @@
using System;
using Cirno.Scripts.Components.FSM.Player;
using Godot;
namespace Cirno.Scripts.Resources.ItemEffects;
[GlobalClass]
public partial class HealEffectResource : ItemEffectResource
{
[Export] public float HealAmount { get; private set; } = 10f;
[Export] public HealResourceType ResourceType { get; private set; } = HealResourceType.Health;
public override IITemEffectMachine Execute(PlayerFSMItemUseModule parent, LootItem item)
{
return new HealEffectmachine(this, parent, item).Execute();
}
public class HealEffectmachine(HealEffectResource resource, PlayerFSMItemUseModule parent, LootItem item)
: IITemEffectMachine
{
public IITemEffectMachine Execute()
{
switch (resource.ResourceType)
{
case HealResourceType.Health:
parent.Health.CurrentResource += resource.HealAmount;
break;
case HealResourceType.Shield:
parent.Shield.CurrentResource += resource.HealAmount;
break;
}
return this;
}
}
public enum HealResourceType
{
Health,
Shield
}
}

View file

@ -0,0 +1 @@
uid://ppuaa7avtsgq

View file

@ -0,0 +1,33 @@
using Cirno.Scripts.Components.FSM.Player;
using Godot;
namespace Cirno.Scripts.Resources.ItemEffects;
[GlobalClass]
public partial class HealPercentEffectResource : ItemEffectResource
{
[Export] public float HealPercentage = 25f;
[Export] public HealEffectResource.HealResourceType ResourceType { get; private set; } = HealEffectResource.HealResourceType.Health;
public override IITemEffectMachine Execute(PlayerFSMItemUseModule parent, LootItem item)
{
return new HealPercentEffectmachine(this, parent, item).Execute();
}
public class HealPercentEffectmachine(HealPercentEffectResource resource, PlayerFSMItemUseModule parent, LootItem item) : IITemEffectMachine
{
public IITemEffectMachine Execute()
{
switch (resource.ResourceType)
{
case HealEffectResource.HealResourceType.Health:
parent.Health.CurrentResource += Mathf.CeilToInt(parent.Health.MaxResource * resource.HealPercentage / 100);
break;
case HealEffectResource.HealResourceType.Shield:
parent.Shield.CurrentResource += Mathf.CeilToInt(parent.Shield.MaxResource * resource.HealPercentage / 100);
break;
}
return this;
}
}
}

View file

@ -0,0 +1 @@
uid://bxgjw8wis5l0t

View file

@ -0,0 +1,8 @@
using Cirno.Scripts.Components.FSM.Player;
namespace Cirno.Scripts.Resources.ItemEffects;
public interface IITemEffectMachine
{
public IITemEffectMachine Execute();
}

View file

@ -0,0 +1 @@
uid://bb8cess7hkvwg

View file

@ -0,0 +1,10 @@
using Cirno.Scripts.Components.FSM.Player;
using Godot;
namespace Cirno.Scripts.Resources.ItemEffects;
[GlobalClass]
public abstract partial class ItemEffectResource : Resource
{
public abstract IITemEffectMachine Execute(PlayerFSMItemUseModule parent, LootItem item);
}

View file

@ -0,0 +1 @@
uid://cmkhe0q83rkmd

View file

@ -0,0 +1,44 @@
using Cirno.Scripts.Components.FSM.Player;
using Godot;
namespace Cirno.Scripts.Resources.ItemEffects;
[GlobalClass]
public partial class SpiderbombEffectResource : ItemEffectResource
{
public override IITemEffectMachine Execute(PlayerFSMItemUseModule parent, LootItem item)
{
return new SpiderbombEffectmachine(this, parent, item).Execute();
}
public class SpiderbombEffectmachine(SpiderbombEffectResource resource, PlayerFSMItemUseModule parent, LootItem item) : IITemEffectMachine
{
public IITemEffectMachine Execute()
{
var bullet = parent.CreateChildOf<Bullet>(GameManager.Instance.BulletsContainer, item.WeaponData.BulletData.BulletScene, parent.GlobalPosition);
var bulletData = item.WeaponData.MakeBullet(parent.GlobalPosition);
bullet.Initialize(bulletData, GameManager.Instance);
bullet.SetDirection(parent.FacingDirection);
bullet.RotateSpriteDegrees(-90);
//bullet.SetDirection(_facingDirection);
bullet.Speed = item.WeaponData.BulletData.BulletSpeed;
parent.Machine.SetState(PlayerState.Controlling);
//RequestMovementDisable(true);
// set camera
GameManager.Instance.CameraTargetObject(bullet);
// set event destroy
bullet.OnDestroy += () =>
{
GameManager.Instance.CameraTargetPlayer();
parent.Machine.SetState(PlayerState.Active);
//RequestMovementDisable(false);
};
return this;
}
}
}

View file

@ -0,0 +1 @@
uid://cyipg3seetrxd

View file

@ -1,4 +1,5 @@
using Godot; using Cirno.Scripts.Resources.ItemEffects;
using Godot;
namespace Cirno.Scripts.Resources; namespace Cirno.Scripts.Resources;
@ -9,6 +10,7 @@ public partial class LootItem : Resource
[Export] public StringName ItemDescription { get; set; } [Export] public StringName ItemDescription { get; set; }
[Export] public StringName ItemKey { get; set; } [Export] public StringName ItemKey { get; set; }
[Export] public ItemTypes Item; [Export] public ItemTypes Item;
[Export] public ItemEffectResource ItemEffect { get; private set; }
[Export] public WeaponResource WeaponData { get; set; } [Export] public WeaponResource WeaponData { get; set; }
[Export] public int Amount; [Export] public int Amount;
[Export] public int Max; [Export] public int Max;