forked from s425077/PotatoPlan
221 lines
5.4 KiB
C#
221 lines
5.4 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using Microsoft.Xna.Framework.Input;
|
|
using System;
|
|
|
|
|
|
class Tractor
|
|
{
|
|
static private Vector2 Position;
|
|
private Vector2 TargetPosition;
|
|
private Vector2 Direction;
|
|
private int Spacing, sizeTile;
|
|
private int Speed = 1;
|
|
private Vector2 Size;
|
|
private Random r = new Random();
|
|
private Farm farm = new Farm();
|
|
private Vector2 housePos;
|
|
private String currentTask;
|
|
private SmartTractor smartTractor = new SmartTractor();
|
|
private int Score;
|
|
private int previousTask;
|
|
|
|
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)
|
|
{
|
|
sizeTile = 56;
|
|
Spacing = 1;
|
|
farm.init(new Vector2(100, (GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / sizeTile) - 125 / sizeTile));
|
|
Position = housePos;
|
|
}
|
|
|
|
private int updateDirection(Vector2 Size, Vector2 newPosition) /// Runs when the tractor reaches a tile
|
|
{
|
|
Vector2 DeltaPosition = TargetPosition - Position;
|
|
if (DeltaPosition.X == 0)
|
|
{
|
|
if (DeltaPosition.Y == 0)
|
|
{
|
|
|
|
|
|
|
|
if (housePos != Position)
|
|
{
|
|
int x = (int)Position.X / (sizeTile + Spacing);
|
|
int y = (int)Position.Y / (sizeTile + Spacing);
|
|
currentTask = currentTaskDecider(farm.getCrop(x, y).Status, 0);
|
|
farm.setCropStatus(x, y, Spacing);
|
|
setTargetPosition(housePos); //Returns to the farm
|
|
|
|
}
|
|
else
|
|
{
|
|
setTargetPosition(smartTractor.returnChoice()); //Sets a random Target
|
|
int xTarget = (int)TargetPosition.X / (sizeTile + Spacing);
|
|
int yTarget = (int)TargetPosition.Y / (sizeTile + Spacing);
|
|
currentTask = currentTaskDecider(farm.getCrop(xTarget, yTarget).Status, 1);
|
|
|
|
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
else if (DeltaPosition.Y > 0)
|
|
{
|
|
Direction = new Vector2(0, 1);
|
|
return 0;
|
|
}
|
|
else if (DeltaPosition.Y < 0)
|
|
{
|
|
Direction = new Vector2(0, -1);
|
|
return 0;
|
|
}
|
|
return 0;
|
|
}
|
|
else if (DeltaPosition.X > 0)
|
|
{
|
|
Direction = new Vector2(1, 0);
|
|
return 0;
|
|
}
|
|
else if (DeltaPosition.X < 0)
|
|
{
|
|
Direction = new Vector2(-1, 0);
|
|
return 0;
|
|
}
|
|
return 0;
|
|
|
|
}
|
|
|
|
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
|
|
{
|
|
smartTractor.updateMap(Position, housePos, farm.getCrops(), Size, sizeTile, Spacing, Score);
|
|
Position = Position + Direction;
|
|
updateDirection(Size, Position);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 getPos()
|
|
{
|
|
return Position;
|
|
}
|
|
|
|
public void increaseSpeed()
|
|
{
|
|
Speed++;
|
|
}
|
|
|
|
public void decreaseSpeed()
|
|
{
|
|
if (Speed > 0)
|
|
{
|
|
Speed--;
|
|
}
|
|
|
|
}
|
|
|
|
private void setTargetPosition(Vector2 newPosition) /// sets the TargetPosition once it reaches its destination
|
|
{
|
|
TargetPosition = newPosition;
|
|
}
|
|
public void setSpeed(int newSpeed)
|
|
{
|
|
Speed = newSpeed;
|
|
}
|
|
|
|
public int getSpeed()
|
|
{
|
|
return Speed;
|
|
}
|
|
|
|
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 Score;
|
|
}
|
|
|
|
private String currentTaskDecider(int Status, int Stage)
|
|
{
|
|
previousTask = Status;
|
|
if (previousTask == 3 && Stage != 1)
|
|
{
|
|
Score++;
|
|
}
|
|
if (Stage == 0)
|
|
{
|
|
if (Status == 0)
|
|
{
|
|
return "Returning with nothing after going out for a stroll";
|
|
}
|
|
else if (Status == 1)
|
|
{
|
|
return "Returning with nothing after planting seeds";
|
|
}
|
|
else if (Status == 2)
|
|
{
|
|
return "Returning with nothing after adding fertilizer";
|
|
}
|
|
else if (Status == 3)
|
|
{
|
|
return "Returning with Crops";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (Status == 0)
|
|
{
|
|
return "Going for a stroll";
|
|
}
|
|
else if (Status == 1)
|
|
{
|
|
return "Planting seeds";
|
|
}
|
|
else if (Status == 2)
|
|
{
|
|
return "Adding fertilizer";
|
|
}
|
|
else if (Status == 3)
|
|
{
|
|
return "Going for Crops";
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|