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

321 lines
8.0 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, Rotation = 180, rotationSpeed = 5;
2020-05-03 13:05:05 +02:00
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;
2020-05-03 13:05:05 +02:00
if (DeltaPosition.X == 0)
{
if (DeltaPosition.Y == 0)
{
calculateNewPath(newPosition);
}
else if (DeltaPosition.Y > 0)
{
updateRotation(0);
2020-05-03 13:05:05 +02:00
}
else if (DeltaPosition.Y < 0)
{
updateRotation(1);
2020-05-03 13:05:05 +02:00
}
}
else if (DeltaPosition.X > 0)
{
updateRotation(2);
2020-05-03 13:05:05 +02:00
}
else if (DeltaPosition.X < 0)
{
updateRotation(3);
2020-05-03 13:05:05 +02:00
}
}
//Moves the tractor
public void updatePosition(Vector2 Size, int Status) /// updates the position
{
2020-05-05 16:27:45 +02:00
//farm.updateSize(Size, sizeTile, Spacing);
2020-05-03 13:05:05 +02:00
for (int i = 0; i < Speed; i++) //Where all the choices the tractor does comes from
{
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);
}
smartTractor.updateMap(Position, housePos, farm.getCrops(), Size, sizeTile, Spacing, scoreSystem.getScore(), Rotation);
path = smartTractor.returnChoice();
// Changes the status of a tile when the tractor reaches its destination
if (path.getFinalDest() == null && farm.getCrop((int)Position.X / (sizeTile + Spacing), (int)Position.Y / (sizeTile + Spacing)).getCropTimer() == 1)
2020-05-03 13:05:05 +02:00
{
2020-05-05 16:27:45 +02:00
farm.setCropStatus(Position.X / (sizeTile + Spacing), Position.Y / (sizeTile + Spacing), Spacing);
2020-05-03 13:05:05 +02:00
}
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);
}
}
private void updateRotation(int Destination)
{
if (Destination == 0)
{
if (Rotation == 0)
{
Direction = new Vector2(0, 1) * tractorSpeed;
Position = Position + Direction;
}
else
{
if (Rotation == 0)
{
//Do nothing
}
else if (Rotation > 180)
{
if (Rotation >= 360)
{
Rotation = 0;
}
Rotation = Rotation + rotationSpeed;
}
else if (Rotation <= 180 && Rotation > 0)
{
Rotation = Rotation - rotationSpeed;
}
}
}
else if (Destination == 1)
{
if (Rotation == 180)
{
Direction = new Vector2(0, -1) * tractorSpeed;
Position = Position + Direction;
}
else
{
if (Rotation == 180)
{
//Do nothing
}
else if (Rotation >= 0 && Rotation < 180)
{
Rotation = Rotation + rotationSpeed;
}
else if (Rotation < 360 && Rotation > 180)
{
Rotation = Rotation - rotationSpeed;
}
}
2020-05-03 13:05:05 +02:00
}
else if (Destination == 2)
{
if (Rotation == 270)
{
Direction = new Vector2(1, 0) * tractorSpeed;
Position = Position + Direction;
}
else
{
if (Rotation == 270)
{
//Do nothing
}
else if (Rotation > 90 && Rotation < 270)
{
Rotation = Rotation + rotationSpeed;
}
else if (Rotation < 90 || Rotation < 360)
{
if (Rotation <= 0)
{
Rotation = 360;
}
Rotation = Rotation - rotationSpeed;
}
}
}
else if (Destination == 3)
{
if (Rotation == 90)
{
Direction = new Vector2(-1, 0) * tractorSpeed;
Position = Position + Direction;
}
else
{
if (Rotation == 90)
{
//Do nothing
}
else if ( Rotation < 270 && Rotation > 90)
{
Rotation = Rotation - rotationSpeed;
}
else if (Rotation >= 0 || Rotation > 270)
{
if (Rotation >= 360)
{
Rotation = 0;
}
Rotation = Rotation + rotationSpeed;
}
}
}
}
public float getRotation()
{
return MathHelper.ToRadians(Rotation);
}
2020-05-03 13:05:05 +02:00
2020-05-05 16:27:45 +02:00
public void updateCrops()
{
for (int i = 0; i < Speed; i++)
{
farm.updateFarm(Size);
}
}
2020-05-03 13:05:05 +02:00
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;
}
2020-05-05 16:27:45 +02:00
public void setNewHousePos(Vector2 pos, bool newState)
{
farm.setNewHousePos(pos, newState);
}
2020-05-03 13:05:05 +02:00
}