fix crashing when respawning with no save

pull/6/head
John Montagu, the 4th Earl of Sandvich 2023-08-15 11:27:03 -07:00
parent 755817e81a
commit 1824cb94cf
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
8 changed files with 62 additions and 9 deletions

View File

@ -0,0 +1,25 @@
[gd_resource type="Resource" load_steps=8 format=3 uid="uid://betjy08c65fp1"]
[ext_resource type="Script" path="res://State/Global/MapState.cs" id="1_aydul"]
[ext_resource type="Script" path="res://State/Global/Progression.cs" id="2_y82md"]
[ext_resource type="Script" path="res://State/Global/Stats.cs" id="3_rktrk"]
[ext_resource type="Script" path="res://Utils/Save.cs" id="4_n4440"]
[sub_resource type="Resource" id="Resource_nf41v"]
script = ExtResource("1_aydul")
_state = {}
[sub_resource type="Resource" id="Resource_7l14i"]
script = ExtResource("2_y82md")
[sub_resource type="Resource" id="Resource_e5qnf"]
script = ExtResource("3_rktrk")
SaveLocation = Vector2(252, 0)
SaveMapKey = "res://Scenes/Maps/ArenaExterior.tscn"
DeathCount = 0
[resource]
script = ExtResource("4_n4440")
Progression = SubResource("Resource_7l14i")
MapState = SubResource("Resource_nf41v")
Stats = SubResource("Resource_e5qnf")

View File

@ -20,6 +20,7 @@ public abstract partial class Boss : Enemy
public abstract int Intensity { get; }
private bool _isActive;
private Events.EventBus _eventBus;
[Export]
public virtual bool IsActive
@ -30,7 +31,10 @@ public abstract partial class Boss : Enemy
_isActive = value;
// register or deregister ourselves when we are active/inactive
this.GetWorld().RegisterBoss(_isActive ? this : null);
_eventBus.EmitSignal(
Events.EventBus.SignalName.RegisteredBoss,
_isActive ? this : null
);
}
}
@ -47,6 +51,8 @@ public abstract partial class Boss : Enemy
{
Reset();
};
_eventBus = this.GetEventBus();
}
protected void UpdateBossStatus(bool status)

View File

@ -331,7 +331,7 @@ public partial class Character : CharacterBody2D, IFaction
if (Health <= 0)
{
EmitSignal(SignalName.Death, args);
GetNode<GpuParticles2D>("DeathParticles")
GetNode<GpuParticles2D>("DeathParticles")?
.CloneOnWorld<GpuParticles2D>()
.EmitOneShot();
}

View File

@ -315,7 +315,6 @@ MaxDistanceToTarget = 128.0
UseItemDistance = 48.0
PassiveState = NodePath("../Idle")
PursueState = NodePath("../Idle")
PursueOnLineOfSight = false
NPC = NodePath("../..")
[node name="Idle" type="Node" parent="ThinkerStateMachine" node_paths=PackedStringArray("PursueState", "NavigationAgent", "NPC")]

View File

@ -18,4 +18,10 @@ public partial class EventBus : Node
[Signal]
public delegate void PlayerHealthChangedEventHandler(HealthChangedArgs args);
[Signal]
public delegate void RegisteredBossEventHandler(Characters.Boss boss);
[Signal]
public delegate void DeregisteredBossEventHandler(Characters.Boss boss);
}

View File

@ -1,13 +1,16 @@
using Godot;
using SupaLidlGame.Characters;
using SupaLidlGame.Extensions;
namespace SupaLidlGame.UI;
public partial class BossBar : VBoxContainer
{
public TextureProgressBar ProgressBar { get; set; }
public Label BossNameLabel { get; set; }
private Events.EventBus _eventBus;
private Boss _boss;
public Boss Boss
@ -24,6 +27,19 @@ public partial class BossBar : VBoxContainer
{
ProgressBar = GetNode<TextureProgressBar>("BarMargin/BossBar");
BossNameLabel = GetNode<Label>("LabelMargin/BossNameLabel");
_eventBus = this.GetEventBus();
_eventBus.RegisteredBoss += RegisterBoss;
_eventBus.DeregisteredBoss += DeregisterBoss;
}
private void RegisterBoss(Boss boss)
{
Boss = boss;
}
private void DeregisterBoss(Boss boss)
{
Boss = null;
}
private void OnBossHurt(Events.HurtArgs args)

View File

@ -67,7 +67,7 @@ public class CacheStore<TKey, TVal>
public bool IsItemValid(TKey key)
{
return !IsItemStale(key) && _store.ContainsKey(key);
return key is not null && _store.ContainsKey(key) && !IsItemStale(key);
}
public bool ContainsKey(TKey key)

View File

@ -99,6 +99,8 @@ public partial class World : Node
{
MoveToArea(args.Area, args.Connector);
};
EventBus.RegisteredBoss += RegisterBoss;
EventBus.DeregisteredBoss += DeregisterBoss;
_uiViewport = GetNode<SubViewport>("CanvasLayer/SubViewportContainer/UIViewport");
@ -111,10 +113,9 @@ public partial class World : Node
base._Ready();
}
public void RegisterBoss(Boss boss)
private void RegisterBoss(Boss boss)
{
CurrentBoss = boss;
UIController.BossBar.Boss = boss;
MusicPlayer.Stream = boss?.Music;
// TODO: use an audio manager
if (MusicPlayer.Stream is null)
@ -127,10 +128,9 @@ public partial class World : Node
}
}
public void DeregisterBoss(Boss boss)
private void DeregisterBoss(Boss boss)
{
CurrentBoss = null;
UIController.BossBar.Boss = null;
MusicPlayer.Stop();
}
@ -304,7 +304,8 @@ public partial class World : Node
}
else
{
return new Save();
return ResourceLoader.Load("res://Assets/default-save.tres")
.Duplicate(true) as Save;
}
}