Make all game components handle new map / new map loaded.

This commit is contained in:
UnknownShadow200 2016-05-01 11:23:38 +10:00
parent afe2d05de0
commit 1e3fad5930
18 changed files with 109 additions and 91 deletions

View File

@ -15,9 +15,10 @@ namespace ClassicalSharp.Gui {
PlayerListWidget playerList;
Font playerFont;
public void Init( Game game ) { Init(); }
public void Init( Game game ) { Init(); }
public void Reset( Game game ) { }
public void OnNewMap( Game game ) { }
public void OnNewMapLoaded( Game game ) { }
public override void Render( double delta ) {
if( game.HideGui ) return;

View File

@ -27,6 +27,8 @@ namespace ClassicalSharp.Audio {
}
public void Reset( Game game ) { }
public void OnNewMap( Game game ) { }
public void OnNewMapLoaded( Game game ) { }
public void SetMusic( bool enabled ) {
if( enabled )

View File

@ -26,6 +26,8 @@ namespace ClassicalSharp.Commands {
}
public void Reset( Game game ) { }
public void OnNewMap( Game game ) { }
public void OnNewMapLoaded( Game game ) { }
public void Register( Command command ) {
command.game = game;

View File

@ -141,14 +141,10 @@ namespace ClassicalSharp.Commands {
void SetNewRenderType( bool legacy, bool minimal ) {
game.MapBordersRenderer.UseLegacyMode( legacy );
if( minimal ) {
game.EnvRenderer.Dispose();
game.EnvRenderer = new MinimalEnvRenderer( game );
game.EnvRenderer.Init();
game.ReplaceComponent( ref game.EnvRenderer, new MinimalEnvRenderer() );
} else {
if( !( game.EnvRenderer is StandardEnvRenderer ) ) {
game.EnvRenderer.Dispose();
game.EnvRenderer = new StandardEnvRenderer( game );
game.EnvRenderer.Init();
if( !(game.EnvRenderer is StandardEnvRenderer) ) {
game.ReplaceComponent( ref game.EnvRenderer, new StandardEnvRenderer() );
}
((StandardEnvRenderer)game.EnvRenderer).UseLegacyMode( legacy );
}

View File

@ -19,6 +19,8 @@ namespace ClassicalSharp {
}
public void Reset( Game game ) { }
public void OnNewMap( Game game ) { }
public void OnNewMapLoaded( Game game ) { }
/// <summary> List of chat messages received from the server and added by client commands. </summary>
/// <remarks> index 0 is the oldest chat message, last index is newest. </remarks>

View File

@ -28,8 +28,14 @@ namespace ClassicalSharp {
/// <summary> Called when the game has loaded. </summary>
void Init( Game game );
/// <summary> Called to reset the state when the user is reconnecting to a server. </summary>
/// <summary> Called to reset the component's state when the user is reconnecting to a server. </summary>
void Reset( Game game );
/// <summary> Called to update the component's state when the user begins loading a new map. </summary>
void OnNewMap( Game game );
/// <summary> Called to update the component's state when the user has finished loading a new map. </summary>
void OnNewMapLoaded( Game game );
}
public partial class Game {

View File

@ -62,6 +62,8 @@ namespace ClassicalSharp {
LoadOptions();
LoadGuiOptions();
Chat = AddComponent( new Chat() );
WorldEvents.OnNewMap += OnNewMapCore;
WorldEvents.OnNewMapLoaded += OnNewMapLoadedCore;
BlockInfo = new BlockInfo();
BlockInfo.Init();
@ -89,8 +91,8 @@ namespace ClassicalSharp {
width = Width;
height = Height;
MapRenderer = new MapRenderer( this );
MapBordersRenderer = new MapBordersRenderer( this );
EnvRenderer = new StandardEnvRenderer( this );
MapBordersRenderer = AddComponent( new MapBordersRenderer() );
EnvRenderer = AddComponent( new StandardEnvRenderer() );
if( IPAddress == null ) {
Network = new Singleplayer.SinglePlayerServer( this );
} else {
@ -117,8 +119,6 @@ namespace ClassicalSharp {
fpsScreen.Init();
hudScreen = AddComponent( new HudScreen( this ) );
Culling = new FrustumCulling();
EnvRenderer.Init();
MapBordersRenderer.Init();
Picking = AddComponent( new PickedPosRenderer() );
AudioPlayer = AddComponent( new AudioPlayer() );
AxisLinesRenderer = AddComponent( new AxisLinesRenderer() );
@ -136,11 +136,6 @@ namespace ClassicalSharp {
Network.Connect( IPAddress, Port );
}
public T AddComponent<T>( T obj ) {
Components.Add( (IGameComponent)obj );
return obj;
}
void LoadOptions() {
ClassicMode = Options.GetBool( "mode-classic", false );
ClassicHacks = Options.GetBool( OptionsKey.AllowClassicHacks, false );
@ -200,6 +195,37 @@ namespace ClassicalSharp {
}
}
void OnNewMapCore( object sender, EventArgs e ) {
foreach( IGameComponent comp in Components )
comp.OnNewMap( this );
}
void OnNewMapLoadedCore( object sender, EventArgs e ) {
foreach( IGameComponent comp in Components )
comp.OnNewMapLoaded( this );
}
public T AddComponent<T>( T obj ) where T : IGameComponent {
Components.Add( obj );
return obj;
}
public bool ReplaceComponent<T>( ref T old, T obj ) where T : IGameComponent {
for( int i = 0; i < Components.Count; i++ ) {
if( !object.ReferenceEquals( Components[i], old ) ) continue;
old.Dispose();
Components[i] = obj;
old = obj;
obj.Init( this );
return true;
}
Components.Add( obj );
obj.Init( this );
return false;
}
public void SetViewDistance( int distance, bool save ) {
ViewDistance = distance;
if( ViewDistance > MaxViewDistance )
@ -485,6 +511,8 @@ namespace ClassicalSharp {
ModelCache.Dispose();
ParticleManager.Dispose();
Players.Dispose();
WorldEvents.OnNewMap -= OnNewMapCore;
WorldEvents.OnNewMapLoaded -= OnNewMapLoadedCore;
foreach( IGameComponent comp in Components )
comp.Dispose();

View File

@ -23,6 +23,8 @@ namespace ClassicalSharp {
}
public void Reset( Game game ) { }
public void OnNewMap( Game game ) { }
public void OnNewMapLoaded( Game game ) { }
public void Dispose() { }

View File

@ -42,6 +42,8 @@ namespace ClassicalSharp.Network {
requests.Clear();
handle.Set();
}
public void OnNewMap( Game game ) { }
public void OnNewMapLoaded( Game game ) { }
/// <summary> Asynchronously downloads a skin. If 'skinName' points to the url then the skin is
/// downloaded from that url, otherwise it is downloaded from the url 'defaultSkinServer'/'skinName'.png </summary>

View File

@ -31,6 +31,8 @@ namespace ClassicalSharp.Renderers {
}
public void Reset( Game game ) { }
public void OnNewMap( Game game ) { }
public void OnNewMapLoaded( Game game ) { }
public void Render( double delta, float t ) {
if( game.Camera.IsThirdPerson || !game.ShowBlockInHand ) return;

View File

@ -6,28 +6,26 @@ using ClassicalSharp.Map;
namespace ClassicalSharp.Renderers {
public abstract class EnvRenderer : IDisposable {
public abstract class EnvRenderer : IGameComponent {
protected World map;
protected Game game;
protected IGraphicsApi graphics;
public virtual void Init() {
public virtual void Init( Game game ) {
this.game = game;
map = game.World;
graphics = game.Graphics;
game.WorldEvents.OnNewMap += OnNewMap;
game.WorldEvents.OnNewMapLoaded += OnNewMapLoaded;
game.WorldEvents.EnvVariableChanged += EnvVariableChanged;
}
public virtual void OnNewMap( object sender, EventArgs e ) {
}
public virtual void OnNewMapLoaded( object sender, EventArgs e ) {
}
public virtual void Reset( Game game ) { OnNewMap( game ); }
public abstract void OnNewMap( Game game );
public abstract void OnNewMapLoaded( Game game );
public virtual void Dispose() {
game.WorldEvents.OnNewMap -= OnNewMap;
game.WorldEvents.OnNewMapLoaded -= OnNewMapLoaded;
game.WorldEvents.EnvVariableChanged -= EnvVariableChanged;
}

View File

@ -9,18 +9,12 @@ using OpenTK;
namespace ClassicalSharp.Renderers {
public unsafe sealed class MapBordersRenderer : IDisposable {
public unsafe sealed class MapBordersRenderer : IGameComponent {
World map;
Game game;
IGraphicsApi graphics;
public MapBordersRenderer( Game game ) {
this.game = game;
map = game.World;
graphics = game.Graphics;
}
int sidesVb = -1, edgesVb = -1;
int edgeTexId, sideTexId;
int sidesVertices, edgesVertices;
@ -31,9 +25,11 @@ namespace ClassicalSharp.Renderers {
ResetSidesAndEdges( null, null );
}
public void Init() {
game.WorldEvents.OnNewMap += OnNewMap;
game.WorldEvents.OnNewMapLoaded += OnNewMapLoaded;
public void Init( Game game ) {
this.game = game;
map = game.World;
graphics = game.Graphics;
game.WorldEvents.EnvVariableChanged += EnvVariableChanged;
game.Events.ViewDistanceChanged += ResetSidesAndEdges;
game.Events.TerrainAtlasChanged += ResetTextures;
@ -74,8 +70,6 @@ namespace ClassicalSharp.Renderers {
}
public void Dispose() {
game.WorldEvents.OnNewMap -= OnNewMap;
game.WorldEvents.OnNewMapLoaded -= OnNewMapLoaded;
game.WorldEvents.EnvVariableChanged -= EnvVariableChanged;
game.Events.ViewDistanceChanged -= ResetSidesAndEdges;
game.Events.TerrainAtlasChanged -= ResetTextures;
@ -87,7 +81,9 @@ namespace ClassicalSharp.Renderers {
sidesVb = edgesVb = -1;
}
void OnNewMap( object sender, EventArgs e ) {
public void Reset( Game game ) { OnNewMap( game ); }
public void OnNewMap( Game game ) {
graphics.DeleteVb( sidesVb );
graphics.DeleteVb( edgesVb );
sidesVb = edgesVb = -1;
@ -96,7 +92,7 @@ namespace ClassicalSharp.Renderers {
MakeTexture( ref sideTexId, ref lastSideTexLoc, map.SidesBlock );
}
void OnNewMapLoaded( object sender, EventArgs e ) {
public void OnNewMapLoaded( Game game ) {
CalculateRects( game.ViewDistance );
RebuildSides( map.SidesHeight, legacy ? 128 : 65536 );
RebuildEdges( map.EdgeHeight, legacy ? 128 : 65536 );

View File

@ -8,25 +8,19 @@ namespace ClassicalSharp.Renderers {
/// (no fog, clouds, or proper overhead sky) </summary>
public class MinimalEnvRenderer : EnvRenderer {
public MinimalEnvRenderer( Game game ) {
this.game = game;
map = game.World;
public override void Init( Game game ) {
base.Init( game );
graphics.Fog = false;
graphics.ClearColour( map.SkyCol );
}
public override void Render( double deltaTime ) {
graphics.ClearColour( map.SkyCol );
}
public override void Init() {
base.Init();
graphics.Fog = false;
graphics.ClearColour( map.SkyCol );
}
public override void OnNewMap( Game game ) { }
public override void OnNewMap( object sender, EventArgs e ) {
}
public override void OnNewMapLoaded( object sender, EventArgs e ) {
public override void OnNewMapLoaded( Game game ) {
graphics.ClearColour( map.SkyCol );
}

View File

@ -8,11 +8,6 @@ namespace ClassicalSharp.Renderers {
public unsafe class StandardEnvRenderer : EnvRenderer {
public StandardEnvRenderer( Game game ) {
this.game = game;
map = game.World;
}
int cloudsVb = -1, cloudVertices, skyVb = -1, skyVertices;
bool legacy;
@ -58,25 +53,25 @@ namespace ClassicalSharp.Renderers {
}
}
public override void OnNewMap( object sender, EventArgs e ) {
public override void Init( Game game ) {
base.Init( game );
graphics.Fog = true;
ResetAllEnv( null, null );
game.Events.ViewDistanceChanged += ResetAllEnv;
}
public override void OnNewMap( Game game ) {
graphics.Fog = false;
graphics.DeleteVb( skyVb );
graphics.DeleteVb( cloudsVb );
skyVb = cloudsVb = -1;
}
public override void OnNewMapLoaded( object sender, EventArgs e ) {
public override void OnNewMapLoaded( Game game ) {
graphics.Fog = true;
ResetAllEnv( null, null );
}
public override void Init() {
base.Init();
graphics.Fog = true;
ResetAllEnv( null, null );
game.Events.ViewDistanceChanged += ResetAllEnv;
}
void ResetAllEnv( object sender, EventArgs e ) {
ResetFog();
ResetSky();

View File

@ -19,11 +19,7 @@ namespace ClassicalSharp.Renderers {
graphics = game.Graphics;
info = game.BlockInfo;
weatherVb = graphics.CreateDynamicVb( VertexFormat.P3fT2fC4b, vertices.Length );
game.WorldEvents.OnNewMap += OnNewMap;
game.WorldEvents.OnNewMapLoaded += OnNewMapLoaded;
}
public void Reset( Game game ) { }
}
int weatherVb;
short[] heightmap;
@ -98,12 +94,14 @@ namespace ClassicalSharp.Renderers {
}
int length, width, maxY, oneY;
void OnNewMap( object sender, EventArgs e ) {
public void Reset( Game game ) { OnNewMap( game ); }
public void OnNewMap( Game game ) {
heightmap = null;
lastPos = new Vector3I( Int32.MaxValue );
}
void OnNewMapLoaded( object sender, EventArgs e ) {
public void OnNewMapLoaded( Game game ) {
length = map.Length;
width = map.Width;
maxY = map.Height - 1;
@ -118,8 +116,6 @@ namespace ClassicalSharp.Renderers {
}
public void Dispose() {
game.WorldEvents.OnNewMap -= OnNewMap;
game.WorldEvents.OnNewMapLoaded -= OnNewMapLoaded;
graphics.DeleteDynamicVb( weatherVb );
}

View File

@ -16,6 +16,8 @@ namespace ClassicalSharp.Selections {
}
public void Reset( Game game ) { }
public void OnNewMap( Game game ) { }
public void OnNewMapLoaded( Game game ) { }
public void Dispose() {
game.Graphics.DeleteDynamicVb( vb );

View File

@ -18,6 +18,8 @@ namespace ClassicalSharp.Renderers {
}
public void Reset( Game game ) { }
public void OnNewMap( Game game ) { }
public void OnNewMapLoaded( Game game ) { }
FastColour col = FastColour.Black;
int index;

View File

@ -16,12 +16,11 @@ namespace ClassicalSharp.Selections {
public void Init( Game game ) {
this.game = game;
Graphics = game.Graphics;
game.WorldEvents.OnNewMap += OnNewMap;
}
public void Reset( Game game ) {
selections.Clear();
}
public void Reset( Game game ) { selections.Clear(); }
public void OnNewMap( Game game ) { selections.Clear(); }
public void OnNewMapLoaded( Game game ) { }
List<SelectionBox> selections = new List<SelectionBox>( 256 );
public void AddSelection( byte id, Vector3I p1, Vector3I p2, FastColour col ) {
@ -71,10 +70,7 @@ namespace ClassicalSharp.Selections {
Graphics.AlphaBlending = false;
}
public void Dispose() {
OnNewMap( null, null );
game.WorldEvents.OnNewMap -= OnNewMap;
public void Dispose() {
if( lineVb <= 0 ) return;
Graphics.DeleteDynamicVb( vb );
Graphics.DeleteDynamicVb( lineVb );
@ -87,9 +83,5 @@ namespace ClassicalSharp.Selections {
vb = Graphics.CreateDynamicVb( VertexFormat.P3fC4b, vertices.Length );
lineVb = Graphics.CreateDynamicVb( VertexFormat.P3fC4b, lineVertices.Length );
}
void OnNewMap( object sender, EventArgs e ) {
selections.Clear();
}
}
}