Modelowanie_Wirtualnych_Swi.../Assets/Scripts/VoxelSpace.cs
2021-05-23 21:13:09 +02:00

233 lines
7.4 KiB
C#

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()
{
//zadanie 2.1
voxels = new byte[sizeX, sizeY];
for(int i = 0; i < sizeX; ++i)
{
for(int j = 0; j < sizeY; ++j)
{
voxels[i, j] = 0;
}
}
lights = new GameObject[sizeX, sizeY];
for(int i = 0; i < sizeX; ++i)
{
for(int j = 0; j < sizeY; ++j)
{
lights[i, j] = Instantiate(previewShape);
lights[i, j].transform.position = positionInWorld(new Vector3(i, 0, j));
}
}
}
public Vector3Int positionInVoxelSpace(Vector3 worldPosition)
{
// zadanie 2.2 a
return new Vector3Int((int)((worldPosition.x - gameObject.transform.position.x + sizeX / 2.0f) * voxelScale), 0,(int)((worldPosition.z - gameObject.transform.position.z + sizeY / 2.0f) * voxelScale));
}
public Vector3 positionInWorld(Vector3 voxelPosition)
{
// zadanie 2.2 b
return new Vector3((float)((voxelPosition.x + gameObject.transform.position.x - sizeX / 2.0f) / voxelScale), 0, (float)((voxelPosition.z + gameObject.transform.position.z - sizeY / 2.0f) / voxelScale));
}
public int getPheromoneAt(float x, float y)
{
//zadanie 2.3
Vector3Int pos = positionInVoxelSpace(new Vector3(x, 0, y));
if(pos.x > 0 && pos.x < sizeX && pos.z > 0 && pos.z < sizeY){
return voxels[pos.x, pos.z];
}
return -1;
}
void updateVoxelSpace()
{
//zadanie 2.5
for(int x = 0; x < sizeX; x++){
for(int y = 0; y < sizeY; y++){
if(voxels[x, y] < 250 && voxels[x, y] > 0){
voxels[x, y] = (byte)(voxels[x, y] - 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<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);
Debug.Log(i+"x"+j+"="+m+" posW "+a+" posV "+b+" phs "+c);
}
}
}
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<Light>().enabled=true;
lights[i,j].transform.GetChild(0).GetComponent<Light>().intensity = newScale*3.0f;
}
else
{
lights[i,j].transform.GetChild(0).GetComponent<Light>().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;
}
}
}