75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Ant : MonoBehaviour
|
|
{
|
|
// Start is called before the first frame update
|
|
public bool hasFood = false;
|
|
public sbyte foodSearch = -1; // 1 it has food, -1 it searches for food
|
|
public VoxelSpace voxelSpace;
|
|
|
|
|
|
public void sniff()
|
|
{
|
|
//zadanie 2.3
|
|
Vector3 position = gameObject.transform.position;
|
|
Vector3Int positionVoxel = voxelSpace.positionInVoxelSpace(new Vector3(position.x, 0, position.z));
|
|
|
|
if(voxelSpace.getPheromoneAt(position.x, position.z) > 250){
|
|
hasFood = true;
|
|
foodSearch = 1;
|
|
}
|
|
|
|
if((int)positionVoxel.x == 0 && (int)positionVoxel.z == 0){
|
|
hasFood = false;
|
|
foodSearch = -1;
|
|
}
|
|
|
|
if(voxelSpace.getPheromoneAt(position.x, position.z) == -1){
|
|
foodSearch = -1;
|
|
}
|
|
|
|
if((int)positionVoxel.x == voxelSpace.sizeX - 1 || (int)positionVoxel.z == voxelSpace.sizeY - 1){
|
|
foodSearch = 1;
|
|
}
|
|
if(voxelSpace.getPheromoneAt((position.x - foodSearch * voxelSpace.voxelScale), position.z) > voxelSpace.getPheromoneAt(position.x, (position.z - foodSearch * voxelSpace.voxelScale))){
|
|
turnHorizontaly();
|
|
}else if(voxelSpace.getPheromoneAt((position.x - foodSearch * voxelSpace.voxelScale), position.z) < voxelSpace.getPheromoneAt(position.x, (position.z - foodSearch * voxelSpace.voxelScale))){
|
|
turnVerticaly();
|
|
}else if (positionVoxel.z - foodSearch < 0){
|
|
turnHorizontaly();
|
|
}else if(positionVoxel.x - foodSearch < 0){
|
|
turnVerticaly();
|
|
}else{
|
|
|
|
int randomDirection = Random.Range(1, 3);
|
|
|
|
if (randomDirection == 1){
|
|
turnVerticaly();
|
|
}else{
|
|
turnHorizontaly();
|
|
}
|
|
}
|
|
|
|
if(hasFood == true){
|
|
voxelSpace.voxels[voxelSpace.positionInVoxelSpace(position).x, voxelSpace.positionInVoxelSpace(position).z] = (byte)(voxelSpace.voxels[voxelSpace.positionInVoxelSpace(position).x, voxelSpace.positionInVoxelSpace(position).z] + 40);
|
|
}
|
|
|
|
go();
|
|
}
|
|
|
|
void turnVerticaly() // /\ \/
|
|
{
|
|
gameObject.transform.rotation = Quaternion.Euler(0,90*(foodSearch*-1.0f)-90, 0);
|
|
}
|
|
void turnHorizontaly() // <-->
|
|
{
|
|
gameObject.transform.rotation = Quaternion.Euler(0,90*(foodSearch*-1.0f),0);
|
|
}
|
|
public void go()
|
|
{
|
|
gameObject.transform.position += gameObject.transform.forward * (1.0f/voxelSpace.animationFrames);
|
|
}
|
|
}
|