From bae3b3d32e082efdcf923b3b834fb6ad8e07822b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 8 May 2015 06:43:50 +1000 Subject: [PATCH] Various simplifications. --- Entities/LocalPlayer.cs | 30 ++++++++++++++---------------- Entities/Player.cs | 27 ++++++++++++--------------- Utils/Camera.cs | 7 +------ Utils/Utils.cs | 5 +++++ 4 files changed, 32 insertions(+), 37 deletions(-) diff --git a/Entities/LocalPlayer.cs b/Entities/LocalPlayer.cs index c69c56062..56a985e1d 100644 --- a/Entities/LocalPlayer.cs +++ b/Entities/LocalPlayer.cs @@ -187,11 +187,8 @@ namespace ClassicalSharp { Velocity.Y -= 0.08f; if( BlockUnderFeet == Block.Ice ) { - // Limit velocity while travelling on ice. - if( Velocity.X > 0.25f ) Velocity.X = 0.25f; - if( Velocity.X < -0.25f ) Velocity.X = -0.25f; - if( Velocity.Z > 0.25f ) Velocity.Z = 0.25f; - if( Velocity.Z < -0.25f ) Velocity.Z = -0.25f; + Utils.Clamp( ref Velocity.X, -0.25f, 0.25f ); + Utils.Clamp( ref Velocity.Z, -0.25f, 0.25f ); } else if( onGround || flying ) { // Apply air resistance or ground friction Velocity.X *= 0.6f; @@ -209,7 +206,8 @@ namespace ClassicalSharp { bool jumping, speeding, flying, noClip, flyingDown, flyingUp; public void ParseHackFlags( string name, string motd ) { - if( name.Contains( "-hax" ) || motd.Contains( "-hax" ) ) { + string joined = name + motd; + if( joined.Contains( "-hax" ) ) { CanFly = CanNoclip = CanSpeed = CanRespawn = false; Window.CanUseThirdPersonCamera = false; Window.SetCamera( false ); @@ -219,32 +217,32 @@ namespace ClassicalSharp { } // Determine if specific hacks are or are not allowed. - if( name.Contains( "+fly" ) || motd.Contains( "+fly" ) ) { + if( joined.Contains( "+fly" ) ) { CanFly = true; - } else if( name.Contains( "-fly" ) || motd.Contains( "-fly" ) ) { + } else if( joined.Contains( "-fly" ) ) { CanFly = false; } - if( name.Contains( "+noclip" ) || motd.Contains( "+noclip" ) ) { + if( joined.Contains( "+noclip" ) ) { CanNoclip = true; - } else if( name.Contains( "-noclip" ) || motd.Contains( "-noclip" ) ) { + } else if( joined.Contains( "-noclip" ) ) { CanNoclip = false; } - if( name.Contains( "+speed" ) || motd.Contains( "+speed" ) ) { + if( joined.Contains( "+speed" ) ) { CanSpeed = true; - } else if( name.Contains( "-speed" ) || motd.Contains( "-speed" ) ) { + } else if( joined.Contains( "-speed" ) ) { CanSpeed = false; } - if( name.Contains( "+respawn" ) || motd.Contains( "+respawn" ) ) { + if( joined.Contains( "+respawn" ) ) { CanRespawn = true; - } else if( name.Contains( "-respawn" ) || motd.Contains( "-respawn" ) ) { + } else if( joined.Contains( "-respawn" ) ) { CanRespawn = false; } // Operator override. if( UserType == 0x64 ) { - if( name.Contains( "+ophax" ) || motd.Contains( "+ophax" ) ) { + if( joined.Contains( "+ophax" ) ) { CanFly = CanNoclip = CanSpeed = CanRespawn = true; - } else if( name.Contains( "-ophax" ) || motd.Contains( "-ophax" ) ) { + } else if( joined.Contains( "-ophax" ) ) { CanFly = CanNoclip = CanSpeed = CanRespawn = false; } } diff --git a/Entities/Player.cs b/Entities/Player.cs index 4320d6e29..1a3ea8bd9 100644 --- a/Entities/Player.cs +++ b/Entities/Player.cs @@ -11,7 +11,7 @@ namespace ClassicalSharp { public const float Width = 0.6f; public const float EyeHeight = 1.625f; public const float Height = 1.8f; - public const float Depth = 0.6f; + public const float Depth = 0.6f; public override Vector3 Size { get { return new Vector3( Width, Height, Depth ); } @@ -43,22 +43,19 @@ namespace ClassicalSharp { /// Gets the block just underneath the player's feet position. public Block BlockUnderFeet { - get { - if( map == null || map.IsNotLoaded ) return Block.Air; - Vector3I blockCoords = Vector3I.Floor( Position.X, Position.Y - 0.01f, Position.Z ); - if( !map.IsValidPos( blockCoords ) ) return Block.Air; - return (Block)map.GetBlock( blockCoords ); - } + get { return GetBlock( new Vector3( Position.X, Position.Y - 0.01f, Position.Z ) ); } } /// Gets the block at player's eye position. public Block BlockAtHead { - get { - if( map == null || map.IsNotLoaded ) return Block.Air; - Vector3I blockCoords = Vector3I.Floor( EyePosition ); - if( !map.IsValidPos( blockCoords ) ) return Block.Air; - return (Block)map.GetBlock( blockCoords ); - } + get { return GetBlock( EyePosition ); } + } + + Block GetBlock( Vector3 coords ) { + if( map == null || map.IsNotLoaded ) return Block.Air; + Vector3I blockCoords = Vector3I.Floor( coords ); + return map.IsValidPos( blockCoords ) ? + (Block)map.GetBlock( blockCoords ) : Block.Air; } public abstract void Tick( double delta ); @@ -91,13 +88,13 @@ namespace ClassicalSharp { const float legMax = (float)( 80 * Math.PI / 180.0 ); const float idleMax = (float)( 3 * Math.PI / 180.0 ); const float idleXPeriod = (float)( 2 * Math.PI / 5f ); - const float idleZPeriod = (float)( 2 * Math.PI / 3.5f ); + const float idleZPeriod = (float)( 2 * Math.PI / 3.5f ); protected void SetCurrentAnimState( float t ) { float swing = Utils.Lerp( swingO, swingN, t ); float walkTime = Utils.Lerp( walkTimeO, walkTimeN, t ); float idleTime = (float)( Window.accumulator ); float idleXRot = (float)( Math.Sin( idleTime * idleXPeriod ) * idleMax ); - float idleZRot = (float)( idleMax + Math.Cos( idleTime * idleZPeriod ) * idleMax ); + float idleZRot = (float)( idleMax + Math.Cos( idleTime * idleZPeriod ) * idleMax ); leftArmXRot = (float)( Math.Cos( walkTime ) * swing * armMax ) - idleXRot; rightArmXRot = -leftArmXRot; diff --git a/Utils/Camera.cs b/Utils/Camera.cs index 7487d5532..564d3af3e 100644 --- a/Utils/Camera.cs +++ b/Utils/Camera.cs @@ -76,7 +76,7 @@ namespace ClassicalSharp { Orientation.X += delta.X * sensitivity; Orientation.Y += delta.Y * sensitivity; - Clamp( ref Orientation.Y, halfPi + 0.01f, threeHalfPi - 0.01f ); + Utils.Clamp( ref Orientation.Y, halfPi + 0.01f, threeHalfPi - 0.01f ); LocationUpdate update = LocationUpdate.MakeOri( (float)Utils.RadiansToDegrees( Orientation.X ), (float)Utils.RadiansToDegrees( Orientation.Y - Math.PI ) @@ -91,11 +91,6 @@ namespace ClassicalSharp { CentreMousePosition(); UpdateMouseRotation(); } - - static void Clamp( ref float value, float min, float max ) { - if( value < min ) value = min; - if( value > max ) value = max; - } } public class ThirdPersonCamera : PerspectiveCamera { diff --git a/Utils/Utils.cs b/Utils/Utils.cs index 0ab007e89..4f8d85c16 100644 --- a/Utils/Utils.cs +++ b/Utils/Utils.cs @@ -22,6 +22,11 @@ namespace ClassicalSharp { public static string AppName = "ClassicalSharp 0.5"; + public static void Clamp( ref float value, float min, float max ) { + if( value < min ) value = min; + if( value > max ) value = max; + } + public static int NextPowerOf2( int value ) { int next = 1; while( value > next ) {