1
0
forked from s425077/PotatoPlan
JoelForkTest/Game1/Sources/Crops/Farm.cs
2020-05-24 13:36:31 +02:00

253 lines
7.4 KiB
C#

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;
class Farm
{
private Crops[,] crops;
private Random r;
private CropTypesHolder PresetCrops = new CropTypesHolder();
private int Update;
private Astar astar = new Astar();
private PerlinNoise perlin = new PerlinNoise();
private Vector2 RainPosition;
private Vector2 WindSpeed = new Vector2(0,0);
private System.Drawing.Color[][] RainfallMap;
private float[][] whiteNoise;
private float[][] perlinNoise;
private int resetBitMap = 0;
//initializes the crops
public void init(Vector2 Size, Vector2 housepos)
{
whiteNoise = PerlinNoise.GenerateWhiteNoise(100, 100);
perlinNoise = PerlinNoise.GeneratePerlinNoise(whiteNoise, 1);
PresetCrops.init();
r = new Random();
crops = new Crops[100, 100];
int dirtCount = 0;
for (int i = 0; i < 99; i++)
{
for (int j = 0; j < 99; j++)
{
int x = 0;
if (perlinNoise[i][j] > 0 && perlinNoise[i][j] < 0.15f)
x = 0;
else if (perlinNoise[i][j] >= 0.15f && perlinNoise[i][j] < 0.8f)
x = 1;
else if (perlinNoise[i][j] >= 0.8f)
x = 2;
crops[i, j] = new Crops();
crops[i, j].setStatus(x);
crops[i, j].setOriginalStatus();
x = r.Next(0, 12);
crops[i, j].setCropType(x, PresetCrops.getPresetCropTypes(x));
crops[i, j].init();
if (crops[i, j].getStatus() == 2)
{
dirtCount++;
}
}
}
for (int i = 0; i < 99; i++)
{
for (int j = 0; j < 99; j++)
{
if (crops[i, j].getStatus() == 2)
{
if (!astar.isReachable(crops, new Vector2((int)i, (int)j), housepos))
{
dirtCount--;
}
}
}
}
if (dirtCount != 0)
init(Size, housepos);
RainPosition.X = r.Next(0, 1900);
RainPosition.Y = r.Next(0, 1950);
RainfallMap = PerlinNoise.LoadImage("C:\\Users\\Joel\\source\\repos\\Oskars Repo\\Game1\\Content\\Rainfall.png");
}
public Rectangle getRainPosition(int TileSize, int x, int y)
{
return new Rectangle(x * TileSize, y * TileSize, TileSize, TileSize);
}
public Rectangle getDestinationRectangle(int x, int y)
{
return new Rectangle(x + (int)Math.Round(RainPosition.X), y + (int)Math.Round(RainPosition.Y), 1, 1);
}
public void drawWeatherInformation(SpriteBatch spriteBatch, SpriteFont Bold, Input input)
{
spriteBatch.DrawString(Bold, "WindSpeed: " + Math.Round(WindSpeed.X, 4), new Vector2(10, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 282), Color.DarkBlue);
spriteBatch.DrawString(Bold, " : ", new Vector2(153, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 282), Color.DarkBlue);
spriteBatch.DrawString(Bold, Math.Round(WindSpeed.Y, 4).ToString(), new Vector2(163, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 282), Color.DarkBlue);
}
public void updateFarm(Vector2 Size, int nDay)
{
Update++;
if (Update == 30)
{
for (int i = 0; i < Size.X; i++)
{
for (int j = 0; j < Size.Y; j++)
{
crops[i, j].updateCrop(Size, RainfallMap[(int)Math.Round(RainPosition.X) + i][(int)Math.Round(RainPosition.Y) + j].GetBrightness());
}
}
Update = 0;
}
updateRainMapPosition();
}
//Changes the properties of the tile when the tractor reaches this tile.
public void setCropStatus(float xfloat, float yfloat)
{
int x = (int)xfloat;
int y = (int)yfloat;
if (crops[x, y].getStatus() >= 3)
{
crops[x, y].setStatus(2);
}
else if(crops[x, y].getStatus() == 0)
{
//do nothing
}
else if (crops[x, y].getStatus() == 2)
{
crops[x, y].setStatus(3);
crops[x, y].setCropTimer();
}
}
public Crops getCrop(int x, int y)
{
return crops[x,y];
}
public Crops[,] getCrops()
{
return crops;
}
private void updateRainMapPosition()
{
float x, y;
x = WindSpeed.X + GetRandomNumber(-1f, 1f) / 2000;
y = WindSpeed.Y + GetRandomNumber(-1f, 1f) / 2000;
if (x < 0.02f && x > -0.02f)
{
WindSpeed.X = x;
}
if (y < 0.02f && y > -0.02f)
{
WindSpeed.Y = y;
}
if (WindSpeed.X > 0 && RainPosition.X < 1900)
{
RainPosition.X = RainPosition.X + WindSpeed.X;
}
else if (WindSpeed.X < 0 && RainPosition.X > 1)
{
RainPosition.X = RainPosition.X + WindSpeed.X;
}
if (WindSpeed.Y > 0 && RainPosition.Y < 1900)
{
RainPosition.Y = RainPosition.Y + WindSpeed.Y;
}
else if (WindSpeed.Y < 0 && RainPosition.Y > 1)
{
RainPosition.Y = RainPosition.Y + WindSpeed.Y;
}
resetBitMap++;
if (resetBitMap == 100000)
{
RainPosition.X = r.Next(700, 1300);
RainPosition.Y = r.Next(700, 1300);
resetBitMap = 0;
}
}
public void setNewHousePos(Vector2 pos, bool newState)
{
crops[(int)pos.X, (int)pos.Y].setHousePos(newState);
}
public CropTypes getPresetCropTypes(int Index)
{
return PresetCrops.getPresetCropTypes(Index - 1);
}
public void setCropType(int x, int y, int Type)
{
crops[x, y].setCropType(Type, PresetCrops.getPresetCropTypes(Type));
}
public void UpdatePreferedCrops(Vector2 Size)
{
for (int i = 0; i < Size.X; i++)
{
for (int j = 0; j < Size.X; j++)
{
if (crops[i, j].getStatus() != 3)
{
int x = getHighestProductionRate(i, j);
crops[i, j].setCropType(x, PresetCrops.getPresetCropTypes(x));
}
}
}
}
private int getHighestProductionRate(int x, int y)
{
int i = 1, holderIndex = 0;
float holder = 0, SampleHolder = 0;
do
{
holder = getProductionRate(x, y, i);
if (SampleHolder < holder)
{
holderIndex = i;
SampleHolder = holder;
}
i++;
} while (i < 12);
return holderIndex;
}
public Color getRainAmount(int x, int y)
{
return Color.FromNonPremultiplied(255, 255, 255, (int)(255 * RainfallMap[x + (int)Math.Round(RainPosition.X)][y + (int)Math.Round(RainPosition.Y)].GetBrightness()));
}
public float getProductionRate(int x, int y, int Type)
{
return crops[x, y].getProductionRate(PresetCrops.getPresetCropTypes(Type));
}
public float GetRandomNumber(double minimum, double maximum)
{
return (float)(Math.Round(r.NextDouble() * (maximum - minimum) + minimum, 2));
}
}