-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial structure: Spriters, Listeners, Xml and so on
- Loading branch information
1 parent
fbd52f2
commit 5b75f09
Showing
52 changed files
with
1,751 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Collections.Generic; | ||
|
||
public class Emitter | ||
{ | ||
protected HashSet<IListener> listeners = new HashSet<IListener>(); | ||
|
||
public void Connect(IListener listener) | ||
{ | ||
listener.Emitter = this; | ||
listeners.Add(listener); | ||
} | ||
public void Disconnect(IListener listener) | ||
{ | ||
listeners.Remove(listener); | ||
} | ||
|
||
protected void Emit(string signal, object[] args = null) | ||
{ | ||
foreach(IListener listener in listeners) | ||
{ | ||
listener.Event(signal, args); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Xml; | ||
using System.Xml.Schema; | ||
using System.Xml.Serialization; | ||
using UnityEngine; | ||
|
||
public class Entity : Emitter, IXmlSerializable { | ||
|
||
public string id; | ||
|
||
public Entity(string id = null) | ||
{ | ||
} | ||
|
||
|
||
public Entity(Entity other) | ||
{ | ||
this.id = other.id; | ||
} | ||
|
||
virtual public XmlSchema GetSchema() | ||
{ | ||
return null; | ||
} | ||
|
||
virtual public void ReadXml(XmlReader reader) | ||
{ | ||
reader.Read(); | ||
Debug.Assert(reader.Name == "Entity", | ||
String.Format("Wrong Xml Name for 'Entity', instead is '{0}'", reader.Name) | ||
); | ||
if (id == null) id = reader.GetAttribute("id"); | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.NodeType == XmlNodeType.Element) | ||
{ | ||
switch (reader.Name) | ||
{ | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
virtual public void WriteXml(XmlWriter writer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Xml; | ||
using System.Xml.Schema; | ||
using System.Xml.Serialization; | ||
using UnityEngine; | ||
|
||
public class EntityBuilding : EntityInanimate, IXmlSerializable | ||
{ | ||
bool installed = false; | ||
private float walkingSpeed = 1; | ||
private Tile tile = null; | ||
|
||
public EntityBuilding(string id = null) | ||
: base(id) | ||
{ | ||
|
||
} | ||
|
||
public EntityBuilding(EntityBuilding other) | ||
: base(other) | ||
{ | ||
this.installed = other.installed; | ||
this.walkingSpeed = other.walkingSpeed; | ||
} | ||
|
||
public EntityBuilding(XmlReader reader) | ||
{ | ||
ReadXml(reader); | ||
} | ||
|
||
public bool Passable | ||
{ | ||
get | ||
{ | ||
return walkingSpeed > 0; | ||
} | ||
} | ||
|
||
public float WalkingSpeed | ||
{ | ||
get | ||
{ | ||
return walkingSpeed; | ||
} | ||
|
||
set | ||
{ | ||
walkingSpeed = value; | ||
} | ||
} | ||
|
||
public Tile Tile | ||
{ | ||
get | ||
{ | ||
return tile; | ||
} | ||
|
||
set | ||
{ | ||
tile = value; | ||
InstallAt(tile.world, tile.coord); | ||
} | ||
} | ||
|
||
override public XmlSchema GetSchema() | ||
{ | ||
return null; | ||
} | ||
|
||
override public void ReadXml(XmlReader reader) | ||
{ | ||
reader.Read(); | ||
|
||
Debug.Assert(reader.Name == "EntityBuilding", | ||
String.Format("Wrong Xml Name for 'EntityBuilding', instead is '{0}'", reader.Name) | ||
); | ||
if (id == null) id = reader.GetAttribute("id"); | ||
|
||
Debug.Log(string.Format("Reading 'EntityBuilding' from Xml, id = '{0}'", id)); | ||
while(reader.Read()) | ||
{ | ||
if(reader.NodeType == XmlNodeType.Element) | ||
{ | ||
switch(reader.Name) | ||
{ | ||
case "EntityInanimate": | ||
XmlReader subReader = reader.ReadSubtree(); | ||
base.ReadXml(subReader); | ||
subReader.Close(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void InstallAt(World world, World.Coord coord) | ||
{ | ||
//world.InstallAt(this, coord); | ||
|
||
foreach(IListener listener in listeners) | ||
{ | ||
if(listener is IEntityBuildingListener) ((IEntityBuildingListener) listener).InstallAt(world, coord); | ||
} | ||
|
||
foreach(Tile neighbour in Tile.GetNeighbours()) | ||
{ | ||
neighbour.OnNeighbourChanged(world, Tile); | ||
} | ||
} | ||
|
||
public void NeighbourChange(World world, Tile neighbour) | ||
{ | ||
foreach (IListener listener in listeners) | ||
{ | ||
if (listener is IEntityBuildingListener) ((IEntityBuildingListener)listener).NeighbourChanged(world, neighbour); | ||
} | ||
} | ||
|
||
override public void WriteXml(XmlWriter writer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
|
||
public BitArray GetConnectingNeighbours() | ||
{ | ||
BitArray connection = new BitArray(8); | ||
for(int i = 0; i < 8; ++i) | ||
{ | ||
Tile neighbour = Tile.GetNeighbour(i); | ||
if (neighbour == null) continue; | ||
connection[i] = neighbour.building != null; | ||
} | ||
|
||
return connection; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Xml; | ||
using System.Xml.Schema; | ||
using System.Xml.Serialization; | ||
using UnityEngine; | ||
|
||
public class EntityInanimate : Entity, IXmlSerializable | ||
{ | ||
|
||
public EntityInanimate(string id = null) | ||
: base(id) | ||
{ | ||
} | ||
|
||
public EntityInanimate(EntityInanimate other) | ||
: base(other) | ||
{ | ||
|
||
} | ||
|
||
override public XmlSchema GetSchema() | ||
{ | ||
return null; | ||
} | ||
|
||
override public void ReadXml(XmlReader reader) | ||
{ | ||
reader.Read(); | ||
Debug.Assert(reader.Name == "EntityInanimate", | ||
String.Format("Wrong Xml Name for 'EntityInanimate', instead is '{0}'", reader.Name) | ||
); | ||
if (id == null) id = reader.GetAttribute("id"); | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.NodeType == XmlNodeType.Element) | ||
{ | ||
switch (reader.Name) | ||
{ | ||
case "Entity": | ||
XmlReader subReader = reader.ReadSubtree(); | ||
base.ReadXml(subReader); | ||
subReader.Close(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
override public void WriteXml(XmlWriter writer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Xml; | ||
using UnityEngine; | ||
using System.IO; | ||
using System; | ||
|
||
public class EntityRegistry { | ||
|
||
static string path = "Data"; | ||
static private Dictionary<string, EntityBuilding> buildingsRegistry = new Dictionary<string, EntityBuilding>(); | ||
|
||
public EntityRegistry() | ||
{ | ||
string buldingsPath = Path.Combine(Path.Combine(Application.streamingAssetsPath, path), "buildings.xml"); | ||
|
||
if (File.Exists(buldingsPath)) | ||
{ | ||
XmlReaderSettings settings = new XmlReaderSettings(); | ||
XmlReader reader = XmlReader.Create(buldingsPath, settings); | ||
|
||
ReadPrototypes(reader); | ||
} else | ||
{ | ||
Debug.LogError("Error while loading prototypes."); | ||
} | ||
} | ||
|
||
internal static EntityBuilding InstantiateEntityBuilding(string id) | ||
{ | ||
return new EntityBuilding(buildingsRegistry[id]); | ||
} | ||
|
||
public void ReadPrototypes(XmlReader xmlReader) | ||
{ | ||
xmlReader.MoveToContent(); | ||
|
||
Debug.Assert(xmlReader.Name == "Prototypes", | ||
String.Format("Wrong Xml Name for 'Prototypes', instead is '{0}'", xmlReader.Name) | ||
); | ||
while(xmlReader.Read()) | ||
{ | ||
if(xmlReader.NodeType == XmlNodeType.Element) | ||
{ | ||
switch(xmlReader.Name) | ||
{ | ||
case "EntityBuilding": | ||
XmlReader subReader = xmlReader.ReadSubtree(); | ||
EntityBuilding entityBuilding = new EntityBuilding(subReader); | ||
buildingsRegistry.Add(entityBuilding.id, entityBuilding); | ||
subReader.Close(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.