Adding battery charges.

This commit is contained in:
Kiri 2023-03-22 20:37:01 -07:00
parent 050351dba6
commit cebcd7ae97

View File

@ -8,6 +8,7 @@ const jumperCablesFixRange = 48; // Maximum distance to a broken linedef to
class JumperCablesMapLoader : EventHandler class JumperCablesMapLoader : EventHandler
{ {
Map<int, int> originalLinedefSpecials2; Map<int, int> originalLinedefSpecials2;
Array<JumperCablesDeployed> deployedCables;
static JumperCablesMapLoader Get() static JumperCablesMapLoader Get()
{ {
@ -20,28 +21,62 @@ class JumperCablesMapLoader : EventHandler
{ {
JumperCablesMapLoader loader = Get(); JumperCablesMapLoader loader = Get();
// Clear out anything from the last level.
deployedCables.Clear();
loader.originalLinedefSpecials2.Clear(); loader.originalLinedefSpecials2.Clear();
// Console.printf("butt\n"); // Go through all the linedefs and make note of what they're
// Console.printf("butt %s\n", level.MapName); // supposed to do.
// Console.printf("linedefs %d\n", level.Lines.size());
for(int k = 0; k < level.Lines.size(); k++) { for(int k = 0; k < level.Lines.size(); k++) {
Line line = level.Lines[k]; Line line = level.Lines[k];
if(line.special != 0) { if(line.special != 0) {
// Console.printf(" %d = %d\n", line.index(), line.special);
loader.originalLinedefSpecials2.Insert(line.index(), line.special); loader.originalLinedefSpecials2.Insert(line.index(), line.special);
} }
} }
}
override void WorldLineActivated(WorldEvent e)
{
// TODO: Deplete batteries.
Console.printf("Line activated: %d %d\n", e.ActivatedLine.index(), e.ActivatedLine.special);
for(int cableIndex = 0; cableIndex < deployedCables.size(); cableIndex++) {
if(deployedCables[cableIndex].lineDefIndex == e.ActivatedLine.index()) {
deployedCables[cableIndex].DecrementBattery();
break;
}
} }
} }
void RegisterCables(JumperCablesDeployed cableActor)
{
if(deployedCables.Find(cableActor) == deployedCables.size()) {
deployedCables.Push(cableActor);
// FIXME: Remove this.
Console.printf("Cables registered %d\n", deployedCables.size());
}
}
void UnregisterCables(JumperCablesDeployed cableActor)
{
Console.printf("UNREGISTERING\n");
int index = deployedCables.find(cableActor);
if(index != deployedCables.size()) {
deployedCables.delete(index);
// FIXME: Remove this.
Console.printf("Cables unregistered %d\n", deployedCables.size());
}
}
}
class JumperCablesDeployed : HDUPK class JumperCablesDeployed : HDUPK
{ {
int lineDefIndex; int lineDefIndex;
int charges;
default default
{ {
@ -50,32 +85,54 @@ class JumperCablesDeployed : HDUPK
states states
{ {
spawn: spawn:
DERP A -1 { DERP A -1 nodelay {
invoker.bNoGravity = true; invoker.bNoGravity = true;
// Register us with the global event handler so we can
// respond to the linedef getting used.
JumperCablesMapLoader loader = JumperCablesMapLoader.Get();
loader.RegisterCables(self);
} }
stop; stop;
give: give:
DERP A 0 { DERP A 0;
Console.printf("yoink\n");
// Re-break the line.
level.Lines[lineDefIndex].special = 0;
Destroy();
}
goto spawn; goto spawn;
} }
override bool OnGrab(Actor grabber) void DecrementBattery()
{
charges -= 1;
Console.printf("battery used, now: %d", charges);
if(charges <= 0) {
DespawnToBattery();
}
}
void DespawnToBattery()
{ {
// Re-break the linedef. // Re-break the linedef.
level.Lines[lineDefIndex].special = 0; level.Lines[lineDefIndex].special = 0;
// TODO: Spawn a battery. // Spawn a battery, with the same level of charge.
HDMagAmmo.SpawnMag(self, "HDBattery", self.charges);
// TODO: Spawn debris. // TODO: Spawn debris.
Console.printf("NO!\n"); JumperCablesMapLoader loader = JumperCablesMapLoader.Get();
loader.UnregisterCables(self);
Destroy(); Destroy();
}
override bool OnGrab(Actor grabber)
{
DespawnToBattery();
return false; return false;
} }
// TODO: Tick function where we add sparks and stuff?
// TODO: Tick function where we set the sprite to indicate the charge level.
// TODO: Despawn if the sector gets broken *again*?
} }
class JumperCablesUsable : HDWeapon class JumperCablesUsable : HDWeapon
@ -314,6 +371,13 @@ class JumperCablesUsable : HDWeapon
action void AttachCables(HDPlayerPawn pawn) action void AttachCables(HDPlayerPawn pawn)
{ {
HDMagAmmo batteryAmmo = HDMagAmmo(FindInventory("HDBattery"));
if(!batteryAmmo || batteryAmmo.amount < 1) {
invoker.owner.A_Log("You need a battery to use this.");
return;
}
JumperCablesMapLoader loader = JumperCablesMapLoader.Get(); JumperCablesMapLoader loader = JumperCablesMapLoader.Get();
FLineTraceData linetraceData; FLineTraceData linetraceData;
DoLineTrace(pawn, linetraceData); DoLineTrace(pawn, linetraceData);
@ -332,10 +396,18 @@ class JumperCablesUsable : HDWeapon
if(originalSpecial != currentSpecial) { if(originalSpecial != currentSpecial) {
// Remove battery from inventory.
int batteryCharges = batteryAmmo.TakeMag(true);
Console.printf("batteryCharges: %d", batteryCharges);
JumperCablesDeployed deployed = JumperCablesDeployed( JumperCablesDeployed deployed = JumperCablesDeployed(
Spawn("JumperCablesDeployed", linetraceData.hitLocation)); Spawn(
"JumperCablesDeployed",
linetraceData.hitLocation - linetraceData.hitDir * 8));
deployed.lineDefIndex = brokenLine.index(); deployed.lineDefIndex = brokenLine.index();
deployed.bNoGravity = true; deployed.bNoGravity = true;
deployed.charges = batteryCharges;
brokenLine.special = originalSpecial; brokenLine.special = originalSpecial;
} }