// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using ClassicalSharp.Events;
namespace ClassicalSharp.Map {
public enum Weather { Sunny, Rainy, Snowy, }
/// Contains the environment metadata for a world.
public sealed class WorldEnv {
/// Colour of the sky located behind/above clouds.
public FastColour SkyCol = DefaultSkyColour;
public static readonly FastColour DefaultSkyColour = new FastColour( 0x99, 0xCC, 0xFF );
/// Colour applied to the fog/horizon looking out horizontally.
/// Note the true horizon colour is a blend of this and sky colour.
public FastColour FogCol = DefaultFogColour;
public static readonly FastColour DefaultFogColour = new FastColour( 0xFF, 0xFF, 0xFF );
/// Colour applied to the clouds.
public FastColour CloudsCol = DefaultCloudsColour;
public static readonly FastColour DefaultCloudsColour = new FastColour( 0xFF, 0xFF, 0xFF );
/// Height of the clouds in world space.
public int CloudHeight;
/// Modifier of how fast clouds travel across the world, defaults to 1.
public float CloudsSpeed = 1;
/// Modifier of how fast rain/snow falls, defaults to 1.
public float WeatherSpeed = 1;
/// Modifier of how fast rain/snow fades, defaults to 1.
public float WeatherFade = 1;
/// Colour applied to blocks located in direct sunlight.
public FastColour Sunlight;
public FastColour SunlightXSide, SunlightZSide, SunlightYBottom;
public static readonly FastColour DefaultSunlight = new FastColour( 0xFF, 0xFF, 0xFF );
/// Colour applied to blocks located in shadow / hidden from direct sunlight.
public FastColour Shadowlight;
public FastColour ShadowlightXSide, ShadowlightZSide, ShadowlightYBottom;
public static readonly FastColour DefaultShadowlight = new FastColour( 0x9B, 0x9B, 0x9B );
/// Current weather for this particular map.
public Weather Weather = Weather.Sunny;
/// Block that surrounds map the map horizontally (default water)
public Block EdgeBlock = Block.StillWater;
/// Height of the map edge in world space.
public int EdgeHeight;
/// Block that surrounds the map that fills the bottom of the map horizontally,
/// fills part of the vertical sides of the map, and also surrounds map the map horizontally. (default bedrock)
public Block SidesBlock = Block.Bedrock;
/// Maximum height of the various parts of the map sides, in world space.
public int SidesHeight { get { return EdgeHeight - 2; } }
Game game;
public WorldEnv( Game game ) {
this.game = game;
ResetLight();
}
/// Resets all of the environment properties to their defaults.
public void Reset() {
EdgeHeight = -1;
CloudHeight = -1;
EdgeBlock = Block.StillWater;
SidesBlock = Block.Bedrock;
CloudsSpeed = 1;
WeatherSpeed = 1;
WeatherFade = 1;
ResetLight();
SkyCol = DefaultSkyColour;
FogCol = DefaultFogColour;
CloudsCol = DefaultCloudsColour;
Weather = Weather.Sunny;
}
void ResetLight() {
Shadowlight = DefaultShadowlight;
FastColour.GetShaded( Shadowlight, ref ShadowlightXSide,
ref ShadowlightZSide, ref ShadowlightYBottom );
Sunlight = DefaultSunlight;
FastColour.GetShaded( Sunlight, ref SunlightXSide,
ref SunlightZSide, ref SunlightYBottom );
}
/// Sets sides block to the given block, and raises
/// EnvVariableChanged event with variable 'SidesBlock'.
public void SetSidesBlock( Block block ) {
if( block == SidesBlock ) return;
if( block == (Block)BlockInfo.MaxDefinedBlock ) {
Utils.LogDebug( "Tried to set sides block to an invalid block: " + block );
block = Block.Bedrock;
}
SidesBlock = block;
game.WorldEvents.RaiseEnvVariableChanged( EnvVar.SidesBlock );
}
/// Sets edge block to the given block, and raises
/// EnvVariableChanged event with variable 'EdgeBlock'.
public void SetEdgeBlock( Block block ) {
if( block == EdgeBlock ) return;
if( block == (Block)BlockInfo.MaxDefinedBlock ) {
Utils.LogDebug( "Tried to set edge block to an invalid block: " + block );
block = Block.StillWater;
}
EdgeBlock = block;
game.WorldEvents.RaiseEnvVariableChanged( EnvVar.EdgeBlock );
}
/// Sets clouds height in world space, and raises
/// EnvVariableChanged event with variable 'CloudsLevel'.
public void SetCloudsLevel( int level ) { Set( level, ref CloudHeight, EnvVar.CloudsLevel ); }
/// Sets clouds speed, and raises EnvVariableChanged
/// event with variable 'CloudsSpeed'.
public void SetCloudsSpeed( float speed ) { Set( speed, ref CloudsSpeed, EnvVar.CloudsSpeed ); }
/// Sets weather speed, and raises EnvVariableChanged
/// event with variable 'WeatherSpeed'.
public void SetWeatherSpeed( float speed ) { Set( speed, ref WeatherSpeed, EnvVar.WeatherSpeed ); }
/// Sets weather fade rate, and raises EnvVariableChanged
/// event with variable 'WeatherFade'.
public void SetWeatherFade( float rate ) { Set( rate, ref WeatherFade, EnvVar.WeatherFade ); }
/// Sets height of the map edges in world space, and raises
/// EnvVariableChanged event with variable 'EdgeLevel'.
public void SetEdgeLevel( int level ) { Set( level, ref EdgeHeight, EnvVar.EdgeLevel ); }
/// Sets tsky colour, and raises
/// EnvVariableChanged event with variable 'SkyColour'.
public void SetSkyColour( FastColour col ) { Set( col, ref SkyCol, EnvVar.SkyColour ); }
/// Sets fog colour, and raises
/// EnvVariableChanged event with variable 'FogColour'.
public void SetFogColour( FastColour col ) { Set( col, ref FogCol, EnvVar.FogColour ); }
/// Sets clouds colour, and raises
/// EnvVariableChanged event with variable 'CloudsColour'.
public void SetCloudsColour( FastColour col ) { Set( col, ref CloudsCol, EnvVar.CloudsColour ); }
/// Sets sunlight colour, and raises
/// EnvVariableChanged event with variable 'SunlightColour'.
public void SetSunlight( FastColour col ) {
if( col == Sunlight ) return;
Sunlight = col;
Set( col, ref Sunlight, EnvVar.SunlightColour );
FastColour.GetShaded( Sunlight, ref SunlightXSide,
ref SunlightZSide, ref SunlightYBottom );
game.WorldEvents.RaiseEnvVariableChanged( EnvVar.SunlightColour );
}
/// Sets current shadowlight colour, and raises
/// EnvVariableChanged event with variable 'ShadowlightColour'.
public void SetShadowlight( FastColour col ) {
if( col == Shadowlight ) return;
Shadowlight = col;
Set( col, ref Shadowlight, EnvVar.ShadowlightColour );
FastColour.GetShaded( Shadowlight, ref ShadowlightXSide,
ref ShadowlightZSide, ref ShadowlightYBottom );
game.WorldEvents.RaiseEnvVariableChanged( EnvVar.ShadowlightColour );
}
/// Sets weather, and raises
/// EnvVariableChanged event with variable 'Weather'.
public void SetWeather( Weather weather ) {
if( weather == Weather ) return;
Weather = weather;
game.WorldEvents.RaiseEnvVariableChanged( EnvVar.Weather );
}
void Set( T value, ref T target, EnvVar var ) where T : IEquatable {
if( value.Equals( target ) ) return;
target = value;
game.WorldEvents.RaiseEnvVariableChanged( var );
}
}
}