-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 -1 -1 0 0 0 0 0 0 0 -1 -1 0 0 0 0 0 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 0 0 0 0 0 0 0 -1 -1 0 0 0 0 0 0 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
0 d := 0
loc, d d + 1 d := d + 1 ( ) ( , < )
, 1 —
using UnityEngine; using System.Collections; public class Battlefield2 : MonoBehaviour { public static int[,] battlefield; // public int enemyNumber = 6, playerNumber = 3; //public public static int x, y; // public GameObject enemyMilitiaman; // public GameObject swordsmanGO; // . public static bool ready = false; // , . void Start () { battlefield = new int[,]{ // . 1 - . 0 - {1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1}}; setEnemyPosition (enemyNumber); // setPlayerPosition (playerNumber); // ready = true; // } // Update is called once per frame void Update () { } void setEnemyPosition(int numberEnemies){ ////// ////// , 1 (14,2) ///// // GameObject go = Instantiate(enemyMilitiaman, new Vector3(14, 2, 10), Quaternion.identity) as GameObject; battlefield[14, 2] = 1; // , , , . } void setPlayerPosition(int numberPlayerWarior){ ////// ////// , 1 (2,6) ///// GameObject go = Instantiate(swordsmanGO, new Vector3(2, 6, 10), Quaternion.identity) as GameObject; battlefield[2, 6] = 1; } public static void printMatrix(){ string arr = ""; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { arr += battlefield[i,j] + ","; } arr += "\n"; } print (arr); } }
using UnityEngine; using System.Collections; using System; public class Swordsman2 : Warrior { private Vector3 currentPosition; private Vector3 lastPosition; private bool ready = true; private bool readyAttack = true; private bool endBattle = false; private GameObject closestEnemy; // GameObject[] gos; // private float waitMove; // void Start () { currentPosition = transform.localPosition; // lastPosition = currentPosition; // . // waitMove = UnityEngine.Random.Range (0.4f, 0.65f); } void Update () { // if (Battlefield.ready) { if(ready){ // , Enemy gos = GameObject.FindGameObjectsWithTag("Enemy"); if(gos.Length == 0){ endBattle = true; // , print ("End Battle"); } //Ń– Ń–, Ń– if(!endBattle){ // GameObject goClosestEnemy = findClosestEnemy(); int targetX, targetY; targetX = (int)goClosestEnemy.transform.localPosition.x; targetY = (int)goClosestEnemy.transform.localPosition.y; int[,] cMap = findWave(targetX, targetY); // if(!stopMove(targetX, targetY)) // , // StartCoroutine(move(cMap, targetX, targetY)); if(readyAttack){//, if(stopMove(targetX, targetY)){ StartCoroutine(attack()); } } } // currentPosition = transform.localPosition; //, Battlefield.battlefield[(int)currentPosition.x, (int)currentPosition.y] = 1; // , , if (currentPosition != lastPosition) { Battlefield.battlefield[(int)lastPosition.x, (int)lastPosition.y] = 0; lastPosition = currentPosition; // } } } } private IEnumerator attack(){ readyAttack = false; // , 1 0.8 yield return new WaitForSeconds(0.8f); //////// //////// //////// readyAttack = true; } /// <summary> /// </summary> /// <param name="cMap"> </param> /// <param name="targetX"> x</param> /// <param name="targetY"> y</param> private IEnumerator move(int[,] cMap, int targetX, int targetY){ ready = false; int[] neighbors = new int[8]; // // Vector3 moveTO = new Vector3(-1,0,10); // , for neighbors[0] = cMap[(int)currentPosition.x+1, (int)currentPosition.y+1]; neighbors[1] = cMap[(int)currentPosition.x, (int)currentPosition.y+1]; neighbors[2] = cMap[(int)currentPosition.x-1, (int)currentPosition.y+1]; neighbors[3] = cMap[(int)currentPosition.x-1, (int)currentPosition.y]; neighbors[4] = cMap[(int)currentPosition.x-1,(int) currentPosition.y-1]; neighbors[5] = cMap[(int)currentPosition.x, (int)currentPosition.y-1]; neighbors[6] = cMap[(int)currentPosition.x+1,(int) currentPosition.y-1]; neighbors[7] = cMap[(int)currentPosition.x+1,(int) currentPosition.y]; for(int i = 0; i < 8; i++){ if(neighbors[i] == -2) // , , // , // neighbors[i] = 99999; } Array.Sort(neighbors); // // . for (int x = (int)currentPosition.x-1; x <= (int)currentPosition.x+1; x++) { for (int y = (int)currentPosition.y+1; y >= (int)currentPosition.y-1; y--) { if(cMap[x,y] == neighbors[0]){ // , moveTO = new Vector3(x,y,10); } } } // , . // , , 8 , if(moveTO == new Vector3(-1,0,10)) moveTO = new Vector3(currentPosition.x, currentPosition.y, 10); // , - // 1 transform.localPosition = moveTO; // . yield return new WaitForSeconds(waitMove); ready = true; } // //TargetX, TargetY - public int[,] findWave(int targetX, int targetY){ bool add = true; // // , int[,] cMap = new int[Battlefield.X, Battlefield.Y]; int x, y, step = 0; // 0 for (x = 0; x < Battlefield.x; x++) { for (y = 0; y < Battlefield.y; y++) { if(Battlefield.battlefield[x,y] == 1) cMap[x,y] = -2; // 1, ( -2) else cMap[x,y] = -1; // } } // , cMap[targetX,targetY] = 0; while (add == true) { add = false; for (x = 0; x < Battlefield.x; x++) { for (y = 0; y < Battlefield.y; y++) { if(cMap[x,y] == step){ // , // + 1 if(y - 1 >= 0 && cMap[x, y - 1] != -2 && cMap[x, y - 1] == -1) cMap[x, y - 1] = step + 1; if(x - 1 >= 0 && cMap[x - 1, y] != -2 && cMap[x - 1, y] == -1) cMap[x - 1, y] = step + 1; if(y + 1 >= 0 && cMap[x, y + 1] != -2 && cMap[x, y + 1] == -1) cMap[x, y + 1] = step + 1; if(x + 1 >= 0 && cMap[x + 1, y] != -2 && cMap[x + 1, y] == -1) cMap[x + 1, y] = step + 1; } } } step++; add = true; if(cMap[(int)transform.localPosition.x, (int)transform.localPosition.y] > 0) // add = false; if(step > Battlefield.X * Battlefield.Y) // , add = false; } return cMap; // , move() } // , public bool stopMove(int targetX, int targetY){ bool move = false; for (int x = (int)currentPosition.x-1; x <= (int)currentPosition.x+1; x++) { for (int y = (int)currentPosition.y+1; y >= (int)currentPosition.y-1; y--) { if(x == targetX && y == targetY){ move = true; } } } return move; } // GameObject findClosestEnemy() { float distance = Mathf.Infinity; Vector3 position = transform.position; foreach (GameObject go in gos) { float curDistance = Vector3.Distance(go.transform.position,position); if (curDistance < distance) { closestEnemy = go; distance = curDistance; } } return closestEnemy; } }
float curDistance = Vector3.Distance(F.transform.position, S.transform.position);
float[] neighbors = new int[9]; int n = 0; Vector3 goTo; for (int x = (int)currentPosition.x-1; x <= (int)currentPosition.x+1; x++) { for (int y = (int)currentPosition.y+1; y >= (int)currentPosition.y-1; y--) { float curDistance = Vector3.Distance(new Vector3(x,y,0), S.transform.position); neighbors[n] = curDistance; } }
Source: https://habr.com/ru/post/264189/
All Articles