Skip to content

Commit

Permalink
Add reactor spawns
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaioru committed Sep 28, 2024
1 parent 20977bc commit 320fdd3
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/common/Edelstein.Common.Gameplay.Game/FieldManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.Threading.Tasks;
using Edelstein.Common.Gameplay.Game.Objects.Mob;
using Edelstein.Common.Gameplay.Game.Objects.NPC;
using Edelstein.Common.Gameplay.Game.Objects.Reactors;
using Edelstein.Protocol.Gameplay.Game;
using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Templates;
using Edelstein.Protocol.Gameplay.Game.Objects.NPC.Templates;
using Edelstein.Protocol.Gameplay.Game.Objects.Reactors.Templates;
using Edelstein.Protocol.Gameplay.Game.Templates;
using Edelstein.Protocol.Utilities.Templates;

Expand All @@ -14,7 +16,8 @@ namespace Edelstein.Common.Gameplay.Game;
public class FieldManager(
ITemplateManager<IFieldTemplate> templates,
ITemplateManager<INPCTemplate> npcs,
ITemplateManager<IMobTemplate> mobs
ITemplateManager<IMobTemplate> mobs,
ITemplateManager<IReactorTemplate> reactors
) : IFieldManager
{
private readonly ConcurrentDictionary<int, IField> _fields = new();
Expand Down Expand Up @@ -63,6 +66,18 @@ await template.Footholds.Retrieve(life.Foothold),
}
}

foreach (var reactor in template.Reactors)
{
var reactorTemplate = await reactors.Retrieve(reactor.TemplateID);
if (reactorTemplate == null) continue;
var reactorObj = new FieldReactor(
reactorTemplate,
reactor.Position
);

await field.Enter(reactorObj);
}

return _fields.TryAdd(key, field) ? field : null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Edelstein.Protocol.Gameplay.Game.Contracts.Packets.Send;
using Edelstein.Protocol.Gameplay.Game.Objects;
using Edelstein.Protocol.Gameplay.Game.Objects.Reactors;
using Edelstein.Protocol.Gameplay.Game.Objects.Reactors.Templates;
using Edelstein.Protocol.Gameplay.Game.Templates.Spatial;
using Edelstein.Protocol.Network.Packets;
using Edelstein.Protocol.Utilities.Spatial;

namespace Edelstein.Common.Gameplay.Game.Objects.Reactors;

public class FieldReactor(
IReactorTemplate template,
IPoint2D position,
bool facingLeft = true
) : AbstractFieldObject(position), IFieldReactor
{
public override FieldObjectType Type => FieldObjectType.Reactor;
public IReactorTemplate Template => template;

public override IDispatchable GetDispatchEnterField(bool isEnterField = false)
=> new ReactorEnterField
{
ObjectID = ObjectID ?? 0,
TemplateID = template.ID,
State = 0,
X = (short)position.X,
Y = (short)position.Y,
Flip = facingLeft
};

public override IDispatchable GetDispatchLeaveField(bool isLeaveField = false)
=> new ReactorLeaveField
{
ObjectID = ObjectID ?? 0,
State = 0,
X = (short)position.X,
Y = (short)position.Y
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ public record FieldTemplate : IFieldTemplate
public int MobCapacityMax { get; }

public IReadOnlyCollection<IFieldTemplateLife> Life { get; }
public IReadOnlyCollection<IFieldTemplateReactor> Reactors { get; }

public FieldTemplate(
int id,
IDataNode foothold,
IDataNode portal,
IDataNode ladderRope,
IDataNode life,
IDataNode reactor,
IDataNode info
)
{
Expand Down Expand Up @@ -109,5 +111,8 @@ IDataNode info
Life = life.Children
.Select(p => new FieldTemplateLife(p))
.ToFrozenSet();
Reactors = reactor.Children
.Select(p => new FieldTemplateReactor(p))
.ToFrozenSet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ await context.Insert(new TemplateProviderLazy<IFieldTemplate>(
n.ResolvePath("portal")!.Cache(),
n.ResolvePath("ladderRope")!.Cache(),
n.ResolvePath("life")!.Cache(),
n.ResolvePath("reactor")!.Cache(),
n.ResolvePath("info")!.Cache()
)
));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Duey.Abstractions;
using Edelstein.Common.Utilities.Spatial;
using Edelstein.Protocol.Gameplay.Game.Templates;
using Edelstein.Protocol.Utilities.Spatial;

namespace Edelstein.Common.Gameplay.Game.Templates;

public record FieldTemplateReactor : IFieldTemplateReactor
{
public string? Name { get; }
public int TemplateID { get; }

public int ReactorTime { get; }

public bool IsFacingLeft { get; }
public IPoint2D Position { get; }

public FieldTemplateReactor(IDataNode node)
{
TemplateID = Convert.ToInt32(node.ResolveString("id") ?? "-1");

ReactorTime = node.ResolveInt("reactorTime") ?? 0;

IsFacingLeft = !(node.ResolveBool("f") ?? false);

Position = new Point2D(
node.ResolveInt("x") ?? int.MinValue,
node.ResolveInt("y") ?? int.MinValue
);

Name = node.ResolveString("name") ?? "NO-NAME";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using BinarySerialization;
using Edelstein.Protocol.Network.Packets;
using Edelstein.Protocol.Network.Packets.Types;

namespace Edelstein.Protocol.Gameplay.Game.Contracts.Packets.Send;

public record ReactorEnterField() : StructuredSendPacket((short)PacketSendOperation.ReactorEnterField)
{
[FieldOrder(0)] public required int ObjectID { get; init; }
[FieldOrder(1)] public required int TemplateID { get; init; }
[FieldOrder(2)] public required byte State { get; init; }
[FieldOrder(3)] public short X { get; init; }
[FieldOrder(4)] public short Y { get; init; }
[FieldOrder(5)] public bool Flip { get; init; }
[FieldOrder(6)] public LPString Name { get; init; } = new();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using BinarySerialization;
using Edelstein.Protocol.Network.Packets;

namespace Edelstein.Protocol.Gameplay.Game.Contracts.Packets.Send;

public record ReactorLeaveField() : StructuredSendPacket((short)PacketSendOperation.ReactorLeaveField)
{
[FieldOrder(0)] public required int ObjectID { get; init; }
[FieldOrder(1)] public required byte State { get; init; }
[FieldOrder(2)] public short X { get; init; }
[FieldOrder(3)] public short Y { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Edelstein.Protocol.Gameplay.Game.Objects.Reactors.Templates;

namespace Edelstein.Protocol.Gameplay.Game.Objects.Reactors;

public interface IFieldReactor : IFieldObject
{
IReactorTemplate Template { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ public interface IFieldTemplate : ITemplate
int MobCapacityMax { get; }

IReadOnlyCollection<IFieldTemplateLife> Life { get; }
IReadOnlyCollection<IFieldTemplateReactor> Reactors { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Edelstein.Protocol.Utilities.Spatial;

namespace Edelstein.Protocol.Gameplay.Game.Templates;

public interface IFieldTemplateReactor
{
string? Name { get; }
int TemplateID { get; }

int ReactorTime { get; }

bool IsFacingLeft { get; }
IPoint2D Position { get; }
}

0 comments on commit 320fdd3

Please sign in to comment.