1
0
forked from s425077/PotatoPlan
PotatoPlan/Game1/Sources/Smart/SmartTractor.cs
2020-05-03 13:05:05 +02:00

119 lines
2.9 KiB
C#

using System;
using Microsoft.Xna.Framework;
using System;
class SmartTractor
{
private Crops[,] crops;
private Vector2 housePos;
private Vector2 tractorPos;
private Vector2 Size;
private Vector2 Target;
private Path path;
private int tileSize;
private int Score;
private int Spacing;
private Random r = new Random();
private Astar astar = new Astar();
//What to do next
public Path returnChoice(int task)
{
astar.update(crops, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing));
if (task == 0)
{
//To the house
getTargetPosition((int)housePos.X / (tileSize + Spacing), (int)housePos.Y / (tileSize + Spacing));
}
else
{
//To the fields
getTargetPosition(r.Next(0, (int)Size.X), r.Next(0, (int)Size.Y));
}
astar.findPath();
createPath();
return path;
}
//Updates the variables every frame
public void updateMap(Vector2 newTractorPos, Vector2 newHousePos, Crops[,] newCropsStatus, Vector2 newSize, int newTileSize, int newSpacing, int newScore)
{
crops = newCropsStatus;
housePos = newHousePos;
tractorPos = newTractorPos;
Size = newSize;
tileSize = newTileSize;
Spacing = newSpacing;
Score = newScore;
}
private void getTargetPosition(int x, int y)
{
Target = new Vector2(x, y) * (tileSize + Spacing);
}
//Only for testing without obstacles
private void createPath()
{
path = new Path();
Vector2 targetPos = Target / (tileSize + Spacing);
Vector2 currentPath = tractorPos / tileSize;
currentPath.X = (float)Math.Round(currentPath.X);
currentPath.Y = (float)Math.Round(currentPath.Y);
do
{
if (currentPath.X == targetPos.X)
{
//found X pos
if (currentPath.Y == targetPos.Y)
{
//found y pos
}
else if (currentPath.Y < targetPos.Y)
{
currentPath = new Vector2(currentPath.X, currentPath.Y + 1);
path.setNode(currentPath);
}
else if (currentPath.Y > targetPos.Y)
{
currentPath = new Vector2(currentPath.X, currentPath.Y - 1);
path.setNode(currentPath);
}
}
else if (currentPath.X < targetPos.X)
{
currentPath = new Vector2(currentPath.X + 1, currentPath.Y);
path.setNode(currentPath);
}
else if (currentPath.X > targetPos.X)
{
currentPath = new Vector2(currentPath.X - 1, currentPath.Y);
path.setNode(currentPath);
}
} while (currentPath != targetPos);
}
}