Skip to content

Commit

Permalink
Merge pull request ArchipelagoMW#7 from Extra-2-Dew/death-link
Browse files Browse the repository at this point in the history
Add Death Link functionality
  • Loading branch information
Chris-Is-Awesome authored Jul 13, 2024
2 parents f847f80 + bddb092 commit c7b058d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
24 changes: 22 additions & 2 deletions Code/APHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Archipelago.MultiClient.Net;
using Archipelago.MultiClient.Net.BounceFeatures.DeathLink;
using Archipelago.MultiClient.Net.Enums;
using Archipelago.MultiClient.Net.Helpers;
using Archipelago.MultiClient.Net.MessageLog.Messages;
Expand All @@ -19,6 +20,7 @@ public class APHandler
public static APHandler Instance { get { return instance; } }
public static ArchipelagoSession Session { get; private set; }
public static Dictionary<string, object> slotData;
public static bool deathLink = true;
public PlayerInfo CurrentPlayer { get; private set; }
public static bool IsConnected { get { return Session != null; } }

Expand All @@ -31,6 +33,7 @@ public bool TryCreateSession(string url, string slot, string password, out strin
{
if (Session != null)
{
DeathLinkHandler.deathLinkService?.DisableDeathLink();
Session.MessageLog.OnMessageReceived -= OnReceiveMessage;
}
try
Expand All @@ -45,7 +48,7 @@ public bool TryCreateSession(string url, string slot, string password, out strin

LoginResult result;

try
try
{
result = Session.TryConnectAndLogin("Ittle Dew 2", slot, ItemsHandlingFlags.AllItems, password: password, requestSlotData: true);
}
Expand All @@ -72,7 +75,15 @@ public bool TryCreateSession(string url, string slot, string password, out strin

OnConnected((LoginSuccessful)result);
var loginSuccess = (LoginSuccessful)result;
message = "Successfully connected!\nNow that you are connected, you can use !help to list commands to run via the server.";
message = "Successfully connected!\nNow that you are connected, you can use !help to list commands to run via the server.";
if (deathLink)
{
DeathLinkHandler.deathLinkService = Session.CreateDeathLinkService();
DeathLinkHandler.deathLinkService.EnableDeathLink();
DeathLinkHandler.deathLinkService.OnDeathLinkReceived += Plugin.Instance.deathLinkHandler.OnDeathLinkReceived;
message += "\nDeathlink enabled. Have fun! :)";
}

return true;
}

Expand Down Expand Up @@ -112,6 +123,15 @@ public void SyncItemsWithServer()
}
}
}

/// <summary>
/// Gets the name of this player
/// </summary>
/// <returns></returns>
public static string GetPlayerName()
{
return Session.Players.GetPlayerAlias(Session.ConnectionInfo.Slot);
}

private void OnConnected(LoginSuccessful loginSuccess)
{
Expand Down
68 changes: 68 additions & 0 deletions Code/DeathLinkHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Archipelago.MultiClient.Net.BounceFeatures.DeathLink;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace ArchipelagoRandomizer
{
public class DeathLinkHandler
{
public DeathLinkHandler()
{
Events.OnEntityDied += OnEntityDied;
}

public static DeathLinkService deathLinkService;

// while this is true, you cannot receive or send Death Links.
private bool deathSafety = false;

private void OnEntityDied(Entity entity, Killable.DetailedDeathData data)
{
if (!deathSafety && entity.name == "PlayerEnt")
{
Plugin.Log.LogMessage("Ittle died! Oh no!");
deathLinkService.SendDeathLink(new DeathLink(APHandler.GetPlayerName(), $"{APHandler.GetPlayerName()} {RandomDeathString()}"));
deathSafety = true;
Plugin.Instance.StartCoroutine(DeathSafetyCounter());
}
}

public void OnDeathLinkReceived(DeathLink deathLink)
{
if (deathSafety) return;
Killable player = ModCore.Utility.GetPlayer().gameObject?.GetComponentInChildren<Killable>();
if (player != null)
{
player.SignalDeath();
player.CurrentHp = 0;
string deathMessage = "";
if (deathLink.Cause != null) deathMessage = deathLink.Cause;
else deathMessage = $"{deathLink.Source} died!";
Plugin.Log.LogWarning("Chris make an Item Get message for this once you've done the rewrite Mjau");
}
}

private string RandomDeathString()
{
string[] deathMessages =
{
"didn't dew.",
"was pummelled to death.",
"got tired of adventuring.",
"ran out of hearts.",
"forgot to pack health potions."
};
return deathMessages[Random.Range((int)0, deathMessages.Length)];
}

// turns off your Death Link invincibility
private IEnumerator DeathSafetyCounter()
{
yield return new WaitForSeconds(10);
deathSafety = false;
}
}
}
3 changes: 3 additions & 0 deletions Code/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace ArchipelagoRandomizer
[BepInDependency("ModCore")]
public class Plugin : BaseUnityPlugin
{
public DeathLinkHandler deathLinkHandler;

internal static Plugin Instance { get; private set; }
internal static ManualLogSource Log { get; private set; }
internal static bool TestingLocally { get; } = false;
Expand All @@ -33,6 +35,7 @@ private void Awake()
apCommandHandler.AddCommands();
DebugMenuManager.LogToConsole("To connect to an ArchipelagoHandler server, use 'ap /connect {server:port} {slot} {password}");
customTextHandler = new CustomTextHandler();
deathLinkHandler = new DeathLinkHandler();

Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly());

Expand Down

0 comments on commit c7b058d

Please sign in to comment.