Skip to content

Commit

Permalink
Initial structure: Spriters, Listeners, Xml and so on
Browse files Browse the repository at this point in the history
  • Loading branch information
FilippoLeon committed Jun 22, 2017
1 parent fbd52f2 commit 5b75f09
Show file tree
Hide file tree
Showing 52 changed files with 1,751 additions and 41 deletions.
9 changes: 9 additions & 0 deletions Assets/Engine/Entities.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Assets/Engine/Entities/Emitter.cs
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);
}
}
}
12 changes: 12 additions & 0 deletions Assets/Engine/Entities/Emitter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions Assets/Engine/Entities/Entity.cs
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.
142 changes: 142 additions & 0 deletions Assets/Engine/Entities/EntityBuilding.cs
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;
}
}
12 changes: 12 additions & 0 deletions Assets/Engine/Entities/EntityBuilding.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions Assets/Engine/Entities/EntityInanimate.cs
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();
}
}
12 changes: 12 additions & 0 deletions Assets/Engine/Entities/EntityInanimate.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions Assets/Engine/Entities/EntityRegister.cs
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;
}
}
}
}
}
Loading

0 comments on commit 5b75f09

Please sign in to comment.