ZMWSLI0-SL2021-GR11/cw4/Ant.cs

79 lines
2.5 KiB
C#
Raw Normal View History

2021-05-23 00:02:10 +02:00
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;
2021-05-23 17:55:53 +02:00
private static readonly System.Random random = new System.Random();
2021-05-23 00:02:10 +02:00
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;
}
2021-05-23 17:55:53 +02:00
else if (
voxelSpace.positionInVoxelSpace(transform.position).x == voxelSpace.sizeX - 1
|| voxelSpace.positionInVoxelSpace(transform.position).z == voxelSpace.sizeY - 1)
2021-05-23 00:02:10 +02:00
{
foodSearch = 1;
}
var pos = voxelSpace.positionInVoxelSpace(transform.position);
2021-05-23 17:55:53 +02:00
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))
2021-05-23 00:02:10 +02:00
{
2021-05-23 17:55:53 +02:00
var next = random.Next(0, 2);
if (next == 0)
2021-05-23 00:02:10 +02:00
{
turnHorizontaly();
}
else
{
turnVerticaly();
}
}
2021-05-23 17:55:53 +02:00
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);
}
2021-05-23 00:02:10 +02:00
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);
}
}