Browse Source

Added Game Loaded event handler and save key.

master
Christopher 8 years ago
parent
commit
71c2b4e357
  1. 19
      Assets/Fungus/Scripts/Commands/SavePoint.cs
  2. 1
      Assets/Fungus/Scripts/Components/Flowchart.cs
  3. 30
      Assets/Fungus/Scripts/Components/SaveManager.cs
  4. 44
      Assets/Fungus/Scripts/Editor/SavePointEditor.cs
  5. 32
      Assets/Fungus/Scripts/EventHandlers/GameLoaded.cs
  6. 4
      Assets/Fungus/Scripts/EventHandlers/GameLoaded.cs.meta
  7. 2
      Assets/Fungus/Scripts/Utils/SavePointData.cs
  8. 179
      Assets/FungusExamples/SaveGame/SceneA.unity

19
Assets/Fungus/Scripts/Commands/SavePoint.cs

@ -12,7 +12,7 @@ namespace Fungus
"Creates a save point which can be saved to persistant storage and loaded again later.")] "Creates a save point which can be saved to persistant storage and loaded again later.")]
public class SavePoint : Command public class SavePoint : Command
{ {
[SerializeField] protected Block resumeBlock; [SerializeField] protected string saveKey;
[SerializeField] protected bool saveNow; [SerializeField] protected bool saveNow;
@ -22,7 +22,7 @@ namespace Fungus
{ {
var saveManager = FungusManager.Instance.SaveManager; var saveManager = FungusManager.Instance.SaveManager;
saveManager.PopulateSaveBuffer(GetFlowchart(), resumeBlock.BlockName); saveManager.PopulateSaveBuffer(GetFlowchart(), saveKey);
if (saveNow) if (saveNow)
{ {
@ -34,12 +34,7 @@ namespace Fungus
public override string GetSummary() public override string GetSummary()
{ {
if (resumeBlock == null) return saveKey;
{
return "Error: No resume block set";
}
return resumeBlock.BlockName;
} }
public override Color GetButtonColor() public override Color GetButtonColor()
@ -47,14 +42,6 @@ namespace Fungus
return new Color32(235, 191, 217, 255); return new Color32(235, 191, 217, 255);
} }
public override void GetConnectedBlocks(ref List<Block> connectedBlocks)
{
if (resumeBlock != null)
{
connectedBlocks.Add(resumeBlock);
}
}
#endregion #endregion
} }
} }

1
Assets/Fungus/Scripts/Components/Flowchart.cs

@ -3,7 +3,6 @@
using UnityEngine; using UnityEngine;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using System; using System;
using System.Text; using System.Text;
using System.Linq; using System.Linq;

30
Assets/Fungus/Scripts/Components/SaveManager.cs

@ -55,14 +55,14 @@ namespace Fungus
return true; return true;
} }
protected virtual string CreateSaveData(Flowchart flowchart, string resumeBlockName) protected virtual string CreateSaveData(Flowchart flowchart, string saveKey)
{ {
var saveData = new SavePointData(); var saveData = new SavePointData();
// Store the scene, flowchart and block to execute on resume // Store the scene, flowchart and block to execute on resume
saveData.sceneName = SceneManager.GetActiveScene().name; saveData.sceneName = SceneManager.GetActiveScene().name;
saveData.flowchartName = flowchart.name; saveData.flowchartName = flowchart.name;
saveData.resumeBlockName = resumeBlockName; saveData.saveKey = saveKey;
for (int i = 0; i < flowchart.Variables.Count; i++) for (int i = 0; i < flowchart.Variables.Count; i++)
{ {
@ -147,7 +147,27 @@ namespace Fungus
flowchart.SetStringVariable(stringVar.key, stringVar.value); flowchart.SetStringVariable(stringVar.key, stringVar.value);
} }
flowchart.ExecuteBlock(saveData.resumeBlockName); // Fire any matching GameLoaded event handler with matching save key.
var eventHandlers = Object.FindObjectsOfType<GameLoaded>();
for (int i = 0; i < eventHandlers.Length; i++)
{
var eventHandler = eventHandlers[i];
eventHandler.OnGameLoaded(saveData.saveKey);
}
// Execute any block with a Label matching the save key
var labels = Object.FindObjectsOfType<Label>();
for (int i = 0; i < labels.Length; i++)
{
var label = labels[i];
if (string.Compare(label.Key, saveData.saveKey) == 0)
{
int index = label.CommandIndex;
var block = label.ParentBlock;
var fc = label.GetFlowchart();
fc.ExecuteBlock(block, index + 1);
}
}
} }
protected virtual void StoreJSONData(string key, string jsonData) protected virtual void StoreJSONData(string key, string jsonData)
@ -233,9 +253,9 @@ namespace Fungus
PlayerPrefs.DeleteKey(key); PlayerPrefs.DeleteKey(key);
} }
public virtual void PopulateSaveBuffer(Flowchart flowchart, string resumeBlockName) public virtual void PopulateSaveBuffer(Flowchart flowchart, string saveKey)
{ {
saveBuffer = CreateSaveData(flowchart, resumeBlockName); saveBuffer = CreateSaveData(flowchart, saveKey);
} }
#endregion #endregion

44
Assets/Fungus/Scripts/Editor/SavePointEditor.cs

@ -1,44 +0,0 @@
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
using UnityEditor;
using UnityEngine;
using Fungus;
namespace Fungus.EditorUtils
{
[CustomEditor(typeof(SavePoint))]
public class SavePointEditor : CommandEditor
{
protected SerializedProperty resumeBlockProp;
protected SerializedProperty saveNowProp;
protected virtual void OnEnable()
{
if (NullTargetCheck()) // Check for an orphaned editor instance
return;
resumeBlockProp = serializedObject.FindProperty("resumeBlock");
saveNowProp = serializedObject.FindProperty("saveNow");
}
public override void DrawCommandGUI()
{
var flowchart = FlowchartWindow.GetFlowchart();
if (flowchart == null)
{
return;
}
serializedObject.Update();
BlockEditor.BlockField(resumeBlockProp,
new GUIContent("Resume Block", "Block to call when save data is loaded again"),
new GUIContent("<None>"),
flowchart);
EditorGUILayout.PropertyField(saveNowProp);
serializedObject.ApplyModifiedProperties();
}
}
}

32
Assets/Fungus/Scripts/EventHandlers/GameLoaded.cs

@ -0,0 +1,32 @@
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
using UnityEngine;
using System.Collections.Generic;
namespace Fungus
{
[EventHandlerInfo("",
"Game Loaded",
"Execute this block when a saved game is loaded.")]
public class GameLoaded : EventHandler
{
[Tooltip("Block will execute if the Save Key of the loaded data matches an entry in the Save Keys list.")]
[SerializeField] protected List<string> saveKeys = new List<string>();
#region Public methods
/// <summary>
/// Called when a saved game is loaded.
/// </summary>
public void OnGameLoaded(string saveKey)
{
if (saveKeys.Contains(saveKey))
{
ExecuteBlock();
}
}
#endregion
}
}

4
Assets/Fungus/Scripts/Editor/SavePointEditor.cs.meta → Assets/Fungus/Scripts/EventHandlers/GameLoaded.cs.meta

@ -1,6 +1,6 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 84d5f60f59d254712915dd59d345e0c6 guid: 9ca33a026786245d4b10323b87e84d5f
timeCreated: 1478701789 timeCreated: 1479142351
licenseType: Free licenseType: Free
MonoImporter: MonoImporter:
serializedVersion: 2 serializedVersion: 2

2
Assets/Fungus/Scripts/Utils/SavePointData.cs

@ -37,7 +37,7 @@ namespace Fungus
{ {
public string sceneName; public string sceneName;
public string flowchartName; public string flowchartName;
public string resumeBlockName; public string saveKey;
public List<StringVar> stringVars = new List<StringVar>(); public List<StringVar> stringVars = new List<StringVar>();
public List<IntVar> intVars = new List<IntVar>(); public List<IntVar> intVars = new List<IntVar>();

179
Assets/FungusExamples/SaveGame/SceneA.unity

@ -104,8 +104,15 @@ GameObject:
- 114: {fileID: 27697868} - 114: {fileID: 27697868}
- 114: {fileID: 27697873} - 114: {fileID: 27697873}
- 114: {fileID: 27697874} - 114: {fileID: 27697874}
- 114: {fileID: 27697875}
- 114: {fileID: 27697876} - 114: {fileID: 27697876}
- 114: {fileID: 27697877}
- 114: {fileID: 27697880}
- 114: {fileID: 27697879}
- 114: {fileID: 27697878}
- 114: {fileID: 27697883}
- 114: {fileID: 27697882}
- 114: {fileID: 27697881}
- 114: {fileID: 27697875}
m_Layer: 0 m_Layer: 0
m_Name: Flowchart m_Name: Flowchart
m_TagString: Untagged m_TagString: Untagged
@ -126,7 +133,7 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemId: 1 itemId: 1
indentLevel: 0 indentLevel: 0
storyText: This is the start of the game. storyText: Step 0 happened
description: description:
character: {fileID: 0} character: {fileID: 0}
portrait: {fileID: 0} portrait: {fileID: 0}
@ -180,6 +187,7 @@ MonoBehaviour:
- {fileID: 27697868} - {fileID: 27697868}
- {fileID: 27697874} - {fileID: 27697874}
- {fileID: 27697876} - {fileID: 27697876}
- {fileID: 27697877}
--- !u!114 &27697871 --- !u!114 &27697871
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -201,12 +209,13 @@ MonoBehaviour:
scrollViewRect: scrollViewRect:
serializedVersion: 2 serializedVersion: 2
x: -350 x: -350
y: -350 y: -362
width: 1121 width: 1328
height: 869 height: 909
selectedBlocks: selectedBlocks:
- {fileID: 27697870} - {fileID: 27697883}
selectedCommands: [] selectedCommands:
- {fileID: 27697875}
variables: variables:
- {fileID: 27697873} - {fileID: 27697873}
description: description:
@ -281,23 +290,114 @@ MonoBehaviour:
m_GameObject: {fileID: 27697867} m_GameObject: {fileID: 27697867}
m_Enabled: 1 m_Enabled: 1
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2fab8abf0343545abbfebd9a7b7b34bd, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 12
indentLevel: 0
logType: 0
logMessage:
stringRef: {fileID: 0}
stringVal: Else
--- !u!114 &27697876
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 27697867}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0b115619cb83b4d6ab8047d0e9407403, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 5
indentLevel: 0
saveKey: save_1
saveNow: 0
--- !u!114 &27697877
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 27697867}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ec422cd568a9c4a31ad7c36d0572b9da, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 6
indentLevel: 0
storyText: 'Step 1 happened
'
description:
character: {fileID: 0}
portrait: {fileID: 0}
voiceOverClip: {fileID: 0}
showAlways: 1
showCount: 1
extendPrevious: 0
fadeWhenDone: 1
waitForClick: 1
stopVoiceover: 1
setSayDialog: {fileID: 0}
--- !u!114 &27697878
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 27697867}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2fab8abf0343545abbfebd9a7b7b34bd, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 8
indentLevel: 0
logType: 0
logMessage:
stringRef: {fileID: 0}
stringVal: Loaded
--- !u!114 &27697879
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 27697867}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9ca33a026786245d4b10323b87e84d5f, type: 3}
m_Name:
m_EditorClassIdentifier:
parentBlock: {fileID: 27697880}
saveKeys:
- save_1
--- !u!114 &27697880
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 27697867}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3d3d73aef2cfc4f51abf34ac00241f60, type: 3} m_Script: {fileID: 11500000, guid: 3d3d73aef2cfc4f51abf34ac00241f60, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
nodeRect: nodeRect:
serializedVersion: 2 serializedVersion: 2
x: 246 x: 246
y: 69 y: 68
width: 120 width: 120
height: 40 height: 40
tint: {r: 1, g: 1, b: 1, a: 1} tint: {r: 1, g: 1, b: 1, a: 1}
useCustomTint: 0 useCustomTint: 0
itemId: 4 itemId: 7
blockName: New Block blockName: GameLoaded
description: description:
eventHandler: {fileID: 0} eventHandler: {fileID: 27697879}
commandList: [] commandList:
--- !u!114 &27697876 - {fileID: 27697878}
--- !u!114 &27697881
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0} m_PrefabParentObject: {fileID: 0}
@ -305,13 +405,58 @@ MonoBehaviour:
m_GameObject: {fileID: 27697867} m_GameObject: {fileID: 27697867}
m_Enabled: 1 m_Enabled: 1
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0b115619cb83b4d6ab8047d0e9407403, type: 3} m_Script: {fileID: 11500000, guid: ea4da378c47144a86979765ac5e9690c, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemId: 5 itemId: 11
indentLevel: 0 indentLevel: 0
resumeBlock: {fileID: 27697875} key: save_1
saveNow: 0 --- !u!114 &27697882
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 27697867}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2fab8abf0343545abbfebd9a7b7b34bd, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 10
indentLevel: 0
logType: 0
logMessage:
stringRef: {fileID: 0}
stringVal: 'Something
'
--- !u!114 &27697883
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 27697867}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3d3d73aef2cfc4f51abf34ac00241f60, type: 3}
m_Name:
m_EditorClassIdentifier:
nodeRect:
serializedVersion: 2
x: 419
y: 66
width: 120
height: 40
tint: {r: 1, g: 1, b: 1, a: 1}
useCustomTint: 0
itemId: 9
blockName: New Block
description:
eventHandler: {fileID: 0}
commandList:
- {fileID: 27697882}
- {fileID: 27697881}
- {fileID: 27697875}
--- !u!1 &132834842 --- !u!1 &132834842
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

Loading…
Cancel
Save