public void Init(eBlockType type, int x, int y) { // TODO move this out of here Vector2 uv = Vector2.zero; switch (type) { case eBlockType.Grass: uv = new Vector2(0, 1); break; case eBlockType.Stone: uv = new Vector2(0, 0); break; default: Debug.LogErrorFormat("Invalid type {0}", type); break; } // TODO //_bData = GetBlockDataForType(type); _mData = new MeshData(x, y, uv); _mesh.Clear(); _mesh.vertices = _mData.vertices; _mesh.triangles = _mData.triangles; _mesh.uv = _mData.uvs; _mesh.RecalculateNormals(); }
private Block CreateBlock(eBlockType type, int x, int y) { GameObject blockObj = (GameObject)Instantiate(_blockPrefab, this.transform); blockObj.name = string.Format("Block[{0},{1}]", x, y); blockObj.transform.localPosition = new Vector3(x * _blockSize, (y + 1) * _blockSize); blockObj.transform.localScale = Vector3.one * _blockSize; Block block = blockObj.GetComponent <Block>(); if (block != null) { block.Init(type, x, y); } else { Debug.LogErrorFormat("No Block Component found on {0}", blockObj.name); } return(block); }
public static IEnumerator EvaluateAI(Character mCharacter) { Debug.Log("Starte AI für " + mCharacter.pName + " Faction:" + mCharacter.pFaction + " Pat:" + mCharacter.pPatrouilleSelection); //mCharacter.pApCurrent = mCharacter.pAp; eAIState pAIState = eAIState.Patrouille; Character pActiveTarget = null; List <Tile> pSteps = new List <Tile>(); #region AI v2 while (mCharacter.pApCurrent > 9) { Debug.Log("AI current AP " + mCharacter.pApCurrent); yield return(new WaitForSeconds(0.5f)); // blocks debug stepping. Remove if nessesary switch (pAIState) { case eAIState.Patrouille: #region Patrouille pActiveTarget = AIfindTarget(mCharacter); if (pActiveTarget != null) { pAIState = eAIState.Fight; Debug.Log("AI switching from patrol to fight mode"); break; } if (mCharacter.pPatrouilleSelection == ePatrouilleSelection.Static) // wenn statischer gegner ohne wegpunkte, early exit { mCharacter.pApCurrent = 0; Debug.Log("AI Nothing to do, skipping turn"); break; } Tile patrolTile; if (mCharacter.pFaction == eFactions.AI1) { if (mCharacter.pPatrouilleSelection == ePatrouilleSelection.A) { patrolTile = GridManager.pInstance.GetTileAt(GridManager.pInstance.pCurrentLevel.pAI1PatrouilleA[mCharacter.mPatWaypointID]); } else { patrolTile = GridManager.pInstance.GetTileAt(GridManager.pInstance.pCurrentLevel.pAI1PatrouilleB[mCharacter.mPatWaypointID]); } } else { if (mCharacter.pPatrouilleSelection == ePatrouilleSelection.A) { patrolTile = GridManager.pInstance.GetTileAt(GridManager.pInstance.pCurrentLevel.pAI2PatrouilleA[mCharacter.mPatWaypointID]); } else { patrolTile = GridManager.pInstance.GetTileAt(GridManager.pInstance.pCurrentLevel.pAI2PatrouilleB[mCharacter.mPatWaypointID]); } } pSteps = GridManager.pInstance.GetPathTo(mCharacter.pTile, patrolTile); if (pSteps == null) { break; } int currentStepsLeft = mCharacter.pWalkRange; if (pSteps.Count < 3) //close enough to pat-point? { Debug.Log("AI patrol to next waypoint"); mCharacter.mPatWaypointID = (mCharacter.mPatWaypointID + 1) % (mCharacter.pPatrouilleSelection == ePatrouilleSelection.A ? GridManager.pInstance.pCurrentLevel.pAI1PatrouilleA.Length : GridManager.pInstance.pCurrentLevel.pAI1PatrouilleB.Length); if (mCharacter.pFaction == eFactions.AI1) { if (mCharacter.pPatrouilleSelection == ePatrouilleSelection.A) { patrolTile = GridManager.pInstance.GetTileAt(GridManager.pInstance.pCurrentLevel.pAI1PatrouilleA[mCharacter.mPatWaypointID]); } else { patrolTile = GridManager.pInstance.GetTileAt(GridManager.pInstance.pCurrentLevel.pAI1PatrouilleB[mCharacter.mPatWaypointID]); } } else { if (mCharacter.pPatrouilleSelection == ePatrouilleSelection.A) { patrolTile = GridManager.pInstance.GetTileAt(GridManager.pInstance.pCurrentLevel.pAI2PatrouilleA[mCharacter.mPatWaypointID]); } else { patrolTile = GridManager.pInstance.GetTileAt(GridManager.pInstance.pCurrentLevel.pAI2PatrouilleB[mCharacter.mPatWaypointID]); } } pSteps = GridManager.pInstance.GetPathTo(mCharacter.pTile, patrolTile); } if (pSteps.Count > mCharacter.pWalkRange) { yield return(mCharacter.MoveEnumerator(pSteps[pSteps.Count - mCharacter.pWalkRange])); } else { yield return(mCharacter.MoveEnumerator(pSteps[0])); } pActiveTarget = AIfindTarget(mCharacter); if (pActiveTarget != null) { pAIState = eAIState.Fight; } #endregion break; case eAIState.Fight: #region Fight Debug.Log(mCharacter.pName + " is searching for cover against " + pActiveTarget.pName); List <Tile> walkableTiles = GridManager.pInstance.GetReachableTiles(mCharacter.pTile, mCharacter.pWalkRange); //remove tiles with players on it for (int i = walkableTiles.Count - 1; i > 0; --i) { if (walkableTiles[i].pCharacterId != -1) { walkableTiles.Remove(walkableTiles[i]); } } eBlockType currentCoverTarget = eBlockType.Empty; //Creating lists with all options to choose from List <Tile> coverPositionsFull = new List <Tile>(); List <Tile> coverPositionsHalf = new List <Tile>(); foreach (Tile walkableTile in walkableTiles) { if (GridManager.pInstance.GetVisibilityToTarget(mCharacter.pTile, pActiveTarget.pTile, mCharacter.pVisionRange) == eVisibility.Seethrough && walkableTile.pCharacterId == -1) // now with occupied tiles { switch (GridManager.pInstance.GetCoverFromTarget(mCharacter.pTile, pActiveTarget.pTile)) { case eBlockType.Blocked: if (currentCoverTarget != eBlockType.HalfBlocked) { currentCoverTarget = eBlockType.Blocked; } coverPositionsFull.Add(walkableTile); break; case eBlockType.HalfBlocked: currentCoverTarget = eBlockType.HalfBlocked; coverPositionsHalf.Add(walkableTile); break; } } } // sort the choosen options by distance. Switch just for avoiding unnessesary sorting of tiles that are not suitable List <Tile> wayToTarget = new List <Tile>(); switch (currentCoverTarget) { case eBlockType.Empty: wayToTarget = GridManager.pInstance.GetPathTo(pActiveTarget.pTile, mCharacter.pTile); break; case eBlockType.Blocked: coverPositionsFull.Sort((a, b) => Tile.Distance(a, pActiveTarget.pTile).CompareTo(Tile.Distance(b, pActiveTarget.pTile))); wayToTarget = GridManager.pInstance.GetPathTo(mCharacter.pTile, coverPositionsFull[0]); break; case eBlockType.HalfBlocked: coverPositionsHalf.Sort((a, b) => Tile.Distance(a, pActiveTarget.pTile).CompareTo(Tile.Distance(b, pActiveTarget.pTile))); wayToTarget = GridManager.pInstance.GetPathTo(mCharacter.pTile, coverPositionsHalf[0]); break; default: break; } /* * // if we are going to get cover * if (currentCoverTarget != eBlockType.Empty && wayToTarget.Count <= mCharacter.pApCurrent / mCharacter.pWalkCost && wayToTarget.Count > 1) * { * Debug.Log("Move to cover because it seems reachable"); * for (int i = 0; i < (wayToTarget.Count > mCharacter.pWalkRange ? mCharacter.pWalkRange : wayToTarget.Count); ++i) // walk until range is spent or target reached * { * yield return AIevaluator.AImove(mCharacter, pSteps[1]); * } * mCharacter.pApCurrent -= 10; * break; * } */ // if we need to get in range and no cover is present if (currentCoverTarget == eBlockType.Empty && wayToTarget.Count > mCharacter.Range) { // weglist durchgehen und einen punkt wählen der dicht genug für schuss ist und nicht blockiert ist int possibleFinalWaypoint = 0; for (int wayPointCounter = wayToTarget.Count - 1; wayPointCounter > 0; --wayPointCounter) { if (wayToTarget[wayPointCounter].pCharacterId == -1 && wayToTarget.Count - wayPointCounter - 1 <= mCharacter.Range) { possibleFinalWaypoint = wayPointCounter; } else if (wayToTarget[wayPointCounter].pCharacterId == -1 && wayToTarget.Count - wayPointCounter - 1 > mCharacter.Range) { possibleFinalWaypoint = wayPointCounter; break; } } Debug.Log("Move to Enemy because out of range, possible " + wayToTarget.Count.ToString() + " steps"); yield return(mCharacter.MoveEnumerator(wayToTarget[mCharacter.pWalkRange])); break; } if (mCharacter.pApCurrent < 15) // not enough AP to shoot { Debug.Log("No shots left, skipping turn"); mCharacter.pApCurrent = 0; break; } Debug.Log("AI shooting Normal spell at " + pActiveTarget.pName); yield return(mCharacter.StandardAttackCoroutine(pActiveTarget.pTile)); // shoot normal spell at target pActiveTarget = AIfindTarget(mCharacter); //check for survivors if (pActiveTarget == null) { pAIState = eAIState.Patrouille; } #endregion break; default: Debug.LogError("AI in unknown state"); break; } }// loop if char has still points to use #endregion // end AI turn Debug.Log("AI spent all AP, end turn"); yield return(new WaitForSeconds(1)); mCharacter.pAura.SetActive(false); EntityManager.pInstance.EndRound(mCharacter); //ending AI turn and switch to next player GameManager.pInstance.pCurrentFaction = GameManager.pInstance.pCurrentFaction == eFactions.AI1 ? eFactions.Player1 : eFactions.Player2; //EntityManager.pInstance.pGetFactionEntities(pCurrentFraction)[0].Select(); // move camera to an faction entity GameManager.pInstance.ChangeState(eGameState.Select); }