merge dev to OskarML

This commit is contained in:
BOTLester 2020-05-05 17:50:54 +02:00
commit 4ea129f803
18 changed files with 400 additions and 139 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -13,6 +13,30 @@
#---------------------------------- Content ---------------------------------# #---------------------------------- Content ---------------------------------#
#begin BerriesIcon.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:BerriesIcon.png
#begin CarronIcon.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:CarronIcon.png;CarrotIcon.png
#begin Crop.png #begin Crop.png
/importer:TextureImporter /importer:TextureImporter
/processor:TextureProcessor /processor:TextureProcessor
@ -68,6 +92,18 @@
/processorParam:TextureFormat=Color /processorParam:TextureFormat=Color
/build:Mountain.png /build:Mountain.png
#begin MouseCursor.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:MouseCursor.png
#begin Plantable.png #begin Plantable.png
/importer:TextureImporter /importer:TextureImporter
/processor:TextureProcessor /processor:TextureProcessor
@ -92,6 +128,30 @@
/processorParam:TextureFormat=Color /processorParam:TextureFormat=Color
/build:Planted.png /build:Planted.png
#begin ProgressionBar.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:ProgressionBar.png
#begin ProgressionBarStatus.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:ProgressionBarStatus.png
#begin Tile.png #begin Tile.png
/importer:TextureImporter /importer:TextureImporter
/processor:TextureProcessor /processor:TextureProcessor
@ -128,3 +188,27 @@
/processorParam:TextureFormat=Color /processorParam:TextureFormat=Color
/build:Tractor.png /build:Tractor.png
#begin TreePlantationIcon.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:TreePlantationIcon.png
#begin WheatIcon.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:WheatIcon.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
Game1/Content/WheatIcon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@ -14,13 +14,21 @@ namespace Game1
SpriteBatch spriteBatch; SpriteBatch spriteBatch;
SpriteFont Bold; SpriteFont Bold;
private Texture2D[] tile = new Texture2D[5]; private Texture2D[] tile = new Texture2D[5];
private Texture2D[] Crops = new Texture2D[5];
private Texture2D tractor; private Texture2D tractor;
private Texture2D house; private Texture2D house;
private Texture2D markers; private Texture2D markers;
private Texture2D mouseCursor;
private Texture2D ProgressionBar;
private Texture2D ProgressionBarStatus;
private Tractor tractorUnit = new Tractor(); private Tractor tractorUnit = new Tractor();
private Input input = new Input(); private Input input = new Input();
private House houseUnit = new House(); private House houseUnit = new House();
private Vector2 mousePosition; private Rectangle mousePosition;
MouseState state;
public Game1() public Game1()
@ -62,7 +70,17 @@ namespace Game1
tile[2] = Content.Load<Texture2D>("Plantable"); tile[2] = Content.Load<Texture2D>("Plantable");
tile[3] = Content.Load<Texture2D>("Planted"); tile[3] = Content.Load<Texture2D>("Planted");
tile[4] = Content.Load<Texture2D>("Crop"); tile[4] = Content.Load<Texture2D>("Crop");
Crops[0] = Content.Load<Texture2D>("CarrotIcon");
Crops[1] = Content.Load<Texture2D>("WheatIcon");
Crops[2] = Content.Load<Texture2D>("BerriesIcon"); //Replace 2 and 3 with the new crop Textures
Crops[3] = Content.Load<Texture2D>("TreePlantationIcon");
ProgressionBar = Content.Load<Texture2D>("ProgressionBar");
ProgressionBarStatus = Content.Load<Texture2D>("ProgressionBarStatus");
tractor = Content.Load<Texture2D>("Tractor"); tractor = Content.Load<Texture2D>("Tractor");
mouseCursor = Content.Load<Texture2D>("MouseCursor");
Bold = Content.Load<SpriteFont>("Font"); Bold = Content.Load<SpriteFont>("Font");
house = Content.Load<Texture2D>("house"); house = Content.Load<Texture2D>("house");
markers = Content.Load<Texture2D>("Markers"); markers = Content.Load<Texture2D>("Markers");
@ -82,15 +100,15 @@ namespace Game1
Exit(); Exit();
MouseState state = Mouse.GetState(); state = Mouse.GetState();
mousePosition = new Vector2(state.X, state.Y); mousePosition = new Rectangle(state.X, state.Y, 1, 1);
tractorUnit.updateSizing(input, 0, houseUnit.getVector()); //Updates the size tractorUnit.updateSizing(input, 0, houseUnit.getVector()); //Updates the size
tractorUnit.setSpeed(input.changeSpeed(tractorUnit.getSpeed())); //Updates the Simulation Speed tractorUnit.setSpeed(input.changeSpeed(tractorUnit.getSpeed())); //Updates the Simulation Speed
tractorUnit.setTractorSpeed(input.changeTractorSpeed(tractorUnit.getTractorSpeed(), tractorUnit.getPos())); //Updates the Tractor 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 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 houseUnit.updateRectangle(input.getSize(), input.getTileSize(), input.getSpacing()); //Updates the position of the house if the house appears out of bound
tractorUnit.updateCrops();
base.Update(gameTime); base.Update(gameTime);
@ -103,13 +121,8 @@ namespace Game1
spriteBatch.Begin(); spriteBatch.Begin();
for(int i = 0; i < input.getSize().X; i++) //Draw the tiles DrawTiles();
{
for (int j = 0; j < input.getSize().Y; j++)
{
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(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); 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++) for (int i = 0; i < tractorUnit.getPath().getCount() + 1; i++)
{ {
@ -131,14 +144,48 @@ namespace Game1
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, "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, "Tile Size:" + input.getTileSize().ToString() + "pix", new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 40), 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, "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.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.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.Draw(mouseCursor, new Rectangle((int)mousePosition.X, (int)mousePosition.Y, 14, 21), Color.White);
spriteBatch.End(); spriteBatch.End();
base.Draw(gameTime); base.Draw(gameTime);
} }
public void DrawTiles()
{
for (int i = 0; i < input.getSize().X; i++) //Draw the tiles
{
for (int j = 0; j < input.getSize().Y; j++)
{
Rectangle tilePos = new Rectangle(i * (input.getSpacingTile()), j * (input.getSpacingTile()), input.getTileSize(), input.getTileSize());
spriteBatch.Draw(tile[tractorUnit.getFarm().getCrop(i, j).getStatus()], tilePos, Color.White);
if (tilePos.Intersects(mousePosition))
{
spriteBatch.Draw(tile[tractorUnit.getFarm().getCrop(i, j).getStatus()], tilePos, Color.FromNonPremultiplied(0, 0, 20, 40));
if (state.LeftButton == ButtonState.Pressed && !tractorUnit.getFarm().getCrop(i, j).getHousePos())
{
tractorUnit.setNewHousePos(new Vector2(i, j), true);
tractorUnit.setNewHousePos(houseUnit.getVector() / input.getSpacingTile(), false);
houseUnit.setRectangle(i, j, input.getTileSize(), input.getSpacing());
}
}
if ((tractorUnit.getFarm().getCrop(i, j).getStatus() == 4))
{
spriteBatch.Draw(ProgressionBar, new Rectangle(i * (input.getSpacingTile()) + input.getTileSize() - input.getTileSize() / 3, j * (input.getSpacingTile()), input.getTileSize() / 3, input.getTileSize()), Color.White);
spriteBatch.Draw(ProgressionBarStatus, new Rectangle(i * (input.getSpacingTile()) + input.getTileSize() - input.getTileSize() / 4, j * (input.getSpacingTile()) + input.getTileSize() / 3, input.getTileSize() / 4, tractorUnit.getFarm().getCrop(i, j).getCropTimerBar((input.getTileSize())) + 1), Color.White);
}
if (tractorUnit.getFarm().getCrop(i, j).getStatus() != 0 && tractorUnit.getFarm().getCrop(i, j).getStatus() != 1)
{
spriteBatch.Draw(Crops[tractorUnit.getFarm().getCrop(i, j).getCropType()], new Rectangle(i * (input.getSpacingTile()) + input.getTileSize() - input.getTileSize() / 3, j * (input.getSpacingTile()), input.getTileSize() / 3, input.getTileSize() / 3), Color.White);
}
}
}
}
} }
} }

View File

@ -53,6 +53,7 @@
<Compile Include="Sources\Crops\Crops.cs" /> <Compile Include="Sources\Crops\Crops.cs" />
<Compile Include="Sources\Crops\Farm.cs" /> <Compile Include="Sources\Crops\Farm.cs" />
<Compile Include="Sources\Controlls\Input.cs" /> <Compile Include="Sources\Controlls\Input.cs" />
<Compile Include="Sources\Objects\Inventory.cs" />
<Compile Include="Sources\Objects\tractorPositionCorrector.cs" /> <Compile Include="Sources\Objects\tractorPositionCorrector.cs" />
<Compile Include="Sources\Pathing\A-Star\Astar.cs" /> <Compile Include="Sources\Pathing\A-Star\Astar.cs" />
<Compile Include="Sources\Pathing\A-Star\PathSaver\MinHeap.cs" /> <Compile Include="Sources\Pathing\A-Star\PathSaver\MinHeap.cs" />
@ -60,6 +61,7 @@
<Compile Include="Sources\Pathing\A-Star\PathSaver\Path.cs" /> <Compile Include="Sources\Pathing\A-Star\PathSaver\Path.cs" />
<Compile Include="Sources\Pathing\A-Star\PathSaver\PriorityQueue.cs" /> <Compile Include="Sources\Pathing\A-Star\PathSaver\PriorityQueue.cs" />
<Compile Include="Sources\Controlls\Controller.cs" /> <Compile Include="Sources\Controlls\Controller.cs" />
<Compile Include="Sources\Smart\AI.cs" />
<Compile Include="Sources\Smart\ScoreSystem.cs" /> <Compile Include="Sources\Smart\ScoreSystem.cs" />
<Compile Include="Sources\Smart\SmartTractor.cs" /> <Compile Include="Sources\Smart\SmartTractor.cs" />
<Compile Include="Sources\Objects\Tractor.cs" /> <Compile Include="Sources\Objects\Tractor.cs" />
@ -123,6 +125,8 @@
<ItemGroup> <ItemGroup>
<Content Include="Content\Crop.png" /> <Content Include="Content\Crop.png" />
<Content Include="Content\house.png" /> <Content Include="Content\house.png" />
<Content Include="Content\ProgressionBarStatus.png" />
<Content Include="Content\ProgressionBar.png" />
<Content Include="Content\Markers.png" /> <Content Include="Content\Markers.png" />
<Content Include="Content\Mountain.png" /> <Content Include="Content\Mountain.png" />
<Content Include="Content\Planted.png" /> <Content Include="Content\Planted.png" />

View File

@ -8,20 +8,17 @@ using Microsoft.Xna.Framework.Graphics;
class Crops class Crops
{ {
public int x; private int Status;
public int y; private int originalStatus;
public int Status; private int cropType = 0;
private int cropType; private int Timer = 1;
private int Timer; private int fullTimer;
private Random r; private bool housePos = false;
public void updateCrop() public void updateCrop()
{ {
if (Status != 0)
{
Timer--; Timer--;
}
} }
public int getCropTimer() public int getCropTimer()
@ -29,6 +26,40 @@ class Crops
return Timer; return Timer;
} }
public int getCropTimerBar(int tileSize)
{
int x = (int)(( 1 - ((float)Timer / fullTimer)) * (tileSize - tileSize / 3));
return x;
}
// Changes the time required for the crops to be harvestable
public void setCropTimer()
{
if (cropType == 0) // Carrots
{
Timer = 300;
fullTimer = Timer;
}
else if (cropType == 1) // Wheat
{
Timer = 600;
fullTimer = Timer;
}
else if (cropType == 2) // Berries
{
Timer = 1200;
fullTimer = Timer;
}
else // Fruit Trees
{
Timer = 2400;
fullTimer = Timer;
}
}
public int getCostOnMovement() public int getCostOnMovement()
{ {
if (Status == 1) //grass if (Status == 1) //grass
@ -41,23 +72,93 @@ class Crops
} }
else if (Status == 3) //crops else if (Status == 3) //crops
{ {
return 15; if (cropType == 0)
{
return 15; //Carrots
}
else if (cropType == 1)
{
return 30; //Wheat
}
else if (cropType == 2)
{
return 40; //Berries
}
else
{
return 50; //Fruit Trees
}
} }
else else //Harvestable Crops
{ {
return 30; if (cropType == 0)
{
return 30; //Carrots
}
else if (cropType == 1)
{
return 40; //Wheat
}
else if (cropType == 2)
{
return 50; //Berries
}
else
{
return 100; //Fruit Trees
}
} }
} }
public void setPosition(int newx, int newy)
{
x = newx;
y = newy;
}
public void setCropType(int Type) public void setCropType(int Type)
{ {
cropType = Type;
}
public int getStatus()
{
if (Status != 4)
{
return Status;
}
else
{
return Status; // + cropType; When unique crop textures have been added
}
}
public int getCropType()
{
return cropType;
}
public void setStatus(int newStatus)
{
Status = newStatus;
}
public void setOriginalStatus()
{
originalStatus = Status;
}
public void setHousePos(bool state)
{
housePos = state;
if (state)
{
Status = 1;
}
else
{
Status = originalStatus;
}
}
public bool getHousePos()
{
return housePos;
} }
} }

View File

@ -30,21 +30,25 @@ class Farm
x = r.Next(1, 3); x = r.Next(1, 3);
} }
crops[i, j] = new Crops(); crops[i, j] = new Crops();
crops[i, j].Status = x; crops[i, j].setStatus(x);
crops[i, j].setOriginalStatus();
crops[i, j].setCropType(x = r.Next(0, 4));
} }
} }
} }
public void updateFarm(Vector2 Size) public void updateFarm(Vector2 Size)
{ {
for (int i = 0; i > Size.X; i++) for (int i = 0; i < Size.X; i++)
{ {
for (int j = 0; j > Size.Y; j++) for (int j = 0; j < Size.Y; j++)
{ {
crops[i, j].updateCrop(); if (crops[i, j].getStatus() == 4 && crops[i, j].getCropTimer() != 1)
{
crops[i, j].updateCrop();
}
} }
} }
} }
//Changes the properties of the tile when the tractor reaches this tile. //Changes the properties of the tile when the tractor reaches this tile.
@ -52,21 +56,22 @@ class Farm
{ {
int x = (int)xfloat / Spacing; int x = (int)xfloat / Spacing;
int y = (int)yfloat / Spacing; int y = (int)yfloat / Spacing;
if (crops[x, y].Status == 4) if (crops[x, y].getStatus() >= 4)
{ {
crops[x, y].Status = 2; crops[x, y].setStatus(2);
} }
else if(crops[x, y].Status == 0) else if(crops[x, y].getStatus() == 0)
{ {
//do nothing //do nothing
} }
else if (crops[x, y].Status == 2) else if (crops[x, y].getStatus() == 2)
{ {
crops[x, y].Status = 3; crops[x, y].setStatus(3);
} }
else if (crops[x, y].Status == 3) else if (crops[x, y].getStatus() == 3)
{ {
crops[x, y].Status = 4; crops[x, y].setStatus(4);
crops[x, y].setCropTimer();
} }
} }
@ -87,10 +92,15 @@ class Farm
{ {
for (int j = 0; j < (int)Size.Y; j++) for (int j = 0; j < (int)Size.Y; j++)
{ {
crops[i, j].x = (tileSize + Spacing) * i; //crops[i, j].x = (tileSize + Spacing) * i;
crops[i, j].y = (tileSize + Spacing) * j; //crops[i, j].y = (tileSize + Spacing) * j;
} }
} }
} }
public void setNewHousePos(Vector2 pos, bool newState)
{
crops[(int)pos.X, (int)pos.Y].setHousePos(newState);
}
} }

View File

@ -43,6 +43,12 @@ class House
return housePos; return housePos;
} }
public void setRectangle(int x, int y, int tileSize, int Spacing)
{
pos = new Vector2(x, y);
housePos = new Rectangle(x * (tileSize + Spacing), y * (tileSize + Spacing), tileSize, tileSize);
}
public Vector2 getVector() public Vector2 getVector()
{ {
return new Vector2(housePos.X, housePos.Y); return new Vector2(housePos.X, housePos.Y);

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Game1.Sources.Objects
{
class Inventory
{
}
}

View File

@ -77,13 +77,10 @@ class Tractor
public void updatePosition(Vector2 Size, int Status) /// updates the position public void updatePosition(Vector2 Size, int Status) /// updates the position
{ {
farm.updateSize(Size, sizeTile, Spacing); //farm.updateSize(Size, sizeTile, Spacing);
for (int i = 0; i < Speed; i++) //Where all the choices the tractor does comes from 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(), Rotation);
updateDirection(Size, Position); updateDirection(Size, Position);
} }
@ -93,22 +90,23 @@ class Tractor
{ {
if (path.getCount() == 0) if (path.getCount() == 0)
{ {
if (housePos != Position) if (Position.X / (sizeTile + Spacing) > Size.X)
{ {
//Returns to the farm Position.X = (Size.X - 1) * (sizeTile + Spacing);
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 else if (Position.Y / (sizeTile + Spacing) > Size.Y)
{ {
//Sets a random Target Position.Y = (Size.Y - 1) * (sizeTile + Spacing);
int xTarget = (int)TargetPosition.X / (sizeTile + Spacing); }
int yTarget = (int)TargetPosition.Y / (sizeTile + Spacing);
currentTask = scoreSystem.MessageAndScore(farm.getCrop(xTarget, yTarget).Status, 1); smartTractor.updateMap(Position, housePos, farm.getCrops(), Size, sizeTile, Spacing, scoreSystem.getScore(), Rotation);
path = smartTractor.returnChoice(1); 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)
{
farm.setCropStatus(Position.X / (sizeTile + Spacing), Position.Y / (sizeTile + Spacing), Spacing);
} }
TargetPosition = path.Reduce().getCords() * (sizeTile + Spacing); TargetPosition = path.Reduce().getCords() * (sizeTile + Spacing);
@ -237,6 +235,14 @@ class Tractor
} }
public void updateCrops()
{
for (int i = 0; i < Speed; i++)
{
farm.updateFarm(Size);
}
}
public Vector2 getPos() public Vector2 getPos()
{ {
return Position; return Position;
@ -306,4 +312,9 @@ class Tractor
{ {
return path; return path;
} }
public void setNewHousePos(Vector2 pos, bool newState)
{
farm.setNewHousePos(pos, newState);
}
} }

View File

@ -56,7 +56,7 @@ class Astar
} }
// return if not an obstacle // return if not an obstacle
return adjacentNodes.Where( return adjacentNodes.Where(
item => crops[(int)item.getCords().X, (int)item.getCords().Y].Status != 0).ToList(); item => (crops[(int)item.getCords().X, (int)item.getCords().Y].getStatus()) != 0).ToList();
} }
// Heuristic function, Manhattan method. // Heuristic function, Manhattan method.

43
Game1/Sources/Smart/AI.cs Normal file
View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
class AI
{
private Vector2 tractorPos;
private Vector2 housePos;
private Crops[,] crops;
private Vector2 Size;
private Vector2 targetPos;
private int Rotation;
private Random r = new Random();
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 Vector2 newTarget()
{
return new Vector2(r.Next(0, (int)Size.X), r.Next(0, (int)Size.Y));
}
}

View File

@ -9,30 +9,31 @@ class SmartTractor
private Vector2 tractorPos; private Vector2 tractorPos;
private Vector2 Size; private Vector2 Size;
private Vector2 Target; private Vector2 Target;
private Path path;
private int tileSize; private int tileSize;
private int Score; private int Score;
private int Spacing; private int Spacing;
private Random r = new Random();
private Astar astar = new Astar(); private Astar astar = new Astar();
private int Rotation; private int Rotation;
private AI ai = new AI();
//What to do next //What to do next
public Path returnChoice(int task) public Path returnChoice()
{ {
if (task == 0) ai.update(crops, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target / (tileSize + Spacing), Rotation);
{
//To the house
getTargetPosition((int)housePos.X / (tileSize + Spacing), (int)housePos.Y / (tileSize + Spacing));
}
else getTargetPosition(ai.newTarget());
{
//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), Rotation); astar.update(crops, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target/(tileSize+Spacing), Rotation);
//astar.FindPath();
return astar.FindPath(); return astar.FindPath();
} }
@ -55,66 +56,8 @@ class SmartTractor
} }
private void getTargetPosition(int x, int y) private void getTargetPosition(Vector2 newTarget)
{ {
Target = new Vector2(x, y) * (tileSize + Spacing); Target = newTarget * (tileSize + Spacing);
} }
/*
//Only for testing without obstacles
private void createPath()
{
path = new Path();
Vector2 targetPos = Target / (tileSize + Spacing);
Vector2 currentPath = tractorPos / tileSize;
currentPath.X = (float)Math.Round(currentPath.X);
currentPath.Y = (float)Math.Round(currentPath.Y);
do
{
if (currentPath.X == targetPos.X)
{
//found X pos
if (currentPath.Y == targetPos.Y)
{
//found y pos
}
else if (currentPath.Y < targetPos.Y)
{
currentPath = new Vector2(currentPath.X, currentPath.Y + 1);
path.setNode(currentPath, crops);
}
else if (currentPath.Y > targetPos.Y)
{
currentPath = new Vector2(currentPath.X, currentPath.Y - 1);
path.setNode(currentPath, crops);
}
}
else if (currentPath.X < targetPos.X)
{
currentPath = new Vector2(currentPath.X + 1, currentPath.Y);
path.setNode(currentPath, crops);
}
else if (currentPath.X > targetPos.X)
{
currentPath = new Vector2(currentPath.X - 1, currentPath.Y);
path.setNode(currentPath, crops);
}
} while (currentPath != targetPos);
}
public void setNode(Vector2 newNode, Crops[,] Crop)
{
nodes[Count] = new Nodes(Crop[(int)newNode.X, (int)newNode.Y].getCostOnMovement(), newNode);
Count++;
}
*/
} }