1
0
forked from s425077/PotatoPlan

added rotation and rotation cost, RC for T2

This commit is contained in:
BOTLester 2020-05-04 01:17:10 +02:00
parent 8433934bcd
commit 5af50f8d3b
10 changed files with 197 additions and 31 deletions

View File

@ -44,6 +44,18 @@
/processorParam:TextureFormat=Color
/build:house.png
#begin Markers.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Markers.png
#begin Mountain.png
/importer:TextureImporter
/processor:TextureProcessor

BIN
Game1/Content/Markers.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 578 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 573 B

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -16,9 +16,11 @@ namespace Game1
private Texture2D[] tile = new Texture2D[5];
private Texture2D tractor;
private Texture2D house;
private Texture2D markers;
private Tractor tractorUnit = new Tractor();
private Input input = new Input();
private House houseUnit = new House();
private Vector2 mousePosition;
public Game1()
@ -63,6 +65,7 @@ namespace Game1
tractor = Content.Load<Texture2D>("Tractor");
Bold = Content.Load<SpriteFont>("Font");
house = Content.Load<Texture2D>("house");
markers = Content.Load<Texture2D>("Markers");
}
@ -77,12 +80,19 @@ namespace Game1
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
MouseState state = Mouse.GetState();
mousePosition = new Vector2(state.X, state.Y);
tractorUnit.updateSizing(input, 0, houseUnit.getVector()); //Updates the size
tractorUnit.setSpeed(input.changeSpeed(tractorUnit.getSpeed())); //Updates the Simulation Speed
tractorUnit.setTractorSpeed(input.changeTractorSpeed(tractorUnit.getTractorSpeed(), tractorUnit.getPos())); //Updates the Tractor Speed
input.controlWindowSize(); //Controls the size of the screen depending on the number of tiles
houseUnit.updateRectangle(input.getSize(), input.getTileSize(), input.getSpacing()); //Updates the position of the house if the house appears out of bound
base.Update(gameTime);
}
@ -100,21 +110,32 @@ namespace Game1
spriteBatch.Draw(tile[tractorUnit.getFarm().getCrop(i, j).Status], new Rectangle(i * (input.getSpacingTile()), j * (input.getSpacingTile()), input.getTileSize(), input.getTileSize()), Color.White);
}
}
spriteBatch.Draw(tractor, new Rectangle((int)tractorUnit.getTargetPosition().X / input.getSpacingTile() * (input.getTileSize() + input.getSpacing()) + input.getTileSize() / 4, (int)tractorUnit.getTargetPosition().Y / input.getSpacingTile() * (input.getTileSize() + input.getSpacing()) + input.getTileSize() / 4, input.getTileSize()/2, input.getTileSize()/2), Color.Green);
spriteBatch.Draw(markers, new Rectangle((int)tractorUnit.getTargetPosition().X / input.getSpacingTile() * (input.getTileSize() + input.getSpacing()) + input.getTileSize() / 4, (int)tractorUnit.getTargetPosition().Y / input.getSpacingTile() * (input.getTileSize() + input.getSpacing()) + input.getTileSize() / 4, input.getTileSize()/2, input.getTileSize()/2), Color.Green);
for (int i = 0; i < tractorUnit.getPath().getCount() + 1; i++)
{
spriteBatch.Draw(tractor, new Rectangle((int)tractorUnit.getPath().getByIndex(i).getCords().X * (input.getSpacingTile()) + input.getTileSize() / 4, (int)tractorUnit.getPath().getByIndex(i).getCords().Y * (input.getSpacingTile()) + input.getTileSize() / 4, input.getTileSize()/2, input.getTileSize()/2), Color.Green);
spriteBatch.Draw(markers, new Rectangle((int)tractorUnit.getPath().getByIndex(i).getCords().X * (input.getSpacingTile()) + input.getTileSize() / 4, (int)tractorUnit.getPath().getByIndex(i).getCords().Y * (input.getSpacingTile()) + input.getTileSize() / 4, input.getTileSize()/2, input.getTileSize()/2), Color.Green);
}
spriteBatch.Draw(tractor, new Rectangle((int)tractorUnit.getPos().X, (int)tractorUnit.getPos().Y, input.getTileSize(), input.getTileSize()), Color.White);
spriteBatch.Draw(house, houseUnit.GetRectangle(), Color.White);
spriteBatch.Draw(markers, new Rectangle((int)tractorUnit.getPath().getFinalDest().getCords().X * (input.getSpacingTile()) + Convert.ToInt32(input.getTileSize() / 6), (int)tractorUnit.getPath().getFinalDest().getCords().Y * (input.getSpacingTile()) + Convert.ToInt32(input.getTileSize() / 6), Convert.ToInt32(input.getTileSize() / 1.5), Convert.ToInt32(input.getTileSize() / 1.5)), Color.Red); //Draws the current target of the tractor
spriteBatch.Draw(tractor, new Vector2((int)tractorUnit.getPos().X + input.getTileSize() / 2, (int)tractorUnit.getPos().Y + input.getTileSize() / 2), new Rectangle(0, 0, input.getTileSize(), input.getTileSize()), Color.White, tractorUnit.getRotation(), new Vector2(input.getTileSize() / 2, input.getTileSize() / 2), 1.0f, SpriteEffects.None, 1);
spriteBatch.DrawString(Bold, "Speed:" + tractorUnit.getSpeed().ToString(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20) , Color.White); //Draws the speed value
spriteBatch.Draw(tractor, new Rectangle((int)tractorUnit.getPath().getFinalDest().getCords().X * (input.getSpacingTile()) + Convert.ToInt32(input.getTileSize() / 6), (int)tractorUnit.getPath().getFinalDest().getCords().Y * (input.getSpacingTile()) + Convert.ToInt32(input.getTileSize() / 6), Convert.ToInt32(input.getTileSize() / 1.5), Convert.ToInt32(input.getTileSize() / 1.5)), Color.Red); //Draws the current target of the tractor
spriteBatch.DrawString(Bold, "Tractor Speed:" + tractorUnit.getTractorSpeed().ToString(), new Vector2(100, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20), Color.White);
spriteBatch.DrawString(Bold, "Tile Size:" + input.getTileSize().ToString() + "pix", new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 40), Color.White); //Draws the tile size
spriteBatch.DrawString(Bold, "Matrix Size: " + input.getSize().X.ToString() + " X " + input.getSize().Y.ToString(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 60), Color.White);
spriteBatch.DrawString(Bold, "Matrix Size: " + input.getSize().X.ToString() + " X " + input.getSize().Y.ToString(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 60), Color.White);spriteBatch.DrawString(Bold, "Tile Size:" + input.getTileSize().ToString() + "pix", new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 40), Color.White);
spriteBatch.DrawString(Bold, "Tractor Rotation:" + tractorUnit.getRotation().ToString() + " Degrees", new Vector2(250, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20), Color.White);
spriteBatch.DrawString(Bold, tractorUnit.getCurrentTask(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 80), Color.White); //Draws the tile size
spriteBatch.DrawString(Bold, tractorUnit.getScore().ToString(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 100), Color.White);
spriteBatch.Draw(tractor, new Rectangle((int)mousePosition.X, (int)mousePosition.Y, input.getTileSize() / 4, input.getTileSize() / 4), Color.White);
spriteBatch.End();
base.Draw(gameTime);

View File

@ -69,6 +69,7 @@
<ItemGroup>
<Content Include="Content\Crop.png" />
<Content Include="Content\house.png" />
<Content Include="Content\Markers.png" />
<Content Include="Content\Mountain.png" />
<Content Include="Content\Planted.png" />
<Content Include="Content\Tile.png" />

View File

@ -41,7 +41,7 @@ class Crops
}
else if (Status == 3)
{
return 20;
return 15;
}
else
{

View File

@ -9,7 +9,7 @@ class Tractor
private int Spacing, sizeTile, Speed = 1;
private int Spacing, sizeTile, Speed = 1, Rotation = 180, rotationSpeed = 5;
private float tractorSpeed = 1;
private String currentTask;
@ -46,6 +46,7 @@ class Tractor
private void updateDirection(Vector2 Size, Vector2 newPosition)
{
Vector2 DeltaPosition = TargetPosition - Position;
if (DeltaPosition.X == 0)
{
if (DeltaPosition.Y == 0)
@ -54,20 +55,20 @@ class Tractor
}
else if (DeltaPosition.Y > 0)
{
Direction = new Vector2(0, 1) * tractorSpeed;
updateRotation(0);
}
else if (DeltaPosition.Y < 0)
{
Direction = new Vector2(0, -1) * tractorSpeed;
updateRotation(1);
}
}
else if (DeltaPosition.X > 0)
{
Direction = new Vector2(1, 0) * tractorSpeed;
updateRotation(2);
}
else if (DeltaPosition.X < 0)
{
Direction = new Vector2(-1, 0) * tractorSpeed;
updateRotation(3);
}
}
@ -79,8 +80,8 @@ class Tractor
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;
smartTractor.updateMap(Position, housePos, farm.getCrops(), Size, sizeTile, Spacing, scoreSystem.getScore(), Rotation);
updateDirection(Size, Position);
}
@ -120,7 +121,121 @@ class Tractor
}
}
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;
}
}
}
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);
}
public Vector2 getPos()
{

View File

@ -14,15 +14,16 @@ class Astar
private Vector2 Size;
private PriorityQueue allPaths;
private Vector2 targetPos;
private int Rotation;
public void update(Crops[,] newCrops, Vector2 newSize, Vector2 newTractorPos, Vector2 newHousePos, Vector2 newtargetPos)
public void update(Crops[,] newCrops, Vector2 newSize, Vector2 newTractorPos, Vector2 newHousePos, Vector2 newtargetPos, int rotation)
{
tractorPos = new Vector2((int)newTractorPos.X, (int)newTractorPos.Y);
housePos = new Vector2((int)newHousePos.X, (int)newHousePos.Y);
targetPos = newtargetPos;
crops = newCrops;
Size = newSize;
Rotation = rotation;
}
public Nodes getOptimalPath()
@ -30,7 +31,7 @@ class Astar
return allPaths.Peek();
}
private List <Nodes> GetAdjacentNodes(Vector2 currentPos)
private List<Nodes> GetAdjacentNodes(Vector2 currentPos)
{
var adjacentNodes = new List<Nodes>()
@ -42,7 +43,7 @@ class Astar
};
//check if out of range
for (int i = 3; i >=0; i--)
for (int i = 3; i >= 0; i--)
{
if (adjacentNodes[i].getCords().X < 0 || adjacentNodes[i].getCords().Y < 0)
adjacentNodes.Remove(adjacentNodes[i]);
@ -68,12 +69,26 @@ class Astar
if (currDir == newDir)
return 0;
else if (Math.Abs(currDir - newDir) == 1 || Math.Abs(currDir - newDir) == 3)
return 1;
return 3;
else if (Math.Abs(currDir - newDir) == 0 || Math.Abs(currDir - newDir) == 2) //its for turning back, so it never should happen anyway, remove this check then?
return 10;
return 9;
return 0;
}
public int ConvertRotation()
{
int rotation = 0;
if (Rotation == 180)
rotation = 0;
else if (Rotation == 270)
rotation = 1;
else if (Rotation == 0)
rotation = 2;
else if (Rotation == 90)
rotation = -1;
return rotation;
}
public Path FindPath()
{
Path path = new Path();
@ -82,10 +97,11 @@ class Astar
//PriorityQueue closedList = new PriorityQueue();
MinHeap closedList = new MinHeap();
Nodes target = new Nodes(targetPos);
Nodes startPos = new Nodes (tractorPos);
int direction = ConvertRotation();
Nodes startPos = new Nodes(tractorPos, direction);
Nodes current = null;
int direction = 6;
int g = 0;
//int direction = SmartTractor.update
//openList.Enqueue(startPos);
openList.Insert(startPos);
@ -103,7 +119,7 @@ class Astar
direction = current.getDirection();
if (current.getCords() == target.getCords())
break;
break;
//if (closedList.Exists(target.getCords()))
@ -111,7 +127,7 @@ class Astar
var adjacentNodes = GetAdjacentNodes(current.getCords());
//g++;
foreach(var adjacentNode in adjacentNodes)
foreach (var adjacentNode in adjacentNodes)
{
if (closedList.Exists(adjacentNode.getCords()))
continue;
@ -128,7 +144,7 @@ class Astar
}
else
{
if(g + adjacentNode.getH() < adjacentNode.getF())
if (g + adjacentNode.getH() < adjacentNode.getF())
{
adjacentNode.setG(g);
adjacentNode.calculateF();

View File

@ -27,7 +27,7 @@ class Nodes
Direction = direction;
}
public Nodes (Nodes node)
public Nodes(Nodes node)
{
F = node.F;
G = node.G;

View File

@ -15,6 +15,7 @@ class SmartTractor
private int Spacing;
private Random r = new Random();
private Astar astar = new Astar();
private int Rotation;
//What to do next
@ -30,7 +31,7 @@ class SmartTractor
//To the fields
getTargetPosition(r.Next(0, (int)Size.X), r.Next(0, (int)Size.Y));
}
astar.update(crops, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target/(tileSize+Spacing));
astar.update(crops, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target/(tileSize+Spacing), Rotation);
//astar.FindPath();
return astar.FindPath();
}
@ -41,7 +42,7 @@ class SmartTractor
//Updates the variables every frame
public void updateMap(Vector2 newTractorPos, Vector2 newHousePos, Crops[,] newCropsStatus, Vector2 newSize, int newTileSize, int newSpacing, int newScore)
public void updateMap(Vector2 newTractorPos, Vector2 newHousePos, Crops[,] newCropsStatus, Vector2 newSize, int newTileSize, int newSpacing, int newScore, int rotation)
{
crops = newCropsStatus;
housePos = newHousePos;
@ -50,7 +51,7 @@ class SmartTractor
tileSize = newTileSize;
Spacing = newSpacing;
Score = newScore;
Rotation = rotation;
}