/* #endregion */ /* #endregion */ /* ======================================================================================== */ /* #region ==== M O V E a c t i o n ====================================================== */ /* #region ---- 1. CHECK FOR MOVEMENT ----------------------------------------------------- */ public void CheckForMovement(PitchTile targetTile) { if (MatchManager.MatchPlayerManager.CurrentActivePlayer.PlayerMode == PlayerMode.Move) { if (targetTile.IsViableTarget) { if (targetTile.IsOccupied) { if (targetTile.OccupiedByPlayer == MatchManager.MatchPlayerManager.CurrentActivePlayer) { Debug.Log("Tile already occupied by the active player"); return; } else { Debug.Log("Can't move here! Tile is occupied by other player"); return; } } else { if (!MatchManager.MatchPlayerManager.PlayInAction) { movePlayerSetup(targetTile); } } } else { Debug.Log("This tile is out of movement reach"); return; } } }
/* #endregion */ /* #region ---- CreatePitch --------------------------------------------------------------- */ private void createPitch() { int pitchWidth = MatchManager.PitchGrid.PitchSettings.PitchWidth; int pitchLength = MatchManager.PitchGrid.PitchSettings.PitchLength; setPosOffset(pitchWidth, pitchLength); PitchTiles = new GameObject [pitchWidth + 1, pitchLength + 1]; PitchTilesList = new List <PitchTile>(); for (int x = 1; x <= pitchWidth; x++) { for (int z = 1; z <= pitchLength; z++) { GameObject pitchTileObj = MatchManager.InstantiateGameObject(returnPitchTilePrefab(x, z)); pitchTileObj.transform.position = new Vector3( pitchTileObj.transform.position.x - XOffset + x, pitchTileObj.transform.position.y, pitchTileObj.transform.position.z - ZOffset + z); pitchTileObj.name = "Pitch tile - " + x + ":" + z; pitchTileObj.transform.SetParent(this.transform); PitchTile pitchTile = pitchTileObj.GetComponent <PitchTile>(); pitchTile.SetCoodinates(x, z); pitchTile.setUnOccupied(); pitchTile.InitRotateGridOverlayObjects(); PitchTiles[x, z] = pitchTileObj; PitchTilesList.Add(pitchTile); getOutLineTiles(pitchTile, x, z); } } }
/* #endregion */ /* #region ---- Set Outline tiles --------------------------------------------------------- */ private void getOutLineTiles(PitchTile tile, int x, int z) { int pitchWidth = MatchManager.PitchGrid.PitchSettings.PitchWidth; int pitchLength = MatchManager.PitchGrid.PitchSettings.PitchLength; Vector3 tilePosition = tile.transform.position; if (x == 1 && z == pitchLength / 2) { tilePosition.x -= outLineOffset; outLineNegX = tilePosition; } if (x == pitchWidth && z == pitchLength / 2) { tilePosition.x += outLineOffset; outLinePosX = tilePosition; } if (z == 1 && x == pitchWidth / 2) { tilePosition.z -= outLineOffset; outLineNegZ = tilePosition; } if (z == pitchLength && x == pitchWidth / 2) { tilePosition.z += outLineOffset; outLinePosZ = tilePosition; } }
public void OnMouseLeftClickPass(MatchPlayer player, PitchTile targetTile) { //TODO: Remove the below code - Just for test... foreach (var ballPoint in targetTile.BallGridPoints) { Debug.Log(ballPoint.gameObject.name); } }
/* #endregion ----------------------------------------------------------------------------- */ /* #endregion */ /* ======================================================================================== */ /* #region ==== P L A Y E R I N S T A N T I A T I O N ==================================== */ public void SetPlayerInfoOnInstantiation(Player player, PitchTile pitchTile, int coordX, int coordZ) { CoordX = coordX; CoordZ = coordZ; CurrentTile = pitchTile; Player = player; Name = Player.Name; IsBallHolder = Player.startWithBall; MaxActionPoints = Player.Stats.MaxActionPoints; CurrentActionPoints = MaxActionPoints; }
/* #endregion */ /* #region ---- Set player Vector position ------------------------------------------------ */ private void setVectorPosition(PitchTile pitchTile, GameObject playerObj) { GameObject pitchTileObj = pitchTile.gameObject; Vector3 position = new Vector3( pitchTileObj.transform.position.x, playerObj.transform.position.y, pitchTileObj.transform.position.z); playerObj.transform.position = position; }
// Check if movement from one tile to another is diagonal and add some small extra cost to make the algorithm to prefer straight movement float costForDiagonalMove(PitchTile fromTile, PitchTile toTile) { if (fromTile.CoordX != toTile.CoordX && fromTile.CoordZ != toTile.CoordZ) { return(0.001f); } else { return(0f); } }
public void AddToReachableTiles(PitchTile tile) { if (ReachedFromTiles == null) { ReachedFromTiles = new List <PitchTile>(); } if (!ReachedFromTiles.Contains(tile)) { ReachedFromTiles.Add(tile); } }
/* #endregion */ /* #endregion */ /* ======================================================================================== */ /* #region ==== GET OVERLAPPING BALL POINTS AND ADD THEIR PITCHTILE TO THIS LIST ========== */ private void OnTriggerEnter(Collider collidingPoint) { if (!collidingPoints.Contains(collidingPoint)) { if (collidingPoint.tag != "BallPointer") { collidingPoints.Add(collidingPoint); BallGridPoint ballPoint = collidingPoint.GetComponent <BallGridPoint>(); PitchTile tile = ballPoint.PitchTile; AddToReachableTiles(tile); } } }
/* #endregion */ /* #region ---- 2. SETUP & START MOVEMENT ------------------------------------------------- */ private void movePlayerSetup(PitchTile targetTile) { MoveTargetTile = targetTile; MoveSourceTile = MatchManager.PitchManager.GetPitchTile(Player.CoordX, Player.CoordZ); PitchManager PitchManager = MatchManager.PitchManager; PathFinding PathFinding = MatchManager.PitchGrid.PathFinding; waypoints = PathFinding.GetPathToTarget(MoveTargetTile, Player); MatchManager.MatchPlayerManager.setPlayerInActionState(true); waypoints.RemoveAt(0); CurrentAction = PlayerAction.Move; }
/* #endregion */ /* ======================================================================================== */ /* #region ==== R O T A T I O N a c t i o n ============================================== */ /* #region ---- 1. CHECK FOR TOTATION ----------------------------------------------------- */ public void CheckForRotation(PitchTile targetTile) { bool isValidTarget = false; if (MatchManager.MatchPlayerManager.CurrentActivePlayer.PlayerMode == PlayerMode.Rotate) { /* #region ---- Check for valid target tile ---- */ foreach (var tile in Player.CurrentTile.NeighbourTiles) { if (tile == targetTile) { isValidTarget = true; break; } } /* #endregion */ /* #region ---- If validTarget ---- */ if (isValidTarget) { int apCost = getApCost(targetTile); if (Player.CurrentActionPoints >= apCost) { if (RotationCounter == MatchManager.MatchPlayerManager.ActionsApCostSettings.MaxRotationsPerTurn) { MatchManager.Hud.UpdateGameMessage(MatchManager.Hud.Messages.NoMoreRotations); MatchManager.Hud.UpdateApCostToString(""); } else { RotationCounter++; Player.FaceTarget(targetTile.transform); Player.UpdateRotationAngle((int)Player.transform.eulerAngles.y); updateStatHud(apCost); clearTryRedrawRotateGrid(); //CurrentAction = PlayerAction.Rotate; USE THIS FOR ANIMATION. } } else { updateHud(); } } /* #endregion */ } }
/* #endregion */ /* ======================================================================================== */ /* #region ==== C R E A T E P L A Y E R S ================================================ */ private void addPlayersToPitch() { List <Player> playersList = MatchManager.MatchTeamManager.PlayerTeam.Players; MatchPlayersList = new List <MatchPlayer>(); Team team = MatchManager.MatchTeamManager.PlayerTeam; if (playersList.Count < 1 || playersList == null) { Debug.Log("The team has no players", this); } else { int count = 0; foreach (var player in playersList) { count++; /* #region ---- Get start position -------------------------------------------- */ int coordX = 0; int coordZ = 0; foreach (KeyValuePair <Player.StartPos, int> pos in player.startPosition) { if (pos.Key == Player.StartPos.X) { coordX = pos.Value; } if (pos.Key == Player.StartPos.Z) { coordZ = pos.Value; } } /* #endregion */ PitchTile pitchTile = MatchManager.PitchManager.GetPitchTile(coordX, coordZ); GameObject playerObj = (GameObject)Instantiate(playerPrefab); MatchPlayer matchPlayer = playerObj.GetComponent <MatchPlayer>(); setPlayerInfo(playerObj, matchPlayer, player, team, pitchTile, count, coordX, coordZ); setPlayerActiveState(playerObj, player); setVectorPosition(pitchTile, playerObj); setTileOccupied(pitchTile, matchPlayer); MatchPlayersList.Add(matchPlayer); SetCurrentBallHolder(matchPlayer); } } }
/* #endregion */ /* #region ---- PitchTile MouseEnter / MouseExit ------------------------------------------ */ public void OnPitchTileMouseEnter(PitchTile tile) { MatchPlayer activePlayer = MatchManager.MatchPlayerManager.CurrentActivePlayer; switch (activePlayer.PlayerMode) { case PlayerMode.Idle: break; case PlayerMode.Move: MatchManager.PitchGrid.PathFinding.DrawPathLine.Draw(tile); break; case PlayerMode.Rotate: MatchManager.PitchGrid.RotateGrid.DrawRotationTarget(activePlayer, tile); break; case PlayerMode.Pass: break; } }
/* #endregion */ /* ======================================================================================== */ /* #region ==== D R A W / C L E A R R O T A T I O N T A R G E T O V E R L A Y ======== */ /* #region ---- Draw Rotation Target Overlay ---------------------------------------------- */ public void DrawRotationTarget(MatchPlayer Player, PitchTile targetTile) { if (Player.PlayerMode == PlayerMode.Rotate) { ClearRotationTarget(Player); List <PitchTile> neighbourTiles = Player.CurrentTile.NeighbourTiles; foreach (var tile in neighbourTiles) { if (tile == targetTile) { tile.ActivateRotateTargetOverlay(true); int direction = Player.GetRotationIndicator(targetTile.transform); int apCost = Player.CalcRotationApCost(direction); MatchManager.Hud.UpdateAccAPCost(apCost); break; } } } }
/* #endregion */ /* #region ---- PitchTile Mouse Left click ------------------------------------------------ */ public void OnPitchTileLeftClick(PitchTile targetTile) { MatchPlayer activePlayer = MatchManager.MatchPlayerManager.CurrentActivePlayer; switch (activePlayer.PlayerMode) { case PlayerMode.Idle: break; case PlayerMode.Move: MatchManager.MatchPlayerManager.CurrentActivePlayer.PlayerActions.CheckForMovement(targetTile); break; case PlayerMode.Rotate: OnMouseLeftClickRotate(activePlayer, targetTile); break; case PlayerMode.Pass: OnMouseLeftClickPass(activePlayer, targetTile); break; } }
/* #endregion */ /* #region ---- Set a pitchTile instance to unoccupied ------------------------------------ */ public void setPitchTileUnOccupied(PitchTile pitchTile) { pitchTile.setUnOccupied(); }
/* #endregion */ /* #region ---- Set a pitchTile instance to occupied (by player) -------------------------- */ public void setPitchTileOccupied(PitchTile pitchTile, MatchPlayer player) { pitchTile.setOccupied(player); }
/* #endregion */ /* #region ---- Set pitchTile Occupied by player ------------------------------------------ */ private void setTileOccupied(PitchTile pitchTile, MatchPlayer playerObj) { MatchManager.PitchManager.setPitchTileOccupied(pitchTile, playerObj); }
/* #endregion */ /* ======================================================================================== */ /* #region ==== D R A W P A T H L I N E ================================================= */ /* #region ---- Draw movement path line from active player to target tile ----------------- */ public void Draw(PitchTile targetIn) { if (MatchManager.MatchPlayerManager.CurrentActivePlayer.PlayerMode == PlayerMode.Move) { if (!MatchManager.MatchPlayerManager.PlayInAction) { PitchManager PitchManager = MatchManager.PitchManager; PitchGrid PitchGrid = MatchManager.PitchGrid; MatchPlayer Player = MatchManager.MatchPlayerManager.GetActivePlayer(); /* #region ---- Reset Path ----------------------------------------- */ ClearPlayerMovePathLines(); setAllTilesNoneViableTarget(PitchManager); ResetAccMoveCost(); MatchManager.PitchGrid.DeactivateMoveTargetOverlay(); /* #endregion ------------------------------------------------------ */ /* #region ---- Set new Path to target ----------------------------- */ List <PitchTile> pathToTarget = PitchGrid.PathFinding.GetPathToTarget(targetIn, Player); listLinePoints = new List <Vector3>(); /* #endregion ------------------------------------------------------ */ /* #region ---- Draw Path Line & Rotate Player --------------------- */ if (pathToTarget != null) { /* #region ---- Player Calculate AP cost for rotation - */ int rotation = Player.GetRotationIndicator(pathToTarget[1].transform); AccumulatedMoveCost = Player.CalcRotationApCost(rotation); /* #endregion --------------------------------------------------- */ /* #region ---- Add to waypoints to List & update HUD ---------- */ GameObject PathLineObject = MatchManager.InstantiateGameObject(PitchGrid.PathLinePrefab); PathLineObject.name = "PlayerMove PathLine"; LineRenderer lineRenderer = PathLineObject.GetComponent <LineRenderer>(); int counter = 0; foreach (PitchTile pathTile in pathToTarget) { counter++; //TODO: QuickFix: Find out why CurrentActionPoints has to be reduced by one for the player to not exceed CurrentActionPoints if (Player.CurrentActionPoints - 1 >= AccumulatedMoveCost) { Vector3 linePoint = new Vector3( pathTile.transform.position.x, PathLineObject.transform.position.y, pathTile.transform.position.z); listLinePoints.Add(linePoint); if (counter != 1) { AccumulatedMoveCost += pathTile.CostToEnter; } pathTile.IsViableTarget = true; } updateAPCostInHUD(AccumulatedMoveCost); } /* #endregion --------------------------------------------------- */ /* #region ---- Draw line and target tile overlay --------------- */ lineRenderer.positionCount = listLinePoints.Count; activateTargetOverlay(listLinePoints); lineRenderer.SetPositions(listLinePoints.ToArray()); /* #endregion --------------------------------------------------- */ } /* #endregion ------------------------------------------------------ */ } } }
/* #endregion ----------------------------------------------------------------------------- */ /* #region ---- Get AP-Cost --------------------------------------------------------------- */ private int getApCost(PitchTile targetTile) { int direction = Player.GetRotationIndicator(targetTile.transform); return(Player.CalcRotationApCost(direction)); }
public void OnMouseLeftClickRotate(MatchPlayer player, PitchTile tile) { MatchManager.MatchPlayerManager.CurrentActivePlayer.PlayerActions.CheckForRotation(tile); }
/* #endregion */ /* ======================================================================================== */ /* #region ==== C R E A T E G R A P H ==================================================== */ public void AddNeighbourTiles() { PitchManager PitchManager = MatchManager.PitchManager; int pitchWidth = MatchManager.PitchGrid.PitchSettings.PitchWidth; int pitchLength = MatchManager.PitchGrid.PitchSettings.PitchLength; for (int x = 1; x <= pitchWidth; x++) { for (int z = 1; z <= pitchLength; z++) { PitchTile PitchTile = PitchManager.GetPitchTile(x, z); PitchTile.NeighbourTiles = new List <PitchTile>(); addNeighbourTiles(x, z, PitchTile); } } void addNeighbourTiles(int x, int z, PitchTile PitchTile) { /* North */ if (z < pitchLength) { PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x, z + 1); PitchTile.NeighbourTiles.Add(neighbourPitchTile); } /* South */ if (z > 1) { PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x, z - 1); PitchTile.NeighbourTiles.Add(neighbourPitchTile); } /* West */ if (x < pitchWidth) { PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x + 1, z); PitchTile.NeighbourTiles.Add(neighbourPitchTile); } /* East */ if (x > 1) { PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x - 1, z); PitchTile.NeighbourTiles.Add(neighbourPitchTile); } /* NorthWest */ if (x < pitchWidth && z < pitchLength) { PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x + 1, z + 1); PitchTile.NeighbourTiles.Add(neighbourPitchTile); } /* NorthEast */ if (x > 1 && z < pitchLength) { PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x - 1, z + 1); PitchTile.NeighbourTiles.Add(neighbourPitchTile); } /* SouthWest*/ if (x < pitchWidth && z > 1) { PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x + 1, z - 1); PitchTile.NeighbourTiles.Add(neighbourPitchTile); } /* SouthEast*/ if (x > 1 && z > 1) { PitchTile neighbourPitchTile = PitchManager.GetPitchTile(x - 1, z - 1); PitchTile.NeighbourTiles.Add(neighbourPitchTile); } } }
/* #endregion */ /* ======================================================================================== */ /* #region ==== C A L C U L A T E & G E T P A T H T O T A R G E T ==================== */ public List <PitchTile> GetPathToTarget(PitchTile targetIn, MatchPlayer sourceIn) { PitchManager PitchManager = MatchManager.PitchManager; Dictionary <PitchTile, float> distanceToSource = new Dictionary <PitchTile, float>(); Dictionary <PitchTile, PitchTile> previousPitchTile = new Dictionary <PitchTile, PitchTile>(); List <PitchTile> unvisitedPitchTiles = new List <PitchTile>(); List <PitchTile> CalculatedPathToTarget = null; //Set target tile PitchTile target = PitchManager.GetPitchTile(targetIn.CoordX, targetIn.CoordZ); //Set source tile PitchTile source = PitchManager.GetPitchTile(sourceIn.CoordX, sourceIn.CoordZ); distanceToSource[source] = 0; previousPitchTile[source] = null; //Set all other tiles and add all (incl. source) to list of unvisited tiles for (int x = 1; x <= MatchManager.PitchGrid.PitchSettings.PitchWidth; x++) { for (int z = 1; z <= MatchManager.PitchGrid.PitchSettings.PitchLength; z++) { PitchTile pitchTile = PitchManager.GetPitchTile(x, z); if (pitchTile != source) { distanceToSource[pitchTile] = Mathf.Infinity; previousPitchTile[pitchTile] = null; } unvisitedPitchTiles.Add(pitchTile); } } while (unvisitedPitchTiles.Count > 0) { //"u" is going to be the unvisited node with the smallest distance. PitchTile u = null; //Find the tile with the shortest Distance to Source foreach (PitchTile possibleU in unvisitedPitchTiles) { if (u == null || distanceToSource[possibleU] < distanceToSource[u]) { u = possibleU; } } if (u == target) { break; } unvisitedPitchTiles.Remove(u); foreach (PitchTile v in u.NeighbourTiles) { //float alt = distanceToSource[u] + u.DistanceToTile(v); float alt = distanceToSource[u] + v.CostToEnter + costForDiagonalMove(u, v); if (alt < distanceToSource[v]) { distanceToSource[v] = alt; previousPitchTile[v] = u; } } } //If the while loop has ended or breaked, either we found the shortest path or there is no path to the target. if (previousPitchTile[target] == null) { return(CalculatedPathToTarget); } else { //Step trough the previousPitchTiles chain and add it to the pathToTarget. CalculatedPathToTarget = new List <PitchTile>(); PitchTile current = target; //start tile while (current != null) { CalculatedPathToTarget.Add(current); current = previousPitchTile[current]; //Reset current to the previous tile in the chain } CalculatedPathToTarget.Reverse(); return(CalculatedPathToTarget); } }
/* #endregion */ /* #region ---- Set player Coordinates and CurrentTile ------------------------------------ */ public void SetCurrentTile(PitchTile tile) { this.CoordX = tile.CoordX; this.CoordZ = tile.CoordZ; this.CurrentTile = tile; }
/* #region ---- Set player info ----------------------------------------------------------- */ private void setPlayerInfo(GameObject playerObj, MatchPlayer matchPlayer, Player player, Team team, PitchTile pitchTile, int count, int coordX, int coordZ) { playerObj.name = $"{team.Name} - Player {count} - {player.Name}"; playerObj.transform.SetParent(this.transform); matchPlayer.SetPlayerInfoOnInstantiation(player, pitchTile, coordX, coordZ); }