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

163 lines
4.3 KiB
C#
Raw Permalink Normal View History

2020-05-03 13:05:05 +02:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
class Tractor
{
2020-05-06 16:22:30 +02:00
private int Spacing, sizeTile, Speed = 1;
2020-05-03 13:05:05 +02:00
private float tractorSpeed = 1;
2020-05-06 16:22:30 +02:00
private Vector2 Position, TargetPosition, Size, housePos;
2020-05-03 13:05:05 +02:00
private Path path = new Path();
private SmartTractor smartTractor = new SmartTractor();
2020-05-06 16:22:30 +02:00
private HandleRotation handleRotation = new HandleRotation();
2020-05-03 13:05:05 +02:00
2020-05-06 16:22:30 +02:00
public void updateSizing(Input input, int Status, Vector2 newHousePos, DayNightCycle Time)
2020-05-03 13:05:05 +02:00
{
Spacing = input.getSpacing();
sizeTile = input.getTileSize();
Size = input.getSize();
updatePosition(input.getSize(), Status);
housePos = newHousePos;
2020-05-06 16:22:30 +02:00
smartTractor.UpdateCrops(Speed);
2020-05-03 13:05:05 +02:00
}
public void init(Rectangle house, Input input)
{
sizeTile = input.getTileSize();
Spacing = input.getSpacing();
Position = housePos;
TargetPosition = new Vector2(house.X, house.Y);
2020-05-06 16:22:30 +02:00
smartTractor.init();
2020-05-03 13:05:05 +02:00
}
// Runs when the tractor reaches a tile
private void updateDirection(Vector2 Size, Vector2 newPosition)
{
Vector2 DeltaPosition = TargetPosition - Position;
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-06 16:22:30 +02:00
Position = handleRotation.UpdatePosition(0, tractorSpeed, Position);
2020-05-03 13:05:05 +02:00
}
else if (DeltaPosition.Y < 0)
{
2020-05-06 16:22:30 +02:00
Position = handleRotation.UpdatePosition(1, tractorSpeed, Position);
2020-05-03 13:05:05 +02:00
}
}
else if (DeltaPosition.X > 0)
{
2020-05-06 16:22:30 +02:00
Position = handleRotation.UpdatePosition(2, tractorSpeed, Position);
2020-05-03 13:05:05 +02:00
}
else if (DeltaPosition.X < 0)
{
2020-05-06 16:22:30 +02:00
Position = handleRotation.UpdatePosition(3, tractorSpeed, Position);
2020-05-03 13:05:05 +02:00
}
}
2020-05-06 16:22:30 +02:00
public void updatePosition(Vector2 Size, int Status) // updates the position
2020-05-03 13:05:05 +02:00
{
2020-05-05 16:27:45 +02:00
//farm.updateSize(Size, sizeTile, Spacing);
2020-05-06 16:22:30 +02:00
for (int i = 0; i < Speed; i++)
2020-05-03 13:05:05 +02:00
{
updateDirection(Size, Position);
}
}
public void calculateNewPath(Vector2 newPosition)
{
if (path.getCount() == 0)
{
2020-05-05 16:27:45 +02:00
if (Position.X / (sizeTile + Spacing) > Size.X)
2020-05-03 13:05:05 +02:00
{
2020-05-05 16:27:45 +02:00
Position.X = (Size.X - 1) * (sizeTile + Spacing);
2020-05-03 13:05:05 +02:00
}
2020-05-05 16:27:45 +02:00
else if (Position.Y / (sizeTile + Spacing) > Size.Y)
{
Position.Y = (Size.Y - 1) * (sizeTile + Spacing);
}
2020-05-06 16:22:30 +02:00
smartTractor.updateMap(Position, housePos, Size, sizeTile, Spacing, handleRotation.getRotation());
2020-05-05 16:27:45 +02:00
path = smartTractor.returnChoice();
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-06 16:22:30 +02:00
public void setSpeed(int newSpeed)
{
2020-05-06 16:22:30 +02:00
Speed = newSpeed;
}
2020-05-06 16:22:30 +02:00
public void setTractorSpeed(tractorPositionCorrector corrector)
{
2020-05-06 16:22:30 +02:00
tractorSpeed = corrector.getTractorSpeed();
Position = corrector.getPosition();
}
2020-05-03 13:05:05 +02:00
2020-05-05 16:27:45 +02:00
2020-05-06 16:22:30 +02:00
public void setPos(Vector2 newPos)
2020-05-03 13:05:05 +02:00
{
2020-05-06 16:22:30 +02:00
Position = newPos;
2020-05-03 13:05:05 +02:00
}
2020-05-06 16:22:30 +02:00
public void setNewHousePos(Vector2 pos, bool newState)
2020-05-03 13:05:05 +02:00
{
2020-05-06 16:22:30 +02:00
smartTractor.setNewHousePos(pos, newState);
2020-05-03 13:05:05 +02:00
}
2020-05-06 16:22:30 +02:00
public float getRotation()
{
return MathHelper.ToRadians(handleRotation.getRotation());
2020-05-03 13:05:05 +02:00
}
2020-05-06 16:22:30 +02:00
public Farm getFarm()
2020-05-03 13:05:05 +02:00
{
2020-05-06 16:22:30 +02:00
return smartTractor.getFarm();
2020-05-03 13:05:05 +02:00
}
2020-05-06 16:22:30 +02:00
public Vector2 getPos()
2020-05-03 13:05:05 +02:00
{
2020-05-06 16:22:30 +02:00
return Position;
2020-05-03 13:05:05 +02:00
}
public int getSpeed()
{
return Speed;
}
public float getTractorSpeed()
{
return tractorSpeed;
}
public Vector2 getTargetPosition()
{
return TargetPosition;
}
public Path getPath()
{
return path;
}
2020-05-05 16:27:45 +02:00
2020-05-06 16:22:30 +02:00
public void drawInventory(Input input, SpriteBatch spriteBatch, SpriteFont Bold, Cargo itemStorageDefined)
2020-05-05 16:27:45 +02:00
{
2020-05-06 16:22:30 +02:00
smartTractor.drawInventory(input, spriteBatch, Bold, itemStorageDefined);
2020-05-05 16:27:45 +02:00
}
2020-05-06 20:26:33 +02:00
public Inventory getInventory()
{
return smartTractor.getInventory();
}
2020-05-03 13:05:05 +02:00
}