Created
August 13, 2012 23:00
-
-
Save martindevans/3344658 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void write(entity e, BinaryWriter w) | |
{ | |
w.write(e.Template.Id); | |
foreach (Component c in e.Components) //e.Components is ordered in a specific way when it's constructed | |
{ | |
Assert(e.Template.Contains(c)); | |
c.Write(w); //Component writes out the data it needs | |
} | |
} | |
Entity Read(BinaryReader r) | |
{ | |
Template t = Template.GetById(r.ReadInt()); | |
foreach (Action constructorAction in t.Components) //Reflection of constructor is done at template construction time (load time) | |
{ | |
constructorAction(r); //Component reads the data is needs in the constructor which accepts a BinaryReader | |
} | |
} | |
class ExampleComponent | |
{ | |
int SomeData; | |
string ImportantState; | |
ExampleComponent(int data, string state) | |
{ | |
SomeData = data; | |
ImportantState = state; | |
} | |
ExampleComponent(BinaryReader r) | |
:this(r.ReadInt(), r.ReadString()) | |
{ | |
} | |
override void Write(BinaryWriter w) | |
{ | |
r.WriteInt(SomeData); | |
r.WriteString(ImportantState); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment