Skip to content

Commit

Permalink
FX
Browse files Browse the repository at this point in the history
  • Loading branch information
FilippoLeon committed Jul 9, 2017
1 parent c20c768 commit 38042a1
Show file tree
Hide file tree
Showing 33 changed files with 546 additions and 197 deletions.
77 changes: 77 additions & 0 deletions Assets/Engine/Emitters/Entities/Action.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using MoonSharp.Interpreter;
using System;
using System.ComponentModel;
using System.Xml.Serialization;
using UnityEngine;

public abstract partial class Emitter : IXmlSerializable, IEmitter, INotifyPropertyChanged
{
public class Action
{
ActionType type;
string name;
string content;
Closure closure;

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


public Action(Emitter emitter, string eventName, 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;
name = emitter.id + "_" + eventName;
//Debug.Log(String.Format("New inline action {0}", name));
LUA.ScriptLoader.DoString(emitter.Category(), name + " = " + 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:
return LUA.ScriptLoader.Call(emitter.Category(), name, args);
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;
}
}
}

}
12 changes: 12 additions & 0 deletions Assets/Engine/Emitters/Entities/Action.cs.meta

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

122 changes: 32 additions & 90 deletions Assets/Engine/Emitters/Entities/Emitter.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using MoonSharp.Interpreter;
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using UnityEngine;

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

Expand All @@ -28,90 +28,6 @@ public Emitter(Emitter other)
}
}

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 Down Expand Up @@ -142,7 +58,10 @@ public virtual void ReadElement(XmlReader reader)

public enum ActionType
{
OnClick, OnUpdate, OnCreate
OnClick, // world
OnUpdate, // UI, world
OnCreate, // UI, world
OnComplete // World world, object[] parameters
}

public void AddAction(string name, string type, string content)
Expand All @@ -153,7 +72,7 @@ public void AddAction(string name, string type, string content)
Debug.LogWarning(String.Format("Try to add empty Action with no-name to list, content ={0}.", content));
return;
}
Action action = new Action(type, content);
Action action = new Action(this, name, type, content);
actions.Add(name, action);
AddAction((ActionType) Enum.Parse(typeof(ActionType), name), action);
}
Expand Down Expand Up @@ -188,5 +107,28 @@ public void Emit(string signal, object[] args = null)
listener.Event(signal, args);
}
}


protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}

protected void SetPropertyField<T>(string propertyName, ref T field, T newValue)
{
if (!EqualityComparer<T>.Default.Equals(field, newValue))
{
field = newValue;
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

#endregion
}
2 changes: 1 addition & 1 deletion Assets/Engine/Emitters/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class Entity : Emitter, IXmlSerializable {
public World world;
private Vector2 position;

public static string category = "entitties";
public static string category = "entities";
public override string Category() { return category; }

public SpriteInfo spriteInfo = new SpriteInfo();
Expand Down
2 changes: 1 addition & 1 deletion Assets/Engine/Emitters/Entities/EntityBuilding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ override public void ReadXml(XmlReader reader)
);
if (id == null) id = reader.GetAttribute("id");

Debug.Log(string.Format("Reading 'EntityBuilding' from Xml, id = '{0}'", id));
//Debug.Log(string.Format("Reading 'EntityBuilding' from Xml, id = '{0}'", id));
while(reader.Read())
{
if(reader.NodeType == XmlNodeType.Element)
Expand Down
10 changes: 9 additions & 1 deletion Assets/Engine/Emitters/Entities/EntityHuman.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ public override void Tic()
if(currentJob == null)
{
FindJob();
} else
{
if(currentJob.CanProgress())
{
currentJob.Progress(this, world);
} else {

}
}

base.Tic();
}

private void FindJob()
{
//currentJob = world.jobs["building"].Dequeue();
currentJob = world.jobs["build"].Dequeue();
//throw new NotImplementedException();
}
}
2 changes: 1 addition & 1 deletion Assets/Engine/Emitters/Entities/SpriteInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public void ReadXml(XmlReader reader) {
this.id = spriteId;
}
string category = reader.GetAttribute("category");
Debug.Log(String.Format("Sprite of {0} has id {1} and type {2}", id, this.id, this.type));
//Debug.Log(String.Format("Sprite of {0} has id {1} and type {2}", id, this.id, this.type));
}
}
28 changes: 22 additions & 6 deletions Assets/Engine/Emitters/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,24 @@ public class World : Emitter {
public int sizeX, sizeY;

List<EntityAnimated> characters = new List<EntityAnimated>();

Emitter selected;
public Tile selectedTile;


public enum WorldMode { None, Build };
public WorldMode selectedMode = WorldMode.None;
public object[] modeArgs;

public void SetMode(WorldMode mode, object[] args)
{
this.selectedMode = mode;
this.modeArgs = args;
}

private WorldTime time = new WorldTime();
public WorldTime Time { get { return time; } set { time = value; } }

public bool Paused
{
get
Expand All @@ -31,6 +45,7 @@ public bool Paused
set
{
paused = value;
SppedChanged(speed);
}
}

Expand All @@ -40,8 +55,8 @@ public bool Paused

private int elapsedLong = 0;
private int elapsedLongLong = 0;
static private int ticLength = 4;
static private int longTicLength = ticLength * 60;
static private int ticLength = 1;
static private int longTicLength = ticLength * 20;
static private int longLongTicLength = longTicLength * 5;


Expand Down Expand Up @@ -248,12 +263,12 @@ public void ScheduleJob(Job prototype, object[] parameters)
jobs[instance.jobCategory] = new SimplePriorityQueue<Job>();
}
jobs[instance.jobCategory].Enqueue(instance, 1);
instance.Schedule(parameters);

foreach(IWorldListener listener in listeners)
{
listener.JobScheduled(this, instance);
}
instance.Schedule(parameters);
}

public Tile GetTileAt(Coord coord)
Expand All @@ -276,7 +291,8 @@ public Tile GetTileAtOrNull(Coord coord)

public void Tic()
{
Debug.Log("Tic");
//Debug.Log("Tic");
Time.Tic();
foreach(EntityAnimated entity in characters)
{
entity.Tic();
Expand All @@ -285,13 +301,13 @@ public void Tic()

public void TicLong()
{
Debug.Log("LongTic");
//Debug.Log("LongTic");

}

public void TicLongLong()
{
Debug.Log("LongLongTic");
//Debug.Log("LongLongTic");

}

Expand Down
Loading

0 comments on commit 38042a1

Please sign in to comment.