Skip to content

Commit

Permalink
streamlined scripts a bit, got rid of unecessary constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan-FGR committed Oct 24, 2017
1 parent 897790f commit e3cc8f3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
6 changes: 4 additions & 2 deletions TestGame/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ public override void Update()

public class Rotate : Script
{
private readonly float speed_;
private float speed_;
public Rotate(Entity entity, float speed) : base(entity)
{
speed_ = speed;
StoreScriptData("speed",speed_);//we store the data for this script, so it persists in save file
}
//constructor used for deserialization
public Rotate(Entity entity, Dictionary<string, object> scriptData) : base(entity, scriptData)
public Rotate(Entity entity) : base(entity){}

protected override void AfterDeserialization()
{
speed_ = RetrieveScriptData<float>("speed");//we retrieve data stored when deserializing
}
Expand Down
4 changes: 1 addition & 3 deletions ælum/Systems/Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public static Component CreateFromData(Entity entity, ComponentData componentDat
if (componentData.typeId == ComponentTypes.Script)
{
ScriptTypeAndData stad = MessagePackSerializer.Deserialize<ScriptTypeAndData>(componentData.serialData);
if (stad.ScriptData != null)
return Activator.CreateInstance(Type.GetType(stad.ScriptType), entity, stad.ScriptData) as Component;
return Activator.CreateInstance(Type.GetType(stad.ScriptType), entity) as Component;
return Script.CreateFromData(entity, stad);
}

throw new Exception("component type couldn't be resolved; make sure to add all serializable types to this method");
Expand Down
29 changes: 14 additions & 15 deletions ælum/Systems/Scripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ protected Script(Entity entity) : base(entity)

}

protected Script(Entity entity, Dictionary<string, object> scriptData) : this(entity)
//cereal factory
internal static Script CreateFromData(Entity entity, ScriptTypeAndData stad)
{
this.scriptData = scriptData;
var newScript = Activator.CreateInstance(Type.GetType(stad.ScriptType), entity) as Script;
newScript.scriptData = stad.ScriptData;
newScript.AfterDeserialization();
return newScript;
}

protected void StoreScriptData(string key, object data)
{
if (scriptData == null) scriptData = new Dictionary<string, object>();
Expand Down Expand Up @@ -53,19 +57,15 @@ public sealed override ComponentData GetSerialData()
}

protected virtual void BeforeSerialization() //ovrd if you need to update script data before entity gets serialized
{

}
{}

protected virtual void AfterDeserialization()
{}

}

class PlayerController : Script
{
public PlayerController(Entity entity, Dictionary<string, object> scriptData) : base(entity, scriptData)
{

}

public PlayerController(Entity entity) : base(entity)
{

Expand Down Expand Up @@ -181,12 +181,11 @@ public Rotator(Entity entity, float speed) : base(entity)
StoreScriptData("spd", speed);
}

public Rotator(Entity entity, Dictionary<string, object> scriptData) : base(entity, scriptData)
protected override void AfterDeserialization()
{
speed = RetrieveScriptData<float>("spd");

}

public override void Update()
{
entity.Rotation += speed*0.1f;
Expand Down Expand Up @@ -217,7 +216,7 @@ public ICANHAZNAME(Entity entity, string name) : base(entity)
StoreScriptData("n", name);
}

public ICANHAZNAME(Entity entity, Dictionary<string, object> scriptData) : base(entity, scriptData)
protected override void AfterDeserialization()
{
name = RetrieveScriptData<string>("n");
}
Expand Down

0 comments on commit e3cc8f3

Please # to comment.