2021-09-14 12:05:36 +03:00
|
|
|
#include "framework.h"
|
|
|
|
#include "savegame.h"
|
2021-09-15 11:13:47 +03:00
|
|
|
#include "weather.h"
|
2021-09-14 12:05:36 +03:00
|
|
|
#include "Sound\sound.h"
|
|
|
|
#include "Scripting\GameScriptLevel.h"
|
|
|
|
|
|
|
|
namespace TEN {
|
|
|
|
namespace Effects {
|
2021-09-15 11:13:47 +03:00
|
|
|
namespace Environment
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 11:13:47 +03:00
|
|
|
EnvironmentController Weather;
|
2021-09-14 12:05:36 +03:00
|
|
|
|
2021-09-15 11:13:47 +03:00
|
|
|
void EnvironmentController::Update()
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
|
|
|
GameScriptLevel* level = g_GameFlow->GetLevel(CurrentLevel);
|
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
UpdateSky(level);
|
|
|
|
UpdateStorm(level);
|
|
|
|
UpdateWind(level);
|
|
|
|
UpdateFlash(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnvironmentController::Clear()
|
|
|
|
{
|
|
|
|
// Clear storm vars
|
|
|
|
StormTimer = 0;
|
|
|
|
StormSkyColor = 1;
|
|
|
|
StormSkyColor2 = 1;
|
|
|
|
|
|
|
|
// Clear wind vars
|
|
|
|
WindCurrent = WindFinalX = WindFinalZ = 0;
|
|
|
|
WindAngle = WindDAngle = 2048;
|
|
|
|
|
|
|
|
// Clear flash vars
|
|
|
|
FlashProgress = 0.0f;
|
2021-09-15 14:24:03 +03:00
|
|
|
FlashColorBase = Vector3::Zero;
|
2021-09-15 12:58:07 +03:00
|
|
|
}
|
2021-09-15 11:13:47 +03:00
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
void EnvironmentController::Flash(int r, int g, int b, float speed)
|
|
|
|
{
|
|
|
|
FlashProgress = 1.0f;
|
2021-09-15 13:16:14 +03:00
|
|
|
FlashSpeed = std::clamp(speed, 0.005f, 1.0f);
|
2021-09-15 14:24:03 +03:00
|
|
|
FlashColorBase = Vector3(std::clamp(r, 0, UCHAR_MAX) / (float)UCHAR_MAX,
|
2021-09-15 12:58:07 +03:00
|
|
|
std::clamp(g, 0, UCHAR_MAX) / (float)UCHAR_MAX,
|
2021-09-15 14:24:03 +03:00
|
|
|
std::clamp(b, 0, UCHAR_MAX) / (float)UCHAR_MAX);
|
2021-09-15 12:58:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void EnvironmentController::UpdateSky(GameScriptLevel* level)
|
|
|
|
{
|
2021-09-14 12:05:36 +03:00
|
|
|
if (level->Layer1.Enabled)
|
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
SkyPosition1 += level->Layer1.CloudSpeed;
|
|
|
|
if (SkyPosition1 <= 9728)
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
if (SkyPosition1 < 0)
|
|
|
|
SkyPosition1 += 9728;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
SkyPosition1 -= 9728;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level->Layer2.Enabled)
|
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
SkyPosition2 += level->Layer2.CloudSpeed;
|
|
|
|
if (SkyPosition2 <= 9728)
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
if (SkyPosition2 < 0)
|
|
|
|
SkyPosition2 += 9728;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
SkyPosition2 -= 9728;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-15 12:58:07 +03:00
|
|
|
}
|
2021-09-14 12:05:36 +03:00
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
void EnvironmentController::UpdateStorm(GameScriptLevel* level)
|
|
|
|
{
|
2021-09-14 12:05:36 +03:00
|
|
|
auto color = Vector4(level->Layer1.R / 255.0f, level->Layer1.G / 255.0f, level->Layer1.B / 255.0f, 1.0f);
|
|
|
|
|
|
|
|
if (level->Storm)
|
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
if (StormCount || StormRand)
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
UpdateLightning();
|
2021-09-14 12:05:36 +03:00
|
|
|
if (StormTimer > -1)
|
|
|
|
StormTimer--;
|
|
|
|
if (!StormTimer)
|
|
|
|
SoundEffect(SFX_TR4_THUNDER_RUMBLE, NULL, 0);
|
|
|
|
}
|
|
|
|
else if (!(rand() & 0x7F))
|
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
StormCount = (rand() & 0x1F) + 16;
|
2021-09-14 12:05:36 +03:00
|
|
|
StormTimer = (rand() & 3) + 12;
|
|
|
|
}
|
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
auto flashBrightness = StormSkyColor / 255.0f;
|
2021-09-14 12:05:36 +03:00
|
|
|
auto r = std::clamp(color.x + flashBrightness, 0.0f, 1.0f);
|
|
|
|
auto g = std::clamp(color.y + flashBrightness, 0.0f, 1.0f);
|
|
|
|
auto b = std::clamp(color.z + flashBrightness, 0.0f, 1.0f);
|
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
SkyCurrentColor = Vector4(r, g, b, color.w);
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
else
|
2021-09-15 12:58:07 +03:00
|
|
|
SkyCurrentColor = color;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
void EnvironmentController::UpdateLightning()
|
2021-09-14 16:00:06 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
StormCount--;
|
2021-09-15 11:13:47 +03:00
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
if (StormCount <= 0)
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
StormSkyColor = 0;
|
|
|
|
StormRand = 0;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
2021-09-15 12:58:07 +03:00
|
|
|
else if (StormCount < 5 && StormSkyColor < 50)
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
auto newColor = StormSkyColor - StormCount * 2;
|
2021-09-14 12:05:36 +03:00
|
|
|
if (newColor < 0)
|
|
|
|
newColor = 0;
|
2021-09-15 12:58:07 +03:00
|
|
|
StormSkyColor = newColor;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
2021-09-15 12:58:07 +03:00
|
|
|
else if (StormCount)
|
2021-09-14 12:05:36 +03:00
|
|
|
{
|
2021-09-15 12:58:07 +03:00
|
|
|
StormRand = ((rand() & 0x1FF - StormRand) >> 1) + StormRand;
|
|
|
|
StormSkyColor2 += StormRand * StormSkyColor2 >> 8;
|
|
|
|
StormSkyColor = StormSkyColor2;
|
|
|
|
if (StormSkyColor > 255)
|
|
|
|
StormSkyColor = 255;
|
2021-09-14 12:05:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 12:58:07 +03:00
|
|
|
void EnvironmentController::UpdateWind(GameScriptLevel* level)
|
|
|
|
{
|
|
|
|
WindCurrent += (GetRandomControl() & 7) - 3;
|
|
|
|
if (WindCurrent <= -2)
|
|
|
|
WindCurrent++;
|
|
|
|
else if (WindCurrent >= 9)
|
|
|
|
WindCurrent--;
|
|
|
|
|
|
|
|
WindDAngle = (WindDAngle + 2 * (GetRandomControl() & 63) - 64) & 0x1FFE;
|
|
|
|
|
|
|
|
if (WindDAngle < 1024)
|
|
|
|
WindDAngle = 2048 - WindDAngle;
|
|
|
|
else if (WindDAngle > 3072)
|
|
|
|
WindDAngle += 6144 - 2 * WindDAngle;
|
|
|
|
|
|
|
|
WindAngle = (WindAngle + ((WindDAngle - WindAngle) >> 3)) & 0x1FFE;
|
|
|
|
|
|
|
|
WindFinalX = WindCurrent * phd_sin(WindAngle << 3);
|
|
|
|
WindFinalZ = WindCurrent * phd_cos(WindAngle << 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnvironmentController::UpdateFlash(GameScriptLevel* level)
|
|
|
|
{
|
|
|
|
if (FlashProgress > 0.0f)
|
|
|
|
{
|
|
|
|
FlashProgress -= FlashSpeed;
|
|
|
|
if (FlashProgress < 0.0f)
|
|
|
|
FlashProgress = 0.0f;
|
|
|
|
}
|
2021-09-15 14:24:03 +03:00
|
|
|
|
|
|
|
if (FlashProgress == 0.0f)
|
|
|
|
FlashColorBase = Vector3::Zero;
|
2021-09-15 12:58:07 +03:00
|
|
|
}
|
2021-09-14 12:05:36 +03:00
|
|
|
}}}
|