PotatoPlan/Game1/Game1.cs

342 lines
18 KiB
C#
Raw Normal View History

2020-04-07 17:50:31 +02:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
2020-05-11 01:30:18 +02:00
using System.IO;
2020-04-07 17:50:31 +02:00
namespace Game1
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont Bold;
2020-05-03 13:05:05 +02:00
private Texture2D[] tile = new Texture2D[5];
2020-05-23 13:54:47 +02:00
private Texture2D[] tileConnected = new Texture2D[5];
private Texture2D[] Crops = new Texture2D[12];
2020-04-07 17:50:31 +02:00
private Texture2D tractor;
private Texture2D house;
private Texture2D markers;
2020-05-05 16:27:45 +02:00
private Texture2D mouseCursor;
2020-05-23 20:37:11 +02:00
private Texture2D Rain;
2020-05-11 01:30:18 +02:00
string directory = Directory.GetCurrentDirectory();
2020-05-05 16:27:45 +02:00
private Texture2D ProgressionBar;
private Texture2D ProgressionBarStatus;
2020-04-07 17:50:31 +02:00
private Tractor tractorUnit = new Tractor();
private Input input = new Input();
private House houseUnit = new House();
2020-05-05 16:27:45 +02:00
private Rectangle mousePosition;
2020-05-06 16:22:30 +02:00
private Inventory inventory = new Inventory();
DayNightCycle Time = new DayNightCycle();
private string[] cropTypesNames = new string[12];
private int x, y;
2020-05-05 16:27:45 +02:00
MouseState state;
2020-04-07 17:50:31 +02:00
2020-05-23 20:37:11 +02:00
private int cloudAnimationSpeed = 2;
private int cloudFrame = 0;
private int cloudSprite;
2020-04-07 17:50:31 +02:00
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
cropTypesNames[0] = "None";
cropTypesNames[1] = "Barley";
cropTypesNames[2] = "Cotton";
cropTypesNames[3] = "Ground Nuts";
cropTypesNames[4] = "Maize";
cropTypesNames[5] = "Millets";
cropTypesNames[6] = "Oil Seeds";
cropTypesNames[7] = "Paddy";
cropTypesNames[8] = "Pulses";
cropTypesNames[9] = "Sugarcane";
cropTypesNames[10] = "Tobacco";
cropTypesNames[11] = "Wheat";
2020-05-06 16:22:30 +02:00
2020-05-10 12:55:13 +02:00
Engine.init();
2020-05-06 16:22:30 +02:00
//Generates the map with some random values
2020-05-06 16:22:30 +02:00
inventory.initInventorySystem();
2020-05-11 01:30:18 +02:00
string path = directory + "Game1/Content/ML/Fertilizer_Prediction.csv";
2020-05-12 01:46:16 +02:00
input.init(graphics, new Vector2(16,16), 56, 0); //Generates the starting size
2020-05-03 13:05:05 +02:00
houseUnit.init(input.getTileSize(), input.getSpacing()); //Generates the house position
tractorUnit.init(houseUnit.GetRectangle(), input); //Generates the Tractor
2020-05-06 16:22:30 +02:00
tractorUnit.updateSizing(input, 0, houseUnit.getVector(), Time); //Updates the first Size of the Tractor
2020-05-03 13:05:05 +02:00
tractorUnit.setPos(houseUnit.getVector()); //Changes the position of the tractor to the houses position at the start
2020-05-06 16:22:30 +02:00
tractorUnit.setNewHousePos(houseUnit.getVector() / input.getSpacingTile(), true);
2020-04-07 17:50:31 +02:00
graphics.PreferredBackBufferWidth = (input.getTileSize() + input.getSpacing()) * (int)input.getSize().X;
2020-05-23 20:37:11 +02:00
graphics.PreferredBackBufferHeight = (input.getTileSize() + input.getSpacing()) * (int)input.getSize().Y + 380;
2020-04-07 17:50:31 +02:00
graphics.ApplyChanges();
}
protected override void LoadContent()
{
2020-04-07 17:50:31 +02:00
spriteBatch = new SpriteBatch(GraphicsDevice);
//Loads the PNG content and Fonts
2020-05-03 13:05:05 +02:00
tile[0] = Content.Load<Texture2D>("Mountain");
tile[1] = Content.Load<Texture2D>("tileunplantable");
tile[2] = Content.Load<Texture2D>("Plantable");
2020-05-07 01:55:52 +02:00
tile[3] = Content.Load<Texture2D>("Crop");
2020-05-05 16:27:45 +02:00
2020-05-23 13:54:47 +02:00
tileConnected[0] = Content.Load<Texture2D>("Left");
tileConnected[1] = Content.Load<Texture2D>("Top");
tileConnected[2] = Content.Load<Texture2D>("Right");
tileConnected[3] = Content.Load<Texture2D>("Bottom");
2020-05-23 20:37:11 +02:00
Rain = Content.Load<Texture2D>("rain");
2020-05-07 01:55:52 +02:00
Crops[0] = Content.Load<Texture2D>("Markers");
Crops[1] = Content.Load<Texture2D>("BarleyIcon");
Crops[2] = Content.Load<Texture2D>("CottonIcon");
Crops[3] = Content.Load<Texture2D>("GroundNutsIcon");
Crops[4] = Content.Load<Texture2D>("MaizeIcon");
Crops[5] = Content.Load<Texture2D>("MilletsIcon");
Crops[6] = Content.Load<Texture2D>("SeedOilIcon");
Crops[7] = Content.Load<Texture2D>("PaddyIcon");
Crops[8] = Content.Load<Texture2D>("PulsesIcon");
Crops[9] = Content.Load<Texture2D>("SugarcaneIcon");
Crops[10] = Content.Load<Texture2D>("TobaccoIcon");
Crops[11] = Content.Load<Texture2D>("WheatIcon");
2020-05-07 01:55:52 +02:00
2020-05-05 16:27:45 +02:00
ProgressionBar = Content.Load<Texture2D>("ProgressionBar");
ProgressionBarStatus = Content.Load<Texture2D>("ProgressionBarStatus");
2020-04-07 17:50:31 +02:00
tractor = Content.Load<Texture2D>("Tractor");
2020-05-05 16:27:45 +02:00
mouseCursor = Content.Load<Texture2D>("MouseCursor");
2020-04-07 17:50:31 +02:00
Bold = Content.Load<SpriteFont>("Font");
house = Content.Load<Texture2D>("house");
markers = Content.Load<Texture2D>("Markers");
2020-04-07 17:50:31 +02:00
}
2020-04-07 17:50:31 +02:00
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime) //updates every 60 seconds
2020-04-07 17:50:31 +02:00
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
2020-05-05 16:27:45 +02:00
state = Mouse.GetState();
mousePosition = new Rectangle(state.X, state.Y, 1, 1);
2020-05-06 16:22:30 +02:00
tractorUnit.updateSizing(input, 0, houseUnit.getVector(), Time); //Updates the size
2020-05-03 13:05:05 +02:00
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
2020-05-03 13:05:05 +02:00
houseUnit.updateRectangle(input.getSize(), input.getTileSize(), input.getSpacing()); //Updates the position of the house if the house appears out of bound
2020-05-06 16:22:30 +02:00
Time.updateTime(tractorUnit.getSpeed());
2020-04-07 17:50:31 +02:00
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime) //Draw Function
{
2020-05-06 16:22:30 +02:00
GraphicsDevice.Clear(Color.FromNonPremultiplied(255,255,255,140));
2020-04-07 17:50:31 +02:00
spriteBatch.Begin();
2020-05-05 16:27:45 +02:00
DrawTiles();
2020-05-12 15:43:03 +02:00
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.White);
2020-05-03 13:05:05 +02:00
for (int i = 0; i < tractorUnit.getPath().getCount() + 1; i++)
{
2020-05-12 15:43:03 +02:00
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.White);
2020-05-03 13:05:05 +02:00
}
2020-04-07 17:50:31 +02:00
2020-05-06 20:48:20 +02:00
spriteBatch.Draw(house, houseUnit.GetRectangle(), Time.GetTimeOfDay());
2020-05-12 15:43:03 +02:00
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.White); //Draws the current target of the tractor
2020-05-06 20:48:20 +02:00
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()), Time.GetTimeOfDay(), tractorUnit.getRotation(), new Vector2(input.getTileSize() / 2, input.getTileSize() / 2), 1.0f, SpriteEffects.None, 1);
2020-05-06 16:22:30 +02:00
for (int i = 0; i < 5; i++)
{
2020-05-23 20:37:11 +02:00
spriteBatch.Draw(tile[0], new Rectangle(i * 227, (int)(input.getSize().Y * (input.getTileSize() + input.getSpacing())), 5, 350), Color.White);
2020-05-06 16:22:30 +02:00
}
2020-05-23 20:37:11 +02:00
for (int i = 0; i < 17; i++)
2020-05-06 16:22:30 +02:00
{
2020-05-23 20:37:11 +02:00
spriteBatch.Draw(tile[0], new Rectangle(0, (int)(input.getSize().Y * (input.getTileSize() + input.getSpacing())) + i * 20, (int)(input.getSize().X * (input.getTileSize() + input.getSpacing())), 1), Color.White);
2020-05-06 16:22:30 +02:00
}
spriteBatch.DrawString(Bold, "Time: ", new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkRed);
2020-05-06 20:48:20 +02:00
spriteBatch.DrawString(Bold, "Days " + Time.getDays(), new Vector2(60, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkBlue);
spriteBatch.DrawString(Bold, "Day Progression: ", new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 22), Color.DarkRed);
spriteBatch.DrawString(Bold, Time.GetTimeOfDayInt().ToString() + "%", new Vector2(140, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 22), Color.DarkBlue);
2020-05-06 16:22:30 +02:00
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);
2020-05-06 20:48:20 +02:00
spriteBatch.DrawString(Bold, "Total Weight: ", new Vector2(700, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 182), Color.DarkRed);
spriteBatch.DrawString(Bold, "(" + tractorUnit.getInventory().getWeight() + "/" + tractorUnit.getInventory().getMaxWeight() + ")", new Vector2(800, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 182), Color.DarkBlue);
2020-05-06 16:22:30 +02:00
tractorUnit.drawInventory(input, spriteBatch, Bold, inventory.getPredefinedItems());
InspectTile();
2020-05-05 16:27:45 +02:00
spriteBatch.Draw(mouseCursor, new Rectangle((int)mousePosition.X, (int)mousePosition.Y, 14, 21), Color.White);
2020-04-07 17:50:31 +02:00
spriteBatch.End();
base.Draw(gameTime);
}
2020-05-05 16:27:45 +02:00
public void DrawTiles()
{
for (int i = 0; i < input.getSize().X; i++) //Draw the tiles
{
for (int j = 0; j < input.getSize().Y; j++)
{
2020-05-07 01:55:52 +02:00
2020-05-23 20:37:11 +02:00
Rectangle tilePos = new Rectangle(i * (input.getSpacingTile()), j * (input.getSpacingTile()), input.getTileSize(), input.getTileSize());
2020-05-06 20:48:20 +02:00
spriteBatch.Draw(tile[tractorUnit.getFarm().getCrop(i, j).getStatus()], tilePos, Time.GetTimeOfDay());
2020-05-23 13:54:47 +02:00
if (i > 0)
{
if (tractorUnit.getFarm().getCrop(i - 1, j).getStatus() == 2 || tractorUnit.getFarm().getCrop(i - 1, j).getStatus() == 3)
{
spriteBatch.Draw(tileConnected[0], tilePos, Time.GetTimeOfDay());
}
}
if (j > 0)
{
if (tractorUnit.getFarm().getCrop(i, j - 1).getStatus() == 2 || tractorUnit.getFarm().getCrop(i, j - 1).getStatus() == 3)
{
spriteBatch.Draw(tileConnected[1], tilePos, Time.GetTimeOfDay());
}
}
if (i < input.getSize().X)
{
if (tractorUnit.getFarm().getCrop(i + 1, j).getStatus() == 2 || tractorUnit.getFarm().getCrop(i + 1, j).getStatus() == 3)
{
spriteBatch.Draw(tileConnected[2], tilePos, Time.GetTimeOfDay());
}
}
if (j < input.getSize().Y)
{
if (tractorUnit.getFarm().getCrop(i, j + 1).getStatus() == 2 || tractorUnit.getFarm().getCrop(i, j + 1).getStatus() == 3)
{
spriteBatch.Draw(tileConnected[3], tilePos, Time.GetTimeOfDay());
}
}
2020-05-05 16:27:45 +02:00
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());
}
2020-05-06 16:22:30 +02:00
if (state.RightButton == ButtonState.Pressed)
{
x = i;
y = j;
}
2020-05-07 01:55:52 +02:00
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(), j * input.getSpacingTile(), input.getTileSize(), input.getTileSize()), Color.White);
}
2020-05-05 16:27:45 +02:00
}
2020-05-07 01:55:52 +02:00
else
2020-05-05 16:27:45 +02:00
{
2020-05-07 01:55:52 +02:00
if (tractorUnit.getFarm().getCrop(i, j).getStatus() != 0 && tractorUnit.getFarm().getCrop(i, j).getStatus() != 1)
{
if ((tractorUnit.getFarm().getCrop(i, j).getStatus() == 3))
{
spriteBatch.Draw(ProgressionBar, new Rectangle(i * (input.getSpacingTile()) + input.getTileSize() - input.getTileSize() / 3, j * (input.getSpacingTile()), input.getTileSize() / 3, input.getTileSize()), Color.White);
2020-05-10 18:17:53 +02:00
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), new Color(tractorUnit.getFarm().getCrop(i, j).getColour()));
2020-05-07 01:55:52 +02:00
}
spriteBatch.Draw(Crops[tractorUnit.getFarm().getCrop(i, j).getCropType()], new Rectangle((int)(i * (input.getSpacingTile()) + input.getTileSize() - input.getTileSize() / 2.5), j * (input.getSpacingTile()), (int)(input.getTileSize() / 2.5), input.getTileSize() / 3), Color.White);
}
2020-05-05 16:27:45 +02:00
}
}
}
2020-05-23 20:37:11 +02:00
cloudFrame++;
if (cloudFrame > cloudAnimationSpeed)
{
cloudSprite++;
if (cloudSprite > 4)
{
cloudSprite = 0;
}
cloudFrame = 0;
}
for (int i = -1; i < input.getSize().X + 1; i++) //Draw the tiles
{
for (int j = -1; j < input.getSize().Y + 1; j++)
{
spriteBatch.Draw(Rain, tractorUnit.getFarm().getRainPosition(input.getTileSize(), i, j), new Rectangle(0, cloudSprite * 96, input.getTileSize(), input.getTileSize()), Color.White);
}
}
2020-05-05 16:27:45 +02:00
}
2020-05-06 16:22:30 +02:00
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);
}
2020-04-07 17:50:31 +02:00
}
}