PotatoPlan/Game1/Sources/Objects/DayNightCycle.cs
2020-05-25 10:59:17 +02:00

163 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
class DayNightCycle
{
private bool Time = true;
private bool Day = true;
private int nightTime = 0;
private int dayTime = 0;
private int lengthOfDay = 20000;
private int lengthOfNight = 20000;
private int Days = 1;
private int DaysOfYear = 0;
private int speed = 1;
private bool nextDay = true;
public void updateTime(int Speed)
{
speed = Speed;
Time = false;
for (int i = 0; i < Speed; i++)
{
if (Day)
{
dayTime++;
if (dayTime == lengthOfDay)
{
Time = false;
Day = false;
dayTime = 0;
}
}
else
{
nightTime++;
if (nightTime == lengthOfNight)
{
Time = true;
Day = true;
nightTime = 0;
Days++;
DaysOfYear++;
}
}
if (DaysOfYear == 48)
{
DaysOfYear = 0;
}
}
}
//Season:
//0 - Whole Year
//1 - Spring
//2 - Summer
//3 - Autumn
//4 - Winter
public string getTimeOfYear()
{
if (DaysOfYear < 12)
return "Spring";
else if (DaysOfYear < 24)
return "Summer";
else if (DaysOfYear < 36)
return "Autumn";
else
return "Winter";
}
public Color GetTimeOfDay()
{
int blue, red, brightness, green, potatorate = 255;
if (nightTime == 0 && dayTime == 0)
{
red = 1;
blue = 1;
green = 1;
brightness = 1;
}
else
{
if ((float)dayTime / lengthOfDay < 0.5)
{
blue = (int)(((float)nightTime / lengthOfNight) * potatorate) + (int)((1.0f - (float)dayTime / lengthOfDay) * potatorate);
}
else
{
blue = (int)(((float)nightTime / lengthOfNight) * potatorate) + (int)(((float)dayTime / lengthOfDay) * potatorate);
}
if ((float)nightTime / lengthOfNight < 0.5)
{
red = (int)((1.0 - (float)nightTime / lengthOfNight) * potatorate) + (int)(((float)dayTime / lengthOfDay) * potatorate);
}
else
{
red = (int)(((float)nightTime / lengthOfNight) * potatorate) + (int)(((float)dayTime / lengthOfDay) * potatorate);
}
if ((float)nightTime / lengthOfNight < 0.5)
{
green = (int)((1.0 - (float)nightTime / lengthOfNight) * potatorate) + (int)(((float)dayTime / lengthOfDay) * potatorate);
}
else
{
green = (int)(((float)nightTime / lengthOfNight) * potatorate) + (int)(((float)dayTime / lengthOfDay) * potatorate);
}
if (Time)
{
brightness = 255;
}
else
{
if ((float)nightTime / lengthOfNight < 0.5)
{
brightness = (int)((1.0 - (float)nightTime / lengthOfNight) * 255) + (int)(((float)dayTime / lengthOfDay) * 255);
}
else
{
brightness = (int)(((float)nightTime / lengthOfNight) * 255) + (int)(((float)dayTime / lengthOfDay) * 255);
}
}
}
return Color.FromNonPremultiplied(red, green, blue, 255);
}
public bool nDay()
{
if (Time)
return true;
else
return false;
}
public int GetTimeOfDayInt()
{
if (Day)
{
return (int)(100 * ((float)(dayTime + nightTime) / (lengthOfDay + lengthOfNight))) + 1;
}
else
{
return (int)(100 * ((float)(lengthOfDay + nightTime) / (lengthOfDay + lengthOfNight))) + 1;
}
}
public int getDays()
{
return Days;
}
}