Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roopesh - Replay setup #9

Open
wants to merge 16 commits into
base: Replay-Setup
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ crashlytics-build.properties
# Temporary auto-generated Android Assets
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*
.idea/.idea.AGD_CommandPattern/.idea
*.meta
*.zip
3 changes: 2 additions & 1 deletion Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!224 &988327834
RectTransform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -2788,6 +2788,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
resultText: {fileID: 1682630446}
homeButton: {fileID: 799045267}
replayButton: {fileID: 988327835}
--- !u!1 &2089575972
GameObject:
m_ObjectHideFlags: 0
Expand Down
10 changes: 8 additions & 2 deletions Assets/Scripts/Battle/BattleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ public BattleService(List<BattleScriptableObject> battleScriptableObjects)
SubscribeToEvents();
}

private void SubscribeToEvents() => GameService.Instance.EventService.OnBattleSelected.AddListener(LoadBattle);

private void SubscribeToEvents()
{
GameService.Instance.EventService.OnBattleSelected.AddListener(LoadBattle);
GameService.Instance.EventService.OnReplayButtonClicked.AddListener(ReplayBattle);
}

private void LoadBattle(int battleId)
{
currentBattleId = battleId;
Expand All @@ -29,5 +33,7 @@ private void LoadBattle(int battleId)
}

private BattleScriptableObject GetBattleDataByID(int battleId) => battleScriptableObjects.Find(battleSO => battleSO.BattleID == battleId);

private void ReplayBattle() => LoadBattle(currentBattleId);
}
}
9 changes: 8 additions & 1 deletion Assets/Scripts/Command/CommandInvoker.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Command.Main;
using System.Collections.Generic;
using UnityEngine;

namespace Command.Commands
{
public class CommandInvoker
{
private Stack<ICommand> commandRegistry = new Stack<ICommand>();
private Stack<ICommand> commandRegistry = new();

public void ProcessCommand(ICommand commandToProcess)
{
Expand All @@ -26,5 +27,11 @@ public void Undo()
private bool RegistryEmpty() => commandRegistry.Count == 0;

private bool CommandBelongsToActivePlayer() => (commandRegistry.Peek() as UnitCommand).commandData.ActorPlayerID == GameService.Instance.PlayerService.ActivePlayerID;

public void SetReplayCommandStack()
{
GameService.Instance.ReplayService.SetCommandStack(commandRegistry);
commandRegistry.Clear();
}
}
}
2 changes: 2 additions & 0 deletions Assets/Scripts/Events/EventService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ public class EventService
{
public GameEventController<int> OnBattleSelected { get; private set; }
public GameEventController<CommandType> OnActionSelected { get; private set; }
public GameEventController OnReplayButtonClicked { get; private set; }

public EventService()
{
OnBattleSelected = new GameEventController<int>();
OnActionSelected = new GameEventController<CommandType>();
OnReplayButtonClicked = new GameEventController();
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Main/GameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Command.Actions;
using UnityEngine.UI;
using Command.Commands;
using Replay;

namespace Command.Main
{
Expand All @@ -30,6 +31,7 @@ public class GameService : GenericMonoSingleton<GameService>
public BattleService BattleService { get; private set; }
public PlayerService PlayerService { get; private set; }
public CommandInvoker CommandInvoker { get; private set; }
public ReplayService ReplayService { get; private set; }

[SerializeField] private UIService uiService;
public UIService UIService => uiService;
Expand All @@ -52,6 +54,7 @@ private void Start()
PlayerService = new PlayerService();
uiService.Init(battleScriptableObjects.Count);
CommandInvoker = new CommandInvoker();
ReplayService = new ReplayService();
}

private void Update() => InputService.UpdateInputService();
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Player/PlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public void PlayerDied(PlayerController deadPlayer)
winnerId = player1.PlayerID;

GameService.Instance.UIService.ShowBattleEndUI(winnerId);
GameService.Instance.CommandInvoker.SetReplayCommandStack();
}

public void ProcessUnitCommand(UnitCommand commandToProcess)
Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/Replay.meta

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

37 changes: 37 additions & 0 deletions Assets/Scripts/Replay/ReplayService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Command.Commands;
using Command.Main;
using UnityEngine;

namespace Replay
{
public class ReplayService
{
private const int waitTime = 1000;
private Stack<ICommand> replayCommandStack;

public ReplayState ReplayState { get; private set; }

public ReplayService() => SetReplayState(ReplayState.DEACTIVE);

public void SetReplayState(ReplayState newState) => ReplayState = newState;

public void SetCommandStack(Stack<ICommand> commandStack) =>
replayCommandStack = new Stack<ICommand>(commandStack);

public void ExecuteNext()
{
if (replayCommandStack.Count > 0)
{
WaitReplay();
}
}

private async void WaitReplay()
{
await Task.Delay(waitTime);
GameService.Instance.ProcessUnitCommand(replayCommandStack.Pop());
}
}
}
8 changes: 8 additions & 0 deletions Assets/Scripts/Replay/ReplayState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Replay
{
public enum ReplayState
{
ACTIVE,
DEACTIVE
}
}
9 changes: 9 additions & 0 deletions Assets/Scripts/UI/BattleEndUI/BattleEndUIController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Command.Input;
using Command.Main;
using Replay;
using UnityEngine.SceneManagement;

namespace Command.UI
Expand All @@ -20,5 +22,12 @@ public BattleEndUIController(BattleEndUIView battleEndView)
public void SetWinner(int winnerId) => battleEndView.SetResultText($"Player {winnerId} Won!");

public void OnHomeButtonClicked() => SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

public void OnReplayButtonClicked()
{
GameService.Instance.InputService.SetInputState(InputState.INACTIVE);
GameService.Instance.ReplayService.SetReplayState(ReplayState.ACTIVE);
GameService.Instance.EventService.OnReplayButtonClicked.InvokeEvent();
}
}
}
2 changes: 2 additions & 0 deletions Assets/Scripts/UI/BattleEndUI/BattleEndUIView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ public class BattleEndUIView : MonoBehaviour, IUIView
private BattleEndUIController controller;
[SerializeField] private TextMeshProUGUI resultText;
[SerializeField] private Button homeButton;
[SerializeField] private Button replayButton;

private void Start() => SubscribeToButtonClicks();

private void SubscribeToButtonClicks()
{
homeButton.onClick.AddListener(controller.OnHomeButtonClicked);
replayButton.onClick.AddListener(controller.OnReplayButtonClicked);
}

public void SetController(BattleEndUIController controllerToSet) => controller = controllerToSet;
Expand Down
22 changes: 19 additions & 3 deletions Assets/Scripts/UI/UIService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Command.Main;
using Command.Input;
using Command.Commands;
using Replay;

namespace Command.UI
{
Expand Down Expand Up @@ -34,8 +35,15 @@ private void Start()
battleEndController = new BattleEndUIController(battleEndView);
}

public void Init(int battleCount) => ShowBattleSelectionView(battleCount);
public void Init(int battleCount)
{
ShowBattleSelectionView(battleCount);
SubscribeToEvents();
}

private void SubscribeToEvents() =>
GameService.Instance.EventService.OnReplayButtonClicked.AddListener(HideBattleEndUI);

private void ShowBattleSelectionView(int battleCount) => battleSelectionController.Show(battleCount);

public void ShowGameplayView() => gameplayController.Show();
Expand All @@ -50,8 +58,16 @@ private void Start()

public void ShowActionSelectionView(List<CommandType> executableActions)
{
actionSelectionController.Show(executableActions);
GameService.Instance.InputService.SetInputState(InputState.SELECTING_ACTION);
switch (GameService.Instance.ReplayService.ReplayState)
{
case ReplayState.ACTIVE:
GameService.Instance.ReplayService.ExecuteNext();
break;
case ReplayState.DEACTIVE:
actionSelectionController.Show(executableActions);
GameService.Instance.InputService.SetInputState(InputState.SELECTING_ACTION);
break;
}
}

public void ShowBattleEndUI(int winnerId)
Expand Down
Loading