asdasdasdasd

This commit is contained in:
Joel 2020-05-06 16:22:30 +02:00
parent c143ae6ade
commit 5ca81367f0
16 changed files with 793 additions and 279 deletions

View File

@ -28,6 +28,14 @@ namespace Game1
private House houseUnit = new House();
private Rectangle mousePosition;
private Inventory inventory = new Inventory();
DayNightCycle Time = new DayNightCycle();
private string[] cropTypesNames = new string[12];
private int x, y;
MouseState state;
@ -42,18 +50,37 @@ namespace Game1
// TODO: Add your initialization logic here
base.Initialize();
cropTypesNames[0] = "Barley";
cropTypesNames[1] = "Cotton";
cropTypesNames[2] = "Ground Nuts";
cropTypesNames[3] = "Maize";
cropTypesNames[4] = "Millets";
cropTypesNames[5] = "Oil Seeds";
cropTypesNames[6] = "Paddy";
cropTypesNames[7] = "Pulses";
cropTypesNames[8] = "Sugarcane";
cropTypesNames[9] = "Tobacco";
cropTypesNames[10] = "Wheat";
//Generates the map with some random values
inventory.initInventorySystem();
input.init(graphics, new Vector2(16,16), 56, 1); //Generates the starting size
houseUnit.init(input.getTileSize(), input.getSpacing()); //Generates the house position
tractorUnit.init(houseUnit.GetRectangle(), input); //Generates the Tractor
tractorUnit.updateSizing(input, 0, houseUnit.getVector()); //Updates the first Size of the Tractor
tractorUnit.updateSizing(input, 0, houseUnit.getVector(), Time); //Updates the first Size of the Tractor
tractorUnit.setPos(houseUnit.getVector()); //Changes the position of the tractor to the houses position at the start
tractorUnit.setNewHousePos(houseUnit.getVector() / input.getSpacingTile(), true);
graphics.PreferredBackBufferWidth = (input.getTileSize() + input.getSpacing()) * (int)input.getSize().X;
graphics.PreferredBackBufferHeight = (input.getTileSize() + input.getSpacing()) * (int)input.getSize().Y + 125;
graphics.PreferredBackBufferHeight = (input.getTileSize() + input.getSpacing()) * (int)input.getSize().Y + 300;
graphics.ApplyChanges();
}
@ -103,12 +130,13 @@ namespace Game1
state = Mouse.GetState();
mousePosition = new Rectangle(state.X, state.Y, 1, 1);
tractorUnit.updateSizing(input, 0, houseUnit.getVector()); //Updates the size
tractorUnit.updateSizing(input, 0, houseUnit.getVector(), Time); //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
tractorUnit.updateCrops();
Time.updateTime(tractorUnit.getSpeed());
base.Update(gameTime);
@ -117,7 +145,7 @@ namespace Game1
protected override void Draw(GameTime gameTime) //Draw Function
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.Clear(Color.FromNonPremultiplied(255,255,255,140));
spriteBatch.Begin();
@ -138,14 +166,37 @@ namespace Game1
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.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, "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);
for (int i = 0; i < 5; i++)
{
spriteBatch.Draw(ProgressionBar, new Rectangle(i * 227, (int)(input.getSize().Y * (input.getTileSize() + input.getSpacing())), 5, 300), Color.White);
}
for (int i = 0; i < 15; i++)
{
spriteBatch.Draw(ProgressionBar, new Rectangle(0, (int)(input.getSize().Y * (input.getTileSize() + input.getSpacing())) + i * 20, (int)(input.getSize().X * (input.getTileSize() + input.getSpacing())), 1), Color.White);
}
spriteBatch.DrawString(Bold, "Time: ", new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkRed);
spriteBatch.DrawString(Bold, Time.getDayNight(), new Vector2(60, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkBlue);
spriteBatch.DrawString(Bold, Time.GetTimeOfDayInt().ToString(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 22), Color.DarkBlue);
spriteBatch.DrawString(Bold, "Tractor Properties:", new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 62), Color.DarkRed);
spriteBatch.DrawString(Bold, "Speed:" + tractorUnit.getSpeed().ToString(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 82) , Color.DarkBlue);
spriteBatch.DrawString(Bold, "Tractor Position:" + new Vector2((float)Math.Round(tractorUnit.getPos().X / input.getSpacingTile(), 1), (float)Math.Round(tractorUnit.getPos().Y / input.getSpacingTile(), 1)), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 102), Color.DarkBlue);
spriteBatch.DrawString(Bold, "Tractor Rotation:" + Math.Round(tractorUnit.getRotation(), 2).ToString() + " Degrees", new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 122), Color.DarkBlue);
spriteBatch.DrawString(Bold, "Tractor Speed:" + tractorUnit.getTractorSpeed().ToString(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 142), Color.DarkBlue);
spriteBatch.DrawString(Bold, "Tractor Target:" + tractorUnit.getPath().getFinalDest().getCords().ToString(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 162), Color.DarkBlue);
spriteBatch.DrawString(Bold, "Map Properties:", new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 202), Color.DarkRed);
spriteBatch.DrawString(Bold, "Tile Size:" + input.getTileSize().ToString() + "pix", new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 222), Color.DarkBlue); //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()) + 242), Color.DarkBlue);
spriteBatch.DrawString(Bold, "House Position: " + houseUnit.getVector() / input.getSpacingTile(), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 262), Color.DarkBlue);
tractorUnit.drawInventory(input, spriteBatch, Bold, inventory.getPredefinedItems());
InspectTile();
spriteBatch.Draw(mouseCursor, new Rectangle((int)mousePosition.X, (int)mousePosition.Y, 14, 21), Color.White);
@ -172,7 +223,11 @@ namespace Game1
tractorUnit.setNewHousePos(houseUnit.getVector() / input.getSpacingTile(), false);
houseUnit.setRectangle(i, j, input.getTileSize(), input.getSpacing());
}
if (state.RightButton == ButtonState.Pressed)
{
x = i;
y = j;
}
}
if ((tractorUnit.getFarm().getCrop(i, j).getStatus() == 4))
{
@ -187,5 +242,12 @@ namespace Game1
}
}
}
public void InspectTile()
{
spriteBatch.DrawString(Bold, "Crop:", new Vector2(240, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkRed);
spriteBatch.DrawString(Bold, "Selected tile: (" + x.ToString() + ", " + y.ToString() + ")", new Vector2(240, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 22), Color.DarkBlue);
tractorUnit.getFarm().getCrop(x, y).Inspect(input.getTileSize(), input.getSpacing(), Bold, new SpriteBatch(GraphicsDevice), cropTypesNames);
}
}
}

View File

@ -44,11 +44,16 @@
<Compile Include="Game1.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sources\Crops\SoilProperties.cs" />
<Compile Include="Sources\Objects\DayNightCycle.cs" />
<Compile Include="Sources\Objects\HandleRotation.cs" />
<Compile Include="Sources\Objects\House.cs" />
<Compile Include="Sources\Crops\Crops.cs" />
<Compile Include="Sources\Crops\Farm.cs" />
<Compile Include="Sources\Controlls\Input.cs" />
<Compile Include="Sources\Objects\Inventory.cs" />
<Compile Include="Sources\Objects\InventorySystem\Cargo.cs" />
<Compile Include="Sources\Objects\InventorySystem\Inventory.cs" />
<Compile Include="Sources\Objects\InventorySystem\Items.cs" />
<Compile Include="Sources\Objects\tractorPositionCorrector.cs" />
<Compile Include="Sources\Pathing\A-Star\Astar.cs" />
<Compile Include="Sources\Pathing\A-Star\PathSaver\MinHeap.cs" />

View File

@ -32,13 +32,13 @@ class Controller
if (state.IsKeyDown(Keys.W) && Size.Y < 20)
{
Size.Y++;
graphics.PreferredBackBufferHeight = (tileSize + Spacing) * (int)Size.Y - Spacing + 100;
graphics.PreferredBackBufferHeight = (tileSize + Spacing) * (int)Size.Y - Spacing + 300;
}
if (state.IsKeyDown(Keys.S) && Size.Y > 2)
{
Size.Y--;
graphics.PreferredBackBufferHeight = (tileSize + Spacing) * (int)Size.Y - Spacing + 100;
graphics.PreferredBackBufferHeight = (tileSize + Spacing) * (int)Size.Y - Spacing + 300;
}
return Size;
}

View File

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
class Crops
{
@ -14,11 +15,19 @@ class Crops
private int Timer = 1;
private int fullTimer;
private bool housePos = false;
private Vector2 Size;
SoilProperties soilProperties = new SoilProperties();
public void updateCrop()
public void updateCrop(Vector2 newSize)
{
if (Status == 4 && Timer != 1)
{
Timer--;
}
Size = newSize;
}
public int getCropTimer()
@ -28,11 +37,16 @@ class Crops
public int getCropTimerBar(int tileSize)
{
int x = (int)(( 1 - ((float)Timer / fullTimer)) * (tileSize - tileSize / 3));
int x = (int)((1 - ((float)Timer / fullTimer)) * (tileSize - tileSize / 3));
return x;
}
public void init()
{
soilProperties.setSoilProperties();
}
// Changes the time required for the crops to be harvestable
@ -57,7 +71,7 @@ class Crops
{
Timer = 2400;
fullTimer = Timer;
}
}
}
public int getCostOnMovement()
@ -109,7 +123,7 @@ class Crops
}
}
}
public void setCropType(int Type)
{
@ -161,4 +175,57 @@ class Crops
{
return housePos;
}
public void Inspect(int tileSize, int Spacing, SpriteFont Bold, SpriteBatch spriteBatch, string[] cropTypesNames)
{
spriteBatch.Begin();
if (housePos)
{
spriteBatch.DrawString(Bold, "Tiletype: House", new Vector2(240, Size.Y * (tileSize + Spacing) + 42), Color.DarkBlue);
}
else if (Status == 0)
{
spriteBatch.DrawString(Bold, "Tiletype: Boulders", new Vector2(240, Size.Y * (tileSize + Spacing) + 42), Color.DarkBlue);
}
else if (Status == 1)
{
spriteBatch.DrawString(Bold, "Tiletype: Grassfield", new Vector2(240, Size.Y * (tileSize + Spacing) + 42), Color.DarkBlue);
}
else if (Status == 2)
{
spriteBatch.DrawString(Bold, "Tiletype: Soil", new Vector2(240, Size.Y * (tileSize + Spacing) + 42), Color.DarkBlue);
}
else if (Status == 3)
{
spriteBatch.DrawString(Bold, "Tiletype: Planted", new Vector2(240, Size.Y * (tileSize + Spacing) + 42), Color.DarkBlue);
}
else if (Status == 4)
{
int x = (int)(((float)Timer / fullTimer) * 100);
x = 100 - x;
spriteBatch.DrawString(Bold, "Tiletype: Crop ", new Vector2(240, Size.Y * (tileSize + Spacing) + 42), Color.DarkBlue);
spriteBatch.DrawString(Bold, "Completion: " + x + "%", new Vector2(240, Size.Y * (tileSize + Spacing) + 82), Color.DarkBlue);
}
if (Status != 4)
{
spriteBatch.DrawString(Bold, "-------------", new Vector2(240, Size.Y * (tileSize + Spacing) + 82), Color.DarkRed);
}
if (Status > 1)
{
spriteBatch.DrawString(Bold, "Prefered Crop: " + cropTypesNames[cropType], new Vector2(240, Size.Y * (tileSize + Spacing) + 62), Color.DarkBlue);
}
else
{
spriteBatch.DrawString(Bold, "None", new Vector2(240, Size.Y * (tileSize + Spacing) + 62), Color.DarkBlue);
}
spriteBatch.DrawString(Bold, "Soil Properties:", new Vector2(240, Size.Y * (tileSize + Spacing) + 122), Color.DarkRed);
spriteBatch.DrawString(Bold, "Soil Type: " + soilProperties.soilType, new Vector2(240, Size.Y * (tileSize + Spacing) + 142), Color.DarkBlue);
spriteBatch.DrawString(Bold, "Temparature: " + soilProperties.Temparature, new Vector2(240, Size.Y * (tileSize + Spacing) + 162), Color.DarkBlue);
spriteBatch.DrawString(Bold, "Moisture: " + soilProperties.Moisture, new Vector2(240, Size.Y * (tileSize + Spacing) + 182), Color.DarkBlue);
spriteBatch.DrawString(Bold, "Humidity: " + soilProperties.Humidity, new Vector2(240, Size.Y * (tileSize + Spacing) + 202), Color.DarkBlue);
spriteBatch.DrawString(Bold, "Phosphorous: " + soilProperties.Phosphorous, new Vector2(240, Size.Y * (tileSize + Spacing) + 222), Color.DarkBlue);
spriteBatch.DrawString(Bold, "Potassium: " + soilProperties.Potassium, new Vector2(240, Size.Y * (tileSize + Spacing) + 242), Color.DarkBlue);
spriteBatch.DrawString(Bold, "Nitrogen: " + soilProperties.Nitrogen, new Vector2(240, Size.Y * (tileSize + Spacing) + 262), Color.DarkBlue);
spriteBatch.End();
}
}

View File

@ -12,8 +12,9 @@ class Farm
private Random r;
//initializes the crops
public void init(Vector2 Size)
public void init(Vector2 Size, Vector2 housepos)
{
r = new Random();
crops = new Crops[100, 100];
for (int i = 0; i < Size.X; i++)
@ -33,6 +34,7 @@ class Farm
crops[i, j].setStatus(x);
crops[i, j].setOriginalStatus();
crops[i, j].setCropType(x = r.Next(0, 4));
crops[i, j].init();
}
}
}
@ -43,19 +45,17 @@ class Farm
{
for (int j = 0; j < Size.Y; j++)
{
if (crops[i, j].getStatus() == 4 && crops[i, j].getCropTimer() != 1)
{
crops[i, j].updateCrop();
}
crops[i, j].updateCrop(Size);
}
}
}
//Changes the properties of the tile when the tractor reaches this tile.
public void setCropStatus(float xfloat, float yfloat, int Spacing)
public void setCropStatus(float xfloat, float yfloat)
{
int x = (int)xfloat / Spacing;
int y = (int)yfloat / Spacing;
int x = (int)xfloat;
int y = (int)yfloat;
if (crops[x, y].getStatus() >= 4)
{
crops[x, y].setStatus(2);

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class SoilProperties
{
static Random r = new Random();
public string soilType = "potato";
public float Temparature;
public float Humidity;
public float Moisture;
public float Nitrogen;
public float Potassium;
public float Phosphorous;
public void setSoilProperties()
{
Temparature = GetRandomNumber(22, 30);
Humidity = GetRandomNumber(1, 5);
Moisture = GetRandomNumber(1, 10);
Nitrogen = GetRandomNumber(0.5 , 1);
Potassium = GetRandomNumber(5, 20);
Phosphorous = GetRandomNumber(4, 50);
}
public float GetRandomNumber(double minimum, double maximum)
{
return (float)(Math.Round(r.NextDouble() * (maximum - minimum) + minimum, 2));
}
}

View File

@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
class DayNightCycle
{
private bool Time = true;
private int nightTime = 0;
private int dayTime = 0;
private int lengthOfDay = 6000;
private int lengthOfNight = 6000;
public void updateTime(int Speed)
{
for (int i = 0; i < Speed; i++)
{
if (Time)
{
dayTime++;
if (dayTime == lengthOfDay)
{
Time = false;
dayTime = 0;
}
}
else
{
nightTime++;
if (nightTime == lengthOfNight)
{
Time = true;
nightTime = 0;
}
}
}
}
public string getDayNight()
{
if (Time)
{
return "Day";
}
else
{
return "Night";
}
}
public Color GetTimeOfDay()
{
int blue;
if (nightTime == 0 || dayTime == 0)
{
blue = 1;
}
else
{
blue = (6000 / nightTime);
}
return Color.FromNonPremultiplied(0, 0, blue, 100);
}
public int GetTimeOfDayInt()
{
if (Time)
{
return dayTime / 60;
}
else
{
return nightTime / 60;
}
}
}

View File

@ -0,0 +1,107 @@
using Microsoft.Xna.Framework;
class HandleRotation
{
int rotationSpeed = 5, Rotation = 180;
public Vector2 UpdatePosition(int Destination, float tractorSpeed, Vector2 Position)
{
Vector2 Direction;
if (Destination == 0)
{
if (Rotation == 0)
{
Direction = new Vector2(0, 1) * tractorSpeed;
Position = Position + Direction;
}
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 >= 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 > 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 < 270 && Rotation > 90)
{
Rotation = Rotation - rotationSpeed;
}
else if (Rotation >= 0 || Rotation > 270)
{
if (Rotation >= 360)
{
Rotation = 0;
}
Rotation = Rotation + rotationSpeed;
}
}
}
return Position;
}
public int getRotation()
{
return Rotation;
}
}

View File

@ -19,7 +19,7 @@ class House
int y = r.Next(0, 8);
pos = new Vector2(x, y);
housePos = new Rectangle((x * tileSize + Spacing), y * (tileSize + Spacing), tileSize, tileSize);
housePos = new Rectangle(x * (tileSize + Spacing), y * (tileSize + Spacing), tileSize, tileSize);
}

View File

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

View File

@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Cargo
{
private Items[,] items = new Items[2, 30];
private int[] Count = new int[2];
// Creation of the house stock
public void initStorageItems()
{
// Name, Weight, Index
// Fertilizer Storage
items[0, 0] = new Items("10-26-26", 1, 0);
items[0, 1] = new Items("14-35-14", 1, 1);
items[0, 2] = new Items("17-17-17", 1, 2);
items[0, 3] = new Items("20-20", 1, 3);
items[0, 4] = new Items("28-28", 1, 4);
items[0, 5] = new Items("DAP", 1, 5);
items[0, 6] = new Items("Urea", 1, 6);
//Crop Seed Storage
items[1, 0] = new Items("Barley", 1, 0);
items[1, 1] = new Items("Cotton", 1, 1);
items[1, 2] = new Items("Ground Nuts", 1, 2);
items[1, 3] = new Items("Maize", 1, 3);
items[1, 4] = new Items("Millets", 1, 4);
items[1, 5] = new Items("Oil Seeds", 1, 5);
items[1, 6] = new Items("Paddy", 1, 6);
items[1, 7] = new Items("Pulses", 1, 7);
items[1, 8] = new Items("Sugarcane", 1, 8);
items[1, 9] = new Items("Tobacco", 1, 9);
items[1, 10] = new Items("Wheat", 1, 10);
}
public void addItem(Items item, int Type)
{
items[Type, Count[Type]] = item;
Count[Type]++;
}
public Items getItemByIndex(int i, int Type)
{
return items[Type, i];
}
public bool itemExists(int Index, int type)
{
if (Count[type] == 0)
{
return false;
}
int i = 0;
do
{
if (items[type, i].getIndex() == Index)
{
return true;
}
i++;
} while (items[type, i] != null && Count[type] > i);
return false;
}
public void removeItem(int Index, int type)
{
int i = 0;
if (itemExists(Index, type))
{
do
{
if (items[type, i].getIndex() == Index)
{
sortBackInPlace(i, type);
break;
}
i++;
} while (items[type, i] != null && Count[type] > i);
}
}
public void sortBackInPlace(int i, int type)
{
do
{
items[type, i] = items[type, i + 1];
i++;
} while (items[type, i] != null && Count[type] > i);
items[type, i] = null;
Count[type]--;
}
public int getCount(int Index, int Type)
{
int Count = 0;
int i = 0;
if (items[Type, 0] != null)
{
do
{
if (items[Type, i].getIndex() == Index)
{
Count++;
}
i++;
} while (items[Type, i] != null);
}
return Count;
}
}

View File

@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
class Inventory
{
private int Weight = 0;
private int maxWeight = 25;
private Cargo cargo = new Cargo();
private Cargo itemStorage = new Cargo();
// Item list by index:
// ---------------------------------------
// (0) Fertilizers
// 1: 10-26-26
// 2: 14-35-14
// 3: 17-17-17
// 4: 20-20
// 5: 28-28
// 6: DAP
// 7: Urea
// (1) Crops:
// 1: Barley
// 2: Cotton
// 3: Ground Nuts
// 4: Maize
// 5: Millets
// 6: Oil Seeds
// 7: Paddy
// 8: Pulses
// 9: Sugarcane
// 10: Tobacco
// 11: Wheat
//
// Number - 1 to get the index in the array
// Adds all the items to the houses inventory for pickup at the houses position
public void initInventorySystem()
{
itemStorage.initStorageItems();
}
public int getWeight()
{
return Weight;
}
// Adds item to the tractor.
// 0 = Fertilizer
// 1 = Crops
public bool addItem(int newItemIndex, int Type)
{
Items newItem;
newItem = itemStorage.getItemByIndex(newItemIndex, Type);
if (maxWeight >= Weight + newItem.getWeight())
{
cargo.addItem(newItem, Type);
Weight = Weight + newItem.getWeight();
return true;
}
else
{
return false;
}
}
// Uses an item from the tractor.
// 0 = Fertilizer
// 1 = Crops
public bool useItem(int Index, int Type)
{
if (cargo.itemExists(Index, Type))
{
removeItem(Index, Type);
return true;
}
return false;
}
public int getMaxWeight()
{
return maxWeight;
}
// Checks whether or not the tractor has the item in its inventory.
// 0 = Fertilizer
// 1 = Crops
public bool itemExists(int Index, int Type)
{
return cargo.itemExists(Index, Type);
}
public void removeItem(int Index, int Type)
{
cargo.removeItem(Index, Type);
}
public Cargo getPredefinedItems()
{
return itemStorage;
}
public void printItems(Input input, SpriteBatch spriteBatch, SpriteFont Bold)
{
spriteBatch.DrawString(Bold, "Crops: ", new Vector2(470, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkRed);
for (int i = 0; i < 11; i++) //Print Crops
{
spriteBatch.DrawString(Bold, cargo.getCount(i, 1) + " " + itemStorage.getItemByIndex(i, 1).getItemType(), new Vector2(470, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20 * i + 22), Color.DarkBlue);
}
spriteBatch.DrawString(Bold, "Fertilizers: ", new Vector2(700, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkRed);
for (int i = 0; i < 7; i++) //Print Fertilizers
{
spriteBatch.DrawString(Bold, cargo.getCount(i, 0) + " " + itemStorage.getItemByIndex(i, 0).getItemType(), new Vector2(700, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20 * i + 22), Color.DarkBlue);
}
}
}

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Items
{
private String itemType;
private int Weight;
private int Index;
public Items(String newItemType, int newWeight, int newIndex)
{
itemType = newItemType;
Weight = newWeight;
Index = newIndex;
}
public void setItem(Items newItem)
{
itemType = newItem.itemType;
Weight = newItem.Weight;
}
public String getItemType()
{
return itemType;
}
public int getWeight()
{
return Weight;
}
public int getIndex()
{
return Index;
}
}

View File

@ -1,45 +1,32 @@
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;
private int Spacing, sizeTile, Speed = 1;
private float tractorSpeed = 1;
private String currentTask;
private Vector2 Position, TargetPosition, Direction, Size, housePos;
private Vector2 Position, TargetPosition, 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();
private HandleRotation handleRotation = new HandleRotation();
public void updateSizing(Input input, int Status, Vector2 newHousePos)
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);
}
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);
smartTractor.init();
}
// Runs when the tractor reaches a tile
@ -55,35 +42,31 @@ class Tractor
}
else if (DeltaPosition.Y > 0)
{
updateRotation(0);
Position = handleRotation.UpdatePosition(0, tractorSpeed, Position);
}
else if (DeltaPosition.Y < 0)
{
updateRotation(1);
Position = handleRotation.UpdatePosition(1, tractorSpeed, Position);
}
}
else if (DeltaPosition.X > 0)
{
updateRotation(2);
Position = handleRotation.UpdatePosition(2, tractorSpeed, Position);
}
else if (DeltaPosition.X < 0)
{
updateRotation(3);
Position = handleRotation.UpdatePosition(3, tractorSpeed, Position);
}
}
//Moves the 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);
for (int i = 0; i < Speed; i++) //Where all the choices the tractor does comes from
for (int i = 0; i < Speed; i++)
{
updateDirection(Size, Position);
}
}
public void calculateNewPath(Vector2 newPosition)
@ -98,17 +81,8 @@ class Tractor
{
Position.Y = (Size.Y - 1) * (sizeTile + Spacing);
}
smartTractor.updateMap(Position, housePos, farm.getCrops(), Size, sizeTile, Spacing, scoreSystem.getScore(), Rotation);
smartTractor.updateMap(Position, housePos, Size, sizeTile, Spacing, handleRotation.getRotation());
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);
updateDirection(Size, newPosition);
}
@ -119,149 +93,6 @@ 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 void updateCrops()
{
for (int i = 0; i < Speed; i++)
{
farm.updateFarm(Size);
}
}
public Vector2 getPos()
{
return Position;
}
public void increaseSpeed()
{
Speed++;
}
public void decreaseSpeed()
{
if (Speed > 0)
{
Speed--;
}
}
public void setSpeed(int newSpeed)
{
Speed = newSpeed;
@ -273,6 +104,32 @@ class Tractor
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;
@ -283,38 +140,18 @@ class Tractor
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;
}
public void setNewHousePos(Vector2 pos, bool newState)
public void drawInventory(Input input, SpriteBatch spriteBatch, SpriteFont Bold, Cargo itemStorageDefined)
{
farm.setNewHousePos(pos, newState);
smartTractor.drawInventory(input, spriteBatch, Bold, itemStorageDefined);
}
}

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
class AI
@ -11,9 +12,10 @@ class AI
private Vector2 tractorPos;
private Vector2 housePos;
private Crops[,] crops;
private Farm farm;
private Vector2 Size;
private Vector2 targetPos;
private Inventory inventory = new Inventory();
private int Rotation;
@ -22,22 +24,52 @@ class AI
private Random r = new Random();
public void init()
{
inventory.initInventorySystem();
inventory.addItem(1, 1);
inventory.addItem(1, 1);
inventory.addItem(3, 1);
inventory.addItem(5, 1);
inventory.addItem(7, 1);
inventory.addItem(7, 1);
inventory.addItem(10, 1);
inventory.addItem(10, 1);
inventory.useItem(10, 1);
}
public void update(Crops[,] newCrops, Vector2 newSize, Vector2 newTractorPos, Vector2 newHousePos, Vector2 newtargetPos, int rotation)
public void update(Farm newFarm, 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;
farm = newFarm;
Size = newSize;
Rotation = rotation;
}
public void drawInventory(Input input, SpriteBatch spriteBatch, SpriteFont Bold, Cargo itemStorageDefined)
{
inventory.printItems(input, spriteBatch, Bold);
}
public Vector2 newTarget()
{
return new Vector2(r.Next(0, (int)Size.X), r.Next(0, (int)Size.Y));
}
public Farm changeCropStatus()
{
if (farm.getCrop((int)tractorPos.X, (int)tractorPos.Y).getCropTimer() == 1)
{
farm.setCropStatus(tractorPos.X, tractorPos.Y);
}
return farm;
}
}

View File

@ -1,38 +1,25 @@
using System;
using Microsoft.Xna.Framework;
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
class SmartTractor
{
private Crops[,] crops;
private Vector2 housePos;
private Vector2 tractorPos;
private Vector2 Size;
private Vector2 Target;
private int tileSize;
private int Score;
private int Spacing;
private Astar astar = new Astar();
private int Rotation;
private AI ai = new AI();
private Farm farm = new Farm();
private Astar astar = new Astar();
private Vector2 housePos, tractorPos, Size, Target;
private int Score, Rotation, tileSize = 56, Spacing = 1;
//What to do next
public Path returnChoice()
{
ai.update(crops, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target / (tileSize + Spacing), Rotation);
ai.update(farm, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target / (tileSize + Spacing), Rotation);
farm = ai.changeCropStatus();
getTargetPosition(ai.newTarget());
astar.update(crops, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target/(tileSize+Spacing), Rotation);
astar.update(farm.getCrops(), Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target/(tileSize+Spacing), Rotation);
return astar.FindPath();
}
@ -43,21 +30,48 @@ class SmartTractor
//Updates the variables every frame
public void updateMap(Vector2 newTractorPos, Vector2 newHousePos, Crops[,] newCropsStatus, Vector2 newSize, int newTileSize, int newSpacing, int newScore, int rotation)
public void updateMap(Vector2 newTractorPos, Vector2 newHousePos, Vector2 newSize, int newTileSize, int newSpacing, int rotation)
{
crops = newCropsStatus;
housePos = newHousePos;
tractorPos = newTractorPos;
Size = newSize;
tileSize = newTileSize;
Spacing = newSpacing;
Score = newScore;
Rotation = rotation;
}
public void init()
{
ai.init();
farm.init(new Vector2(100, (GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / tileSize) - 125 / tileSize), housePos / (tileSize + Spacing));
}
public void drawInventory(Input input, SpriteBatch spriteBatch, SpriteFont Bold, Cargo itemStorageDefined)
{
ai.drawInventory(input, spriteBatch, Bold, itemStorageDefined);
}
private void getTargetPosition(Vector2 newTarget)
{
Target = newTarget * (tileSize + Spacing);
}
public Farm getFarm()
{
return farm;
}
public void setNewHousePos(Vector2 pos, bool newState)
{
farm.setNewHousePos(pos, newState);
}
public void UpdateCrops(int Speed)
{
for (int i = 0; i < Speed; i++)
{
farm.updateFarm(Size);
}
}
}