From 2cb3322c34c1b5bf54b65d02fc7ed7b56e726d8e Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 16 Feb 2016 23:58:17 +1100 Subject: [PATCH] Modularise Entity and Player class more. --- ClassicalSharp/ClassicalSharp.csproj | 1 + ClassicalSharp/Entities/Entity.Bounds.cs | 94 ++++++++++++++++++++++++ ClassicalSharp/Entities/Entity.cs | 92 ++++------------------- ClassicalSharp/Entities/LocalPlayer.cs | 6 ++ ClassicalSharp/Entities/Player.cs | 25 ------- ClassicalSharp/Model/BlockModel.cs | 12 +-- ClassicalSharp/Model/ChickenModel.cs | 12 +-- ClassicalSharp/Model/CreeperModel.cs | 12 +-- ClassicalSharp/Model/IModel.cs | 2 +- ClassicalSharp/Model/PigModel.cs | 12 +-- ClassicalSharp/Model/PlayerModel.cs | 12 +-- ClassicalSharp/Model/SheepModel.cs | 12 +-- ClassicalSharp/Model/SkeletonModel.cs | 12 +-- ClassicalSharp/Model/SpiderModel.cs | 12 +-- ClassicalSharp/Model/ZombieModel.cs | 12 +-- 15 files changed, 144 insertions(+), 184 deletions(-) create mode 100644 ClassicalSharp/Entities/Entity.Bounds.cs diff --git a/ClassicalSharp/ClassicalSharp.csproj b/ClassicalSharp/ClassicalSharp.csproj index c69628a79..13f5ad48c 100644 --- a/ClassicalSharp/ClassicalSharp.csproj +++ b/ClassicalSharp/ClassicalSharp.csproj @@ -148,6 +148,7 @@ + diff --git a/ClassicalSharp/Entities/Entity.Bounds.cs b/ClassicalSharp/Entities/Entity.Bounds.cs new file mode 100644 index 000000000..f13cfd1fc --- /dev/null +++ b/ClassicalSharp/Entities/Entity.Bounds.cs @@ -0,0 +1,94 @@ +using System; +using ClassicalSharp.Model; +using OpenTK; + +namespace ClassicalSharp { + + /// Contains a model, along with position, velocity, and rotation. + /// May also contain other fields and properties. + public abstract partial class Entity { + + /// Returns the bounding box that contains the model, assuming it is not rotated. + public BoundingBox PickingBounds { + get { UpdateModel(); return Model.PickingBounds.Offset( Position ); } + } + + /// Bounding box of the model that collision detection + /// is performed with, in world coordinates. + public virtual BoundingBox CollisionBounds { + get { + Vector3 pos = Position; + Vector3 size = CollisionSize; + return new BoundingBox( pos.X - size.X / 2, pos.Y, pos.Z - size.Z / 2, + pos.X + size.X / 2, pos.Y + size.Y, pos.Z + size.Z / 2 ); + } + } + + /// Determines whether any of the blocks that intersect the + /// bounding box of this entity satisfy the given condition. + public bool TouchesAny( Predicate condition ) { + return TouchesAny( CollisionBounds, condition ); + } + + /// Determines whether any of the blocks that intersect the + /// given bounding box satisfy the given condition. + public bool TouchesAny( BoundingBox bounds, Predicate condition ) { + Vector3I bbMin = Vector3I.Floor( bounds.Min ); + Vector3I bbMax = Vector3I.Floor( bounds.Max ); + + // Order loops so that we minimise cache misses + for( int y = bbMin.Y; y <= bbMax.Y; y++ ) + for( int z = bbMin.Z; z <= bbMax.Z; z++ ) + for( int x = bbMin.X; x <= bbMax.X; x++ ) + { + if( !game.Map.IsValidPos( x, y, z ) ) continue; + byte block = game.Map.GetBlock( x, y, z ); + Vector3 min = new Vector3( x, y, z ) + info.MinBB[block]; + Vector3 max = new Vector3( x, y, z ) + info.MaxBB[block]; + + BoundingBox blockBB = new BoundingBox( min, max ); + if( !blockBB.Intersects( bounds ) ) continue; + if( condition( block ) ) return true; + } + return false; + } + + /// Constant offset used to avoid floating point roundoff errors. + public const float Adjustment = 0.001f; + static readonly Vector3 liqExpand = new Vector3( 0.25f/16f, 0/16f, 0.25f/16f ); + + /// Determines whether any of the blocks that intersect the + /// bounding box of this entity are lava or still lava. + protected bool TouchesAnyLava() { + BoundingBox bounds = CollisionBounds.Expand( liqExpand ); + AdjustLiquidTestBounds( ref bounds ); + return TouchesAny( bounds, + b => b == (byte)Block.Lava || b == (byte)Block.StillLava ); + } + + /// Determines whether any of the blocks that intersect the + /// bounding box of this entity are rope. + protected bool TouchesAnyRope() { + BoundingBox bounds = CollisionBounds; + bounds.Max.Y += 0.5f/16f; + return TouchesAny( bounds, b => b == (byte)Block.Rope ); + } + + /// Determines whether any of the blocks that intersect the + /// bounding box of this entity are water or still water. + protected bool TouchesAnyWater() { + BoundingBox bounds = CollisionBounds.Expand( liqExpand ); + AdjustLiquidTestBounds( ref bounds ); + return TouchesAny( bounds, + b => b == (byte)Block.Water || b == (byte)Block.StillWater ); + } + + void AdjustLiquidTestBounds( ref BoundingBox bounds ) { + // Even though we collide with lava 3 blocks above our feet, vanilla client thinks + // that we do not.. so we have to maintain compatibility here. + float height = bounds.Max.Y - bounds.Min.Y; + const float pHeight = (28.5f - 4f)/16f; + bounds.Max.Y = bounds.Min.Y + Math.Min( height, pHeight ); + } + } +} \ No newline at end of file diff --git a/ClassicalSharp/Entities/Entity.cs b/ClassicalSharp/Entities/Entity.cs index 8b6dcea4e..6c17d84e7 100644 --- a/ClassicalSharp/Entities/Entity.cs +++ b/ClassicalSharp/Entities/Entity.cs @@ -6,7 +6,7 @@ namespace ClassicalSharp { /// Contains a model, along with position, velocity, and rotation. /// May also contain other fields and properties. - public abstract class Entity { + public abstract partial class Entity { public Entity( Game game ) { this.game = game; @@ -49,28 +49,12 @@ namespace ClassicalSharp { get { UpdateModel(); return Model.CollisionSize; } } - /// Returns the bounding box that contains the model, assuming it is not rotated. - public BoundingBox PickingBounds { - get { UpdateModel(); return Model.PickingBounds.Offset( Position ); } - } - void UpdateModel() { BlockModel model = Model as BlockModel; if( model != null ) model.CalcState( byte.Parse( ModelName ) ); } - /// Bounding box of the model that collision detection - /// is performed with, in world coordinates. - public virtual BoundingBox CollisionBounds { - get { - Vector3 pos = Position; - Vector3 size = CollisionSize; - return new BoundingBox( pos.X - size.X / 2, pos.Y, pos.Z - size.Z / 2, - pos.X + size.X / 2, pos.Y + size.Y, pos.Z + size.Z / 2 ); - } - } - public abstract void Tick( double delta ); public abstract void SetLocation( LocationUpdate update, bool interpolate ); @@ -84,71 +68,23 @@ namespace ClassicalSharp { /// Assumes that RenderModel was previously called this frame. public abstract void RenderName(); - /// Determines whether any of the blocks that intersect the - /// bounding box of this entity satisfy the given condition. - public bool TouchesAny( Predicate condition ) { - return TouchesAny( CollisionBounds, condition ); + /// Gets the position of the player's eye in the world. + public Vector3 EyePosition { + get { return new Vector3( Position.X, Position.Y + Model.GetEyeY( this ), Position.Z ); } + } + + /// Gets the block just underneath the player's feet position. + public Block BlockUnderFeet { + get { return GetBlock( new Vector3( Position.X, Position.Y - 0.01f, Position.Z ) ); } } - /// Determines whether any of the blocks that intersect the - /// given bounding box satisfy the given condition. - public bool TouchesAny( BoundingBox bounds, Predicate condition ) { - Vector3I bbMin = Vector3I.Floor( bounds.Min ); - Vector3I bbMax = Vector3I.Floor( bounds.Max ); - - // Order loops so that we minimise cache misses - for( int y = bbMin.Y; y <= bbMax.Y; y++ ) - for( int z = bbMin.Z; z <= bbMax.Z; z++ ) - for( int x = bbMin.X; x <= bbMax.X; x++ ) - { - if( !game.Map.IsValidPos( x, y, z ) ) continue; - byte block = game.Map.GetBlock( x, y, z ); - Vector3 min = new Vector3( x, y, z ) + info.MinBB[block]; - Vector3 max = new Vector3( x, y, z ) + info.MaxBB[block]; - - BoundingBox blockBB = new BoundingBox( min, max ); - if( !blockBB.Intersects( bounds ) ) continue; - if( condition( block ) ) return true; - } - return false; + /// Gets the block at player's eye position. + public Block BlockAtHead { + get { return GetBlock( EyePosition ); } } - /// Constant offset used to avoid floating point roundoff errors. - public const float Adjustment = 0.001f; - static readonly Vector3 liqExpand = new Vector3( 0.25f/16f, 0/16f, 0.25f/16f ); - - /// Determines whether any of the blocks that intersect the - /// bounding box of this entity are lava or still lava. - protected bool TouchesAnyLava() { - BoundingBox bounds = CollisionBounds.Expand( liqExpand ); - AdjustLiquidTestBounds( ref bounds ); - return TouchesAny( bounds, - b => b == (byte)Block.Lava || b == (byte)Block.StillLava ); - } - - /// Determines whether any of the blocks that intersect the - /// bounding box of this entity are rope. - protected bool TouchesAnyRope() { - BoundingBox bounds = CollisionBounds; - bounds.Max.Y += 0.5f/16f; - return TouchesAny( bounds, b => b == (byte)Block.Rope ); - } - - /// Determines whether any of the blocks that intersect the - /// bounding box of this entity are water or still water. - protected bool TouchesAnyWater() { - BoundingBox bounds = CollisionBounds.Expand( liqExpand ); - AdjustLiquidTestBounds( ref bounds ); - return TouchesAny( bounds, - b => b == (byte)Block.Water || b == (byte)Block.StillWater ); - } - - void AdjustLiquidTestBounds( ref BoundingBox bounds ) { - // Even though we collide with lava 3 blocks above our feet, vanilla client thinks - // that we do not.. so we have to maintain compatibility here. - float height = bounds.Max.Y - bounds.Min.Y; - const float pHeight = (28.5f - 4f)/16f; - bounds.Max.Y = bounds.Min.Y + Math.Min( height, pHeight ); + protected Block GetBlock( Vector3 coords ) { + return (Block)game.Map.SafeGetBlock( Vector3I.Floor( coords ) ); } } } \ No newline at end of file diff --git a/ClassicalSharp/Entities/LocalPlayer.cs b/ClassicalSharp/Entities/LocalPlayer.cs index aed9c5458..a9c6aca25 100644 --- a/ClassicalSharp/Entities/LocalPlayer.cs +++ b/ClassicalSharp/Entities/LocalPlayer.cs @@ -228,6 +228,12 @@ namespace ClassicalSharp { } } + void RemoveOldest(T[] array, ref int count) { + for( int i = 0; i < array.Length - 1; i++ ) + array[i] = array[i + 1]; + count--; + } + internal bool HandleKeyDown( Key key ) { KeyMap keys = game.InputHandler.Keys; if( key == keys[KeyBinding.Respawn] && Hacks.CanRespawn ) { diff --git a/ClassicalSharp/Entities/Player.cs b/ClassicalSharp/Entities/Player.cs index 31d048bde..f159ec9f6 100644 --- a/ClassicalSharp/Entities/Player.cs +++ b/ClassicalSharp/Entities/Player.cs @@ -11,11 +11,6 @@ namespace ClassicalSharp { public abstract partial class Player : Entity { - /// Gets the position of the player's eye in the world. - public Vector3 EyePosition { - get { return new Vector3( Position.X, Position.Y + Model.GetEyeY( this ), Position.Z ); } - } - public string DisplayName, SkinName, SkinIdentifier; public SkinType SkinType; internal AnimatedComponent anim; @@ -28,20 +23,6 @@ namespace ClassicalSharp { SetModel( "humanoid" ); } - /// Gets the block just underneath the player's feet position. - public Block BlockUnderFeet { - 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 { return GetBlock( EyePosition ); } - } - - protected Block GetBlock( Vector3 coords ) { - return (Block)game.Map.SafeGetBlock( Vector3I.Floor( coords ) ); - } - protected void CheckSkin() { DownloadedItem item; game.AsyncDownloader.TryGetItem( SkinIdentifier, out item ); @@ -113,11 +94,5 @@ namespace ClassicalSharp { } } } - - protected void RemoveOldest(T[] array, ref int count) { - for( int i = 0; i < array.Length - 1; i++ ) - array[i] = array[i + 1]; - count--; - } } } \ No newline at end of file diff --git a/ClassicalSharp/Model/BlockModel.cs b/ClassicalSharp/Model/BlockModel.cs index 6401b0a1f..1e5739f0a 100644 --- a/ClassicalSharp/Model/BlockModel.cs +++ b/ClassicalSharp/Model/BlockModel.cs @@ -17,16 +17,12 @@ namespace ClassicalSharp.Model { public BlockModel( Game game ) : base( game ) { } - public override bool Bobbing { - get { return false; } - } + public override bool Bobbing { get { return false; } } - public override float NameYOffset { - get { return height + 0.075f; } - } + public override float NameYOffset { get { return height + 0.075f; } } - public override float GetEyeY( Player player ) { - byte block = Byte.Parse( player.ModelName ); + public override float GetEyeY( Entity entity ) { + byte block = Byte.Parse( entity.ModelName ); float minY = game.BlockInfo.MinBB[block].Y; float maxY = game.BlockInfo.MaxBB[block].Y; return block == 0 ? 1 : (minY + maxY) / 2; diff --git a/ClassicalSharp/Model/ChickenModel.cs b/ClassicalSharp/Model/ChickenModel.cs index cff71792b..b5dd0f9b1 100644 --- a/ClassicalSharp/Model/ChickenModel.cs +++ b/ClassicalSharp/Model/ChickenModel.cs @@ -32,17 +32,11 @@ namespace ClassicalSharp.Model { return new ModelPart( index - 2 * 4, 2 * 4 ); } - public override bool Bobbing { - get { return true; } - } + public override bool Bobbing { get { return true; } } - public override float NameYOffset { - get { return 1.0125f; } - } + public override float NameYOffset { get { return 1.0125f; } } - public override float GetEyeY( Player player ) { - return 14/16f; - } + public override float GetEyeY( Entity entity ) { return 14/16f; } public override Vector3 CollisionSize { get { return new Vector3( 8/16f, 12/16f, 8/16f ); } diff --git a/ClassicalSharp/Model/CreeperModel.cs b/ClassicalSharp/Model/CreeperModel.cs index 9aeb13d98..d7da5471d 100644 --- a/ClassicalSharp/Model/CreeperModel.cs +++ b/ClassicalSharp/Model/CreeperModel.cs @@ -23,17 +23,11 @@ namespace ClassicalSharp.Model { .SetTexOrigin( 0, 16 ) ); } - public override bool Bobbing { - get { return true; } - } + public override bool Bobbing { get { return true; } } - public override float NameYOffset { - get { return 1.7f; } - } + public override float NameYOffset { get { return 1.7f; } } - public override float GetEyeY( Player player ) { - return 22/16f; - } + public override float GetEyeY( Entity entity ) { return 22/16f; } public override Vector3 CollisionSize { get { return new Vector3( 8/16f, 26/16f, 8/16f ); } diff --git a/ClassicalSharp/Model/IModel.cs b/ClassicalSharp/Model/IModel.cs index 5e3c9beaa..669582ba5 100644 --- a/ClassicalSharp/Model/IModel.cs +++ b/ClassicalSharp/Model/IModel.cs @@ -32,7 +32,7 @@ namespace ClassicalSharp.Model { public abstract float NameYOffset { get; } /// Vertical offset from the model's feet/base that the model's eye is located. - public abstract float GetEyeY( Player player ); + public abstract float GetEyeY( Entity entity ); /// The size of the bounding box that is used when /// performing collision detection for this model. diff --git a/ClassicalSharp/Model/PigModel.cs b/ClassicalSharp/Model/PigModel.cs index 533f2cfb7..b5b7dc0ae 100644 --- a/ClassicalSharp/Model/PigModel.cs +++ b/ClassicalSharp/Model/PigModel.cs @@ -22,17 +22,11 @@ namespace ClassicalSharp.Model { .SetTexOrigin( 0, 16 ) ); } - public override bool Bobbing { - get { return true; } - } + public override bool Bobbing { get { return true; } } - public override float NameYOffset { - get { return 1.075f; } - } + public override float NameYOffset { get { return 1.075f; } } - public override float GetEyeY( Player player ) { - return 12/16f; - } + public override float GetEyeY( Entity entity ) { return 12/16f; } public override Vector3 CollisionSize { get { return new Vector3( 14/16f, 14/16f, 14/16f ); } diff --git a/ClassicalSharp/Model/PlayerModel.cs b/ClassicalSharp/Model/PlayerModel.cs index 196612c06..2fb3fc631 100644 --- a/ClassicalSharp/Model/PlayerModel.cs +++ b/ClassicalSharp/Model/PlayerModel.cs @@ -40,17 +40,11 @@ namespace ClassicalSharp.Model { SetSlim.Hat = Set.Hat; } - public override bool Bobbing { - get { return true; } - } + public override bool Bobbing { get { return true; } } - public override float NameYOffset { - get { return 2.1375f; } - } + public override float NameYOffset { get { return 2.1375f; } } - public override float GetEyeY( Player player ) { - return 26/16f; - } + public override float GetEyeY( Entity entity ) { return 26/16f; } public override Vector3 CollisionSize { get { return new Vector3( 8/16f + 0.6f/16f, 28.1f/16f, 8/16f + 0.6f/16f ); } diff --git a/ClassicalSharp/Model/SheepModel.cs b/ClassicalSharp/Model/SheepModel.cs index 7fee128d0..ef66b4655 100644 --- a/ClassicalSharp/Model/SheepModel.cs +++ b/ClassicalSharp/Model/SheepModel.cs @@ -44,17 +44,11 @@ namespace ClassicalSharp.Model { FurRightLegBack = BuildBox( legDesc.SetModelBounds( 0.5f, 5.5f, 4.5f, 5.5f, 12.5f, 9.5f ) ); } - public override bool Bobbing { - get { return true; } - } + public override bool Bobbing { get { return true; } } - public override float NameYOffset { - get { return Fur ? 1.48125f: 1.075f; } - } + public override float NameYOffset { get { return Fur ? 1.48125f: 1.075f; } } - public override float GetEyeY( Player player ) { - return 20/16f; - } + public override float GetEyeY( Entity entity ) { return 20/16f; } public override Vector3 CollisionSize { get { return new Vector3( 14/16f, 20/16f, 14/16f ); } diff --git a/ClassicalSharp/Model/SkeletonModel.cs b/ClassicalSharp/Model/SkeletonModel.cs index 0c8f268f8..8db52439f 100644 --- a/ClassicalSharp/Model/SkeletonModel.cs +++ b/ClassicalSharp/Model/SkeletonModel.cs @@ -22,17 +22,11 @@ namespace ClassicalSharp.Model { .SetTexOrigin( 40, 16 ) ); } - public override bool Bobbing { - get { return true; } - } + public override bool Bobbing { get { return true; } } - public override float NameYOffset { - get { return 2.075f; } - } + public override float NameYOffset { get { return 2.075f; } } - public override float GetEyeY( Player player ) { - return 26/16f; - } + public override float GetEyeY( Entity entity ) { return 26/16f; } public override Vector3 CollisionSize { get { return new Vector3( 8/16f, 30/16f, 8/16f ); } diff --git a/ClassicalSharp/Model/SpiderModel.cs b/ClassicalSharp/Model/SpiderModel.cs index b549693e9..999d42a7d 100644 --- a/ClassicalSharp/Model/SpiderModel.cs +++ b/ClassicalSharp/Model/SpiderModel.cs @@ -20,17 +20,11 @@ namespace ClassicalSharp.Model { .SetTexOrigin( 18, 0 ) ); } - public override bool Bobbing { - get { return true; } - } + public override bool Bobbing { get { return true; } } - public override float NameYOffset { - get { return 1.0125f; } - } + public override float NameYOffset { get { return 1.0125f; } } - public override float GetEyeY( Player player ) { - return 8/16f; - } + public override float GetEyeY( Entity entity ) { return 8/16f; } public override Vector3 CollisionSize { get { return new Vector3( 15/16f, 12/16f, 15/16f ); } diff --git a/ClassicalSharp/Model/ZombieModel.cs b/ClassicalSharp/Model/ZombieModel.cs index 7813763d3..e433b023d 100644 --- a/ClassicalSharp/Model/ZombieModel.cs +++ b/ClassicalSharp/Model/ZombieModel.cs @@ -22,17 +22,11 @@ namespace ClassicalSharp.Model { .SetTexOrigin( 40, 16 ) ); } - public override bool Bobbing { - get { return true; } - } + public override bool Bobbing { get { return true; } } - public override float NameYOffset { - get { return 2.075f; } - } + public override float NameYOffset { get { return 2.075f; } } - public override float GetEyeY( Player player ) { - return 26/16f; - } + public override float GetEyeY( Entity entity ) { return 26/16f; } public override Vector3 CollisionSize { get { return new Vector3( 8/16f, 30/16f, 8/16f ); }