From deb63d21b7598686979c75a5fdc9b27668d9a942 Mon Sep 17 00:00:00 2001 From: mridul-outscal Date: Tue, 11 Jul 2023 18:33:07 +0530 Subject: [PATCH 01/12] Created Abstract Commands and Command Invoker --- Assets/Scripts/Command.meta | 8 ++++++++ Assets/Scripts/Command/Abstract Commands.meta | 8 ++++++++ .../Command/Abstract Commands/ICommand.cs | 7 +++++++ .../Abstract Commands/ICommand.cs.meta | 11 ++++++++++ .../Command/Abstract Commands/IUnitCommand.cs | 19 ++++++++++++++++++ .../Abstract Commands/IUnitCommand.cs.meta | 11 ++++++++++ Assets/Scripts/Command/CommandInvoker.cs | 20 +++++++++++++++++++ Assets/Scripts/Command/CommandInvoker.cs.meta | 11 ++++++++++ Assets/Scripts/Main/GameService.cs | 3 +++ 9 files changed, 98 insertions(+) create mode 100644 Assets/Scripts/Command.meta create mode 100644 Assets/Scripts/Command/Abstract Commands.meta create mode 100644 Assets/Scripts/Command/Abstract Commands/ICommand.cs create mode 100644 Assets/Scripts/Command/Abstract Commands/ICommand.cs.meta create mode 100644 Assets/Scripts/Command/Abstract Commands/IUnitCommand.cs create mode 100644 Assets/Scripts/Command/Abstract Commands/IUnitCommand.cs.meta create mode 100644 Assets/Scripts/Command/CommandInvoker.cs create mode 100644 Assets/Scripts/Command/CommandInvoker.cs.meta diff --git a/Assets/Scripts/Command.meta b/Assets/Scripts/Command.meta new file mode 100644 index 00000000..1e9e808d --- /dev/null +++ b/Assets/Scripts/Command.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 057c0ab2ac9ca0e4fa171bc5a7b7c2dd +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Command/Abstract Commands.meta b/Assets/Scripts/Command/Abstract Commands.meta new file mode 100644 index 00000000..69cc3bee --- /dev/null +++ b/Assets/Scripts/Command/Abstract Commands.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6bd3bf9b13445164c8eae27ef19b4ca3 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Command/Abstract Commands/ICommand.cs b/Assets/Scripts/Command/Abstract Commands/ICommand.cs new file mode 100644 index 00000000..ed8255ca --- /dev/null +++ b/Assets/Scripts/Command/Abstract Commands/ICommand.cs @@ -0,0 +1,7 @@ +namespace Command.Commands +{ + public interface ICommand + { + public void Execute(); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Command/Abstract Commands/ICommand.cs.meta b/Assets/Scripts/Command/Abstract Commands/ICommand.cs.meta new file mode 100644 index 00000000..16fb8f1e --- /dev/null +++ b/Assets/Scripts/Command/Abstract Commands/ICommand.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1694b2a5c5fb77e4b8c12b63c23898ca +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Command/Abstract Commands/IUnitCommand.cs b/Assets/Scripts/Command/Abstract Commands/IUnitCommand.cs new file mode 100644 index 00000000..58bfa664 --- /dev/null +++ b/Assets/Scripts/Command/Abstract Commands/IUnitCommand.cs @@ -0,0 +1,19 @@ +using Command.Player; + +namespace Command.Commands +{ + public abstract class UnitCommand : ICommand + { + public int ActorUnitID; + public int TargetUnitID; + public int ActorPlayerID; + public int TargetPlayerID; + + protected UnitController actorUnit; + protected UnitController targetUnit; + + public abstract void Execute(); + + public abstract bool WillHitTarget(); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Command/Abstract Commands/IUnitCommand.cs.meta b/Assets/Scripts/Command/Abstract Commands/IUnitCommand.cs.meta new file mode 100644 index 00000000..c015f0d6 --- /dev/null +++ b/Assets/Scripts/Command/Abstract Commands/IUnitCommand.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 90e5347a76c8bba418c5b957b9ccc682 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Command/CommandInvoker.cs b/Assets/Scripts/Command/CommandInvoker.cs new file mode 100644 index 00000000..4212eef1 --- /dev/null +++ b/Assets/Scripts/Command/CommandInvoker.cs @@ -0,0 +1,20 @@ +using Command.Main; +using System.Collections.Generic; + +namespace Command.Commands +{ + public class CommandInvoker + { + private Stack commandRegistry = new Stack(); + + public void ProcessCommand(ICommand commandToProcess) + { + ExecuteCommand(commandToProcess); + RegisterCommand(commandToProcess); + } + + public void ExecuteCommand(ICommand commandToExecute) => commandToExecute.Execute(); + + public void RegisterCommand(ICommand commandToRegister) => commandRegistry.Push(commandToRegister); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Command/CommandInvoker.cs.meta b/Assets/Scripts/Command/CommandInvoker.cs.meta new file mode 100644 index 00000000..afeecb98 --- /dev/null +++ b/Assets/Scripts/Command/CommandInvoker.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ef2285a6c836f4345b10e73a227cde34 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Main/GameService.cs b/Assets/Scripts/Main/GameService.cs index 83a6af27..0a8ac96c 100644 --- a/Assets/Scripts/Main/GameService.cs +++ b/Assets/Scripts/Main/GameService.cs @@ -9,6 +9,7 @@ using Command.Battle; using Command.Actions; using UnityEngine.UI; +using Command.Commands; namespace Command.Main { @@ -21,6 +22,7 @@ public class GameService : GenericMonoSingleton public InputService InputService { get; private set; } public BattleService BattleService { get; private set; } public PlayerService PlayerService { get; private set; } + public CommandInvoker CommandInvoker { get; private set; } [SerializeField] private UIService uiService; public UIService UIService => uiService; @@ -43,6 +45,7 @@ private void Start() BattleService = new BattleService(battleScriptableObjects, backgroundImage); PlayerService = new PlayerService(); uiService.Init(battleScriptableObjects.Count); + CommandInvoker = new CommandInvoker(); } private void Update() => InputService.UpdateInputService(); From c99e2c4bc31cbb313d2965f32b74014b045d2127 Mon Sep 17 00:00:00 2001 From: mridul-outscal Date: Tue, 11 Jul 2023 19:29:09 +0530 Subject: [PATCH 02/12] Implemented Commands for encapsulating Attack & Heal Actions and used those commands in the game. --- Assets/Scripts/Action/ActionService.cs | 23 ++++++------- Assets/Scripts/Action/Actions/AttackAction.cs | 9 +++--- .../Action/Actions/AttackStanceAction.cs | 8 ++--- .../Action/Actions/BerserkAttackAction.cs | 10 +++--- .../Scripts/Action/Actions/CleanseAction.cs | 10 +++--- Assets/Scripts/Action/Actions/HealAction.cs | 9 +++--- .../Scripts/Action/Actions/MeditateAction.cs | 9 +++--- .../Scripts/Action/Actions/ThirdEyeAction.cs | 9 +++--- Assets/Scripts/Action/IAction.cs | 4 +-- .../{IUnitCommand.cs => UnitCommand.cs} | 4 +++ ...nitCommand.cs.meta => UnitCommand.cs.meta} | 0 .../ActionType.cs => Command/CommandType.cs} | 4 +-- .../CommandType.cs.meta} | 0 Assets/Scripts/Command/ConcreteCommands.meta | 8 +++++ .../Command/ConcreteCommands/AttackCommand.cs | 23 +++++++++++++ .../ConcreteCommands/AttackCommand.cs.meta | 11 +++++++ .../Command/ConcreteCommands/HealCommand.cs | 23 +++++++++++++ .../ConcreteCommands/HealCommand.cs.meta | 11 +++++++ Assets/Scripts/Events/EventService.cs | 6 ++-- Assets/Scripts/Input/InputService.cs | 32 +++++++++++++++---- Assets/Scripts/Main/GameService.cs | 2 ++ Assets/Scripts/Player/PlayerController.cs | 3 ++ Assets/Scripts/Player/PlayerService.cs | 18 +++++++++-- Assets/Scripts/Player/Unit/UnitController.cs | 6 ++-- .../Player/Unit/UnitScriptableObject.cs | 4 +-- .../UI/ActionSelectionUI/ActionButtonView.cs | 6 ++-- .../ActionSelectionUIController.cs | 10 +++--- Assets/Scripts/UI/UIService.cs | 4 +-- 28 files changed, 188 insertions(+), 78 deletions(-) rename Assets/Scripts/Command/Abstract Commands/{IUnitCommand.cs => UnitCommand.cs} (69%) rename Assets/Scripts/Command/Abstract Commands/{IUnitCommand.cs.meta => UnitCommand.cs.meta} (100%) rename Assets/Scripts/{Action/ActionType.cs => Command/CommandType.cs} (72%) rename Assets/Scripts/{Action/ActionType.cs.meta => Command/CommandType.cs.meta} (100%) create mode 100644 Assets/Scripts/Command/ConcreteCommands.meta create mode 100644 Assets/Scripts/Command/ConcreteCommands/AttackCommand.cs create mode 100644 Assets/Scripts/Command/ConcreteCommands/AttackCommand.cs.meta create mode 100644 Assets/Scripts/Command/ConcreteCommands/HealCommand.cs create mode 100644 Assets/Scripts/Command/ConcreteCommands/HealCommand.cs.meta diff --git a/Assets/Scripts/Action/ActionService.cs b/Assets/Scripts/Action/ActionService.cs index 957c65c0..a3b546ec 100644 --- a/Assets/Scripts/Action/ActionService.cs +++ b/Assets/Scripts/Action/ActionService.cs @@ -1,27 +1,28 @@ using Command.Input; +using Command.Commands; using System.Collections.Generic; namespace Command.Actions { public class ActionService { - private Dictionary actions; + private Dictionary actions; public ActionService() => CreateActions(); private void CreateActions() { - actions = new Dictionary(); - actions.Add(ActionType.Attack, new AttackAction()); - actions.Add(ActionType.Heal, new HealAction()); - actions.Add(ActionType.AttackStance, new AttackStanceAction()); - actions.Add(ActionType.Cleanse, new CleanseAction()); - actions.Add(ActionType.Meditate, new MeditateAction()); - actions.Add(ActionType.BerserkAttack, new BerserkAttackAction()); - actions.Add(ActionType.ThirdEye, new ThirdEyeAction()); + actions = new Dictionary(); + actions.Add(CommandType.Attack, new AttackAction()); + actions.Add(CommandType.Heal, new HealAction()); + actions.Add(CommandType.AttackStance, new AttackStanceAction()); + actions.Add(CommandType.Cleanse, new CleanseAction()); + actions.Add(CommandType.Meditate, new MeditateAction()); + actions.Add(CommandType.BerserkAttack, new BerserkAttackAction()); + actions.Add(CommandType.ThirdEye, new ThirdEyeAction()); } - public IAction GetActionByType(ActionType type) + public IAction GetActionByType(CommandType type) { if (actions.ContainsKey(type)) return actions[type]; @@ -29,6 +30,6 @@ public IAction GetActionByType(ActionType type) throw new System.Exception($"No Action found for the type {type} in the dictionary"); } - public TargetType GetTargetTypeForAction(ActionType actionType) => actions[actionType].TargetType; + public TargetType GetTargetTypeForAction(CommandType actionType) => actions[actionType].TargetType; } } \ No newline at end of file diff --git a/Assets/Scripts/Action/Actions/AttackAction.cs b/Assets/Scripts/Action/Actions/AttackAction.cs index 71791ea6..8e059430 100644 --- a/Assets/Scripts/Action/Actions/AttackAction.cs +++ b/Assets/Scripts/Action/Actions/AttackAction.cs @@ -1,3 +1,4 @@ +using Command.Commands; using Command.Input; using Command.Main; using Command.Player; @@ -8,15 +9,13 @@ public class AttackAction : IAction { public TargetType TargetType => TargetType.Enemy; - public void PerformAction(UnitController actorUnit, UnitController targetUnit) + public void PerformAction(UnitController actorUnit, UnitController targetUnit, bool successful) { - actorUnit.PlayActionAnimation(ActionType.Attack); - if (IsSuccessful()) + actorUnit.PlayActionAnimation(CommandType.Attack); + if (successful) targetUnit.TakeDamage(actorUnit.CurrentPower); else GameService.Instance.UIService.ActionMissed(); } - - public bool IsSuccessful() => true; } } \ No newline at end of file diff --git a/Assets/Scripts/Action/Actions/AttackStanceAction.cs b/Assets/Scripts/Action/Actions/AttackStanceAction.cs index 1231b76c..de598d6f 100644 --- a/Assets/Scripts/Action/Actions/AttackStanceAction.cs +++ b/Assets/Scripts/Action/Actions/AttackStanceAction.cs @@ -1,6 +1,7 @@ using Command.Player; using Command.Input; using Command.Main; +using Command.Commands; namespace Command.Actions { @@ -8,14 +9,13 @@ public class AttackStanceAction : IAction { TargetType IAction.TargetType { get => TargetType.Self; } - public void PerformAction(UnitController actorUnit, UnitController targetUnit) + public void PerformAction(UnitController actorUnit, UnitController targetUnit, bool successful) { - actorUnit.PlayActionAnimation(ActionType.AttackStance); - if(IsSuccessful()) + actorUnit.PlayActionAnimation(CommandType.AttackStance); + if(successful) targetUnit.CurrentPower += (int)(targetUnit.CurrentPower * 0.2f); else GameService.Instance.UIService.ActionMissed(); } - public bool IsSuccessful() => true; } } \ No newline at end of file diff --git a/Assets/Scripts/Action/Actions/BerserkAttackAction.cs b/Assets/Scripts/Action/Actions/BerserkAttackAction.cs index 45f5f588..02dac080 100644 --- a/Assets/Scripts/Action/Actions/BerserkAttackAction.cs +++ b/Assets/Scripts/Action/Actions/BerserkAttackAction.cs @@ -1,3 +1,4 @@ +using Command.Commands; using Command.Input; using Command.Player; using UnityEngine; @@ -6,13 +7,12 @@ namespace Command.Actions { public class BerserkAttackAction : IAction { - private const float hitChance = 0.66f; public TargetType TargetType => TargetType.Enemy; - public void PerformAction(UnitController actorUnit, UnitController targetUnit) + public void PerformAction(UnitController actorUnit, UnitController targetUnit, bool successful) { - actorUnit.PlayActionAnimation(ActionType.BerserkAttack); - if (IsSuccessful()) + actorUnit.PlayActionAnimation(CommandType.BerserkAttack); + if (successful) targetUnit.TakeDamage(actorUnit.CurrentPower * 2); else { @@ -20,7 +20,5 @@ public void PerformAction(UnitController actorUnit, UnitController targetUnit) Debug.Log("actor unit must be hit now."); } } - - public bool IsSuccessful() => Random.Range(0f, 1f) < hitChance; } } \ No newline at end of file diff --git a/Assets/Scripts/Action/Actions/CleanseAction.cs b/Assets/Scripts/Action/Actions/CleanseAction.cs index d708624b..1ed34686 100644 --- a/Assets/Scripts/Action/Actions/CleanseAction.cs +++ b/Assets/Scripts/Action/Actions/CleanseAction.cs @@ -2,23 +2,21 @@ using Command.Player; using Command.Main; using UnityEngine; +using Command.Commands; namespace Command.Actions { public class CleanseAction : IAction { - private const float hitChance = 0.2f; public TargetType TargetType => TargetType.Enemy; - public void PerformAction(UnitController actorUnit, UnitController targetUnit) + public void PerformAction(UnitController actorUnit, UnitController targetUnit, bool successful) { - actorUnit.PlayActionAnimation(ActionType.Cleanse); - if(IsSuccessful()) + actorUnit.PlayActionAnimation(CommandType.Cleanse); + if(successful) targetUnit.ResetStats(); else GameService.Instance.UIService.ActionMissed(); } - - public bool IsSuccessful() => Random.Range(0f, 1f) < hitChance; } } diff --git a/Assets/Scripts/Action/Actions/HealAction.cs b/Assets/Scripts/Action/Actions/HealAction.cs index 0109875c..c5f632c0 100644 --- a/Assets/Scripts/Action/Actions/HealAction.cs +++ b/Assets/Scripts/Action/Actions/HealAction.cs @@ -1,3 +1,4 @@ +using Command.Commands; using Command.Input; using Command.Player; @@ -7,13 +8,11 @@ public class HealAction : IAction { public TargetType TargetType => TargetType.Friendly; - public void PerformAction(UnitController actorUnit, UnitController targetUnit) + public void PerformAction(UnitController actorUnit, UnitController targetUnit, bool successful) { - actorUnit.PlayActionAnimation(ActionType.Heal); - if(IsSuccessful()) + actorUnit.PlayActionAnimation(CommandType.Heal); + if(successful) targetUnit.RestoreHealth(actorUnit.CurrentPower); } - - public bool IsSuccessful() => true; } } \ No newline at end of file diff --git a/Assets/Scripts/Action/Actions/MeditateAction.cs b/Assets/Scripts/Action/Actions/MeditateAction.cs index 843c7f2c..fa522098 100644 --- a/Assets/Scripts/Action/Actions/MeditateAction.cs +++ b/Assets/Scripts/Action/Actions/MeditateAction.cs @@ -1,3 +1,4 @@ +using Command.Commands; using Command.Input; using Command.Main; using Command.Player; @@ -8,10 +9,10 @@ public class MeditateAction : IAction { public TargetType TargetType => TargetType.Self; - public void PerformAction(UnitController actorUnit, UnitController targetUnit) + public void PerformAction(UnitController actorUnit, UnitController targetUnit, bool successful) { - actorUnit.PlayActionAnimation(ActionType.Meditate); - if(IsSuccessful()) + actorUnit.PlayActionAnimation(CommandType.Meditate); + if(successful) { var healthToIncrease = (int)(targetUnit.CurrentMaxHealth * 0.2f); targetUnit.CurrentMaxHealth += healthToIncrease; @@ -20,7 +21,5 @@ public void PerformAction(UnitController actorUnit, UnitController targetUnit) else GameService.Instance.UIService.ActionMissed(); } - - public bool IsSuccessful() => true; } } \ No newline at end of file diff --git a/Assets/Scripts/Action/Actions/ThirdEyeAction.cs b/Assets/Scripts/Action/Actions/ThirdEyeAction.cs index 5f6192cf..a58e15e5 100644 --- a/Assets/Scripts/Action/Actions/ThirdEyeAction.cs +++ b/Assets/Scripts/Action/Actions/ThirdEyeAction.cs @@ -1,3 +1,4 @@ +using Command.Commands; using Command.Input; using Command.Main; using Command.Player; @@ -8,10 +9,10 @@ public class ThirdEyeAction : IAction { public TargetType TargetType => TargetType.Self; - public void PerformAction(UnitController actorUnit, UnitController targetUnit) + public void PerformAction(UnitController actorUnit, UnitController targetUnit, bool successful) { - actorUnit.PlayActionAnimation(ActionType.BerserkAttack); - if (IsSuccessful()) + actorUnit.PlayActionAnimation(CommandType.BerserkAttack); + if (successful) { int healthToConvert = (int)(targetUnit.CurrentHealth * 0.25f); targetUnit.TakeDamage(healthToConvert); @@ -20,7 +21,5 @@ public void PerformAction(UnitController actorUnit, UnitController targetUnit) else GameService.Instance.UIService.ActionMissed(); } - - public bool IsSuccessful() => true; } } \ No newline at end of file diff --git a/Assets/Scripts/Action/IAction.cs b/Assets/Scripts/Action/IAction.cs index b3a09179..dc0d910b 100644 --- a/Assets/Scripts/Action/IAction.cs +++ b/Assets/Scripts/Action/IAction.cs @@ -7,8 +7,6 @@ public interface IAction { public TargetType TargetType { get; } - public void PerformAction(UnitController actorUnit, UnitController targetUnit); - - public bool IsSuccessful(); + public void PerformAction(UnitController actorUnit, UnitController targetUnit, bool isSuccessful); } } \ No newline at end of file diff --git a/Assets/Scripts/Command/Abstract Commands/IUnitCommand.cs b/Assets/Scripts/Command/Abstract Commands/UnitCommand.cs similarity index 69% rename from Assets/Scripts/Command/Abstract Commands/IUnitCommand.cs rename to Assets/Scripts/Command/Abstract Commands/UnitCommand.cs index 58bfa664..e0f55406 100644 --- a/Assets/Scripts/Command/Abstract Commands/IUnitCommand.cs +++ b/Assets/Scripts/Command/Abstract Commands/UnitCommand.cs @@ -15,5 +15,9 @@ public abstract class UnitCommand : ICommand public abstract void Execute(); public abstract bool WillHitTarget(); + + public void SetActorUnit(UnitController actorUnit) => this.actorUnit = actorUnit; + + public void SetTargetUnit(UnitController targetUnit) => this.targetUnit = targetUnit; } } \ No newline at end of file diff --git a/Assets/Scripts/Command/Abstract Commands/IUnitCommand.cs.meta b/Assets/Scripts/Command/Abstract Commands/UnitCommand.cs.meta similarity index 100% rename from Assets/Scripts/Command/Abstract Commands/IUnitCommand.cs.meta rename to Assets/Scripts/Command/Abstract Commands/UnitCommand.cs.meta diff --git a/Assets/Scripts/Action/ActionType.cs b/Assets/Scripts/Command/CommandType.cs similarity index 72% rename from Assets/Scripts/Action/ActionType.cs rename to Assets/Scripts/Command/CommandType.cs index e412947a..aa8016f2 100644 --- a/Assets/Scripts/Action/ActionType.cs +++ b/Assets/Scripts/Command/CommandType.cs @@ -1,6 +1,6 @@ -namespace Command.Actions +namespace Command.Commands { - public enum ActionType + public enum CommandType { Attack, Heal, diff --git a/Assets/Scripts/Action/ActionType.cs.meta b/Assets/Scripts/Command/CommandType.cs.meta similarity index 100% rename from Assets/Scripts/Action/ActionType.cs.meta rename to Assets/Scripts/Command/CommandType.cs.meta diff --git a/Assets/Scripts/Command/ConcreteCommands.meta b/Assets/Scripts/Command/ConcreteCommands.meta new file mode 100644 index 00000000..23e370f1 --- /dev/null +++ b/Assets/Scripts/Command/ConcreteCommands.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 55cc47bf89042a94291adf77ae1962bc +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Command/ConcreteCommands/AttackCommand.cs b/Assets/Scripts/Command/ConcreteCommands/AttackCommand.cs new file mode 100644 index 00000000..75046ab3 --- /dev/null +++ b/Assets/Scripts/Command/ConcreteCommands/AttackCommand.cs @@ -0,0 +1,23 @@ +using Command.Main; + +namespace Command.Commands +{ + public class AttackCommand : UnitCommand + { + private bool willHitTarget; + + public AttackCommand(int actorUnitId, int targetUnitId, int actorPlayerId, int targetPlayerId) + { + ActorUnitID = actorUnitId; + TargetUnitID = targetUnitId; + ActorPlayerID = actorPlayerId; + TargetPlayerID = targetPlayerId; + + willHitTarget = WillHitTarget(); + } + + public override bool WillHitTarget() => true; + + public override void Execute() => GameService.Instance.ActionService.GetActionByType(CommandType.Attack).PerformAction(actorUnit, targetUnit, willHitTarget); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Command/ConcreteCommands/AttackCommand.cs.meta b/Assets/Scripts/Command/ConcreteCommands/AttackCommand.cs.meta new file mode 100644 index 00000000..22275d85 --- /dev/null +++ b/Assets/Scripts/Command/ConcreteCommands/AttackCommand.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 00ed892a7cb8738439eb141a0e0cddb0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Command/ConcreteCommands/HealCommand.cs b/Assets/Scripts/Command/ConcreteCommands/HealCommand.cs new file mode 100644 index 00000000..cc6841d8 --- /dev/null +++ b/Assets/Scripts/Command/ConcreteCommands/HealCommand.cs @@ -0,0 +1,23 @@ +using Command.Main; + +namespace Command.Commands +{ + public class HealCommand : UnitCommand + { + private bool willHitTarget; + + public HealCommand(int actorUnitId, int targetUnitId, int actorPlayerId, int targetPlayerId) + { + ActorUnitID = actorUnitId; + TargetUnitID = targetUnitId; + ActorPlayerID = actorPlayerId; + TargetPlayerID = targetPlayerId; + + willHitTarget = WillHitTarget(); + } + + public override void Execute() => GameService.Instance.ActionService.GetActionByType(CommandType.Heal).PerformAction(actorUnit, targetUnit, willHitTarget); + + public override bool WillHitTarget() => true; + } +} \ No newline at end of file diff --git a/Assets/Scripts/Command/ConcreteCommands/HealCommand.cs.meta b/Assets/Scripts/Command/ConcreteCommands/HealCommand.cs.meta new file mode 100644 index 00000000..f55e3d0f --- /dev/null +++ b/Assets/Scripts/Command/ConcreteCommands/HealCommand.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5c01465896d721740947c4d0328cfb17 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Events/EventService.cs b/Assets/Scripts/Events/EventService.cs index 59b664c2..87a30b58 100644 --- a/Assets/Scripts/Events/EventService.cs +++ b/Assets/Scripts/Events/EventService.cs @@ -1,4 +1,4 @@ -using Command.Actions; +using Command.Commands; /** This script demonstrates implementation of the Observer Pattern. * If you're interested in learning about Observer Pattern, @@ -11,12 +11,12 @@ namespace Command.Events public class EventService { public GameEventController OnBattleSelected { get; private set; } - public GameEventController OnActionSelected { get; private set; } + public GameEventController OnActionSelected { get; private set; } public EventService() { OnBattleSelected = new GameEventController(); - OnActionSelected = new GameEventController(); + OnActionSelected = new GameEventController(); } } } \ No newline at end of file diff --git a/Assets/Scripts/Input/InputService.cs b/Assets/Scripts/Input/InputService.cs index d287fc0e..1ff596b0 100644 --- a/Assets/Scripts/Input/InputService.cs +++ b/Assets/Scripts/Input/InputService.cs @@ -1,6 +1,6 @@ using Command.Main; using Command.Player; -using Command.Actions; +using Command.Commands; namespace Command.Input { @@ -9,7 +9,7 @@ public class InputService private MouseInputHandler mouseInputHandler; private InputState currentState; - private ActionType selectedActionType; + private CommandType selectedCommandType; private TargetType targetType; public InputService() @@ -29,19 +29,39 @@ public void UpdateInputService() mouseInputHandler.HandleTargetSelection(targetType); } - public void OnActionSelected(ActionType selectedActionType) + public void OnActionSelected(CommandType selectedActionType) { - this.selectedActionType = selectedActionType; + this.selectedCommandType = selectedActionType; SetInputState(InputState.SELECTING_TARGET); SetTargetType(selectedActionType); } - private void SetTargetType(ActionType selectedActionType) => targetType = GameService.Instance.ActionService.GetTargetTypeForAction(selectedActionType); + private void SetTargetType(CommandType selectedActionType) => targetType = GameService.Instance.ActionService.GetTargetTypeForAction(selectedActionType); public void OnTargetSelected(UnitController targetUnit) { SetInputState(InputState.EXECUTING_INPUT); - GameService.Instance.PlayerService.PerformAction(selectedActionType, targetUnit); + UnitCommand commandToProcess = CreateUnitCommand(targetUnit); + GameService.Instance.ProcessUnitCommand(commandToProcess); + } + + private UnitCommand CreateUnitCommand(UnitController targetUnit) + { + switch (selectedCommandType) + { + case CommandType.Attack: + return new AttackCommand(GameService.Instance.PlayerService.ActiveUnitID, + targetUnit.UnitID, + GameService.Instance.PlayerService.ActivePlayerID, + targetUnit.Owner.PlayerID); + case CommandType.Heal: + return new HealCommand(GameService.Instance.PlayerService.ActiveUnitID, + targetUnit.UnitID, + GameService.Instance.PlayerService.ActivePlayerID, + targetUnit.Owner.PlayerID); + default: + throw new System.Exception($"No Command found of type: {selectedCommandType}"); + } } } diff --git a/Assets/Scripts/Main/GameService.cs b/Assets/Scripts/Main/GameService.cs index 0a8ac96c..294eb2ab 100644 --- a/Assets/Scripts/Main/GameService.cs +++ b/Assets/Scripts/Main/GameService.cs @@ -49,5 +49,7 @@ private void Start() } private void Update() => InputService.UpdateInputService(); + + public void ProcessUnitCommand(ICommand commandToProcess) => PlayerService.ProcessUnitCommand(commandToProcess as UnitCommand); } } \ No newline at end of file diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index 828a9cf5..db3431e8 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -1,3 +1,4 @@ +using Command.Commands; using System.Collections.Generic; using UnityEngine; @@ -78,6 +79,8 @@ public void DestroyAllUnits() units.Clear(); } + public void ProcessUnitCommand(UnitCommand commandToProcess) => GetUnitByID(commandToProcess.ActorUnitID).ProcessUnitCommand(commandToProcess); + public void ResetCurrentActivePlayer() { units[activeUnitIndex].ResetUnitIndicator(); diff --git a/Assets/Scripts/Player/PlayerService.cs b/Assets/Scripts/Player/PlayerService.cs index 9b520a5a..fecfbb80 100644 --- a/Assets/Scripts/Player/PlayerService.cs +++ b/Assets/Scripts/Player/PlayerService.cs @@ -1,4 +1,4 @@ -using Command.Actions; +using Command.Commands; using Command.Main; namespace Command.Player @@ -65,8 +65,6 @@ private void SetActivePlayer() public void OnPlayerTurnCompleted() => StartNextTurn(); - public void PerformAction(ActionType actionSelected, UnitController targetUnit) => GameService.Instance.ActionService.GetActionByType(actionSelected).PerformAction(activePlayer.GetUnitByID(ActiveUnitID), targetUnit); - public void PlayerDied(PlayerController deadPlayer) { int winnerId; @@ -79,6 +77,20 @@ public void PlayerDied(PlayerController deadPlayer) GameService.Instance.UIService.ShowBattleEndUI(winnerId); } + public void ProcessUnitCommand(UnitCommand commandToProcess) + { + SetUnitReferences(commandToProcess); + GetPlayerById(commandToProcess.ActorPlayerID).ProcessUnitCommand(commandToProcess); + } + + private void SetUnitReferences(UnitCommand commandToProcess) + { + var actorUnit = GetPlayerById(commandToProcess.ActorPlayerID).GetUnitByID(commandToProcess.ActorUnitID); + var targetUnit = GetPlayerById(commandToProcess.TargetPlayerID).GetUnitByID(commandToProcess.TargetUnitID); + commandToProcess.SetActorUnit(actorUnit); + commandToProcess.SetTargetUnit(targetUnit); + } + private PlayerController GetPlayerById(int playerId) { if (player1.PlayerID == playerId) diff --git a/Assets/Scripts/Player/Unit/UnitController.cs b/Assets/Scripts/Player/Unit/UnitController.cs index 13de2fa8..f52fb857 100644 --- a/Assets/Scripts/Player/Unit/UnitController.cs +++ b/Assets/Scripts/Player/Unit/UnitController.cs @@ -1,6 +1,6 @@ using UnityEngine; using Command.Main; -using Command.Actions; +using Command.Commands; namespace Command.Player { @@ -85,7 +85,7 @@ private void UnitDied() // Play Death Animation. } - public void PlayActionAnimation(ActionType actionType) + public void PlayActionAnimation(CommandType actionType) { if (actionType == unitScriptableObject.executableCommands[0]) unitView.PlayAnimation(UnitAnimations.ACTION1); @@ -110,6 +110,8 @@ public void OnActionExecuted() public void ResetUnitIndicator() => unitView.SetUnitIndicator(false); + public void ProcessUnitCommand(UnitCommand commandToProcess) => GameService.Instance.CommandInvoker.ProcessCommand(commandToProcess); + } public enum UnitUsedState diff --git a/Assets/Scripts/Player/Unit/UnitScriptableObject.cs b/Assets/Scripts/Player/Unit/UnitScriptableObject.cs index 9913a8bb..62f15cdd 100644 --- a/Assets/Scripts/Player/Unit/UnitScriptableObject.cs +++ b/Assets/Scripts/Player/Unit/UnitScriptableObject.cs @@ -1,4 +1,4 @@ -using Command.Actions; +using Command.Commands; using System.Collections.Generic; using UnityEngine; @@ -11,6 +11,6 @@ public class UnitScriptableObject : ScriptableObject public UnitView UnitPrefab; public int MaxHealth; public int Power; - public List executableCommands; + public List executableCommands; } } \ No newline at end of file diff --git a/Assets/Scripts/UI/ActionSelectionUI/ActionButtonView.cs b/Assets/Scripts/UI/ActionSelectionUI/ActionButtonView.cs index f3016d06..69863dff 100644 --- a/Assets/Scripts/UI/ActionSelectionUI/ActionButtonView.cs +++ b/Assets/Scripts/UI/ActionSelectionUI/ActionButtonView.cs @@ -1,4 +1,4 @@ -using Command.Actions; +using Command.Commands; using TMPro; using UnityEngine; using UnityEngine.UI; @@ -9,7 +9,7 @@ public class ActionButtonView : MonoBehaviour { [SerializeField] private TextMeshProUGUI buttonText; private ActionSelectionUIController owner; - private ActionType actionType; + private CommandType actionType; private void Start() => GetComponent