made the initial inventory loadout into a resource to initialize the injected dependency with

This commit is contained in:
2026-04-26 17:38:25 +02:00
parent 319cbf722e
commit cd7a230615
11 changed files with 331 additions and 247 deletions

View File

@@ -33,54 +33,32 @@ namespace Movementtests.systems;
[GlobalClass, Icon("res://assets/ui/IconGodotNode/node_3D/icon_sword.png")]
public partial class WeaponSystem : RigidBody3D, IDamageDealer, IForgeEntity
{
#region Enums
public enum WeaponEvent
{
StartedFlying,
StoppedFlying,
FlyingTick
}
#endregion
#region Signals
[Signal]
public delegate void WeaponThrownEventHandler();
[Signal]
public delegate void WeaponRetrievedEventHandler();
[Export]
public ForgeTagContainer BaseTags { get; set; }
[Export] public ForgeAbilityData FlyingTickAbility { get; set; }
[Export]
public RDamage RDamage { get; set; }
[Export(PropertyHint.Range, "0,2,0.01,or_greater")]
public float ThrowForce { get; set; } = 1f;
[Export(PropertyHint.Range, "0,0.2,0.01,or_greater")]
public float StraightThrowDuration { get; set; } = 0.1f;
public EntityAttributes Attributes { get; set; } = null!;
public EntityTags Tags { get; set; } = null!;
public EffectsManager EffectsManager { get; set; } = null!;
public EntityAbilities Abilities { get; set; } = null!;
public EventManager Events { get; set; } = null!;
public Variables SharedVariables { get; }
private StateChart _weaponState = null!;
public StateChartState InHandState = null!;
public StateChartState FlyingState = null!;
public StateChartState PlantedState = null!;
private Transition _handToFlying = null!;
private Transition _flyingToHand = null!;
private Transition _plantedToHand = null!;
private Transition _plantedToFlying = null!;
private Transition _toPlanted = null!;
#endregion
private ShapeCast3D _dashCast3D = null!;
public Timer WeaponFlyingTick = null!;
private Transform3D _startTransform;
private Vector3 _startMeshRotation;
#region Forge
private Vector3 _throwDirection;
public Vector3 PlantLocation { get; set; }
public Vector3 PlantNormal { get; set; }
public Node? PlantObject { get; set; }
public MeshInstance3D WeaponLocationIndicator { get; set; } = null!;
public StandardMaterial3D WeaponLocationIndicatorMaterial { get; set; } = null!;
public MeshInstance3D WeaponMesh { get; set; } = null!;
private Dictionary<ForgeAbilityBehavior, ActiveEffectHandle> _grantedWeaponStartedFlyingAbilities = new ();
private Dictionary<ForgeAbilityBehavior, ActiveEffectHandle> _grantedWeaponStoppedFlyingAbilities = new ();
private Dictionary<ForgeAbilityBehavior, ActiveEffectHandle> _grantedWeaponFlyingTickAbilities = new ();
public Tag WeaponFlyingTickEventTag;
public Tag WeaponStartedFlyingEventTag;
@@ -99,6 +77,66 @@ public partial class WeaponSystem : RigidBody3D, IDamageDealer, IForgeEntity
private AbilityHandle? _weaponFlyingAbility;
#endregion
#region Inspector
[Export]
public ForgeTagContainer BaseTags { get; set; }
[Export] public ForgeAbilityData FlyingTickAbility { get; set; }
[Export]
public RDamage RDamage { get; set; }
[Export(PropertyHint.Range, "0,2,0.01,or_greater")]
public float ThrowForce { get; set; } = 1f;
[Export(PropertyHint.Range, "0,0.2,0.01,or_greater")]
public float StraightThrowDuration { get; set; } = 0.1f;
#endregion
#region Publics
public EntityAttributes Attributes { get; set; } = null!;
public EntityTags Tags { get; set; } = null!;
public EffectsManager EffectsManager { get; set; } = null!;
public EntityAbilities Abilities { get; set; } = null!;
public EventManager Events { get; set; } = null!;
public Variables SharedVariables { get; }
public StateChartState InHandState = null!;
public StateChartState FlyingState = null!;
public StateChartState PlantedState = null!;
public Timer WeaponFlyingTick = null!;
public Vector3 PlantLocation { get; set; }
public Vector3 PlantNormal { get; set; }
public Node? PlantObject { get; set; }
public MeshInstance3D WeaponLocationIndicator { get; set; } = null!;
public StandardMaterial3D WeaponLocationIndicatorMaterial { get; set; } = null!;
public MeshInstance3D WeaponMesh { get; set; } = null!;
#endregion
#region Privates
private StateChart _weaponState = null!;
private Transition _handToFlying = null!;
private Transition _flyingToHand = null!;
private Transition _plantedToHand = null!;
private Transition _plantedToFlying = null!;
private Transition _toPlanted = null!;
private ShapeCast3D _dashCast3D = null!;
private Transform3D _startTransform;
private Vector3 _startMeshRotation;
private Vector3 _throwDirection;
#endregion
public void Init()
{
_weaponState = StateChart.Of(GetNode("StateChart"));
@@ -163,7 +201,6 @@ public partial class WeaponSystem : RigidBody3D, IDamageDealer, IForgeEntity
Abilities = new(this);
Events = new();
// TODO: Waiting on bug resolve
_weaponFlyingAbility = Abilities.GrantAbilityPermanently(FlyingTickAbility.GetAbilityData(), 1, LevelComparison.None, this);
BodyEntered += OnThrownWeaponReachesGround;
@@ -171,6 +208,8 @@ public partial class WeaponSystem : RigidBody3D, IDamageDealer, IForgeEntity
InHandState.StateExited += WeaponLeft;
InHandState.StateEntered += WeaponBack;
#region EventRaising
_handToFlying.Taken += () =>
{
Events.Raise(new EventData
@@ -237,24 +276,14 @@ public partial class WeaponSystem : RigidBody3D, IDamageDealer, IForgeEntity
});
};
#endregion
Events.Subscribe(WeaponStoppedFlyingEventTag, _ => { _weaponFlyingAbility.Cancel(); });
}
private Dictionary<ForgeAbilityBehavior, ActiveEffectHandle> _grantedWeaponStartedFlyingAbilities = new ();
private Dictionary<ForgeAbilityBehavior, ActiveEffectHandle> _grantedWeaponStoppedFlyingAbilities = new ();
private Dictionary<ForgeAbilityBehavior, ActiveEffectHandle> _grantedWeaponFlyingTickAbilities = new ();
public enum WeaponEvent
{
StartedFlying,
StoppedFlying,
FlyingTick
}
public void GrantNewAbilityForEvent(WeaponEvent forEvent, ForgeAbilityBehavior abilityBehavior)
{
var relevantMap = GetGrantedAbilities(forEvent);
GD.Print($"Granted {abilityBehavior} for {forEvent}");
if (relevantMap.ContainsKey(abilityBehavior)) return;
var eventTagsMap = new Dictionary<WeaponEvent, Tag>
@@ -296,6 +325,19 @@ public partial class WeaponSystem : RigidBody3D, IDamageDealer, IForgeEntity
relevantMap.Remove(abilityBehavior);
}
public void RemoveAllEventAbilities()
{
foreach (var weaponEvent in Enum.GetValues<WeaponEvent>())
{
var abilities = GetGrantedAbilities(weaponEvent);
foreach (var ability in abilities.Values)
{
EffectsManager.RemoveEffect(ability);
}
abilities.Clear();
}
}
public Dictionary<ForgeAbilityBehavior, ActiveEffectHandle> GetGrantedAbilities(WeaponEvent forEvent)
{
var abilitiesMap = new Dictionary<WeaponEvent, Dictionary<ForgeAbilityBehavior, ActiveEffectHandle>>

View File

@@ -48,19 +48,13 @@ public partial class PlayerController : CharacterBody3D,
{
public override void _Notification(int what) => this.Notify(what);
#region Dependencies
[Dependency]
public InventoryManager InventoryManager => this.DependOn<InventoryManager>();
// Enums
public enum AllowedInputs
{
All,
MoveCamera,
None,
}
private bool _isUsingGamepad;
public AllowedInputs CurrentlyAllowedInputs { get; set; } = AllowedInputs.All;
#endregion
#region Enums
public enum BufferedActions
{
None,
@@ -69,22 +63,22 @@ public partial class PlayerController : CharacterBody3D,
Dash,
MantleDash
}
private BufferedActions _bufferedAction = BufferedActions.None;
///////////////////////////
// Signals and events //
///////////////////////////
#endregion
#region Signals
[Signal]
public delegate void PlayerDiedEventHandler();
public event Action<IDamageable, DamageRecord> DamageTaken = null!;
public event Action<IHealthable, HealthChangedRecord> HealthChanged = null!;
public event Action<IHealthable> HealthDepleted = null!;
///////////////////////////
// Public stuff //
///////////////////////////
#endregion
#region Publics
public HeadSystem HeadSystem = null!;
public StairsSystem StairsSystem = null!;
public MantleSystem MantleSystem = null!;
@@ -114,7 +108,9 @@ public partial class PlayerController : CharacterBody3D,
public EventManager Events { get; set; } = null!;
public Variables SharedVariables { get; }
// Inspector stuff
#endregion
#region Inspector
[Export] public bool HasSword { get; set; } = true;
[Export] public bool HasParry { get; set; } = true;
@@ -306,10 +302,10 @@ public partial class PlayerController : CharacterBody3D,
public float WallRunAltitudeLossSpeed { get; set; } = 10f;
[Export(PropertyHint.Range, "0,20,0.1,or_greater")]
public float WallRunSpeedThreshold { get; set; } = 8f;
#endregion
#region Privates
///////////////////////////
// Private stuff //
///////////////////////////
// Stairs and shit
private float _lastFrameWasOnFloor = -Mathf.Inf;
private const int NumOfHeadCollisionDetectors = 4;
@@ -317,7 +313,8 @@ public partial class PlayerController : CharacterBody3D,
private AudioStreamPlaybackInteractive _audioStream = null!;
// Basic movement
private bool _movementEnabled = true;
private bool _isUsingGamepad;
private BufferedActions _bufferedAction = BufferedActions.None;
private Vector3 _inputMove = Vector3.Zero;
private Vector3 _inputMoveKeyboard = Vector3.Zero;
private float _inputRotateY;
@@ -434,22 +431,21 @@ public partial class PlayerController : CharacterBody3D,
private AbilityHandle? _empoweredActionHandle;
#endregion
public void OnReady()
{
LoadSettings();
///////////////////////////
// Getting components /////
///////////////////////////
// General use stuff
PlayerUi = GetNode<PlayerUi>("UI");
_closeEnemyDetector = GetNode<ShapeCast3D>("%CloseEnemyDetector");
_closeEnemyDetector.TargetPosition = _closeEnemyDetector.TargetPosition.Normalized() * TargetingDistance;
_aimAssisRayCast = GetNode<RayCast3D>("AimAssistRayCast");
_aimAssisRayCast.TargetPosition = _aimAssisRayCast.TargetPosition.Normalized() * (TargetingDistance*1.5f);
#region Forge
// Forge stuff
var tagsManager = ForgeManagers.Instance.TagsManager;
var cuesManager = ForgeManagers.Instance.CuesManager;
@@ -500,6 +496,7 @@ public partial class PlayerController : CharacterBody3D,
EffectsManager.ApplyEffect(new Effect(effect.GetEffectData(), new EffectOwnership(this, this)));
});
}
#endregion
// DashIndicator = GetNode<TextureRect>("%DashIndicator");
TargetSpeed = WalkSpeed;
@@ -567,8 +564,9 @@ public partial class PlayerController : CharacterBody3D,
CDamageable.DamageTaken += (_, record) => RegisterKnockback(new KnockbackRecord(record));
CHealth.HealthChanged += PlayerUi.OnHealthChanged;
CHealth.HealthDepleted += (_) => Kill();
#region StateManagement
// State management
_playerState = StateChart.Of(GetNode("StateChart"));
_aiming = StateChartState.Of(GetNode("StateChart/Root/Aim/On"));
@@ -617,6 +615,7 @@ public partial class PlayerController : CharacterBody3D,
_airborneDashCooldownTimer = GetNode<Timer>("AirborneDashCooldown");
_invincibilityTimer = GetNode<Timer>("InvincibilityTime");
_attackCooldown = GetNode<Timer>("AttackCooldown");
#endregion
///////////////////////////
// Initialize components //
@@ -642,10 +641,9 @@ public partial class PlayerController : CharacterBody3D,
// PlaceWeaponForTutorial();
HeadSystem.SetWeaponsVisible(HasSword, HasParry);
#region SignalBinding
///////////////////////////
// Signal setup ///////////
///////////////////////////
_invincibilityTimer.Timeout += ResetInvincibility;
_attackCooldown.Timeout += ResetAttackCooldown;
@@ -723,6 +721,8 @@ public partial class PlayerController : CharacterBody3D,
_attackDash.StateEntered += OnDashAttackStarted;
_parryStandard.StateEntered += OnStandardParryStarted;
_parryDash.StateEntered += OnDashParryStarted;
#endregion
// Forge events
var weaponLeftToken = WeaponSystem.Events.Subscribe(WeaponSystem.WeaponStartedFlyingEventTag, OnWeaponLeft);
@@ -731,14 +731,28 @@ public partial class PlayerController : CharacterBody3D,
public void OnResolved()
{
// All of my dependencies are now available! Do whatever you want with
// them here.
// Initialize weapon with inventory abilities
WeaponSystem.RemoveAllEventAbilities();
foreach (var (weaponEvent, abilities) in InventoryManager.WeaponEventsInventory)
{
foreach (var ability in abilities)
{
if (ability is not ForgeAbilityBehavior abilityBehavior) continue;
WeaponSystem.GrantNewAbilityForEvent(weaponEvent, abilityBehavior);
}
}
// Inventory Management
// Inventory changes signal binding
InventoryManager.WeaponEventAbilityAdded += OnWeaponEventAbilityAdded;
InventoryManager.WeaponEventAbilityRemoved += OnWeaponEventAbilityRemoved;
}
public void OnExitTree()
{
InventoryManager.WeaponEventAbilityAdded -= OnWeaponEventAbilityAdded;
InventoryManager.WeaponEventAbilityRemoved -= OnWeaponEventAbilityRemoved;
}
public void OnWeaponLeft(EventData data)
{
var target = data.Target;
@@ -768,33 +782,17 @@ public partial class PlayerController : CharacterBody3D,
public void OnWeaponEventAbilityAdded(WeaponEventAbilityData data)
{
if (data.Ability is not ForgeAbilityBehavior abilityBehavior) return;
WeaponSystem.GrantNewAbilityForEvent(data.ForEvent, abilityBehavior);
WeaponSystem.GrantNewAbilityForEvent(data.ForEvent, data.Ability);
}
public void OnWeaponEventAbilityRemoved(WeaponEventAbilityData data)
{
if (data.Ability is not ForgeAbilityBehavior abilityBehavior) return;
WeaponSystem.RemoveAbilityForEvent(data.ForEvent, abilityBehavior);
WeaponSystem.RemoveAbilityForEvent(data.ForEvent, data.Ability);
}
///////////////////////////
// Settings & tutorial //
///////////////////////////
public void SetAllowedInputsAll()
{
CurrentlyAllowedInputs = AllowedInputs.All;
}
public void SetAllowedInputsMoveCamera()
{
CurrentlyAllowedInputs = AllowedInputs.MoveCamera;
}
public void SetAllowedInputsNone()
{
CurrentlyAllowedInputs = AllowedInputs.None;
}
public void LoadSettings()
{
var config = new ConfigFile();