79 lines
2.5 KiB
C#
79 lines
2.5 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;
|
|
|
|
private static readonly System.Random random = new System.Random();
|
|
|
|
public void sniff()
|
|
{
|
|
//zadanie 2.3
|
|
if (voxelSpace.getPheromoneAt(transform.position.x, transform.position.z) > 250)
|
|
{
|
|
hasFood = true;
|
|
foodSearch = 1;
|
|
}
|
|
else if (voxelSpace.positionInVoxelSpace(transform.position) == new Vector3Int(0,0,0))
|
|
{
|
|
hasFood = false;
|
|
foodSearch = -1;
|
|
}
|
|
else if (
|
|
voxelSpace.positionInVoxelSpace(transform.position).x == voxelSpace.sizeX - 1
|
|
|| voxelSpace.positionInVoxelSpace(transform.position).z == voxelSpace.sizeY - 1)
|
|
{
|
|
foodSearch = 1;
|
|
}
|
|
var pos = voxelSpace.positionInVoxelSpace(transform.position);
|
|
Debug.Log(pos);
|
|
var nextX = pos.x < voxelSpace.sizeX - 1 ? voxelSpace.voxels[pos.x + 1, pos.z] : -1;
|
|
var nextZ = pos.z < voxelSpace.sizeY - 1 ? voxelSpace.voxels[pos.x, pos.z + 1] : -1;
|
|
var previousX = pos.x > 0 ? voxelSpace.voxels[pos.x - 1, pos.z] : -1;
|
|
var previuosZ = pos.z > 0 ? voxelSpace.voxels[pos.x, pos.z - 1] : -1;
|
|
if ((foodSearch == -1 && nextX == nextZ) || (foodSearch == 1 && previousX == previuosZ))
|
|
{
|
|
var next = random.Next(0, 2);
|
|
if (next == 0)
|
|
{
|
|
turnHorizontaly();
|
|
}
|
|
else
|
|
{
|
|
turnVerticaly();
|
|
}
|
|
}
|
|
else if ((foodSearch == -1 && nextX > nextZ) || (foodSearch == 1 && previousX < previuosZ))
|
|
{
|
|
turnHorizontaly();
|
|
}
|
|
else if ((foodSearch == -1 && nextX < nextZ) || (foodSearch == 1 && previousX > previuosZ))
|
|
{
|
|
turnVerticaly();
|
|
}
|
|
if (hasFood)
|
|
{
|
|
voxelSpace.voxels[pos.x, pos.z] = (byte)(voxelSpace.voxels[pos.x, pos.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);
|
|
}
|
|
}
|