PotatoPlan/Game1/Sources/Crops/Farm.cs
2020-05-23 20:55:45 +02:00

251 lines
7.1 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))
{
//crops[i, j].setStatus(1);
dirtCount--;
}
}
}
}
if (dirtCount != 0)
init(Size, housepos);
RainPosition.X = r.Next(0, 1900);
RainPosition.Y = r.Next(0, 1950);
WindSpeed.X = r.Next(-1, 1);
WindSpeed.Y = r.Next(-1, 1);
RainfallMap = PerlinNoise.LoadImage("C:\\Users\\Joel\\source\\repos\\Oskars Repo\\Game1\\Content\\Rainfall.bmp");
}
public Rectangle getRainPosition(int TileSize, int x, int y)
{
return new Rectangle((int)(-TileSize * (RainPosition.X - Math.Truncate(RainPosition.X))) + TileSize * x, (int)(-TileSize * (RainPosition.Y - Math.Truncate(RainPosition.Y))) + TileSize * y, TileSize, TileSize);
}
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(-0.02, 0.02);
y = WindSpeed.Y + GetRandomNumber(-0.02, 0.02);
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)
{
if (RainfallMap[x + (int)Math.Round(RainPosition.X)][y + (int)Math.Round(RainPosition.Y)].GetBrightness() < 0.60f)
{
return Color.FromNonPremultiplied(255, 255, 255, 0);
}
else
{
return Color.FromNonPremultiplied(255, 255, 255, (int)(300 * 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));
}
}