Skip to content

Commit

Permalink
Emitter improved, more XMl and UI
Browse files Browse the repository at this point in the history
  • Loading branch information
FilippoLeon committed Jul 7, 2017
1 parent b249887 commit c20c768
Show file tree
Hide file tree
Showing 65 changed files with 1,347 additions and 322 deletions.
174 changes: 171 additions & 3 deletions Assets/Engine/Emitters/Entities/Emitter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,117 @@
using System.Collections.Generic;
using MoonSharp.Interpreter;
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using UnityEngine;

public class Emitter
abstract public class Emitter : IXmlSerializable, IEmitter
{
public string id;

protected HashSet<IEmitterListener> listeners = new HashSet<IEmitterListener>();

protected Dictionary<string, Emitter.Action> actions = new Dictionary<string, Emitter.Action>();

public abstract string Category();

public Emitter() { }

public Emitter(Emitter other)
{
id = other.id;
actions = other.actions;
if(listeners.Count >= 1)
{
Debug.LogWarning("Copying Emitter with non-empty listeners, listeners will not be copied.");
}
}

public class Action
{
ActionType type;
string content;
Script script;
Closure closure;

public enum ActionType { FunctionName, Inline, None, Closure };


public Action(string type, string content)
{
this.content = content;
if(content == "")
{
Debug.LogWarning("Empty or Invalid Action field.");
this.type = ActionType.None;
return;
}
switch(type)
{
case "script":
this.type = ActionType.FunctionName;
break;
default:
this.type = ActionType.Inline;
script = new Script();
script.Options.DebugPrint += (string str) =>
{
Debug.Log("LUA inline: " + str);
};
script.DoString("action =" + content);
return;
}
}

public Action(Closure closure)
{
this.closure = closure;
this.type = ActionType.Closure;
}

public DynValue Call(Emitter emitter, object[] args)
{
switch (type)
{
case ActionType.FunctionName:
return LUA.ScriptLoader.Call(emitter.Category(), content, args);
case ActionType.Inline:
try
{
return script.Call(script.Globals["action"], args);
}
catch (ScriptRuntimeException e)
{
Debug.LogError("Script exception: " + e.DecoratedMessage);
return null;
}
catch (ArgumentException e)
{
Debug.LogError("Script exception while running call to action: " + e.Message);
return null;
}
case ActionType.Closure:
try
{
return closure.Call(args);
}
catch (ScriptRuntimeException e)
{
Debug.LogError("Script exception: " + e.DecoratedMessage);
return null;
}
catch (ArgumentException e)
{
Debug.LogError("Script exception while running call to action: " + e.Message);
return null;
}
default:
return null;
}
}
}

public void Connect(IEmitterListener listener)
{
listener.Emitter = this;
Expand All @@ -14,11 +122,71 @@ public void Disconnect(IEmitterListener listener)
listeners.Remove(listener);
}

protected void Emit(string signal, object[] args = null)
virtual public XmlSchema GetSchema()
{
return null;
}

public virtual void ReadElement(XmlReader reader)
{
switch (reader.Name)
{
case "Action":
string actionName = reader.GetAttribute("event");
string type = reader.GetAttribute("type");
string actionFunction = reader.ReadElementContentAsString();
AddAction(actionName, type, actionFunction);
break;
}
}

public enum ActionType
{
OnClick, OnUpdate, OnCreate
}

public void AddAction(string name, string type, string content)
{

if (name == null)
{
Debug.LogWarning(String.Format("Try to add empty Action with no-name to list, content ={0}.", content));
return;
}
Action action = new Action(type, content);
actions.Add(name, action);
AddAction((ActionType) Enum.Parse(typeof(ActionType), name), action);
}

public virtual void AddAction(ActionType actionType, Action action) { }

public virtual void AddAction(string eventName, MoonSharp.Interpreter.Closure closure)
{
Action action = new Action(closure);
actions.Add(eventName, action);
}

virtual public void ReadXml(XmlReader reader)
{
throw new NotImplementedException();
}

virtual public void WriteXml(XmlWriter writer)
{
throw new NotImplementedException();
}

public void Emit(string signal, object[] args = null)
{
if(actions.ContainsKey(signal))
{
actions[signal].Call(this, args);
}

foreach(IEmitterListener listener in listeners)
{
listener.Event(signal, args);
}
}

}
53 changes: 20 additions & 33 deletions Assets/Engine/Emitters/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
using UnityEngine;

[MoonSharpUserData]
public class Entity : Emitter, IXmlSerializable {

public string id;
public partial class Entity : Emitter, IXmlSerializable {

public World world;
private Vector2 position;

public struct SpriteInfo {
public string id;
public string type;
}
public static string category = "entitties";
public override string Category() { return category; }

public SpriteInfo spriteInfo = new SpriteInfo();

Expand Down Expand Up @@ -48,7 +45,7 @@ public Entity(Entity other)
this.spriteInfo = other.spriteInfo;
}

virtual public XmlSchema GetSchema()
override public XmlSchema GetSchema()
{
return null;
}
Expand All @@ -64,7 +61,7 @@ public void Spawn(World world, Vector2 position)
}
}

virtual public void ReadXml(XmlReader reader)
override public void ReadXml(XmlReader reader)
{
reader.Read();
Debug.Assert(reader.Name == "Entity",
Expand All @@ -76,34 +73,24 @@ virtual public void ReadXml(XmlReader reader)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Sprite":
//Debug.Log("Reading sprite information.");
string type = reader.GetAttribute("type");
if (type != null)
{
//Debug.Log("Reading sprite information.");
spriteInfo.type = type;
}
string spriteId = reader.GetAttribute("id");
if(spriteId == null)
{
Debug.LogWarning("No id for Sprite!");
} else
{
spriteInfo.id = spriteId;
}
Debug.Log(String.Format("Sprite of {0} has id {1} and type {2}", id, spriteInfo.id, spriteInfo.type));
break;
default:
break;
}
ReadElement(reader);
}
}
}

virtual public void WriteXml(XmlWriter writer)
public override void ReadElement(XmlReader reader)
{
switch (reader.Name)
{
case "Sprite":
spriteInfo = new SpriteInfo(reader);
break;
default:
break;
}
}

override public void WriteXml(XmlWriter writer)
{
throw new NotImplementedException();
}
Expand Down
8 changes: 5 additions & 3 deletions Assets/Engine/Emitters/Entities/EntityAnimated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ public void NextWaypoint()
theta = 0f;
}

public void Tic()
virtual public void Tic()
{


UpdatePosition();
}

Expand Down Expand Up @@ -95,8 +97,8 @@ public LinkedList<Tile> CreatePath(Vector2 startPosition, Vector2 endPosition)

LinkedList<Tile> list = Pathfinding.ReconstructPath(comeFrom, startTile, endTile);

Vector2 firstCoord = list.First.Value.coord.ToVector2();
Vector2 secondCoord = list.First.Next.Value.coord.ToVector2();
//Vector2 firstCoord = list.First.Value.coord.ToVector2();
//Vector2 secondCoord = list.First.Next.Value.coord.ToVector2();

// Remove first tile if is faster to go to the second tile
list.RemoveFirst();
Expand Down
58 changes: 32 additions & 26 deletions Assets/Engine/Emitters/Entities/EntityBuilding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
[MoonSharpUserData]
public class EntityBuilding : EntityInanimated, IXmlSerializable
{

public static new string category = "buildings";
public override string Category() { return category; }


bool installed = false;
bool roomBoundary = false;
bool connecting = false;
Expand Down Expand Up @@ -106,34 +111,35 @@ override public void ReadXml(XmlReader reader)
{
if(reader.NodeType == XmlNodeType.Element)
{
switch(reader.Name)
ReadElement(reader);
}
}
}

public override void ReadElement(XmlReader reader)
{
switch (reader.Name)
{
case "Pathfinding":
string walkingSpeedAttribute = reader.GetAttribute("walkingSpeed");
if (walkingSpeedAttribute != null)
{
case "EntityInanimate":
XmlReader subReader = reader.ReadSubtree();
base.ReadXml(subReader);
subReader.Close();
break;
case "Pathfinding":
string walkingSpeedAttribute = reader.GetAttribute("walkingSpeed");
if (walkingSpeedAttribute != null)
{
walkingSpeed = float.Parse(walkingSpeedAttribute);
}
string roomBoundaryAttribute = reader.GetAttribute("roomBoundary");
if (roomBoundaryAttribute != null)
{
roomBoundary = bool.Parse(roomBoundaryAttribute);
}
string connectingAttribute = reader.GetAttribute("connecting");
if (connectingAttribute != null)
{
connecting = bool.Parse(connectingAttribute);
}
break;
default:
break;
walkingSpeed = float.Parse(walkingSpeedAttribute);
}
}
string roomBoundaryAttribute = reader.GetAttribute("roomBoundary");
if (roomBoundaryAttribute != null)
{
roomBoundary = bool.Parse(roomBoundaryAttribute);
}
string connectingAttribute = reader.GetAttribute("connecting");
if (connectingAttribute != null)
{
connecting = bool.Parse(connectingAttribute);
}
break;
default:
base.ReadElement(reader);
break;
}
}

Expand Down
Loading

0 comments on commit c20c768

Please sign in to comment.