PotatoPlan/Game1/Sources/Objects/Tractor.cs
2020-05-24 21:22:17 +02:00

188 lines
5.6 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
class Tractor
{
private int Spacing, sizeTile, Speed = 1;
private float tractorSpeed = 1;
private Vector2 Position, TargetPosition, Size, housePos, oldDeltaPosition, DeltaPosition;
private Path path = new Path();
private SmartTractor smartTractor = new SmartTractor();
private HandleRotation handleRotation = new HandleRotation();
private int WaitFrame = 30;
private int j;
public void updateSizing(Input input, int Status, Vector2 newHousePos, DayNightCycle Time)
{
Spacing = input.getSpacing();
sizeTile = input.getTileSize();
Size = input.getSize();
updatePosition(input.getSize(), Status);
housePos = newHousePos;
smartTractor.UpdateCrops(Speed, Time);
}
public void init(Rectangle house, Input input)
{
sizeTile = input.getTileSize();
Spacing = input.getSpacing();
Position = housePos;
TargetPosition = new Vector2(house.X, house.Y);
smartTractor.init(new Vector2(house.X, house.Y));
}
// Runs when the tractor reaches a tile
private void updateDirection(Vector2 Size, Vector2 newPosition)
{
DeltaPosition = TargetPosition - Position;
handleRotation.checkTile(Position, sizeTile, Spacing, tractorSpeed, smartTractor.getFarm().getCrop((int)Math.Round(Position.X / (sizeTile + Spacing)), (int)Math.Round(Position.Y / (sizeTile + Spacing))));
if (DeltaPosition.X == 0)
{
if (DeltaPosition.Y == 0)
{
calculateNewPath(newPosition);
}
else if (DeltaPosition.Y > 0)
{
Position = handleRotation.UpdatePosition(0, tractorSpeed, Position, smartTractor.getFarm().getCrop((int)Math.Round(Position.X / (sizeTile + Spacing)), (int)Math.Round(Position.Y / (sizeTile + Spacing))), DeltaPosition, TargetPosition);
}
else if (DeltaPosition.Y < 0)
{
Position = handleRotation.UpdatePosition(1, tractorSpeed, Position, smartTractor.getFarm().getCrop((int)Math.Round(Position.X / (sizeTile + Spacing)), (int)Math.Round(Position.Y / (sizeTile + Spacing))), DeltaPosition, TargetPosition);
}
}
else if (DeltaPosition.X > 0)
{
Position = handleRotation.UpdatePosition(2, tractorSpeed, Position, smartTractor.getFarm().getCrop((int)Math.Round(Position.X / (sizeTile + Spacing)), (int)Math.Round(Position.Y / (sizeTile + Spacing))), DeltaPosition, TargetPosition);
}
else if (DeltaPosition.X < 0)
{
Position = handleRotation.UpdatePosition(3, tractorSpeed, Position, smartTractor.getFarm().getCrop((int)Math.Round(Position.X / (sizeTile + Spacing)), (int)Math.Round(Position.Y / (sizeTile + Spacing))), DeltaPosition, TargetPosition);
}
}
public void updatePosition(Vector2 Size, int Status) // updates the position
{
//farm.updateSize(Size, sizeTile, Spacing);
for (int i = 0; i < Speed; i++)
{
updateDirection(Size, Position);
/*
if (!smartTractor.getWaitTwoFrames())
{
updateDirection(Size, Position);
j = WaitFrame;
}
else if (j != 0)
{
j--;
}
else
{
smartTractor.setWaitTwoFrames(false);
}*/
}
}
public void calculateNewPath(Vector2 newPosition)
{
if (path.getCount() == 0)
{
if (Position.X / (sizeTile + Spacing) > Size.X)
{
Position.X = (Size.X - 1) * (sizeTile + Spacing);
}
else if (Position.Y / (sizeTile + Spacing) > Size.Y)
{
Position.Y = (Size.Y - 1) * (sizeTile + Spacing);
}
smartTractor.updateMap(Position, housePos, Size, sizeTile, Spacing, handleRotation.getRotation());
path = smartTractor.returnChoice();
TargetPosition = path.Reduce().getCords() * (sizeTile + Spacing);
updateDirection(Size, newPosition);
}
else
{
TargetPosition = path.Reduce().getCords() * (sizeTile + Spacing);
updateDirection(Size, newPosition);
}
}
public void setSpeed(int newSpeed)
{
Speed = newSpeed;
}
public void setTractorSpeed(tractorPositionCorrector corrector)
{
tractorSpeed = corrector.getTractorSpeed();
Position = corrector.getPosition();
}
public void setPos(Vector2 newPos)
{
Position = newPos;
}
public void setNewHousePos(Vector2 pos, bool newState)
{
smartTractor.setNewHousePos(pos, newState);
}
public float getRotation()
{
return MathHelper.ToRadians(handleRotation.getRotation());
}
public Farm getFarm()
{
return smartTractor.getFarm();
}
public Vector2 getPos()
{
return Position;
}
public int getSpeed()
{
return Speed;
}
public float getTractorSpeed()
{
return tractorSpeed;
}
public Vector2 getTargetPosition()
{
return TargetPosition;
}
public Path getPath()
{
return path;
}
public void drawInventory(Input input, SpriteBatch spriteBatch, SpriteFont Bold, Cargo itemStorageDefined)
{
smartTractor.drawInventory(input, spriteBatch, Bold, itemStorageDefined);
}
public Inventory getInventory()
{
return smartTractor.getInventory();
}
}