Skip to content

Commit

Permalink
Save/Load room behaviors (#1605)
Browse files Browse the repository at this point in the history
  • Loading branch information
koosemose authored and bjubes committed Nov 8, 2016
1 parent 76e489c commit c4f9a68
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Assets/Scripts/Models/Area/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public string[] GetGasNames()
return atmosphericGasses.Keys.ToArray();
}

public object ToJson()
public JObject ToJson()
{
JObject roomGasses = new JObject();
foreach (string k in atmosphericGasses.Keys)
Expand Down
10 changes: 10 additions & 0 deletions Assets/Scripts/Models/Area/RoomBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
// file LICENSE, which is part of this source code package, for details.
// ====================================================
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Interop;
using Newtonsoft.Json.Linq;

namespace ProjectPorcupine.Rooms
{
Expand Down Expand Up @@ -402,6 +404,14 @@ public void Control(Room room)
EventActions.Trigger("OnControl", this);
}

public JObject ToJson()
{
JObject behaviorJson = new JObject();
behaviorJson.Add("Room", Room.ID);
behaviorJson.Add("Behavior", Type);
return behaviorJson;
}

[MoonSharpVisible(true)]
private void CallEventAction(string actionName, params object[] parameters)
{
Expand Down
24 changes: 24 additions & 0 deletions Assets/Scripts/Models/Area/RoomManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
// file LICENSE, which is part of this source code package, for details.
// ====================================================
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;

namespace ProjectPorcupine.Rooms
Expand Down Expand Up @@ -429,6 +431,28 @@ public void FromJson(JToken roomsToken)
}
}

public JToken BehaviorsToJson()
{
JArray behaviorsJson = new JArray();
foreach (RoomBehavior behavior in rooms.SelectMany(room => room.RoomBehaviors.Values))
{
behaviorsJson.Add(behavior.ToJson());
}

return behaviorsJson;
}

public void BehaviorsFromJson(JToken behaviorsToken)
{
foreach (JToken behaviorToken in behaviorsToken)
{
int roomId = (int)behaviorToken["Room"];
string type = (string)behaviorToken["Behavior"];
RoomBehavior behavior = PrototypeManager.RoomBehavior.Get(type);
this[roomId].DesignateRoomBehavior(behavior.Clone());
}
}

protected Room ActualFloodFill(Tile sourceTile, Room oldRoom, int sizeOfOldRoom)
{
if (sourceTile == null)
Expand Down
2 changes: 2 additions & 0 deletions Assets/Scripts/Models/Area/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ public JObject ToJson()
worldJson.Add("Inventories", InventoryManager.ToJson());
worldJson.Add("Furnitures", FurnitureManager.ToJson());
worldJson.Add("Utilities", UtilityManager.ToJson());
worldJson.Add("RoomBehaviors", RoomManager.BehaviorsToJson());
worldJson.Add("Characters", CharacterManager.ToJson());
worldJson.Add("CameraData", CameraData.ToJson());
worldJson.Add("Skybox", skybox.name);
Expand All @@ -321,6 +322,7 @@ public void ReadJson(string filename)
InventoryManager.FromJson(worldJson["Inventories"]);
FurnitureManager.FromJson(worldJson["Furnitures"]);
UtilityManager.FromJson(worldJson["Utilities"]);
RoomManager.BehaviorsFromJson(worldJson["RoomBehaviors"]);
CharacterManager.FromJson(worldJson["Characters"]);
CameraData.FromJson(worldJson["CameraData"]);
LoadSkybox((string)worldJson["Skybox"]);
Expand Down

0 comments on commit c4f9a68

Please sign in to comment.