MWS/voxelSpace/Assets/Scripts/VoxelSpace.cs

226 lines
7.4 KiB
C#
Raw Normal View History

2021-09-16 19:42:18 +02:00
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<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;
}
}
}