using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class VoxelSpace : MonoBehaviour { public int sizeX = 10; public int sizeY = 10; public int animationFrames = 30; public int fermonsWeakenTime = 500; public int relocateFoodTime = 2000; public int numberOfAnts = 5; public float voxelScale = 1.0f; public GameObject previewShape; public Ant antShape; public GameObject food; public byte[,] voxels; Ant[] ants; GameObject[,] lights; Settings settings; int frame; // -------------------------------------------------------- // -------------------------------------------------------- // ----------------funkcje-do-zadan------------------------ // -------------------------------------------------------- // -------------------------------------------------------- void createVoxelSpace() { this.voxels = new byte[this.sizeX, this.sizeY]; this.lights = new GameObject[this.sizeX, this.sizeY]; for(int x = 0; x < this.sizeX; x++) { for(int y = 0; y< this.sizeY; y++) { this.voxels[x, y] = 0; this.lights[x, y] = Instantiate(previewShape); this.lights[x, y].transform.position = positionInWorld(new Vector3(x, 0, y)); } } } public Vector3Int positionInVoxelSpace(Vector3 worldPosition) { int x = (int)((worldPosition.x - this.transform.position.x) + (sizeX / 2.0f) * voxelScale); int y = (int)((worldPosition.z - this.transform.position.z) + (sizeY / 2.0f) * voxelScale); return new Vector3Int(0,0,0); } public Vector3 positionInWorld(Vector3 voxelPosition) { float x = ((voxelPosition.x + this.transform.position.x - sizeX / 2.0f) / voxelScale); float y = ((voxelPosition.z + this.transform.position.z - sizeY / 2.0f) / voxelScale); return new Vector3(0,0,0); } public int getPheromoneAt(float x, float y) { //zadanie 2.3 Vector3Int vectorVoxel = positionInVoxelSpace(new Vector3(x, 0, y)); if (vectorVoxel.x < 0 || vectorVoxel.x >= sizeX || vectorVoxel.z < 0 || vectorVoxel.z >= sizeY) { return -1; } return voxels[vectorVoxel.x, vectorVoxel.z]; } void updateVoxelSpace() { //zadanie 2.5 if (voxelSpace.positionInVoxelSpace(transform.position).x == voxelSpace.sizeX - 1 || voxelSpace.positionInVoxelSpace(transform.position).z == voxelSpace.sizeY - 1) { //hasFood = false; foodSearch = 1; } } // -------------------------------------------------------- // -------------------------------------------------------- // ----------------koniec-zadan---funkcje pomocnicze------- // -------------------------------------------------------- // -------------------------------------------------------- // Start is called before the first frame update void Start() { loadSettings(); frame = 0; setTableScale(); createVoxelSpace(); // placeFood(); addAnts(); } // Update is called once per frame void Update() { Debug.Log("max="+maxVoxel()); Debug.Log("start: "+(new Vector3(3.5f,5.5f,4.5f))); Debug.Log("midle: "+(positionInVoxelSpace(new Vector3(3.5f,5.5f,4.5f)))); Debug.Log("endin: "+(positionInWorld(positionInVoxelSpace(new Vector3(3.5f,5.5f,4.5f))))); Debug.Log("zeroo: "+(positionInWorld(new Vector3(0,0,0)))); if(frame%animationFrames == 0) { foreach(Ant ant in ants) { ant.sniff(); } updateLights(); GameObject.Find("anthill").transform.position = positionInWorld(new Vector3(-1,0,-1));// gameObject.transform.position - (gameObject.transform.localScale)/2.0f; } else { foreach(Ant ant in ants) { ant.go(); } } if(frame%fermonsWeakenTime == 0) { updateVoxelSpace(); } if(frame%relocateFoodTime == 0) { placeFood(); } frame++; if (Input.GetKeyUp(KeyCode.Escape)) { SceneManager.LoadScene (sceneName:"menu"); } } int maxVoxel() { int m = 0; for(int i = 0; i 0.51f) { lights[i,j].transform.GetChild(0).GetComponent().enabled=true; lights[i,j].transform.GetChild(0).GetComponent().intensity = newScale*3.0f; } else { lights[i,j].transform.GetChild(0).GetComponent().enabled=false; } } } } Vector2 foodPos; void placeFood() { if(foodPos != null) { voxels[(int)(foodPos.x),(int)(foodPos.y)] = 0; } int x = (int)Random.Range(4,sizeX-1); int y = (int)Random.Range(4,sizeY-1); food.transform.position = positionInWorld(new Vector3(x,0,y)); foodPos = new Vector2(x,y); voxels[x,y] = 255; } void setTableScale() { gameObject.transform.localScale = new Vector3(sizeX+2.0f,sizeY+2.0f, 12.0f); Vector3 randomMove = new Vector3(Random.Range(-10.0f, 10.0f), 0, Random.Range(-10.0f, 10.0f)); gameObject.transform.position = new Vector3(sizeX/2.0f, -0.1f, sizeY/2.0f)+randomMove; GameObject camera = GameObject.Find("Main Camera"); camera.transform.position = new Vector3(sizeX/2.0f, sizeX/2.0f+sizeY/2.0f+1.0f, sizeY/2.0f)+randomMove; } void loadSettings() { if(SettingsMenu.settings != null) { settings = SettingsMenu.settings; sizeX = settings.sizeX; sizeY = settings.sizeY; animationFrames = settings.fps; fermonsWeakenTime = settings.weakenPheromones; relocateFoodTime = settings.refreshFood; numberOfAnts = settings.antsCount; } } }