Browse Source

Refactored Game commands to use closures

master
chrisgregan 11 years ago
parent
commit
de532d5231
  1. 109
      Assets/Fungus/Scripts/Commands/GameCommands.cs

109
Assets/Fungus/Scripts/Commands/GameCommands.cs

@ -15,27 +15,25 @@ namespace Fungus
*/ */
public class Call : CommandQueue.Command public class Call : CommandQueue.Command
{ {
Action callAction; Action onExecute;
public Call(Action _callAction) public Call(Action callAction)
{ {
if (_callAction == null) if (callAction == null)
{ {
Debug.LogError("Action must not be null."); Debug.LogError("Action must not be null.");
return; return;
} }
callAction = _callAction; onExecute = callAction;
} }
public override void Execute(CommandQueue commandQueue, Action onComplete) public override void Execute(CommandQueue commandQueue, Action onComplete)
{ {
if (callAction != null) if (onExecute != null)
{ onExecute();
callAction();
}
// Execute next command if (onComplete != null)
onComplete(); onComplete();
} }
} }
@ -104,23 +102,18 @@ namespace Fungus
*/ */
public class MoveToRoom : CommandQueue.Command public class MoveToRoom : CommandQueue.Command
{ {
Room room; Action onExecute;
public MoveToRoom(Room _room) public MoveToRoom(Room room)
{ {
if (_room == null) if (room == null)
{ {
Debug.LogError("Room must not be null."); Debug.LogError("Room must not be null.");
return; return;
} }
room = _room; onExecute = delegate {
}
public override void Execute(CommandQueue commandQueue, Action onComplete)
{
Game game = Game.GetInstance(); Game game = Game.GetInstance();
game.waiting = true; game.waiting = true;
// Fade out screen // Fade out screen
@ -137,7 +130,15 @@ namespace Fungus
game.waiting = false; game.waiting = false;
}); });
}); });
// MoveToRoom always resets the command queue so no need to call onComplete };
}
public override void Execute(CommandQueue commandQueue, Action onComplete)
{
if (onExecute != null)
onExecute();
// This command resets the command queue so no need to call onComplete
} }
} }
@ -146,29 +147,33 @@ namespace Fungus
*/ */
public class MoveToScene : CommandQueue.Command public class MoveToScene : CommandQueue.Command
{ {
string sceneName; Action onExecute;
public MoveToScene(string _sceneName) public MoveToScene(string sceneName)
{ {
if (_sceneName == "") if (sceneName == "")
{ {
Debug.LogError("Scene name must not be empty"); Debug.LogError("Scene name must not be empty");
return; return;
} }
sceneName = _sceneName; onExecute = delegate {
}
public override void Execute(CommandQueue commandQueue, Action onComplete)
{
Game game = Game.GetInstance(); Game game = Game.GetInstance();
game.waiting = true; game.waiting = true;
// Fade out screen // Fade out screen
game.cameraController.Fade(0f, game.roomFadeDuration / 2f, delegate { game.cameraController.Fade(0f, game.roomFadeDuration / 2f, delegate {
Game.GetInstance().LoadScene(sceneName, true); Game.GetInstance().LoadScene(sceneName, true);
}); });
};
}
public override void Execute(CommandQueue commandQueue, Action onComplete)
{
if (onExecute != null)
onExecute();
// This command resets the command queue so no need to call onComplete
} }
} }
@ -177,97 +182,93 @@ namespace Fungus
*/ */
public class SetValue : CommandQueue.Command public class SetValue : CommandQueue.Command
{ {
string key; Action onExecute;
int value;
public SetValue(string _key, int _value) public SetValue(string key, int value)
{ {
key = _key; onExecute = delegate {
value = _value; Game.GetInstance().SetGameValue(key, value);
};
} }
public override void Execute(CommandQueue commandQueue, Action onComplete) public override void Execute(CommandQueue commandQueue, Action onComplete)
{ {
Game.GetInstance().SetGameValue(key, value); if (onExecute != null)
onExecute();
if (onComplete != null) if (onComplete != null)
{
onComplete(); onComplete();
} }
} }
}
/** /**
* Sets a globally accessible string value * Sets a globally accessible string value
*/ */
public class SetString : CommandQueue.Command public class SetString : CommandQueue.Command
{ {
string key; Action onExecute;
string value;
public SetString(string _key, string _value) public SetString(string key, string value)
{ {
key = _key; onExecute = delegate {
value = _value; Game.stringTable.SetString(key, value);
};
} }
public override void Execute(CommandQueue commandQueue, Action onComplete) public override void Execute(CommandQueue commandQueue, Action onComplete)
{ {
Game.stringTable.SetString(key, value); if (onExecute != null)
onExecute();
if (onComplete != null) if (onComplete != null)
{
onComplete(); onComplete();
} }
} }
}
/** /**
* Save the current game state to persistant storage. * Save the current game state to persistant storage.
*/ */
public class Save : CommandQueue.Command public class Save : CommandQueue.Command
{ {
Action commandAction; Action onExecute;
public Save(string saveName) public Save(string saveName)
{ {
commandAction = delegate { onExecute = delegate {
Game.GetInstance().SaveGame(saveName); Game.GetInstance().SaveGame(saveName);
}; };
} }
public override void Execute(CommandQueue commandQueue, Action onComplete) public override void Execute(CommandQueue commandQueue, Action onComplete)
{ {
commandAction(); onExecute();
if (onComplete != null) if (onComplete != null)
{
onComplete(); onComplete();
} }
} }
}
/** /**
* Load the game state from persistant storage. * Load the game state from persistant storage.
*/ */
public class Load : CommandQueue.Command public class Load : CommandQueue.Command
{ {
Action commandAction; Action onExecute;
public Load(string saveName) public Load(string saveName)
{ {
commandAction = delegate { onExecute = delegate {
Game.GetInstance().LoadGame(saveName); Game.GetInstance().LoadGame(saveName);
}; };
} }
public override void Execute(CommandQueue commandQueue, Action onComplete) public override void Execute(CommandQueue commandQueue, Action onComplete)
{ {
commandAction(); onExecute();
if (onComplete != null) if (onComplete != null)
{
onComplete(); onComplete();
} }
} }
} }
}
} }
Loading…
Cancel
Save