private void Awake() { grid = GameObject.FindGameObjectWithTag("Grid").GetComponent <GridManager>(); tilesWithinRange = new List <Hex>(); //Fetch the Raycaster from the GameObject (the Canvas) UIRaycaster = Manager.instance.transform.GetChild(0).GetComponent <GraphicRaycaster>(); //Fetch the Event System from the Scene UIEventSystem = Manager.instance.transform.GetChild(0).GetComponent <EventSystem>(); tilesAffectByAction = new List <Hex>(); enemiesAffectByAction = new List <Unit>(); cameraRig = GameObject.FindGameObjectWithTag("CameraRig").GetComponent <CameraController>(); if (selectionRuleset == null) { Debug.LogError("Player Controller selection ruleset not set!!!"); } else { currentRuleset = selectionRuleset; } lineRenderer = GetComponent <LineRenderer>(); }
// Set currentRuleset back to selectionRuleset and remove all highlighting private void CancelCurrentAction() { lineRenderer.positionCount = 0; currentRuleset = selectionRuleset; RemoveHighlightedTiles(); if (threatHeightlightTiles) { HighlightEnemiesThreatTiles(enemiesAlive, enemyThreatColor); } if (lightningAttackHex1 != null) { lightningAttackHex1 = null; } if (tileUnderMouse) { tileUnderMouse.MouseExit(); } }
// Process Mouse Input private void ProcessMouseInput() { #region Tile / Unit hover Highlighting Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; // make sure the mouse pointeer isn't above UI and that the raycast is hitting something if (!EventSystem.current.IsPointerOverGameObject(-1) && Physics.Raycast(mouseRay, out hitInfo, Mathf.Infinity, currentRuleset.interactableLayers)) { #region Remove Previous highlighting // if there is a tile under the cursor and it's a new tile then remove the highlight from the previous tile if (tileUnderMouse) { if (previousTileUnderMouse && previousTileUnderMouse != tileUnderMouse) { previousTileUnderMouse.MouseExit(); } previousTileUnderMouse = tileUnderMouse; } // if there is a unit under the cursor and it's a new unit then remove the highlight from the previous unit if (unitUnderMouse) { if (previousUnitUnderMouse && previousUnitUnderMouse != unitUnderMouse) { previousUnitUnderMouse.Highlight(false, Color.green); } previousUnitUnderMouse = unitUnderMouse; } #endregion // try set values, will be null if there isn't an object under the cursor tileUnderMouse = hitInfo.transform.GetComponent <Hex>(); unitUnderMouse = hitInfo.transform.GetComponent <Unit>(); Hex currentHex; if (selectedUnit == null) { selectedUnit = earthUnit; } currentHex = selectedUnit.CurrentTile; if (lightningAttackHex1 != null) { currentHex = lightningAttackHex1; } if (tileUnderMouse) { if (currentAttack == PlayerAttack.lightningSpecial) { // Check if the tile under the mouse is a valid target currentRuleset.CheckValidity(currentHex, tileUnderMouse, true); } else { // Check if the tile under the mouse is a valid target currentRuleset.CheckValidity(currentHex, tileUnderMouse); } // show attack shape if the tile is within attack range if (currentRuleset.WithinRange && currentRuleset.actionType == ActionType.attack || currentRuleset.actionType == ActionType.specialAttack) { ProcessActionHighlighting(tileUnderMouse, hitInfo); } else if (currentRuleset.IsValid && currentRuleset.actionType == ActionType.movement) { // if trying to move then simply highlight the tile under the cursor tileUnderMouse.MouseEnter(currentRuleset.HighlightColour); // get path from Navigation and set the points of the lineRenderer to match it List <Hex> path = Navigation.FindPath(currentHex, tileUnderMouse); if (lineRenderer == null) { lineRenderer = GetComponent <LineRenderer>(); } else { lineRenderer.positionCount = path.Count + 1; lineRenderer.SetPosition(0, currentHex.transform.position + Vector3.up * 0.5f); for (int i = 0; i < path.Count; i++) { lineRenderer.SetPosition(i + 1, path[i].transform.position + Vector3.up * 0.5f); } } } else { // no valid tile under the cursor, remove all highlighting and previewing lineRenderer.positionCount = 0; if (tilesAffectByAction.Count > 0) { foreach (Hex tile in tilesAffectByAction) { tile.MouseExit(); } tilesAffectByAction.Clear(); } if (enemiesAffectByAction.Count > 0) { foreach (Unit unit in enemiesAffectByAction) { unit.PreviewDamage(0); } enemiesAffectByAction.Clear(); } } } else { // no tile is under the mouse, remove highlighting // If there was a tile that was highlighted remove the highlight if (previousTileUnderMouse) { previousTileUnderMouse.MouseExit(); previousTileUnderMouse = null; } } if (unitUnderMouse) { // check if the unit under the cursor is a valid target currentRuleset.CheckValidity(currentHex, unitUnderMouse); // remove highlight from previous unit if there is one and it isn't this unit if (previousUnitUnderMouse != null && previousUnitUnderMouse != unitUnderMouse) { previousUnitUnderMouse.Highlight(false, currentRuleset.HighlightColour); previousUnitUnderMouse = unitUnderMouse; } unitUnderMouse.Highlight(true, currentRuleset.HighlightColour); if (currentRuleset.actionType == ActionType.selection && unitUnderMouse.CompareTag("Enemy")) { // HighlightTilesInRange(unitUnderMouse.MoveRange); } } else { // no unit is under the mouse, remove highlighting // If there was a unit that was highlighted remove the highlight if (previousUnitUnderMouse) { previousUnitUnderMouse.Highlight(false, currentRuleset.HighlightColour); previousUnitUnderMouse = null; } } } else { // If there was a unit that was highlighted remove the highlight if (unitUnderMouse) { unitUnderMouse.Highlight(false, currentRuleset.HighlightColour); unitUnderMouse = null; } // If there was a tile that was highlighted remove the highlight if (previousTileUnderMouse) { previousTileUnderMouse.MouseExit(); previousTileUnderMouse = null; } if (enemiesAffectByAction.Count > 0) { foreach (Unit unit in enemiesAffectByAction) { unit.PreviewDamage(0); } enemiesAffectByAction.Clear(); } } #endregion #region Left Click if (Input.GetMouseButtonDown(0) && currentRuleset.IsValid) { UIPointerEventData = new PointerEventData(UIEventSystem); UIPointerEventData.position = Input.mousePosition; List <RaycastResult> results = new List <RaycastResult>(); UIRaycaster.Raycast(UIPointerEventData, results); bool raycastHitButton = false; if (results.Count != 0) { raycastHitButton = true; } if (raycastHitButton == false) { switch (currentRuleset.actionType) { case ActionType.selection: if (unitUnderMouse) { SelectUnit(unitUnderMouse); } break; case ActionType.movement: selectedUnit.MoveTo(tileUnderMouse, UnitFinishedWalking); foreach (var child in selectedUnit.transform.GetComponentsInChildren <ParticleSystem>()) { if (child.gameObject.name == "HighlightParticle") { HighlightPartical = child; } } HighlightPartical.gameObject.SetActive(false); RemoveHighlightedTiles(); currentRuleset = selectionRuleset; tileUnderMouse.MouseExit(); lineRenderer.positionCount = 0; canInteract = false; Manager.instance.UIController.LockUI(); break; case ActionType.attack: if (tilesAffectByAction.Count > 0) { selectedUnit.BasicAttack(tilesAffectByAction.ToArray(), SpecialAttackStart, SpecialAttackEnd); RemoveHighlightedTiles(); currentRuleset = selectionRuleset; canInteract = false; } break; case ActionType.specialAttack: if (tilesAffectByAction.Count > 0) { // simply attack if it's the earth unit if (selectedUnit == earthUnit) { selectedUnit.SpecialAttack(tilesAffectByAction.ToArray(), AttackStart, AttackEnd); RemoveHighlightedTiles(); currentRuleset = selectionRuleset; canInteract = false; } else { if (lightningAttackHex1 != null && lightningAttackHex1 != tileUnderMouse) // hex1 has been set, set hex2 { selectedUnit.SpecialAttack(tilesAffectByAction.ToArray(), AttackStart, AttackEnd); RemoveHighlightedTiles(); currentRuleset = selectionRuleset; canInteract = false; lightningAttackHex1 = null; lineRenderer.positionCount = 0; break; } // set the hex1 if it's null if (lightningAttackHex1 == null) { lightningAttackHex1 = tileUnderMouse; RemoveHighlightedTiles(); currentRuleset = selectedUnit.GetAction(2).otherRuleset[0]; HighlightTilesInRange(selectedUnit.SpecialAttackRange, lightningAttackHex1, false, true); } } } break; } } } #endregion #region Right Click if (Input.GetMouseButton(1)) { // Right click cancels current action CancelCurrentAction(); } #endregion }
// Set which action of the selected unit is currently being used public void SelectAction(int actionIndex) { RemoveHighlightedTiles(); currentRuleset = selectedUnit.GetAction(actionIndex).ruleset; if (currentAttack == PlayerAttack.lightningSpecial && actionIndex != 2) { lightningAttackHex1 = null; } // set the current attack if (selectedUnit == earthUnit) { if (actionIndex == 1) { currentAttack = PlayerAttack.earthBasic; } else if (actionIndex == 2) { currentAttack = PlayerAttack.earthSpecial; } } else if (selectedUnit == lightningUnit) { if (actionIndex == 1) { currentAttack = PlayerAttack.lightningBasic; } else if (actionIndex == 2) { currentAttack = PlayerAttack.lightningSpecial; } } if (threatHeightlightTiles) { HighlightEnemiesThreatTiles(enemiesAlive, enemyThreatColor); } // Highlight area in range to walk if (currentRuleset.actionType == ActionType.movement) { HighlightTilesInRange(selectedUnit.MoveRange, null, true, true); } // Highlight area in range to attack if (currentRuleset.actionType == ActionType.attack) { if (selectedUnit == earthUnit) { HighlightTilesInRange(selectedUnit.AttackRange, null, false, true, true); } else if (selectedUnit == lightningUnit) { HighlightTilesInRange(selectedUnit.AttackRange, null, false, true, false); } } // Highlight area in range to special attack if (currentRuleset.actionType == ActionType.specialAttack) { HighlightTilesInRange(selectedUnit.SpecialAttackRange, null, false, true); } }