diff --git a/ChaoticCrates/Assets/Scripts/Logic/RobotAction/ActionValidator.cs b/ChaoticCrates/Assets/Scripts/Logic/RobotAction/ActionValidator.cs index 6ce9e24d4443f3f99ce71d0c6336a6f698c88b57..da966152a486ab1e41470b87a071442b7aaab174 100644 --- a/ChaoticCrates/Assets/Scripts/Logic/RobotAction/ActionValidator.cs +++ b/ChaoticCrates/Assets/Scripts/Logic/RobotAction/ActionValidator.cs @@ -10,10 +10,16 @@ using UnityEngine; namespace Logic.RobotAction { + /// <summary> + /// Responsible for initiating and validating all in-game actions : Move, Wait, Clean, Rotate,Link, Unlink, Connect, Disconnect, Special + /// </summary> public class ActionValidator { - // private fields + # region Fields + // Game manager private GameManager _gameManager; + + // Action lists, dictionaries private List<InputData> _playerInputsAndActions; private List<RobotAction> _actions; private List<Tuple<RobotAction,int>> _moveAndRotateActions; @@ -24,11 +30,19 @@ namespace Logic.RobotAction [Team.Red] = 0, [Team.Blue] = 0 }; + #endregion - // events + # region Events + // Action Validating event for the communication with the game manager public static event Action<List<RobotAction>> OnActionsValidated; + #endregion - // Constructor + # region Constructor + /// <summary> + /// Non singleton constructor for the action validator class + /// </summary> + /// <param name="playerInputsAndActions">All actions in a round waiting to be validated</param> + /// /// <param name="gameManager">GameManager instance that handles the execution of the acitons</param> public ActionValidator( List<InputData> playerInputsAndActions, GameManager gameManager) { this._playerInputsAndActions = playerInputsAndActions; @@ -36,15 +50,18 @@ namespace Logic.RobotAction _actions = new List<RobotAction>(); _moveAndRotateActions = new List<Tuple<RobotAction,int>>(); _linkActions = new List<RobotAction>(); - - } + # endregion - // public methods + # region Validate Method + /// <summary> + /// Validate function: Handles the validation of multiplayer movements (the collision of movements and + /// other actions between multiple players and places the validated actions into the actions array that will + /// be used by the gamemanager to execute the actions. + /// </summary> public void Validate() { _playerInputsAndActions.Sort(); - foreach (var x in _playerInputsAndActions) { NetworkObject nobj; @@ -86,8 +103,7 @@ namespace Logic.RobotAction } } - // ####### Lists for further operations ###### - + // ####### Lists for further operations ##### // spaces - for securing that we do not move or rotate at the same place with multiple players // includes: a position vector, a robotaction that places the vector, the ID of the Robotaction, anda gameobject that is the moveobject List<Tuple<Vector3Int,RobotAction, int, GameObject>> spaces=new List<Tuple<Vector3Int,RobotAction, int, GameObject>>(); @@ -103,7 +119,6 @@ namespace Logic.RobotAction // Handling MOVE and ROTATE actions (checking for collision) foreach (var x in _moveAndRotateActions) { - //Debug.Log("Belep moveandRotateActions for"); // ############# ROTATE ACTION ############## if (x.Item1.Type == ActionType.Rotate) { @@ -121,35 +136,27 @@ namespace Logic.RobotAction int diffY = playerPos.y - childPos.y; if (diffX == 0 && diffY == 0) continue; Vector3Int newPos = new Vector3Int(playerPos.x - (dir * diffY), playerPos.y + (dir * diffX), 0); - //Debug.Log("Elforgatott elemek koordinatai: "+newPos); spaces.Add( new Tuple<Vector3Int, RobotAction, int, GameObject>(newPos,x.Item1, x.Item2,moveObject)); } } - //Debug.Log("ActionType check:"+x.Item1.Type); //############ MOVE ACTION ##############x if (x.Item1.Type == ActionType.Move) { - //Debug.Log("BELEP"); Vector3 vec = ((MoveRobotAction)x.Item1).Vec; GameObject moveObject = ((MoveRobotAction)x.Item1).MoveObj; GameObject pObject = moveObject.transform.GetChild(0).gameObject; if (x.Item1.SingleRobot) { - //Debug.Log("<color=red>single</color>"); Vector3Int childPos = Vector3Int.RoundToInt(pObject.transform.position+vec) ; - //Debug.Log("A moveolt koordinatak: " + childPos); spaces.Add( new Tuple<Vector3Int, RobotAction, int, GameObject>(childPos,x.Item1, x.Item2, moveObject)); } else { - //Debug.Log("Nonsingle: ChildCount Kinn:"+moveObject.transform.childCount); for (int i = 0; i < moveObject.transform.childCount; i++) { - //Debug.Log("Kinn ITT Volt"+ i); GameObject child = moveObject.transform.GetChild(i).gameObject; Vector3Int childPos = Vector3Int.RoundToInt(child.transform.position+vec); - //Debug.Log("A move vector: " + childPos); spaces.Add( new Tuple<Vector3Int, RobotAction, int,GameObject>(childPos,x.Item1, x.Item2, moveObject)); } @@ -163,66 +170,54 @@ namespace Logic.RobotAction { foreach (var y in spaces) { - //Debug.Log("Double foreach"); - bool rem = ((y.Item1.x == x.Item1.x && y.Item1.y == x.Item1.y) || - (y.Item1.y == x.Item1.x && y.Item1.x == x.Item1.y)) && (y.Item2.Type != x.Item2.Type /*|| - y.Item2 != x.Item2*/); + (y.Item1.y == x.Item1.x && y.Item1.x == x.Item1.y)) && (y.Item2.Type != x.Item2.Type ); bool unique = true; - // MEG KELL NEZNI JO E, hogy a transform.pos azt adja e vissza amit akarok + // We check the transform.positiion if ((int)Math.Round(x.Item4.transform.position.x) == (int)Math.Round(y.Item4.transform.position.x)) { - // Nem azonos a move / rotate action + // Move and rotate action if (x.Item2.Type != y.Item2.Type) { rem = true; - //Debug.Log("MASODIK"); } - // Moveaction minketto de nem ugyanarra + // Two or more move actions in different directions if (x.Item2.Type == ActionType.Move && y.Item2.Type == ActionType.Move) { if(((int)Math.Round(((MoveRobotAction)x.Item2).Vec.x)!= (int)Math.Round(((MoveRobotAction)y.Item2).Vec.x) ) || ((int)Math.Round(((MoveRobotAction)x.Item2).Vec.y)!= (int)Math.Round(((MoveRobotAction)y.Item2).Vec.y))) { rem = true; - //Debug.Log("HARMADIK"); } else { if (x.Item3 != y.Item3) { - - //Debug.Log("NonUniquMoveFound"); unique = false; } } } - // RotateAction nem ugyanara + // Two or more rotate actions in different directions if (x.Item2.Type == ActionType.Rotate && y.Item2.Type == ActionType.Rotate) { if((((RotateRobotAction)x.Item2).Dir!= ((RotateRobotAction)y.Item2).Dir ) || (((RotateRobotAction)x.Item2).Dir!= ((RotateRobotAction)y.Item2).Dir)) { rem = true; - //Debug.Log("Negyedik"); } else { if (x.Item3 != y.Item3) { - //Debug.Log("NonUniquMoveFound"); unique = false; } } } } - //Debug.Log(y.Item2.Type+" "+y.Item1+" "+x.Item2.Type+" "+ x.Item1); - // Debug.Log("Rem erteke: " + rem); if (!rem && !unique) { - //Debug.Log("UNIQUE"); RobotAction tmpAction=x.Item2; int tmpID=-1; foreach (var k in _moveAndRotateActions.ToArray()) @@ -240,38 +235,21 @@ namespace Logic.RobotAction if(rem) { - // Debug.Log("A tomb tartalma REMOVE elott: " ); - /* foreach (var l in _moveAndRotateActions) - { - Debug.Log(">>"+l.Item2); - } - Debug.Log("Az azonositok a space tombben: "+y.Item3+" " + x.Item3); - */ foreach (var k in _moveAndRotateActions.ToArray()) { - // Debug.Log("Az aktuĂĄlis action a foreachben: " + k.Item2); - // Debug.Log("Egyenlok: " + (k.Item2 == x.Item3 || k.Item2 == y.Item3)); if (k.Item2 == x.Item3 || k.Item2 == y.Item3) { - // Debug.Log("REMOVED"); _moveAndRotateActions.Remove(k); } } } - //Debug.Log("A tomb tartalma REMOVE utĂĄn: " ); - /* foreach (var l in _moveAndRotateActions) - { - Debug.Log(">>"+l.Item2); - } - */ } } - //Debug.Log("Spaces count after: "+spaces.Count); + // ########### LINKING ACTIONS ############# int counter = 0; foreach (var z in _linkActions) { - //Debug.Log("linkactionok"); int c1X = (int)Math.Round(((LinkRobotAction)z).Cube1.transform.position.x); int c1Y = (int)Math.Round(((LinkRobotAction)z).Cube1.transform.position.y); int c2X = (int)Math.Round(((LinkRobotAction)z).Cube2.transform.position.x); @@ -285,7 +263,6 @@ namespace Logic.RobotAction { foreach (var l in cubesToBeLinked) { - if (((k.Item1 == l.Item1 && k.Item2 == l.Item2 && k.Item3 == l.Item3 && k.Item4 == l.Item4)|| (k.Item1 == l.Item3 && k.Item2 == l.Item4 && k.Item3 == l.Item1 && k.Item4 == l.Item1)) && k.Item6 != l.Item6) { @@ -307,7 +284,6 @@ namespace Logic.RobotAction if (unCube1) { uniqueCubes.Add(new Tuple<int, int>(k.Item1, k.Item2)); - } if (unCube2) @@ -316,16 +292,13 @@ namespace Logic.RobotAction } if (unCube1 && unCube2) { - - //uniqueCubes.Add(new Tuple<GameObject, GameObject>(x.Item1, x.Item2)); - //Debug.Log("Valid link"); links.Add(k.Item5); } } } } - // VĂŠgĂŠn hozzĂĄadom a kiszurt actionoket + Collisioncheck OVERALL + // Overall collision check and adding the validated actions into the actions array foreach (var z in links.Distinct()) { int c1X = (int)Math.Round(((LinkRobotAction)z).Cube1.transform.position.x); @@ -381,7 +354,6 @@ namespace Logic.RobotAction { GameObject child = moveObject.transform.GetChild(i).gameObject; Vector3Int childPos = Vector3Int.RoundToInt(child.transform.position+vec); - //Debug.Log("A move vector: " + childPos); CollisionCheck.Add(new Tuple<int, int, RobotAction>(childPos.x, childPos.y, x.Item1)); } @@ -407,9 +379,18 @@ namespace Logic.RobotAction } CollisionCheck.Clear(); + // Raising the event OnActionsValidated?.Invoke(_actions); } - + # endregion + + # region Individual Validation Methods + /// <summary> + /// Validation function for the special action of the game (dropping all cubes) + /// </summary> + /// <param name="invoker">ulog that signs whether the action was called</param> + /// <param name="playerObject">GameObject that contains the player who initiated the action</param> + /// <param name="xInputs">Input vector signifying where the player is</param> private void ValidateSpecial(ulong invoker,GameObject playerObject, Vector3[] xInputs) { Player player = playerObject.GetComponent<Player>(); @@ -419,24 +400,31 @@ namespace Logic.RobotAction specials[player.team]++; } } - - // ################################# ACTION FUNCTIONS ############################ - // CLEAN ACTION + + /// <summary> + /// Validation function for the clean action of the game + /// </summary> + /// <param name="invoker">ulog that signs whether the action was called</param> + /// <param name="playerObject">GameObject that contains the player who initiated the action</param> + /// <param name="inputs">Input vector signifying where the cube is to be cleaned</param> private void ValidateClean(ulong invoker,GameObject playerObject, Vector3[] inputs) { Vector3Int vec = Vector3Int.RoundToInt(inputs[0]); GameObject obj = MapManager.Instance.CheckCollision(vec.x, vec.y); - //Debug.Log(obj == null); if (obj != null && obj.CompareTag("Obstacle")) { RobotAction action = new CleanRobotAction(invoker,obj); action.Type = ActionType.Clean; - _actions.Add(action); } } - // LINK ACTION + /// <summary> + /// Validation function for the link action of the game + /// </summary> + /// <param name="invoker">ulog that signs whether the action was called</param> + /// <param name="playerObject">GameObject that contains the player who initiated the action</param> + /// <param name="input">Input vector signifying where the cube is to be linked</param> private void ValidateLink(ulong invoker,GameObject playerObject, Vector3[] input) { Vector3 vec1 = Vector3Int.RoundToInt(input[0]); @@ -457,21 +445,23 @@ namespace Logic.RobotAction } } } - - if (cube1 != null && cube2 != null) { if (cube2.transform.parent.GameObject() != cube1.transform.parent.GameObject()) { RobotAction action = new LinkRobotAction(invoker,cube1, cube2); - action.Type = ActionType.Link; _linkActions.Add(action); } } } - // ROTATE MOVEMENT + /// <summary> + /// Validation function for the Rotate action of the game + /// </summary> + /// <param name="invoker">ulog that signs whether the action was called</param> + /// <param name="playerObject">GameObject that contains the player who initiated the action</param> + /// <param name="input">Input vector signifying where the cube is to be rotated</param> private void ValidateRotate(ulong invoker,GameObject playerObject, Vector3[] input) { Vector3Int vec = Vector3Int.RoundToInt(input[0]); @@ -507,7 +497,13 @@ namespace Logic.RobotAction } } - // UNLINK ACTION + + /// <summary> + /// Validation function for the unlink action of the game + /// </summary> + /// <param name="invoker">ulog that signs whether the action was called</param> + /// <param name="playerObject">GameObject that contains the player who initiated the action</param> + /// <param name="input">Input vector signifying where the cubes are to be unlinked</param> private void ValidateUnlink(ulong invoker,GameObject playerObject, Vector3[] input) { Vector3 vec1 = Vector3Int.RoundToInt(input[0]); @@ -533,7 +529,7 @@ namespace Logic.RobotAction } } - if (cube1 != null && cube2 != null && mCube1 == mCube2 /*&& nextTo*/) + if (cube1 != null && cube2 != null && mCube1 == mCube2 ) { RobotAction action = new UnlinkRobotAction(invoker,cube1, cube2); @@ -548,18 +544,19 @@ namespace Logic.RobotAction } } - // MOVE ACTION + /// <summary> + /// Validation function for the Move action of the game + /// </summary> + /// <param name="invoker">ulog that signs whether the action was called</param> + /// <param name="playerObject">GameObject that contains the player who initiated the action</param> + /// <param name="input">Input vector signifying where the cube is to be moved</param> private void ValidateMove(ulong invoker,GameObject playerObject, Vector3[] input) { Vector3 vec = Vector3Int.RoundToInt(input[0]) - playerObject.transform.position; bool wasSingle = true; GameObject moveObject = playerObject.GetComponent<Movable>().moveObject; - - - Vector3Int playerPos = Vector3Int.RoundToInt(input[0]); int mapHalf = MapManager.Instance.mapSize * 10 / 2; - bool valid = (playerPos.x >= (-mapHalf)) && (playerPos.x <= (mapHalf - 1) ) && (playerPos.y >= -mapHalf ) && (playerPos.y <= (mapHalf - 1)); if(playerObject == moveObject) @@ -582,27 +579,20 @@ namespace Logic.RobotAction } } } - } + } else { wasSingle = false; for (int i = 0; i < moveObject.transform.childCount; i++) { - //Debug.Log("A CHILDCOUNT:" + moveObject.transform.childCount); GameObject child = moveObject.transform.GetChild(i).gameObject; Vector3Int childPos = Vector3Int.RoundToInt(child.transform.position + vec); - //Debug.Log("Original Child poz: " + child.transform.position.x + " " + child.transform.position.y); - //Debug.Log("vec: "+vec.x+" " +vec.y); - //Debug.Log("ChildPoz: " + childPos.x + " " + childPos.y); - foreach (GameObject o in MapManager.Instance.mapObjects) { if ((int)Math.Round(o.transform.position.x) == childPos.x && (int)Math.Round(o.transform.position.y )== (childPos.y)) { if (o.CompareTag("Cube") || o.CompareTag(("Player"))) { - //Debug.Log("o x"+(int)(o.transform.position.x) + " o y: "+(int)(o.transform.position.y )+" childpoz x: "+ - // childPos.x+ "c y:"+childPos.y + " :"); if (o.GetComponent<Movable>().moveObject != child.GetComponent<Movable>().moveObject) { valid = false; @@ -623,12 +613,16 @@ namespace Logic.RobotAction RobotAction robotAction = new MoveRobotAction(invoker,mo,vec); robotAction.Type = ActionType.Move; robotAction.SingleRobot = wasSingle; - // Debug.Log("ITT: "+ robotAction.Type); _moveAndRotateActions.Add(new Tuple<RobotAction, int>(robotAction,_moveAndRotateActions.Count+1)); } } - // WAIT ACTION + /// <summary> + /// Validation function for the Wait action of the game + /// </summary> + /// <param name="invoker">ulog that signs whether the action was called</param> + /// <param name="playerObject">GameObject that contains the player who initiated the action</param> + /// <param name="input">Input vector signifying where the player is</param> private void ValidateWait(ulong invoker,GameObject playerObject, Vector3[] input) { RobotAction robotAction = new WaitRobotAction(invoker,playerObject); @@ -637,7 +631,12 @@ namespace Logic.RobotAction } - // CONNECT ACTION + /// <summary> + /// Validation function for the connect action of the game + /// </summary> + /// <param name="invoker">ulog that signs whether the action was called</param> + /// <param name="playerObject">GameObject that contains the player who initiated the action</param> + /// <param name="input">Input vector signifying where the cubes are to be connected</param> private void ValidateConnect(ulong invoker,GameObject playerObject, Vector3[] input) { Vector3 vec = Vector3Int.RoundToInt(input[0]); @@ -666,7 +665,12 @@ namespace Logic.RobotAction } } - // Disconnect ACTION + /// <summary> + /// Validation function for the disconnect action of the game + /// </summary> + /// <param name="invoker">ulog that signs whether the action was called</param> + /// <param name="playerObject">GameObject that contains the player who initiated the action</param> + /// <param name="input">Input vector signifying where the cube is to be disconnected</param> private void ValidateDisconnect(ulong invoker,GameObject playerObject, Vector3[] input) { Vector3 vec = Vector3Int.RoundToInt(input[0]); @@ -694,6 +698,7 @@ namespace Logic.RobotAction } } } + #endregion } } \ No newline at end of file