//The head moves if there is nothing blocking its current direction in the world. public void Move(int vx, int vy) { if (World.grid[posy][posx] != World.HEAD) { World.grid[posy][posx] = World.FREE; } posy = posy - vy; posx = posx + vx; if (isTail) { World.grid[posy][posx] = World.TAIL; } else { World.grid[posy][posx] = World.BODY; } transform.position = new Vector3(posx * World.blockSize, -1 * posy * World.blockSize, 0); if (!isTail) { next.Move(this.vx, this.vy); } this.vx = vx; this.vy = vy; }
//The head moves if there is nothing blocking its current direction in the world. virtual public void Move() { PrintGrid(); if (oneMoveDelayDone) { oldVx = vx; oldVy = vy; } if (dir == dirs.LEFT) { vx = -1; vy = 0; } else if (dir == dirs.RIGHT) { vx = 1; vy = 0; } else if (dir == dirs.UP) { vx = 0; vy = 1; } else if (dir == dirs.DOWN) { vx = 0; vy = -1; } if (World.grid[posy - vy][posx + vx] <= 2) { if (digesting) { bodyController oldNext = next; next = ((GameObject)Instantiate(Resources.Load("prefabs/body"), new Vector3(posx * World.blockSize, -1 * posy * World.blockSize, 0), Quaternion.identity)).GetComponent <bodyController>(); next.setV(oldVx, oldVy); if (oldNext != null) { next.setNext(oldNext); } digesting = false; finishedDigesting = true; } if (World.grid[posy - vy][posx + vx] == World.FOOD) { Destroy(nextFood); World.generateFood(this); digesting = true; } if (!finishedDigesting) { World.grid[posy][posx] = World.FREE; } posy = posy - vy; posx = posx + vx; World.grid[posy][posx] = World.HEAD; transform.position = new Vector3(posx * World.blockSize, -1 * posy * World.blockSize, 0); if (!finishedDigesting && next != null) { next.Move(oldVx, oldVy); } oneMoveDelayDone = true; finishedDigesting = false; } else { Debug.Log("Game Over!"); World.Reset(); SceneManager.LoadScene("snake"); } }