From e0c97a13070965a47f32d02bc353076139972ce4 Mon Sep 17 00:00:00 2001 From: Kenny van Vulpen Date: Sat, 2 Apr 2022 17:50:23 +0200 Subject: [PATCH] Small rendering fixes --- src/Alex.Common/Blocks/IMaterial.cs | 3 +- .../Graphics/Typography/BitmapFont.cs | 4 ++ src/Alex/Blocks/Minecraft/Ladder.cs | 2 +- .../Blocks/Minecraft/Liquid/LiquidBlock.cs | 31 ++++++++++--- src/Alex/Blocks/Minecraft/StoneCutter.cs | 1 + .../Blocks/Minecraft/Walls/AbstractWall.cs | 3 +- .../Entities/Components/MovementComponent.cs | 43 ++++++++++--------- .../Entities/Components/PhysicsComponent.cs | 4 +- src/Alex/Entities/Entity.cs | 2 +- .../Models/Blocks/LiquidBlockModel.cs | 8 ++++ src/Alex/Graphics/Models/ModelMatrixHolder.cs | 11 +++-- 11 files changed, 74 insertions(+), 38 deletions(-) diff --git a/src/Alex.Common/Blocks/IMaterial.cs b/src/Alex.Common/Blocks/IMaterial.cs index f469ed532..022410e6e 100644 --- a/src/Alex.Common/Blocks/IMaterial.cs +++ b/src/Alex.Common/Blocks/IMaterial.cs @@ -65,6 +65,7 @@ public interface IMapColor public enum BlockCollisionBehavior { None, - Blocking + Blocking, + VerticalClimb } } \ No newline at end of file diff --git a/src/Alex.Common/Graphics/Typography/BitmapFont.cs b/src/Alex.Common/Graphics/Typography/BitmapFont.cs index 2bf77690b..27f0a644b 100644 --- a/src/Alex.Common/Graphics/Typography/BitmapFont.cs +++ b/src/Alex.Common/Graphics/Typography/BitmapFont.cs @@ -176,6 +176,10 @@ public Vector2 MeasureString(string text) return new Vector2(width, offset.Y + finalLineHeight); } + private void Rotate(float rotation) + { + + } public void DrawString(SpriteBatch sb, string text, diff --git a/src/Alex/Blocks/Minecraft/Ladder.cs b/src/Alex/Blocks/Minecraft/Ladder.cs index 8f377aaa1..9f84129dd 100644 --- a/src/Alex/Blocks/Minecraft/Ladder.cs +++ b/src/Alex/Blocks/Minecraft/Ladder.cs @@ -15,7 +15,7 @@ public Ladder() : base() IsFullCube = false; - BlockMaterial = Material.Wood.Clone().WithHardness(0.4f); + BlockMaterial = Material.Wood.Clone().WithHardness(0.4f).SetCollisionBehavior(BlockCollisionBehavior.VerticalClimb); //Hardness = 0.4f; HasHitbox = true; } diff --git a/src/Alex/Blocks/Minecraft/Liquid/LiquidBlock.cs b/src/Alex/Blocks/Minecraft/Liquid/LiquidBlock.cs index 77e0f371f..406bf0847 100644 --- a/src/Alex/Blocks/Minecraft/Liquid/LiquidBlock.cs +++ b/src/Alex/Blocks/Minecraft/Liquid/LiquidBlock.cs @@ -1,5 +1,6 @@ using Alex.Blocks.Materials; using Alex.Blocks.Properties; +using Alex.Blocks.State; using Alex.Common.Blocks; using Alex.Common.Blocks.Properties; using Alex.Interfaces; @@ -12,17 +13,33 @@ public class LiquidBlock : Block protected LiquidBlock() { } - public override bool ShouldRenderFace(BlockFace face, Block neighbor) + private int GetLevel(BlockState state) { - int myLevelValue = LEVEL.GetValue(BlockState); + int neighborLevel = LEVEL.GetValue(state); + + if (neighborLevel == 0) + neighborLevel = 8; + return neighborLevel; + } + + public override bool ShouldRenderFace(BlockFace face, Block neighbor) + { + if (face == BlockFace.Up && (neighbor.BlockMaterial.IsLiquid || neighbor is LiquidBlock)) + return false; + if (neighbor.BlockMaterial == Material.WaterPlant || neighbor.BlockMaterial == Material.ReplaceableWaterPlant) return false; if (neighbor.BlockMaterial.IsLiquid || neighbor is LiquidBlock) { - int neighborLevel = LEVEL.GetValue(neighbor.BlockState); + int myLevelValue = GetLevel(BlockState);//LEVEL.GetValue(BlockState); + int neighborLevel = GetLevel(BlockState); + + if (neighborLevel == myLevelValue) + return false; + //var neighborLevel = neighbor.BlockState.GetTypedValue(LEVEL); if (neighborLevel > myLevelValue) @@ -32,12 +49,12 @@ public override bool ShouldRenderFace(BlockFace face, Block neighbor) return false; } - - if (neighbor.Solid && (!neighbor.Transparent || neighbor.BlockMaterial.IsOpaque)) - return false; - + if (neighbor.Solid && neighbor.Transparent && !neighbor.IsFullCube) return true; + + if (neighbor.Solid && (!neighbor.Transparent || neighbor.BlockMaterial.IsOpaque)) + return false; //else if (neighbor.Transparent) return base.ShouldRenderFace(face, neighbor); diff --git a/src/Alex/Blocks/Minecraft/StoneCutter.cs b/src/Alex/Blocks/Minecraft/StoneCutter.cs index b16fb7554..f2901c509 100644 --- a/src/Alex/Blocks/Minecraft/StoneCutter.cs +++ b/src/Alex/Blocks/Minecraft/StoneCutter.cs @@ -5,6 +5,7 @@ public class StoneCutter : Block public StoneCutter() { Solid = true; + Transparent = true; } } } \ No newline at end of file diff --git a/src/Alex/Blocks/Minecraft/Walls/AbstractWall.cs b/src/Alex/Blocks/Minecraft/Walls/AbstractWall.cs index 0272f94a6..376f3acd0 100644 --- a/src/Alex/Blocks/Minecraft/Walls/AbstractWall.cs +++ b/src/Alex/Blocks/Minecraft/Walls/AbstractWall.cs @@ -26,6 +26,7 @@ protected AbstractWall() /// public override BlockState BlockPlaced(IBlockAccess world, BlockState state, BlockCoordinates position) { + state = state.WithProperty("up", "true"); state = Check(world, position, position + BlockCoordinates.North, state); state = Check(world, position, position + BlockCoordinates.South, state); state = Check(world, position, position + BlockCoordinates.East, state); @@ -41,7 +42,7 @@ public override void BlockUpdate(World world, BlockCoordinates position, BlockCo { var state = Check(world, position, updatedBlock, BlockState); - if (state != BlockState) + if (state.Id != BlockState.Id) world.SetBlockState(position, state); //base.BlockUpdate(world, position, updatedBlock); diff --git a/src/Alex/Entities/Components/MovementComponent.cs b/src/Alex/Entities/Components/MovementComponent.cs index d818ad330..2f9883799 100644 --- a/src/Alex/Entities/Components/MovementComponent.cs +++ b/src/Alex/Entities/Components/MovementComponent.cs @@ -403,7 +403,8 @@ private bool CheckX(IBlockAccess blockAccess, if (!TestTerrainCollisionX(blockAccess, amount, out _, out var collisionX, boxes, checkOther)) return false; - if (CheckJump(blockAccess, amount, out float yValue)) + var climbingResult = CheckClimbing(blockAccess, amount, out float yValue); + if (climbingResult == CollisionResult.ClimbHalfBlock) { amount.Y = yValue; } @@ -427,7 +428,8 @@ private bool CheckZ(IBlockAccess blockAccess, if (!TestTerrainCollisionZ(blockAccess, amount, out _, out var collisionZ, boxes, checkOther)) return false; - if (CheckJump(blockAccess, amount, out float yValue)) + var climbingResult = CheckClimbing(blockAccess, amount, out float yValue); + if (climbingResult == CollisionResult.ClimbHalfBlock) { amount.Y = yValue; } @@ -445,20 +447,26 @@ private bool CheckZ(IBlockAccess blockAccess, public ColoredBoundingBox[] LastCollision { get; private set; } = new ColoredBoundingBox[0]; - private bool CheckJump(IBlockAccess blockAccess, Vector3 amount, out float yValue) + private enum CollisionResult + { + DoNothing, + ClimbHalfBlock, + VerticalClimb + } + + private CollisionResult CheckClimbing(IBlockAccess blockAccess, Vector3 amount, out float yValue) { yValue = amount.Y; var canJump = false; + var canClimb = false; - //float yTarget = amount.Y; if (Entity.KnownPosition.OnGround && MathF.Abs(Entity.Velocity.Y) < 0.001f) { canJump = true; var adjusted = Entity.GetBoundingBox(Entity.KnownPosition + amount); - var intersecting = GetIntersecting(Entity.Level, adjusted); + var intersecting = GetIntersecting(blockAccess, adjusted); var targetY = 0f; - //if (!PhysicsManager.GetIntersecting(Entity.Level, adjusted).Any(bb => bb.Max.Y >= adjusted.Min.Y && bb.Min.Y <= adjusted.Max.Y)) foreach (var box in intersecting) { var yDifference = box.Max.Y - adjusted.Min.Y; @@ -466,7 +474,6 @@ private bool CheckJump(IBlockAccess blockAccess, Vector3 amount, out float yValu if (yDifference > MaxClimbingDistance) { canJump = false; - break; } @@ -476,16 +483,12 @@ private bool CheckJump(IBlockAccess blockAccess, Vector3 amount, out float yValu if (canJump && targetY > 0f) { - //var originalY = amount.Y; - //yTarget = targetY; - //var a = intersecting. adjusted = Entity.GetBoundingBox(Entity.KnownPosition + new Vector3(amount.X, targetY, amount.Z)); if (GetIntersecting(Entity.Level, adjusted).Any( bb => bb.Max.Y > adjusted.Min.Y && bb.Min.Y <= adjusted.Max.Y)) { canJump = false; - //yTarget = originalY; } } else @@ -496,17 +499,21 @@ private bool CheckJump(IBlockAccess blockAccess, Vector3 amount, out float yValu if (canJump) { yValue = targetY; - //amount.Y = targetY; } } - return canJump; + if (canClimb) + return CollisionResult.VerticalClimb; + + if (canJump) + return CollisionResult.ClimbHalfBlock; + + return CollisionResult.DoNothing; } private bool DetectOnGround(IBlockAccess blockAccess, Vector3 position) { var entityBoundingBox = Entity.GetBoundingBox(position); - //entityBoundingBox.Inflate(0.01f); var offset = 0f; @@ -515,15 +522,9 @@ private bool DetectOnGround(IBlockAccess blockAccess, Vector3 position) offset = -1f; } - bool foundGround = false; - var minX = entityBoundingBox.Min.X; - //if (minX < 0f) - // minX -= 1; var minZ = entityBoundingBox.Min.Z; - //if (minZ < 0f) - // minZ -= 1; var maxX = entityBoundingBox.Max.X; var maxZ = entityBoundingBox.Max.Z; @@ -558,7 +559,7 @@ private bool DetectOnGround(IBlockAccess blockAccess, Vector3 position) } } - return foundGround; + return false; } private bool TestTerrainCollisionY(IBlockAccess blockAccess, diff --git a/src/Alex/Entities/Components/PhysicsComponent.cs b/src/Alex/Entities/Components/PhysicsComponent.cs index 8a1144a34..12763f01b 100644 --- a/src/Alex/Entities/Components/PhysicsComponent.cs +++ b/src/Alex/Entities/Components/PhysicsComponent.cs @@ -86,7 +86,7 @@ protected override void OnUpdate(float deltaTime) forward *= 0.3f; } - heading = ConvertHeading( + heading = ApplyHeading( e.KnownPosition.HeadYaw, strafing, forward, (e.IsFlying || e.IsSwimming) ? heading.Y : 0f, movementFactor); @@ -136,7 +136,7 @@ private float GetSlipperiness(Entity entity) return slipperiness; } - private Vector3 ConvertHeading(float yaw, float strafe, float forward, float vertical, float multiplier) + private Vector3 ApplyHeading(float yaw, float strafe, float forward, float vertical, float multiplier) { var speed = MathF.Sqrt(strafe * strafe + forward * forward + vertical * vertical); diff --git a/src/Alex/Entities/Entity.cs b/src/Alex/Entities/Entity.cs index ef035ed80..eefc3e007 100644 --- a/src/Alex/Entities/Entity.cs +++ b/src/Alex/Entities/Entity.cs @@ -1363,7 +1363,7 @@ public virtual void Update(IUpdateArgs args) var headYaw = (KnownPosition.HeadYaw - KnownPosition.Yaw); var pitch = KnownPosition.Pitch; - head.Rotation = new Vector3(-pitch, headYaw, 0f); + head.Rotation = new Vector3(pitch, headYaw, 0f); //_head.Rotation = Quaternion.CreateFromYawPitchRoll(MathUtils.ToRadians(headYaw), MathUtils.ToRadians(pitch), 0f); } diff --git a/src/Alex/Graphics/Models/Blocks/LiquidBlockModel.cs b/src/Alex/Graphics/Models/Blocks/LiquidBlockModel.cs index 9e091c974..ca2aec23c 100644 --- a/src/Alex/Graphics/Models/Blocks/LiquidBlockModel.cs +++ b/src/Alex/Graphics/Models/Blocks/LiquidBlockModel.cs @@ -92,6 +92,11 @@ public override void GetVertices(IBlockAccess blockAccess, bottomRight = GetAverageLiquidLevels(blockAccess, position + new BlockCoordinates(1, 0, 1)); } + if (topLeft == 0 && topRight == 0 && bottomLeft == 0 && bottomRight == 0) + { + topLeft = topRight = bottomLeft = bottomRight = 7; //GetLevel(baseBlock); + } + if (baseBlock.Block is FlowingWater || baseBlock.Block is FlowingLava) isFlowing = true; @@ -217,6 +222,9 @@ public override void GetVertices(IBlockAccess blockAccess, height = (int)((16.0 / 8.0) * (bottomRight)); } + //if (height <= 0) + // height = 8; + vert.Position.Y = ((float)height) / 16f; } diff --git a/src/Alex/Graphics/Models/ModelMatrixHolder.cs b/src/Alex/Graphics/Models/ModelMatrixHolder.cs index 811a249cc..a2e736cb1 100644 --- a/src/Alex/Graphics/Models/ModelMatrixHolder.cs +++ b/src/Alex/Graphics/Models/ModelMatrixHolder.cs @@ -83,7 +83,7 @@ private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e } public bool IsMatricesDirty { get; set; } = false; - private Matrix[] _transforms = null; + protected Matrix[] Transformations { get; private set; }= null; public Matrix[] GetTransforms() { @@ -92,8 +92,8 @@ public Matrix[] GetTransforms() if (model?.Bones == null) return null; - if (_transforms != null && !IsMatricesDirty && _transforms.Length == model.Bones.Count) - return _transforms; + if (Transformations != null && !IsMatricesDirty && Transformations.Length == model.Bones.Count) + return Transformations; var source = model.Bones.ImmutableArray; Matrix[] destinationBoneTransforms = new Matrix[source.Length]; @@ -132,7 +132,7 @@ public Matrix[] GetTransforms() } } - _transforms = destinationBoneTransforms; + Transformations = destinationBoneTransforms; IsMatricesDirty = false; return destinationBoneTransforms; @@ -158,6 +158,9 @@ public virtual void Update(IUpdateArgs args) bone.Update(args); //bone.Update(args); } + + //if (IsMatricesDirty) + // Transformations = GetTransforms(); } public virtual void ApplyMovement()