2020-05-03 13:05:05 +02:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Tractor
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-05-04 01:17:10 +02:00
|
|
|
|
private int Spacing, sizeTile, Speed = 1, Rotation = 180, rotationSpeed = 5;
|
2020-05-03 13:05:05 +02:00
|
|
|
|
private float tractorSpeed = 1;
|
|
|
|
|
private String currentTask;
|
|
|
|
|
|
|
|
|
|
private Vector2 Position, TargetPosition, Direction, Size, housePos;
|
|
|
|
|
|
|
|
|
|
private Path path = new Path();
|
|
|
|
|
private Random r = new Random();
|
|
|
|
|
private Farm farm = new Farm();
|
|
|
|
|
|
|
|
|
|
private SmartTractor smartTractor = new SmartTractor();
|
|
|
|
|
private ScoreSystem scoreSystem = new ScoreSystem();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void updateSizing(Input input, int Status, Vector2 newHousePos)
|
|
|
|
|
{
|
|
|
|
|
Spacing = input.getSpacing();
|
|
|
|
|
sizeTile = input.getTileSize();
|
|
|
|
|
Size = input.getSize();
|
|
|
|
|
updatePosition(input.getSize(), Status);
|
|
|
|
|
housePos = newHousePos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void init(Rectangle house, Input input)
|
|
|
|
|
{
|
|
|
|
|
sizeTile = input.getTileSize();
|
|
|
|
|
Spacing = input.getSpacing();
|
|
|
|
|
farm.init(new Vector2(100, (GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / sizeTile) - 125 / sizeTile));
|
|
|
|
|
Position = housePos;
|
|
|
|
|
TargetPosition = new Vector2(house.X, house.Y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Runs when the tractor reaches a tile
|
|
|
|
|
private void updateDirection(Vector2 Size, Vector2 newPosition)
|
|
|
|
|
{
|
|
|
|
|
Vector2 DeltaPosition = TargetPosition - Position;
|
2020-05-04 01:17:10 +02:00
|
|
|
|
|
2020-05-03 13:05:05 +02:00
|
|
|
|
if (DeltaPosition.X == 0)
|
|
|
|
|
{
|
|
|
|
|
if (DeltaPosition.Y == 0)
|
|
|
|
|
{
|
|
|
|
|
calculateNewPath(newPosition);
|
|
|
|
|
}
|
|
|
|
|
else if (DeltaPosition.Y > 0)
|
|
|
|
|
{
|
2020-05-04 01:17:10 +02:00
|
|
|
|
updateRotation(0);
|
2020-05-03 13:05:05 +02:00
|
|
|
|
}
|
|
|
|
|
else if (DeltaPosition.Y < 0)
|
|
|
|
|
{
|
2020-05-04 01:17:10 +02:00
|
|
|
|
updateRotation(1);
|
2020-05-03 13:05:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (DeltaPosition.X > 0)
|
|
|
|
|
{
|
2020-05-04 01:17:10 +02:00
|
|
|
|
updateRotation(2);
|
2020-05-03 13:05:05 +02:00
|
|
|
|
}
|
|
|
|
|
else if (DeltaPosition.X < 0)
|
|
|
|
|
{
|
2020-05-04 01:17:10 +02:00
|
|
|
|
updateRotation(3);
|
2020-05-03 13:05:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Moves the tractor
|
|
|
|
|
public void updatePosition(Vector2 Size, int Status) /// updates the position
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
farm.updateSize(Size, sizeTile, Spacing);
|
|
|
|
|
for (int i = 0; i < Speed; i++) //Where all the choices the tractor does comes from
|
|
|
|
|
{
|
2020-05-04 01:17:10 +02:00
|
|
|
|
smartTractor.updateMap(Position, housePos, farm.getCrops(), Size, sizeTile, Spacing, scoreSystem.getScore(), Rotation);
|
|
|
|
|
|
2020-05-03 13:05:05 +02:00
|
|
|
|
updateDirection(Size, Position);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void calculateNewPath(Vector2 newPosition)
|
|
|
|
|
{
|
|
|
|
|
if (path.getCount() == 0)
|
|
|
|
|
{
|
|
|
|
|
if (housePos != Position)
|
|
|
|
|
{
|
|
|
|
|
//Returns to the farm
|
|
|
|
|
int x = (int)Position.X / (sizeTile + Spacing);
|
|
|
|
|
int y = (int)Position.Y / (sizeTile + Spacing);
|
|
|
|
|
currentTask = scoreSystem.MessageAndScore(farm.getCrop(x, y).Status, 0);
|
|
|
|
|
farm.setCropStatus(x, y, Spacing);
|
|
|
|
|
path = smartTractor.returnChoice(0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//Sets a random Target
|
|
|
|
|
int xTarget = (int)TargetPosition.X / (sizeTile + Spacing);
|
|
|
|
|
int yTarget = (int)TargetPosition.Y / (sizeTile + Spacing);
|
|
|
|
|
currentTask = scoreSystem.MessageAndScore(farm.getCrop(xTarget, yTarget).Status, 1);
|
2020-05-03 22:28:52 +02:00
|
|
|
|
path = smartTractor.returnChoice(1);
|
2020-05-03 13:05:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-03 16:35:46 +02:00
|
|
|
|
TargetPosition = path.Reduce().getCords() * (sizeTile + Spacing);
|
2020-05-03 13:05:05 +02:00
|
|
|
|
updateDirection(Size, newPosition);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-05-03 16:35:46 +02:00
|
|
|
|
TargetPosition = path.Reduce().getCords() * (sizeTile + Spacing);
|
2020-05-03 13:05:05 +02:00
|
|
|
|
updateDirection(Size, newPosition);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 01:17:10 +02:00
|
|
|
|
private void updateRotation(int Destination)
|
|
|
|
|
{
|
|
|
|
|
if (Destination == 0)
|
|
|
|
|
{
|
|
|
|
|
if (Rotation == 0)
|
|
|
|
|
{
|
|
|
|
|
Direction = new Vector2(0, 1) * tractorSpeed;
|
|
|
|
|
Position = Position + Direction;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (Rotation == 0)
|
|
|
|
|
{
|
|
|
|
|
//Do nothing
|
|
|
|
|
}
|
|
|
|
|
else if (Rotation > 180)
|
|
|
|
|
{
|
|
|
|
|
if (Rotation >= 360)
|
|
|
|
|
{
|
|
|
|
|
Rotation = 0;
|
|
|
|
|
}
|
|
|
|
|
Rotation = Rotation + rotationSpeed;
|
|
|
|
|
}
|
|
|
|
|
else if (Rotation <= 180 && Rotation > 0)
|
|
|
|
|
{
|
|
|
|
|
Rotation = Rotation - rotationSpeed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (Destination == 1)
|
|
|
|
|
{
|
|
|
|
|
if (Rotation == 180)
|
|
|
|
|
{
|
|
|
|
|
Direction = new Vector2(0, -1) * tractorSpeed;
|
|
|
|
|
Position = Position + Direction;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (Rotation == 180)
|
|
|
|
|
{
|
|
|
|
|
//Do nothing
|
|
|
|
|
}
|
|
|
|
|
else if (Rotation >= 0 && Rotation < 180)
|
|
|
|
|
{
|
|
|
|
|
Rotation = Rotation + rotationSpeed;
|
|
|
|
|
}
|
|
|
|
|
else if (Rotation < 360 && Rotation > 180)
|
|
|
|
|
{
|
|
|
|
|
Rotation = Rotation - rotationSpeed;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-03 13:05:05 +02:00
|
|
|
|
|
2020-05-04 01:17:10 +02:00
|
|
|
|
}
|
|
|
|
|
else if (Destination == 2)
|
|
|
|
|
{
|
|
|
|
|
if (Rotation == 270)
|
|
|
|
|
{
|
|
|
|
|
Direction = new Vector2(1, 0) * tractorSpeed;
|
|
|
|
|
Position = Position + Direction;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (Rotation == 270)
|
|
|
|
|
{
|
|
|
|
|
//Do nothing
|
|
|
|
|
}
|
|
|
|
|
else if (Rotation > 90 && Rotation < 270)
|
|
|
|
|
{
|
|
|
|
|
Rotation = Rotation + rotationSpeed;
|
|
|
|
|
}
|
|
|
|
|
else if (Rotation < 90 || Rotation < 360)
|
|
|
|
|
{
|
|
|
|
|
if (Rotation <= 0)
|
|
|
|
|
{
|
|
|
|
|
Rotation = 360;
|
|
|
|
|
}
|
|
|
|
|
Rotation = Rotation - rotationSpeed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (Destination == 3)
|
|
|
|
|
{
|
|
|
|
|
if (Rotation == 90)
|
|
|
|
|
{
|
|
|
|
|
Direction = new Vector2(-1, 0) * tractorSpeed;
|
|
|
|
|
Position = Position + Direction;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (Rotation == 90)
|
|
|
|
|
{
|
|
|
|
|
//Do nothing
|
|
|
|
|
}
|
|
|
|
|
else if ( Rotation < 270 && Rotation > 90)
|
|
|
|
|
{
|
|
|
|
|
Rotation = Rotation - rotationSpeed;
|
|
|
|
|
}
|
|
|
|
|
else if (Rotation >= 0 || Rotation > 270)
|
|
|
|
|
{
|
|
|
|
|
if (Rotation >= 360)
|
|
|
|
|
{
|
|
|
|
|
Rotation = 0;
|
|
|
|
|
}
|
|
|
|
|
Rotation = Rotation + rotationSpeed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float getRotation()
|
|
|
|
|
{
|
|
|
|
|
return MathHelper.ToRadians(Rotation);
|
|
|
|
|
|
|
|
|
|
}
|
2020-05-03 13:05:05 +02:00
|
|
|
|
|
|
|
|
|
public Vector2 getPos()
|
|
|
|
|
{
|
|
|
|
|
return Position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void increaseSpeed()
|
|
|
|
|
{
|
|
|
|
|
Speed++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void decreaseSpeed()
|
|
|
|
|
{
|
|
|
|
|
if (Speed > 0)
|
|
|
|
|
{
|
|
|
|
|
Speed--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setSpeed(int newSpeed)
|
|
|
|
|
{
|
|
|
|
|
Speed = newSpeed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setTractorSpeed(tractorPositionCorrector corrector)
|
|
|
|
|
{
|
|
|
|
|
tractorSpeed = corrector.getTractorSpeed();
|
|
|
|
|
Position = corrector.getPosition();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getSpeed()
|
|
|
|
|
{
|
|
|
|
|
return Speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float getTractorSpeed()
|
|
|
|
|
{
|
|
|
|
|
return tractorSpeed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setPos(Vector2 newPos)
|
|
|
|
|
{
|
|
|
|
|
Position = newPos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Farm getFarm()
|
|
|
|
|
{
|
|
|
|
|
return farm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector2 getTargetPosition()
|
|
|
|
|
{
|
|
|
|
|
return TargetPosition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getCurrentTask()
|
|
|
|
|
{
|
|
|
|
|
return currentTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getScore()
|
|
|
|
|
{
|
|
|
|
|
return scoreSystem.getScore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Path getPath()
|
|
|
|
|
{
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
}
|