void CalculateGameLogic() { // 1. get the tile i stand now. Tile TileStand = BoardClass._game.AllTiles.Single(o => o.X == Piece.Location.X && o.Y == Piece.Location.Y); // get the tile GameObject HumanOnTile = BoardClass._gamePieces_Humans.Find( o => ( (HumanBehaviour)o.GetComponent("HumanBehaviour")).Piece.X == TileStand.X && ( (HumanBehaviour)o.GetComponent("HumanBehaviour")).Piece.Y == TileStand.Y ); // the same place ... Battle ! if(HumanOnTile) { ((HumanBehaviour)HumanOnTile.GetComponent("HumanBehaviour")).m_HumanPiece_Behaviour = HumanBehaviour.HUMAN_BEHAVIOUR.HUMAN_BEHAVIOUR_FIGHT; TileStand.IsBattleField = true; // stay at the same place m_AnimalPiece_Behaviour_Prev = m_AnimalPiece_Behaviour; m_AnimalPiece_Behaviour = ANIMAL_BEHAVIOUR.ANIMAL_BEHAVIOUR_FIGHT; Position_Destination =BoardBehavior.GetWorldCoordinates(TileStand.Location.X, TileStand.Location.Y, 0f); return; } TileStand.CanPass = true; // release this tile TileStand.Tile_StandStatus = TILE_STAND_STATUS.TILE_STAND_STATUS_NONE; // 2.get all the neighbours int nNewX = Piece.X; int nNewY = Piece.Y; //List <Tile>AvailableTiles = TileStand.Neighbours.ToList(); List <Tile>AvailableTiles = TileStand.NeighboursWithHuman.ToList(); // // 2. include self , calculate all it's attribute , apply to probability AvailableTiles.Add(TileStand); int nRandomChoose = UnityEngine.Random.Range(0 , AvailableTiles.Count); Debug.Log( "CalculateGameLogic:"+ Piece.X + ","+Piece.Y +"neib # "+AvailableTiles.Count+",choose:"+ nRandomChoose ); nNewX = AvailableTiles[nRandomChoose].X; nNewY = AvailableTiles[nRandomChoose].Y; //3. search the human , go to find or escape /// 3-1 find the nearest one /// 3-2 decide to attack or escape int nCount = BoardClass._gamePieces_Humans.Count; GameObject nearestHuman; var sp = Piece; GamePiece dp; int nShortestIndex=0; int nShortestDistance=1000; for( int i = 0 ; i <nCount ; i++ ) { // get the nearest one , find the position nearestHuman= BoardClass._gamePieces_Humans[i]; dp = ((HumanBehaviour)nearestHuman.GetComponent("HumanBehaviour")).Piece; int tmepDistance = Math.Max( Math.Abs( dp.Location.X - sp.Location.X ), Math.Abs( dp.Location.Y - sp.Location.Y ) ); if(tmepDistance < nShortestDistance) { nShortestDistance = tmepDistance; nShortestIndex = i; } } // ok , we get the nearest human , now go for it ! Debug.Log("shortest distance["+nShortestIndex+"]="+ nShortestDistance); nearestHuman= BoardClass._gamePieces_Humans[nShortestIndex]; dp = ((HumanBehaviour)nearestHuman.GetComponent("HumanBehaviour")).Piece; /// find the nearest one to Selected human from all Available Tiles nShortestIndex=0; nShortestDistance=1000; for(int i = 0 ; i < AvailableTiles.Count ; i++) { int tmepDistance = Math.Max( Math.Abs( dp.Location.X - AvailableTiles[i].Location.X ), Math.Abs( dp.Location.Y - AvailableTiles[i].Location.Y ) ); if(tmepDistance < nShortestDistance) { nShortestDistance = tmepDistance; nShortestIndex = i; } } nRandomChoose = nShortestIndex; nNewX = AvailableTiles[nRandomChoose].Location.X; nNewY = AvailableTiles[nRandomChoose].Location.Y; // 4. AvailableTiles[nRandomChoose].CanPass = false; // occupy now //AvailableTiles[nRandomChoose].Tile_StandStatus =TILE_STAND_STATUS.TILE_STAND_STATUS_HUMAN; /// hey now i stand on this tile AvailableTiles[nRandomChoose].Tile_StandStatus =TILE_STAND_STATUS.TILE_STAND_STATUS_ANIMAL; /// hey now i stand on this tile // 5. decide the action depends on probability m_AnimalPiece_Behaviour_Prev = m_AnimalPiece_Behaviour; m_AnimalPiece_Behaviour = ANIMAL_BEHAVIOUR.ANIMAL_BEHAVIOUR_MOVE_CASUAL; /* * process the detail behavior * */ /// To process the AI activity Piece = new GamePiece(new Point(nNewX, nNewY)); //transform.position = BoardBehavior.GetWorldCoordinates(Piece.X, Piece.Y, 0f); Position_Destination =BoardBehavior.GetWorldCoordinates(Piece.X, Piece.Y, 0f); }
// Use this for initialization void Start() { Piece.m_StrongValue=2; CoreGame = GameObject.Find("coreGame"); BoardClass = CoreGame.GetComponent("BoardBehavior") as BoardBehavior; /// Init the TextUI GameObject go = new GameObject("Text_"+ this.name); UI_StatusText = (GUIText)go.AddComponent( typeof(GUIText) ); UI_StatusText.transform.parent = transform; Font BroadwayFont = (Font)Resources.Load("Fonts/Arial"); UI_StatusText.font = BroadwayFont; UI_StatusText.text = "Init"; UI_StatusText.fontSize=16; UI_StatusText.material.color = Color.green; UI_StatusText.transform.position=new Vector3(0,0,0); TextCoords = new Vector3(0,0,0); /// Status control init m_AnimalPiece_Behaviour =m_AnimalPiece_Behaviour_Prev = ANIMAL_BEHAVIOUR.ANIMAL_BEHAVIOUR_IDLE; m_AnimalStatus = ANIMAL_STATUS.ANIMAL_STATUS_INIT; m_nCurrentFrame = 0 ; m_bDistroy = false; UI_StatusText.enabled = ( CoreGame.GetComponent("MainGameWorld") as MainGameWorld).bDebugShowMsg_Creature; }