PotatoPlan/Game1/Sources/Objects/Tractor.cs

196 lines
4.8 KiB
C#
Raw Normal View History

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
{
private int Spacing, sizeTile, Speed = 1;
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;
if (DeltaPosition.X == 0)
{
if (DeltaPosition.Y == 0)
{
calculateNewPath(newPosition);
}
else if (DeltaPosition.Y > 0)
{
Direction = new Vector2(0, 1) * tractorSpeed;
}
else if (DeltaPosition.Y < 0)
{
Direction = new Vector2(0, -1) * tractorSpeed;
}
}
else if (DeltaPosition.X > 0)
{
Direction = new Vector2(1, 0) * tractorSpeed;
}
else if (DeltaPosition.X < 0)
{
Direction = new Vector2(-1, 0) * tractorSpeed;
}
}
//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
{
smartTractor.updateMap(Position, housePos, farm.getCrops(), Size, sizeTile, Spacing, scoreSystem.getScore());
Position = Position + Direction;
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
path = smartTractor.returnChoice(1);
int xTarget = (int)TargetPosition.X / (sizeTile + Spacing);
int yTarget = (int)TargetPosition.Y / (sizeTile + Spacing);
currentTask = scoreSystem.MessageAndScore(farm.getCrop(xTarget, yTarget).Status, 1);
}
TargetPosition = path.Reduce().getVector() * (sizeTile + Spacing);
updateDirection(Size, newPosition);
}
else
{
TargetPosition = path.Reduce().getVector() * (sizeTile + Spacing);
updateDirection(Size, newPosition);
}
}
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;
}
}