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 - gameObject.transform.position.x + (sizeX / 2.0f)) * voxelScale); int z = (int)((worldPosition.z - gameObject.transform.position.z + (sizeY / 2.0f)) * voxelScale); return new Vector3Int(x, 0, z); } public Vector3 positionInWorld(Vector3 voxelPosition) { float x = ((voxelPosition.x + gameObject.transform.position.x - sizeX / 2.0f) / voxelScale); float z = ((voxelPosition.z + gameObject.transform.position.z - sizeY / 2.0f) / voxelScale); return new Vector3(x, 0, z); } public int getPheromoneAt(float x, float y) { Vector3Int pos = positionInVoxelSpace(new Vector3(x, 0, y)); if (0 < pos.x && pos.x < sizeX && 0 < pos.z && pos.z < sizeY) { return voxels[pos.x, pos.z]; } return -1; } void updateVoxelSpace() { //zadanie 2.5 for(var i = 0; i < voxels.GetLength(0); i++) { for(var j = 0; j < voxels.GetLength(1); j++) { var v = voxels[i,j]; if (0 < v && v < 250) { v--; } } } } // -------------------------------------------------------- // -------------------------------------------------------- // ----------------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() { 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 < sizeX; i++) { for (int j = 0; j < sizeY; j++) { if (m < voxels[i, j]) { m = voxels[i, j]; var a = positionInWorld(new Vector3(i, 0, j)); var b = positionInVoxelSpace(a); var c = getPheromoneAt(a.x, a.z); } } } return m; } void addAnts() { ants = new Ant[numberOfAnts]; for (int i = 0; i < numberOfAnts; i++) { ants[i] = Instantiate(antShape); ants[i].gameObject.transform.position = positionInWorld(new Vector3(1, 0.3f, 1)); ants[i].gameObject.transform.rotation = Quaternion.Euler(0, -90, 0); ants[i].foodSearch = -1; ants[i].hasFood = false; ants[i].voxelSpace = this; } } void updateLights() { for (int i = 0; i < sizeX; i++) { for (int j = 0; j < sizeY; j++) { float newScale = voxels[i, j] / 1020.0f; lights[i, j].transform.localScale = new Vector3(newScale, newScale, newScale); if (newScale * 3.0f > 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; } } }