1
0
forked from s425077/PotatoPlan
JoelForkTest/Game1/Sources/Tractor.cs

221 lines
5.4 KiB
C#
Raw Permalink Normal View History

2020-04-07 17:50:31 +02:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
class Tractor
{
static private Vector2 Position;
2020-04-07 17:50:31 +02:00
private Vector2 TargetPosition;
private Vector2 Direction;
private int Spacing, sizeTile;
private int Speed = 1;
private Vector2 Size;
2020-04-07 17:50:31 +02:00
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;
2020-04-07 17:50:31 +02:00
public void updateSizing(Input input, int Status, Vector2 newHousePos)
2020-04-07 17:50:31 +02:00
{
Spacing = input.getSpacing();
sizeTile = input.getTileSize();
Size = input.getSize();
updatePosition(input.getSize(), Status);
housePos = newHousePos;
2020-04-07 17:50:31 +02:00
}
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
2020-04-07 17:50:31 +02:00
{
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;
2020-04-07 17:50:31 +02:00
}
else if (DeltaPosition.Y > 0)
{
Direction = new Vector2(0, 1);
return 0;
2020-04-07 17:50:31 +02:00
}
else if (DeltaPosition.Y < 0)
{
Direction = new Vector2(0, -1);
return 0;
2020-04-07 17:50:31 +02:00
}
return 0;
2020-04-07 17:50:31 +02:00
}
else if (DeltaPosition.X > 0)
{
Direction = new Vector2(1, 0);
return 0;
2020-04-07 17:50:31 +02:00
}
else if (DeltaPosition.X < 0)
{
Direction = new Vector2(-1, 0);
return 0;
2020-04-07 17:50:31 +02:00
}
return 0;
2020-04-07 17:50:31 +02:00
}
public void updatePosition(Vector2 Size, int Status) /// updates the position
2020-04-07 17:50:31 +02:00
{
farm.updateSize(Size, sizeTile, Spacing);
for (int i = 0; i < Speed; i++) //Where all the choices the tractor does comes from
2020-04-07 17:50:31 +02:00
{
smartTractor.updateMap(Position, housePos, farm.getCrops(), Size, sizeTile, Spacing, Score);
2020-04-07 17:50:31 +02:00
Position = Position + Direction;
updateDirection(Size, Position);
2020-04-07 17:50:31 +02:00
}
}
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;
}
2020-04-07 17:50:31 +02:00
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;
}
2020-04-07 17:50:31 +02:00
}