Skip to content

Commit 4466e91

Browse files
committed
Merge pull request #24 from Victormafire/GameEvent-collection-expansion
Event Changes
2 parents 108ff02 + be3b609 commit 4466e91

36 files changed

Lines changed: 2267 additions & 62 deletions

Editor.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Prefabs.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Resources.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Game/GameEvent.cs

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using UnityEngine;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
[System.Serializable]
5-
public class GameEvent : ScriptableObject{
6+
public class GameEvent : ScriptableObject, JSONAble{
67

78
void Awake(){
89
if (args == null || args.Count != keys.Count) {
@@ -33,11 +34,10 @@ public object getParameter(string param){
3334

3435
public void setParameter(string param, object content){
3536
param = param.ToLower();
36-
object c = content;
37-
if(c is System.ValueType || c is string){
38-
c = ScriptableObject.CreateInstance<IsoUnityBasicType>();
39-
((IsoUnityBasicType)c).Value = content;
40-
}
37+
object c = IsoUnityTypeFactory.Instance.getIsoUnityType(content);
38+
if (c == null)
39+
c = content;
40+
4141
if(args.ContainsKey(param)) args[param] = (Object)c;
4242
else args.Add(param, (Object)c);
4343

@@ -47,8 +47,14 @@ public void setParameter(string param, object content){
4747

4848
public void removeParameter(string param){
4949
param = param.ToLower();
50-
if(args.ContainsKey(param))
51-
args.Remove(param);
50+
if (args.ContainsKey(param))
51+
{
52+
UnityEngine.Object v = args[param];
53+
if (v is IsoUnityBasicType)
54+
IsoUnityTypeFactory.Instance.Destroy(v as IsoUnityBasicType);
55+
56+
args.Remove(param);
57+
}
5258

5359
this.keys = new List<string> (args.Keys);
5460
this.values = new List<Object> (args.Values);
@@ -106,5 +112,57 @@ public override int GetHashCode ()
106112
{
107113
return !(ge1 == ge2);
108114
}
115+
116+
117+
public JSONObject toJSONObject()
118+
{
119+
JSONObject json = new JSONObject();
120+
json.AddField("name", name);
121+
JSONObject parameters = new JSONObject();
122+
foreach (KeyValuePair<string, Object> entry in args)
123+
{
124+
if (entry.Value is JSONAble)
125+
{
126+
var jsonAble = entry.Value as JSONAble;
127+
parameters.AddField(entry.Key, JSONSerializer.Serialize(jsonAble));
128+
}
129+
else
130+
{
131+
parameters.AddField(entry.Key, entry.Value.GetInstanceID());
132+
}
133+
}
134+
135+
136+
json.AddField("parameters", parameters);
137+
return json;
138+
}
139+
140+
private static void destroyBasic(Dictionary<string, Object> args)
141+
{
142+
if (args == null || args.Count == 0)
143+
return;
144+
145+
foreach (KeyValuePair<string, Object> entry in args)
146+
if (entry.Value is IsoUnityBasicType)
147+
IsoUnityBasicType.DestroyImmediate(entry.Value);
148+
}
149+
150+
public void fromJSONObject(JSONObject json)
151+
{
152+
this.name = json["name"].ToString();
153+
154+
//Clean basic types
155+
destroyBasic(this.args);
156+
157+
this.args = new Dictionary<string, Object>();
158+
159+
JSONObject parameters = json["parameters"];
160+
foreach (string key in parameters.keys)
161+
{
162+
JSONObject param = parameters[key];
163+
JSONAble unserialized = JSONSerializer.UnSerialize(param);
164+
this.setParameter(key, unserialized);
165+
}
166+
}
109167
}
110168

Source/Game/IsoUnityBasicType.cs

Lines changed: 0 additions & 53 deletions
This file was deleted.

Source/Game/IsoUnityType.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public abstract class IsoUnityType : ScriptableObject, JSONAble
5+
{
6+
public abstract bool canHandle(object o);
7+
public abstract IsoUnityType clone();
8+
public abstract object Value
9+
{
10+
get;
11+
set;
12+
}
13+
public abstract JSONObject toJSONObject();
14+
15+
public abstract void fromJSONObject(JSONObject json);
16+
}

Source/Game/IsoUnityType.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Game/IsoUnityTypeFactory.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using UnityEngine;
2+
using System.Collections.Generic;
3+
4+
public abstract class IsoUnityTypeFactory
5+
{
6+
private static IsoUnityTypeFactory instance;
7+
public static IsoUnityTypeFactory Instance
8+
{
9+
get
10+
{
11+
if (instance == null)
12+
instance = new IsoUnityTypeFactoryImp();
13+
return instance;
14+
}
15+
}
16+
public abstract void Destroy(IsoUnityType i);
17+
public abstract IsoUnityType getIsoUnityType(object c);
18+
19+
private class IsoUnityTypeFactoryImp : IsoUnityTypeFactory
20+
{
21+
private List<IsoUnityType> types;
22+
23+
public IsoUnityTypeFactoryImp()
24+
{
25+
types = new List<IsoUnityType>();
26+
types.Add(ScriptableObject.CreateInstance<IsoUnityBasicType>());
27+
types.Add(ScriptableObject.CreateInstance<IsoUnityCollectionType>());
28+
}
29+
30+
public override void Destroy(IsoUnityType i)
31+
{
32+
IsoUnityType.DestroyImmediate(i);
33+
}
34+
35+
public override IsoUnityType getIsoUnityType(object c)
36+
{
37+
IsoUnityType r = null;
38+
foreach (IsoUnityType t in types)
39+
{
40+
if (t.canHandle(c))
41+
{
42+
r = t.clone();
43+
r.Value = c;
44+
break;
45+
}
46+
}
47+
return r;
48+
}
49+
}
50+
51+
}

0 commit comments

Comments
 (0)