Skip to content

Commit

Permalink
Better Teach Lesson Code (#1657)
Browse files Browse the repository at this point in the history
# Description

Thanks to Router for pointing out we can actually reuse code that is
apparently just always running exclusively on player characters. So it
turns out that this game actually has a system for tracking who the
killer is. We can skip enumeration outright if the killer was the one
who downed the player. There's still BETTER ways to do this, like
subscribing to KillReportedEvent instead of MobStateChanged, but I'm a
bit too zonked out to pursue that right now.
  • Loading branch information
VMSolidus authored Jan 26, 2025
1 parent 8eafa7a commit f325d2e
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,40 @@ public override void Initialize()
SubscribeLocalEvent<MobStateChangedEvent>(OnMobStateChanged);
}

// TODO: subscribe by ref at some point in the future
private void OnMobStateChanged(MobStateChangedEvent args)
{
if (args.NewMobState != MobState.Dead)
if (args.NewMobState != MobState.Critical || args.OldMobState >= args.NewMobState
|| !TryComp<MindContainerComponent>(args.Target, out var mc) || mc.OriginalMind is not { } mindId)
return;

// Get the mind of the entity that just died (if it had one)
// Uses OriginalMind so if someone ghosts or otherwise loses control of a mob, you can still greentext
if (!TryComp<MindContainerComponent>(args.Target, out var mc) || mc.OriginalMind is not {} mindId)
// If the attacker actually has the objective, we can just skip any enumeration outright.
if (args.Origin is not null
&& HasComp<TeachLessonConditionComponent>(args.Origin)
&& TryComp<TargetObjectiveComponent>(args.Origin, out var targetComp)
&& targetComp.Target == mindId)
{
_codeCondition.SetCompleted(args.Origin!.Value);
return;
}

// Get all TeachLessonConditionComponent entities
var query = EntityQueryEnumerator<TeachLessonConditionComponent, TargetObjectiveComponent>();

while (query.MoveNext(out var uid, out var conditionComp, out var targetObjective))
while (query.MoveNext(out var ent, out var conditionComp, out var targetObjective))
{
// Check if this objective's target matches the entity that died
if (targetObjective.Target != mindId)
continue;

var userWorldPos = _transform.GetWorldPosition(uid);
var userWorldPos = _transform.GetWorldPosition(ent);
var targetWorldPos = _transform.GetWorldPosition(args.Target);

var distance = (userWorldPos - targetWorldPos).Length();
if (distance > conditionComp.MaxDistance
|| Transform(uid).MapID != Transform(args.Target).MapID)
|| Transform(ent).MapID != Transform(args.Target).MapID)
continue;

_codeCondition.SetCompleted(uid);
_codeCondition.SetCompleted(ent);
}
}
}

0 comments on commit f325d2e

Please sign in to comment.