PotatoPlan/Game1/Sources/Crops/Farm.cs

184 lines
4.8 KiB
C#
Raw Normal View History

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;
2020-05-06 22:37:04 +02:00
private CropTypesHolder PresetCrops = new CropTypesHolder();
2020-05-07 13:53:07 +02:00
private int Update;
2020-05-10 21:16:24 +02:00
private Astar astar = new Astar();
2020-05-11 01:16:01 +02:00
private PerlinNoise perlin = new PerlinNoise();
private float[][] whiteNoise;
private float[][] perlinNoise;
2020-05-03 13:05:05 +02:00
//initializes the crops
2020-05-06 16:22:30 +02:00
public void init(Vector2 Size, Vector2 housepos)
{
2020-05-11 01:16:01 +02:00
whiteNoise = PerlinNoise.GenerateWhiteNoise(100, 100);
perlinNoise = PerlinNoise.GeneratePerlinNoise(whiteNoise, 1);
2020-05-06 22:37:04 +02:00
PresetCrops.init();
r = new Random();
2020-05-03 13:05:05 +02:00
crops = new Crops[100, 100];
2020-05-10 23:56:52 +02:00
int dirtCount = 0;
2020-05-07 21:46:33 +02:00
for (int i = 0; i < 99; i++)
{
2020-05-07 21:46:33 +02:00
for (int j = 0; j < 99; j++)
{
2020-05-11 01:16:01 +02:00
int x = 0;
/*
2020-05-03 13:05:05 +02:00
int x = r.Next(0, 3);
if (x == 0)
{
x = r.Next(0, 2);
}
2020-05-03 19:30:41 +02:00
if (x == 2)
{
x = r.Next(1, 3);
}
2020-05-11 01:16:01 +02:00
*/
2020-05-11 01:54:47 +02:00
if (perlinNoise[i][j] > 0 && perlinNoise[i][j] < 0.15f)
2020-05-11 01:16:01 +02:00
x = 0;
2020-05-11 01:54:47 +02:00
else if (perlinNoise[i][j] >= 0.15f && perlinNoise[i][j] < 0.8f)
2020-05-11 01:16:01 +02:00
x = 1;
2020-05-11 01:54:47 +02:00
else if (perlinNoise[i][j] >= 0.8f)
2020-05-11 01:16:01 +02:00
x = 2;
crops[i, j] = new Crops();
2020-05-05 16:27:45 +02:00
crops[i, j].setStatus(x);
crops[i, j].setOriginalStatus();
x = r.Next(0, 12);
2020-05-07 01:55:52 +02:00
crops[i, j].setCropType(x, PresetCrops.getPresetCropTypes(x));
2020-05-06 16:22:30 +02:00
crops[i, j].init();
2020-05-10 23:56:52 +02:00
if (crops[i, j].getStatus() == 2)
{
dirtCount++;
}
}
}
2020-05-10 21:16:24 +02:00
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))
{
2020-05-10 23:56:52 +02:00
//crops[i, j].setStatus(1);
2020-05-10 21:16:24 +02:00
dirtCount--;
}
}
}
}
2020-05-11 00:04:59 +02:00
if (dirtCount != 0)
2020-05-10 21:16:24 +02:00
init(Size, housepos);
}
public void updateFarm(Vector2 Size)
{
2020-05-07 13:53:07 +02:00
Update++;
if (Update == 30)
{
2020-05-07 13:53:07 +02:00
for (int i = 0; i < Size.X; i++)
{
2020-05-07 13:53:07 +02:00
for (int j = 0; j < Size.Y; j++)
{
2020-05-06 16:22:30 +02:00
2020-05-07 13:53:07 +02:00
crops[i, j].updateCrop(Size);
}
}
2020-05-07 13:53:07 +02:00
Update = 0;
}
2020-05-07 13:53:07 +02:00
}
2020-05-03 13:05:05 +02:00
//Changes the properties of the tile when the tractor reaches this tile.
2020-05-06 16:22:30 +02:00
public void setCropStatus(float xfloat, float yfloat)
{
2020-05-06 16:22:30 +02:00
int x = (int)xfloat;
int y = (int)yfloat;
2020-05-07 01:55:52 +02:00
if (crops[x, y].getStatus() >= 3)
{
2020-05-05 16:27:45 +02:00
crops[x, y].setStatus(2);
}
2020-05-05 16:27:45 +02:00
else if(crops[x, y].getStatus() == 0)
{
//do nothing
}
2020-05-05 16:27:45 +02:00
else if (crops[x, y].getStatus() == 2)
{
2020-05-10 01:38:08 +02:00
2020-05-05 16:27:45 +02:00
crops[x, y].setStatus(3);
crops[x, y].setCropTimer();
2020-05-03 13:05:05 +02:00
}
}
public Crops getCrop(int x, int y)
{
return crops[x,y];
}
public Crops[,] getCrops()
{
return crops;
}
2020-05-05 16:27:45 +02:00
public void setNewHousePos(Vector2 pos, bool newState)
{
crops[(int)pos.X, (int)pos.Y].setHousePos(newState);
}
2020-05-06 22:37:04 +02:00
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));
}
2020-05-07 21:46:33 +02:00
public void UpdatePreferedCrops(Vector2 Size)
{
for (int i = 0; i < Size.X; i++)
{
for (int j = 0; j < Size.X; j++)
{
2020-05-10 19:21:52 +02:00
if (crops[i, j].getStatus() != 3)
{
int x = getHighestProductionRate(i, j);
crops[i, j].setCropType(x, PresetCrops.getPresetCropTypes(x));
}
2020-05-07 21:46:33 +02:00
}
}
}
private int getHighestProductionRate(int x, int y)
{
2020-05-07 21:46:33 +02:00
int i = 1, holderIndex = 0;
float holder = 0, SampleHolder = 0;
do
{
2020-05-07 21:46:33 +02:00
holder = getProductionRate(x, y, i);
if (SampleHolder < holder)
{
holderIndex = i;
2020-05-07 21:46:33 +02:00
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));
2020-05-06 22:37:04 +02:00
}
}