First adjustments to the Entity System to make it work with different types
This commit is contained in:
@@ -1,23 +1,27 @@
|
||||
using System;
|
||||
using Babushka.scripts.CSharp.GameEntity.LoadSave;
|
||||
using Babushka.scripts.CSharp.GameEntity.Types;
|
||||
using Godot;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Babushka.scripts.CSharp.GameEntity.Entities;
|
||||
|
||||
public partial class Entity : Node2D
|
||||
public class Entity
|
||||
{
|
||||
private long _id;
|
||||
protected virtual EntityType Type => EntityType.None;
|
||||
public long id;
|
||||
public virtual string EntityType => "";
|
||||
|
||||
public Entity()
|
||||
{
|
||||
id = new Random().NextInt64();
|
||||
}
|
||||
|
||||
protected virtual void SaveEntity(JObject json)
|
||||
{
|
||||
json["id"] = _id;
|
||||
json["type"] = (int)Type;
|
||||
json["id"] = id;
|
||||
json["type"] = EntityType;
|
||||
}
|
||||
|
||||
protected virtual void LoadEntity(JObject json)
|
||||
{
|
||||
_id = json.GetLongValue("id");
|
||||
id = json.GetLongValue("id");
|
||||
}
|
||||
}
|
||||
@@ -4,24 +4,30 @@ using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Babushka.scripts.CSharp.GameEntity.Entities;
|
||||
|
||||
public partial class PositionalEntity : Entity
|
||||
public abstract class PositionalEntity : Entity
|
||||
{
|
||||
private Node2D _positionalNodeRef;
|
||||
public Vector2 position;
|
||||
public string sceneName = "none";
|
||||
|
||||
protected override void SaveEntity(JObject json)
|
||||
{
|
||||
base.SaveEntity(json);
|
||||
json["posx"] = Position.X;
|
||||
json["posy"] = Position.Y;
|
||||
json["posx"] = _positionalNodeRef.Position.X;
|
||||
json["posy"] = _positionalNodeRef.Position.Y;
|
||||
json["scene"] = sceneName;
|
||||
}
|
||||
|
||||
protected override void LoadEntity(JObject json)
|
||||
{
|
||||
base.LoadEntity(json);
|
||||
Position = new Vector2(
|
||||
_positionalNodeRef.Position = new Vector2(
|
||||
json.GetFloatValue("posx"),
|
||||
json.GetFloatValue("posy"));
|
||||
sceneName = json.GetStringValue("scene");
|
||||
}
|
||||
|
||||
// Deals with Instantiation of the node
|
||||
public abstract void AddEntity(Node2D parent);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Babushka.scripts.CSharp.GameEntity.Management;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.GameEntity.Entities;
|
||||
|
||||
public class TrashEntity : PositionalEntity
|
||||
{
|
||||
public override string EntityType => OWN_TYPE_NAME;
|
||||
public const string OWN_TYPE_NAME = "TrashEntity";
|
||||
private EntityNodeCreator _creator;
|
||||
|
||||
public TrashEntity()
|
||||
{
|
||||
}
|
||||
|
||||
public override void AddEntity(Node2D parent)
|
||||
{
|
||||
if(_creator == null) _creator = EntityManager.Instance.NodeCreator;
|
||||
var entityNode = _creator.CreateNode2D(EntityType);
|
||||
parent.AddChild(entityNode);
|
||||
entityNode.GlobalPosition = position;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://c31k34epunk5t
|
||||
Reference in New Issue
Block a user