PotatoPlan/Game1/Sources/Crops/Farm.cs
2020-05-11 01:54:47 +02:00

184 lines
4.8 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 float[][] whiteNoise;
private float[][] perlinNoise;
//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;
/*
int x = r.Next(0, 3);
if (x == 0)
{
x = r.Next(0, 2);
}
if (x == 2)
{
x = r.Next(1, 3);
}
*/
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);
}
public void updateFarm(Vector2 Size)
{
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);
}
}
Update = 0;
}
}
//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;
}
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 float getProductionRate(int x, int y, int Type)
{
return crops[x, y].getProductionRate(PresetCrops.getPresetCropTypes(Type));
}
}