Skip to content

Commit

Permalink
Small rendering fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyvv committed Apr 2, 2022
1 parent 4218c38 commit e0c97a1
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 38 deletions.
3 changes: 2 additions & 1 deletion src/Alex.Common/Blocks/IMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public interface IMapColor
public enum BlockCollisionBehavior
{
None,
Blocking
Blocking,
VerticalClimb
}
}
4 changes: 4 additions & 0 deletions src/Alex.Common/Graphics/Typography/BitmapFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Alex/Blocks/Minecraft/Ladder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
31 changes: 24 additions & 7 deletions src/Alex/Blocks/Minecraft/Liquid/LiquidBlock.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/Alex/Blocks/Minecraft/StoneCutter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class StoneCutter : Block
public StoneCutter()
{
Solid = true;
Transparent = true;
}
}
}
3 changes: 2 additions & 1 deletion src/Alex/Blocks/Minecraft/Walls/AbstractWall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected AbstractWall()
/// <inheritdoc />
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);
Expand All @@ -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);
Expand Down
43 changes: 22 additions & 21 deletions src/Alex/Entities/Components/MovementComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -445,28 +447,33 @@ 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;

if (yDifference > MaxClimbingDistance)
{
canJump = false;

break;
}

Expand All @@ -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
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -558,7 +559,7 @@ private bool DetectOnGround(IBlockAccess blockAccess, Vector3 position)
}
}

return foundGround;
return false;
}

private bool TestTerrainCollisionY(IBlockAccess blockAccess,
Expand Down
4 changes: 2 additions & 2 deletions src/Alex/Entities/Components/PhysicsComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Alex/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
8 changes: 8 additions & 0 deletions src/Alex/Graphics/Models/Blocks/LiquidBlockModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
11 changes: 7 additions & 4 deletions src/Alex/Graphics/Models/ModelMatrixHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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];
Expand Down Expand Up @@ -132,7 +132,7 @@ public Matrix[] GetTransforms()
}
}

_transforms = destinationBoneTransforms;
Transformations = destinationBoneTransforms;
IsMatricesDirty = false;

return destinationBoneTransforms;
Expand All @@ -158,6 +158,9 @@ public virtual void Update(IUpdateArgs args)
bone.Update(args);
//bone.Update(args);
}

//if (IsMatricesDirty)
// Transformations = GetTransforms();
}

public virtual void ApplyMovement()
Expand Down

0 comments on commit e0c97a1

Please sign in to comment.