From a682a57e3127d402429fb68fa2008103e96d1988 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 31 Jan 2016 22:53:42 +1100 Subject: [PATCH] Don't allocate an object reference to the game class for every particle, instead pass it as an argument. Also use new more organised options key, which are still backwards compatible. --- .../Particles/CollidableParticle.cs | 41 +++++++------- ClassicalSharp/Particles/Particle.cs | 8 +-- ClassicalSharp/Particles/ParticleManager.cs | 13 +++-- ClassicalSharp/Particles/RainParticle.cs | 13 ++--- ClassicalSharp/Particles/TerrainParticle.cs | 11 ++-- ClassicalSharp/Utils/Options.cs | 53 ++++++++++--------- 6 files changed, 71 insertions(+), 68 deletions(-) diff --git a/ClassicalSharp/Particles/CollidableParticle.cs b/ClassicalSharp/Particles/CollidableParticle.cs index 32c2d9a5a..6b4cbb6b5 100644 --- a/ClassicalSharp/Particles/CollidableParticle.cs +++ b/ClassicalSharp/Particles/CollidableParticle.cs @@ -6,7 +6,6 @@ namespace ClassicalSharp.Particles { public abstract class CollidableParticle : Particle { protected bool hitTerrain = false, throughLiquids = true; - public CollidableParticle( Game game ) : base( game) { } public void ResetState( Vector3 pos, Vector3 velocity, double lifetime ) { Position = lastPos = nextPos = pos; @@ -15,14 +14,14 @@ namespace ClassicalSharp.Particles { hitTerrain = false; } - protected bool Tick( float gravity, double delta ) { + protected bool Tick( Game game, float gravity, double delta ) { hitTerrain = false; lastPos = Position = nextPos; - byte curBlock = GetBlock( (int)Position.X, (int)Position.Y, (int)Position.Z ); + byte curBlock = GetBlock( game, (int)Position.X, (int)Position.Y, (int)Position.Z ); float minY = Utils.Floor( Position.Y ) + game.BlockInfo.MinBB[curBlock].Y; float maxY = Utils.Floor( Position.Y ) + game.BlockInfo.MaxBB[curBlock].Y; - if( !CanPassThrough( curBlock ) && Position.Y >= minY && - Position.Y < maxY && CollideHor( curBlock ) ) + if( !CanPassThrough( game, curBlock ) && Position.Y >= minY && + Position.Y < maxY && CollideHor( game, curBlock ) ) return true; Velocity.Y -= gravity * (float)delta; @@ -32,16 +31,16 @@ namespace ClassicalSharp.Particles { if( Velocity.Y > 0 ) { // don't test block we are already in - for( int y = startY + 1; y <= endY && TestY( y, false ); y++ ); + for( int y = startY + 1; y <= endY && TestY( game, y, false ); y++ ); } else { - for( int y = startY; y >= endY && TestY( y, true ); y-- ); + for( int y = startY; y >= endY && TestY( game, y, true ); y-- ); } nextPos = Position; Position = lastPos; - return base.Tick( delta ); + return base.Tick( game, delta ); } - bool TestY( int y, bool topFace ) { + bool TestY( Game game, int y, bool topFace ) { if( y < 0 ) { Position.Y = nextPos.Y = lastPos.Y = 0 + Entity.Adjustment; Velocity = Vector3.Zero; @@ -49,14 +48,14 @@ namespace ClassicalSharp.Particles { return false; } - byte block = GetBlock( (int)Position.X, y, (int)Position.Z ); - if( CanPassThrough( block ) ) return true; + byte block = GetBlock( game, (int)Position.X, y, (int)Position.Z ); + if( CanPassThrough( game, block ) ) return true; Vector3 minBB = game.BlockInfo.MinBB[block]; Vector3 maxBB = game.BlockInfo.MaxBB[block]; float collideY = y + (topFace ? maxBB.Y : minBB.Y); bool collideVer = topFace ? (Position.Y < collideY) : (Position.Y > collideY); - if( collideVer && CollideHor( block ) ) { + if( collideVer && CollideHor( game, block ) ) { float adjust = topFace ? Entity.Adjustment : -Entity.Adjustment; Position.Y = nextPos.Y = lastPos.Y = collideY + adjust; Velocity = Vector3.Zero; @@ -66,23 +65,19 @@ namespace ClassicalSharp.Particles { return true; } - bool CanPassThrough( byte block ) { + bool CanPassThrough( Game game, byte block ) { return game.BlockInfo.IsAir[block] || game.BlockInfo.IsSprite[block] || (throughLiquids && game.BlockInfo.IsLiquid[block]); } - bool CollideHor( byte block ) { - Vector3 min = game.BlockInfo.MinBB[block] + Floor( Position ); - Vector3 max = game.BlockInfo.MaxBB[block] + Floor( Position ); + bool CollideHor( Game game, byte block ) { + Vector3 min = game.BlockInfo.MinBB[block] + FloorHor( Position ); + Vector3 max = game.BlockInfo.MaxBB[block] + FloorHor( Position ); return Position.X >= min.X && Position.Z >= min.Z && Position.X < max.X && Position.Z < max.Z; } - static Vector3 Floor( Vector3 v ) { - return new Vector3( Utils.Floor( v.X ), 0, Utils.Floor( v.Z ) ); - } - - byte GetBlock( int x, int y, int z ) { + byte GetBlock( Game game, int x, int y, int z ) { if( game.Map.IsValidPos( x, y, z ) ) return game.Map.GetBlock( x, y, z ); @@ -90,5 +85,9 @@ namespace ClassicalSharp.Particles { if( y >= game.Map.SidesHeight ) return (byte)game.Map.EdgeBlock; return (byte)game.Map.SidesBlock; } + + static Vector3 FloorHor( Vector3 v ) { + return new Vector3( Utils.Floor( v.X ), 0, Utils.Floor( v.Z ) ); + } } } diff --git a/ClassicalSharp/Particles/Particle.cs b/ClassicalSharp/Particles/Particle.cs index d47021963..7395f1de6 100644 --- a/ClassicalSharp/Particles/Particle.cs +++ b/ClassicalSharp/Particles/Particle.cs @@ -8,14 +8,14 @@ namespace ClassicalSharp.Particles { public Vector3 Position; public Vector3 Velocity; public float Lifetime; - protected Game game; protected Vector3 lastPos, nextPos; - public abstract void Render( double delta, float t, VertexPos3fTex2fCol4b[] vertices, ref int index ); + public abstract void CountVertices( Game game, int[] counts ); - public Particle( Game game ) { this.game = game; } + public abstract void Render( Game game, double delta, float t, + VertexPos3fTex2fCol4b[] vertices, ref int index ); - public virtual bool Tick( double delta ) { + public virtual bool Tick( Game game, double delta ) { Lifetime -= (float)delta; return Lifetime < 0; } diff --git a/ClassicalSharp/Particles/ParticleManager.cs b/ClassicalSharp/Particles/ParticleManager.cs index b3ecf3478..b884d186c 100644 --- a/ClassicalSharp/Particles/ParticleManager.cs +++ b/ClassicalSharp/Particles/ParticleManager.cs @@ -11,6 +11,7 @@ namespace ClassicalSharp.Particles { TerrainParticle[] terrainParticles = new TerrainParticle[maxParticles]; RainParticle[] rainParticles = new RainParticle[maxParticles]; int terrainCount, rainCount; + int[] terrain1DCount; Game game; Random rnd; @@ -53,7 +54,7 @@ namespace ClassicalSharp.Particles { int index = 0; for( int i = 0; i < elems; i++ ) - particles[i].Render( delta, t, vertices, ref index ); + particles[i].Render( game, delta, t, vertices, ref index ); return Math.Min( count, maxParticles * 4 ); } @@ -65,7 +66,7 @@ namespace ClassicalSharp.Particles { void TickParticles( Particle[] particles, ref int count, double delta ) { for( int i = 0; i < count; i++ ) { Particle particle = particles[i]; - if( particle.Tick( delta ) ) { + if( particle.Tick( game, delta ) ) { RemoveAt( i, particles, ref count ); i--; } @@ -139,7 +140,7 @@ namespace ClassicalSharp.Particles { } } - T AddParticle( T[] particles, ref int count, bool rain ) where T : Particle { + T AddParticle( T[] particles, ref int count, bool rain ) where T : Particle, new() { if( count == maxParticles ) RemoveAt( 0, particles, ref count ); count++; @@ -147,8 +148,7 @@ namespace ClassicalSharp.Particles { T old = particles[count - 1]; if( old != null ) return old; - T newT = rain ? (T)(object)new RainParticle( game ) - : (T)(object)new TerrainParticle( game ); + T newT = rain ? (T)(object)new RainParticle() : (T)(object)new TerrainParticle(); particles[count - 1] = newT; return newT; } @@ -161,7 +161,6 @@ namespace ClassicalSharp.Particles { particles[count - 1] = removed; count--; - } - + } } } diff --git a/ClassicalSharp/Particles/RainParticle.cs b/ClassicalSharp/Particles/RainParticle.cs index 824604020..cdeb39cf3 100644 --- a/ClassicalSharp/Particles/RainParticle.cs +++ b/ClassicalSharp/Particles/RainParticle.cs @@ -10,18 +10,19 @@ namespace ClassicalSharp.Particles { static Vector2 tinySize = new Vector2( 0.5f/16f, 0.5f/16f ); static TextureRec rec = new TextureRec( 2/128f, 14/128f, 3/128f, 2/128f ); - public RainParticle( Game game ) : base( game ) { - throughLiquids = false; - } + public RainParticle() { throughLiquids = false; } public bool Big, Tiny; - public override bool Tick( double delta ) { - bool dies = Tick( 3.5f, delta ); + public override bool Tick( Game game, double delta ) { + bool dies = Tick( game, 3.5f, delta ); return hitTerrain ? true : dies; } - public override void Render( double delta, float t, VertexPos3fTex2fCol4b[] vertices, ref int index ) { + public override void CountVertices( Game game, int[] counts ) { } + + public override void Render( Game game, double delta, float t, + VertexPos3fTex2fCol4b[] vertices, ref int index ) { Position = Vector3.Lerp( lastPos, nextPos, t ); Vector3 p111, p121, p212, p222; Vector2 size = Big ? bigSize : (Tiny ? tinySize : smallSize ); diff --git a/ClassicalSharp/Particles/TerrainParticle.cs b/ClassicalSharp/Particles/TerrainParticle.cs index 08269995c..a77ad336e 100644 --- a/ClassicalSharp/Particles/TerrainParticle.cs +++ b/ClassicalSharp/Particles/TerrainParticle.cs @@ -8,13 +8,14 @@ namespace ClassicalSharp.Particles { static Vector2 terrainSize = new Vector2( 1/8f, 1/8f ); internal TextureRec rec; - public TerrainParticle( Game game ) : base( game ) { } - - public override bool Tick( double delta ) { - return Tick( 5.4f, delta ); + public override bool Tick( Game game, double delta ) { + return Tick( game, 5.4f, delta ); } - public override void Render( double delta, float t, VertexPos3fTex2fCol4b[] vertices, ref int index ) { + public override void CountVertices( Game game, int[] counts ) { } + + public override void Render( Game game, double delta, float t, + VertexPos3fTex2fCol4b[] vertices, ref int index ) { Position = Vector3.Lerp( lastPos, nextPos, t ); Vector3 p111, p121, p212, p222; Utils.CalcBillboardPoints( terrainSize, Position, ref game.View, diff --git a/ClassicalSharp/Utils/Options.cs b/ClassicalSharp/Utils/Options.cs index dc31aa0c9..f71b454a2 100644 --- a/ClassicalSharp/Utils/Options.cs +++ b/ClassicalSharp/Utils/Options.cs @@ -21,23 +21,23 @@ namespace ClassicalSharp { public const string AutoCloseLauncher = "autocloselauncher"; public const string ViewBobbing = "viewbobbing"; - public const string HacksEnabled = "hacksenabled"; - public const string FieldOfView = "fov"; - public const string Speed = "speedmultiplier"; - public const string LiquidsBreakable = "liquidsbreakable"; - public const string PushbackPlacing = "pushbackplacing"; - public const string NoclipSlide = "noclipslide"; - public const string CameraClipping = "cameraclipping"; - public const string DoubleJump = "doublejump"; + public const string HacksEnabled = "hacks-hacksenabled"; + public const string FieldOfView = "hacks-fov"; + public const string Speed = "hacks-speedmultiplier"; + public const string LiquidsBreakable = "hacks-liquidsbreakable"; + public const string PushbackPlacing = "hacks-pushbackplacing"; + public const string NoclipSlide = "hacks-noclipslide"; + public const string CameraClipping = "hacks-cameraclipping"; + public const string DoubleJump = "hacks-doublejump"; - public const string TabAutocomplete = "tab-autocomplete"; - public const string ShowBlockInHand = "blockinhand"; - public const string ChatLines = "chatlines"; - public const string ClickableChat = "chatclickable"; - public const string ArialChatFont = "arialchatfont"; + public const string TabAutocomplete = "gui-tab-autocomplete"; + public const string ShowBlockInHand = "gui-blockinhand"; + public const string ChatLines = "gui-chatlines"; + public const string ClickableChat = "gui-chatclickable"; + public const string ArialChatFont = "gui-arialchatfont"; public const string HotbarScale = "gui-hotbarscale"; public const string InventoryScale = "gui-inventoryscale"; - public const string ChatScale = "chatscale"; + public const string ChatScale = "gui-chatscale"; public const string ShowFPS = "gui-showfps"; public const string AllowCustomBlocks = "nostalgia-customblocks"; @@ -65,16 +65,23 @@ namespace ClassicalSharp { const string OptionsFile = "options.txt"; + static bool TryGetValue( string key, out string value ) { + if( OptionsSet.TryGetValue( key, out value ) ) return true; + int index = key.IndexOf( '-' ); + + if( index == -1 ) return false; + return OptionsSet.TryGetValue( key.Substring( index + 1 ), out value ); + } + public static string Get( string key ) { string value; - return OptionsSet.TryGetValue( key, out value ) ? value : null; + return TryGetValue( key, out value ) ? value : null; } public static int GetInt( string key, int min, int max, int defValue ) { string value; int valueInt = 0; - if( !OptionsSet.TryGetValue( key, out value ) || String.IsNullOrEmpty( value ) - || !Int32.TryParse( value, out valueInt ) ) + if( !TryGetValue( key, out value ) || !Int32.TryParse( value, out valueInt ) ) return defValue; Utils.Clamp( ref valueInt, min, max ); @@ -84,8 +91,7 @@ namespace ClassicalSharp { public static bool GetBool( string key, bool defValue ) { string value; bool valueBool = false; - if( !OptionsSet.TryGetValue( key, out value ) || String.IsNullOrEmpty( value ) - || !Boolean.TryParse( value, out valueBool ) ) + if( !TryGetValue( key, out value ) || !Boolean.TryParse( value, out valueBool ) ) return defValue; return valueBool; } @@ -93,10 +99,8 @@ namespace ClassicalSharp { public static float GetFloat( string key, float min, float max, float defValue ) { string value; float valueFloat = 0; - if( !OptionsSet.TryGetValue( key, out value ) || String.IsNullOrEmpty( value ) - || !Single.TryParse( value, out valueFloat ) ) + if( !TryGetValue( key, out value ) || !Single.TryParse( value, out valueFloat ) ) return defValue; - Utils.Clamp( ref valueFloat, min, max ); return valueFloat; } @@ -116,11 +120,10 @@ namespace ClassicalSharp { public static void Set( string key, string value ) { key = key.ToLower(); - if( value != null ) { + if( value != null ) OptionsSet[key] = value; - } else { + else OptionsSet.Remove( key ); - } OptionsChanged[key] = true; }