Corrected some tasks from cw 4
This commit is contained in:
parent
13ce6e74be
commit
8e1a532137
42
cw4/Ant.cs
42
cw4/Ant.cs
@ -9,6 +9,7 @@ public class Ant : MonoBehaviour
|
|||||||
public sbyte foodSearch = -1; // 1 it has food, -1 it searches for food
|
public sbyte foodSearch = -1; // 1 it has food, -1 it searches for food
|
||||||
public VoxelSpace voxelSpace;
|
public VoxelSpace voxelSpace;
|
||||||
|
|
||||||
|
private static readonly System.Random random = new System.Random();
|
||||||
|
|
||||||
public void sniff()
|
public void sniff()
|
||||||
{
|
{
|
||||||
@ -23,17 +24,22 @@ public class Ant : MonoBehaviour
|
|||||||
hasFood = false;
|
hasFood = false;
|
||||||
foodSearch = -1;
|
foodSearch = -1;
|
||||||
}
|
}
|
||||||
else if (voxelSpace.positionInVoxelSpace(transform.position).x == voxelSpace.sizeX - 1 || voxelSpace.positionInVoxelSpace(transform.position).z == voxelSpace.sizeY - 1)
|
else if (
|
||||||
|
voxelSpace.positionInVoxelSpace(transform.position).x == voxelSpace.sizeX - 1
|
||||||
|
|| voxelSpace.positionInVoxelSpace(transform.position).z == voxelSpace.sizeY - 1)
|
||||||
{
|
{
|
||||||
foodSearch = 1;
|
foodSearch = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
var pos = voxelSpace.positionInVoxelSpace(transform.position);
|
var pos = voxelSpace.positionInVoxelSpace(transform.position);
|
||||||
if ((foodSearch == -1 && pos.x < voxelSpace.voxels.GetLength(0) -1 && pos.z < voxelSpace.voxels.GetLength(1) -1 && voxelSpace.voxels[pos.x+1, pos.z] == voxelSpace.voxels[pos.x, pos.z+1]) || (foodSearch == 1 && pos.x > 0 && pos.z > 0 && voxelSpace.voxels[pos.x-1, pos.z] == voxelSpace.voxels[pos.x, pos.z-1]))
|
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 random = new System.Random();
|
var next = random.Next(0, 2);
|
||||||
var next = random.Next(0, 1);
|
if (next == 0)
|
||||||
if (next == 0)
|
|
||||||
{
|
{
|
||||||
turnHorizontaly();
|
turnHorizontaly();
|
||||||
}
|
}
|
||||||
@ -42,18 +48,18 @@ public class Ant : MonoBehaviour
|
|||||||
turnVerticaly();
|
turnVerticaly();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ((foodSearch == -1 && pos.x < voxelSpace.voxels.GetLength(0) -1 && pos.z < voxelSpace.voxels.GetLength(1) -1 && voxelSpace.voxels[pos.x+1, pos.z] > voxelSpace.voxels[pos.x, pos.z+1]) || (foodSearch == 1 && pos.x > 0 && pos.z > 0 && voxelSpace.voxels[pos.x-1, pos.z] < voxelSpace.voxels[pos.x, pos.z-1]))
|
else if ((foodSearch == -1 && nextX > nextZ) || (foodSearch == 1 && previousX < previuosZ))
|
||||||
{
|
{
|
||||||
turnVerticaly();
|
turnHorizontaly();
|
||||||
}
|
}
|
||||||
else if ((foodSearch == -1 && pos.x < voxelSpace.voxels.GetLength(0) -1 && pos.z < voxelSpace.voxels.GetLength(1) -1 && voxelSpace.voxels[pos.x+1, pos.z] < voxelSpace.voxels[pos.x, pos.z+1]) || (foodSearch == 1 && pos.x > 0 && pos.z > 0 && voxelSpace.voxels[pos.x-1, pos.z] > voxelSpace.voxels[pos.x, pos.z-1]))
|
else if ((foodSearch == -1 && nextX < nextZ) || (foodSearch == 1 && previousX > previuosZ))
|
||||||
{
|
{
|
||||||
turnVerticaly();
|
turnVerticaly();
|
||||||
}
|
}
|
||||||
if (hasFood)
|
if (hasFood)
|
||||||
{
|
{
|
||||||
voxelSpace.voxels[pos.x, pos.z] = (byte)(voxelSpace.voxels[pos.x, pos.z] + 40);
|
voxelSpace.voxels[pos.x, pos.z] = (byte)(voxelSpace.voxels[pos.x, pos.z] + 40);
|
||||||
}
|
}
|
||||||
go();
|
go();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ public class VoxelSpace : MonoBehaviour
|
|||||||
frame = 0;
|
frame = 0;
|
||||||
setTableScale();
|
setTableScale();
|
||||||
createVoxelSpace();
|
createVoxelSpace();
|
||||||
// placeFood();
|
placeFood();
|
||||||
addAnts();
|
addAnts();
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -109,13 +109,6 @@ public class VoxelSpace : MonoBehaviour
|
|||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
void Update()
|
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)
|
if (frame % animationFrames == 0)
|
||||||
{
|
{
|
||||||
foreach (Ant ant in ants)
|
foreach (Ant ant in ants)
|
||||||
@ -159,8 +152,6 @@ public class VoxelSpace : MonoBehaviour
|
|||||||
var a = positionInWorld(new Vector3(i, 0, j));
|
var a = positionInWorld(new Vector3(i, 0, j));
|
||||||
var b = positionInVoxelSpace(a);
|
var b = positionInVoxelSpace(a);
|
||||||
var c = getPheromoneAt(a.x, a.z);
|
var c = getPheromoneAt(a.x, a.z);
|
||||||
|
|
||||||
Debug.Log(i + "x" + j + "=" + m + " posW " + a + " posV " + b + " phs " + c);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user