Skip to content

Commit

Permalink
Mostly port entity components to C.
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Apr 19, 2018
1 parent 468e958 commit bfe4bc0
Show file tree
Hide file tree
Showing 42 changed files with 853 additions and 435 deletions.
4 changes: 2 additions & 2 deletions ClassicalSharp/2D/Utils/FastColour.cs
Expand Up @@ -82,7 +82,7 @@ public struct FastColour : IEquatable<FastColour> {
public int ToArgb() { return A << 24 | R << 16 | G << 8 | B; }

public static FastColour Argb(int c) {
FastColour col = default(FastColour);
FastColour col;
col.A = (byte)(c >> 24);
col.R = (byte)(c >> 16);
col.G = (byte)(c >> 8);
Expand All @@ -102,7 +102,7 @@ public struct FastColour : IEquatable<FastColour> {
}

public static FastColour Unpack(int c) {
FastColour col = default(FastColour);
FastColour col;
col.A = (byte)(c >> 24);
col.G = (byte)(c >> 8);
#if USE_DX
Expand Down
1 change: 0 additions & 1 deletion ClassicalSharp/ClassicalSharp.csproj
Expand Up @@ -144,7 +144,6 @@
<Compile Include="Entities\Mobs\AI.cs" />
<Compile Include="Entities\Components\AnimatedComponent.cs" />
<Compile Include="Entities\Components\HacksComponent.cs" />
<Compile Include="Entities\Components\InputComponent.cs" />
<Compile Include="Entities\Components\CollisionsComponent.cs" />
<Compile Include="Entities\Components\NewCollisionsComponent.cs" />
<Compile Include="Entities\Components\PhysicsComponent.cs" />
Expand Down
2 changes: 1 addition & 1 deletion ClassicalSharp/Entities/Components/AnimatedComponent.cs
Expand Up @@ -117,7 +117,7 @@ public sealed class TiltComponent {

// TODO: the Tilt code was designed for 60 ticks/second, fix it up for 20 ticks/second
for (int i = 0; i < 3; i++) {
AnimatedComponent.DoTilt(ref velTiltStrengthN, p.Hacks.Noclip || p.Hacks.Flying);
AnimatedComponent.DoTilt(ref velTiltStrengthN, p.Hacks.Floating);
}
}

Expand Down
38 changes: 19 additions & 19 deletions ClassicalSharp/Entities/Components/CollisionsComponent.cs
Expand Up @@ -111,31 +111,31 @@ public sealed class CollisionsComponent {
}

void ClipXMin(ref AABB blockBB, ref AABB entityBB, bool wasOn, ref AABB finalBB, ref AABB extentBB, ref Vector3 size) {
if (!wasOn || !DidSlide(blockBB, ref size, ref finalBB, ref entityBB, ref extentBB)) {
if (!wasOn || !DidSlide(ref blockBB, ref size, ref finalBB, ref entityBB, ref extentBB)) {
entity.Position.X = blockBB.Min.X - size.X / 2 - Adjustment;
ClipX(ref size, ref entityBB, ref extentBB);
hitXMin = true;
}
}

void ClipXMax(ref AABB blockBB, ref AABB entityBB, bool wasOn, ref AABB finalBB, ref AABB extentBB, ref Vector3 size) {
if (!wasOn || !DidSlide(blockBB, ref size, ref finalBB, ref entityBB, ref extentBB)) {
if (!wasOn || !DidSlide(ref blockBB, ref size, ref finalBB, ref entityBB, ref extentBB)) {
entity.Position.X = blockBB.Max.X + size.X / 2 + Adjustment;
ClipX(ref size, ref entityBB, ref extentBB);
hitXMax = true;
}
}

void ClipZMax(ref AABB blockBB, ref AABB entityBB, bool wasOn, ref AABB finalBB, ref AABB extentBB, ref Vector3 size) {
if (!wasOn || !DidSlide(blockBB, ref size, ref finalBB, ref entityBB, ref extentBB)) {
if (!wasOn || !DidSlide(ref blockBB, ref size, ref finalBB, ref entityBB, ref extentBB)) {
entity.Position.Z = blockBB.Max.Z + size.Z / 2 + Adjustment;
ClipZ(ref size, ref entityBB, ref extentBB);
hitZMax = true;
}
}

void ClipZMin(ref AABB blockBB, ref AABB entityBB, bool wasOn, ref AABB finalBB, ref AABB extentBB, ref Vector3 size) {
if (!wasOn || !DidSlide(blockBB, ref size, ref finalBB, ref entityBB, ref extentBB)) {
if (!wasOn || !DidSlide(ref blockBB, ref size, ref finalBB, ref entityBB, ref extentBB)) {
entity.Position.Z = blockBB.Min.Z - size.Z / 2 - Adjustment;
ClipZ(ref size, ref entityBB, ref extentBB);
hitZMin = true;
Expand All @@ -155,26 +155,24 @@ public sealed class CollisionsComponent {
hitYMax = true;
}

bool DidSlide(AABB blockBB, ref Vector3 size, ref AABB finalBB, ref AABB entityBB, ref AABB extentBB) {
bool DidSlide(ref AABB blockBB, ref Vector3 size, ref AABB finalBB, ref AABB entityBB, ref AABB extentBB) {
float yDist = blockBB.Max.Y - entityBB.Min.Y;
if (yDist > 0 && yDist <= entity.StepSize + 0.01f) {
float blockXMin = blockBB.Min.X, blockZMin = blockBB.Min.Z;
blockBB.Min.X = Math.Max(blockBB.Min.X, blockBB.Max.X - size.X / 2);
blockBB.Max.X = Math.Min(blockBB.Max.X, blockXMin + size.X / 2);
blockBB.Min.Z = Math.Max(blockBB.Min.Z, blockBB.Max.Z - size.Z / 2);
blockBB.Max.Z = Math.Min(blockBB.Max.Z, blockZMin + size.Z / 2);
float blockBB_MinX = Math.Max(blockBB.Min.X, blockBB.Max.X - size.X / 2);
float blockBB_MaxX = Math.Min(blockBB.Max.X, blockBB.Min.X + size.X / 2);
float blockBB_MinZ = Math.Max(blockBB.Min.Z, blockBB.Max.Z - size.Z / 2);
float blockBB_MaxZ = Math.Min(blockBB.Max.Z, blockBB.Min.Z + size.Z / 2);

AABB adjBB;
adjBB.Min.X = Math.Min(finalBB.Min.X, blockBB.Min.X + Adjustment);
adjBB.Max.X = Math.Max(finalBB.Max.X, blockBB.Max.X - Adjustment);
adjBB.Min.X = Math.Min(finalBB.Min.X, blockBB_MinX + Adjustment);
adjBB.Max.X = Math.Max(finalBB.Max.X, blockBB_MaxX - Adjustment);
adjBB.Min.Y = blockBB.Max.Y + Adjustment;
adjBB.Max.Y = adjBB.Min.Y + size.Y;
adjBB.Min.Z = Math.Min(finalBB.Min.Z, blockBB.Min.Z + Adjustment);
adjBB.Max.Z = Math.Max(finalBB.Max.Z, blockBB.Max.Z - Adjustment);
adjBB.Min.Z = Math.Min(finalBB.Min.Z, blockBB_MinZ + Adjustment);
adjBB.Max.Z = Math.Max(finalBB.Max.Z, blockBB_MaxZ - Adjustment);

if (!CanSlideThrough(ref adjBB)) return false;

entity.Position.Y = blockBB.Max.Y + Adjustment;
if (!CanSlideThrough(ref adjBB)) return false;
entity.Position.Y = adjBB.Min.Y;
entity.onGround = true;
ClipY(ref size, ref entityBB, ref extentBB);
return true;
Expand All @@ -187,13 +185,15 @@ public sealed class CollisionsComponent {
Vector3I bbMax = Vector3I.Floor(adjFinalBB.Max);
AABB blockBB;

Vector3 pos;
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++)
{
pos.X = x; pos.Y = y; pos.Z = z;
BlockID block = game.World.GetPhysicsBlock(x, y, z);
blockBB.Min = new Vector3(x, y, z) + BlockInfo.MinBB[block];
blockBB.Max = new Vector3(x, y, z) + BlockInfo.MaxBB[block];
blockBB.Min = pos + BlockInfo.MinBB[block];
blockBB.Max = pos + BlockInfo.MaxBB[block];

if (!blockBB.Intersects(adjFinalBB)) continue;
if (BlockInfo.Collide[block] == CollideType.Solid) return false;
Expand Down
2 changes: 1 addition & 1 deletion ClassicalSharp/Entities/Components/HacksComponent.cs
Expand Up @@ -51,7 +51,7 @@ public sealed class HacksComponent {
public bool Noclip, Flying, FlyingUp, FlyingDown, Speeding, HalfSpeeding;

public bool CanJumpHigher { get { return Enabled && CanAnyHacks && CanSpeed; } }
public bool Floating { get { return Noclip || Flying; } }
public bool Floating; // true if Noclip or Flying
public string HacksFlags;

string GetFlagValue(string flag) {
Expand Down
93 changes: 0 additions & 93 deletions ClassicalSharp/Entities/Components/InputComponent.cs

This file was deleted.

28 changes: 13 additions & 15 deletions ClassicalSharp/Entities/Components/PhysicsComponent.cs
Expand Up @@ -7,7 +7,7 @@

namespace ClassicalSharp.Entities {

/// <summary> Entity component that performs collision detection. </summary>
/// <summary> Entity component that performs collisions. </summary>
public sealed class PhysicsComponent {

bool useLiquidGravity = false; // used by BlockDefinitions.
Expand All @@ -32,7 +32,7 @@ public sealed class PhysicsComponent {
int dir = (hacks.FlyingUp || jumping) ? 1 : (hacks.FlyingDown ? -1 : 0);

entity.Velocity.Y += 0.12f * dir;
if (hacks.Speeding && hacks.CanSpeed) entity.Velocity.Y += 0.12f * dir;
if (hacks.Speeding && hacks.CanSpeed) entity.Velocity.Y += 0.12f * dir;
if (hacks.HalfSpeeding && hacks.CanSpeed) entity.Velocity.Y += 0.06f * dir;
} else if (jumping && entity.TouchesAnyRope() && entity.Velocity.Y > 0.02f) {
entity.Velocity.Y = 0.02f;
Expand Down Expand Up @@ -60,19 +60,20 @@ public sealed class PhysicsComponent {
if (!pastJumpPoint) {
canLiquidJump = true;
entity.Velocity.Y += 0.04f;
if (hacks.Speeding && hacks.CanSpeed) entity.Velocity.Y += 0.04f;
if (hacks.Speeding && hacks.CanSpeed) entity.Velocity.Y += 0.04f;
if (hacks.HalfSpeeding && hacks.CanSpeed) entity.Velocity.Y += 0.02f;
} else if (pastJumpPoint) {
// either A) jump bob in water B) climb up solid on side
if (collisions.HorizontalCollision)
// either A) climb up solid on side B) jump bob in water
if (collisions.HorizontalCollision) {
entity.Velocity.Y += touchLava ? 0.30f : 0.13f;
else if (canLiquidJump)
} else if (canLiquidJump) {
entity.Velocity.Y += touchLava ? 0.20f : 0.10f;
}
canLiquidJump = false;
}
} else if (useLiquidGravity) {
entity.Velocity.Y += 0.04f;
if (hacks.Speeding && hacks.CanSpeed) entity.Velocity.Y += 0.04f;
if (hacks.Speeding && hacks.CanSpeed) entity.Velocity.Y += 0.04f;
if (hacks.HalfSpeeding && hacks.CanSpeed) entity.Velocity.Y += 0.02f;
canLiquidJump = false;
} else if (entity.TouchesAnyRope()) {
Expand All @@ -87,7 +88,7 @@ public sealed class PhysicsComponent {
if (jumpVel == 0 || hacks.MaxJumps == 0) return;

entity.Velocity.Y = jumpVel;
if (hacks.Speeding && hacks.CanSpeed) entity.Velocity.Y += jumpVel;
if (hacks.Speeding && hacks.CanSpeed) entity.Velocity.Y += jumpVel;
if (hacks.HalfSpeeding && hacks.CanSpeed) entity.Velocity.Y += jumpVel / 2;
canLiquidJump = false;
}
Expand Down Expand Up @@ -152,17 +153,14 @@ public sealed class PhysicsComponent {

bool OnIce(Entity entity) {
Vector3 under = entity.Position; under.Y -= 0.01f;
if (BlockInfo.ExtendedCollide[GetBlock(under)] == CollideType.Ice) return true;
BlockID blockUnder = game.World.SafeGetBlock(Vector3I.Floor(under));
if (BlockInfo.ExtendedCollide[blockUnder] == CollideType.Ice) return true;

AABB bounds = entity.Bounds;
bounds.Min.Y -= 0.01f; bounds.Max.Y = bounds.Min.Y;
return entity.TouchesAny(bounds, touchesSlipperyIce);
}

BlockID GetBlock(Vector3 coords) {
return game.World.SafeGetBlock(Vector3I.Floor(coords));
}

static Predicate<BlockID> touchesSlipperyIce = IsSlipperyIce;
static bool IsSlipperyIce(BlockID b) { return BlockInfo.ExtendedCollide[b] == CollideType.SlipperyIce; }

Expand Down Expand Up @@ -227,7 +225,7 @@ public sealed class PhysicsComponent {
Vector3I max = Vector3I.Floor(bounds.Max);
float modifier = inf;

AABB blockBB = default(AABB);
AABB blockBB;
min.X = min.X < 0 ? 0 : min.X; max.X = max.X > game.World.MaxX ? game.World.MaxX : max.X;
min.Y = min.Y < 0 ? 0 : min.Y; max.Y = max.Y > game.World.MaxY ? game.World.MaxY : max.Y;
min.Z = min.Z < 0 ? 0 : min.Z; max.Z = max.Z > game.World.MaxZ ? game.World.MaxZ : max.Z;
Expand All @@ -237,7 +235,7 @@ public sealed class PhysicsComponent {
for (int x = min.X; x <= max.X; x++)
{
BlockID block = game.World.GetBlock(x, y, z);
if (block == 0) continue;
if (block == Block.Air) continue;
byte collide = BlockInfo.Collide[block];
if (collide == CollideType.Solid && !checkSolid) continue;

Expand Down
2 changes: 1 addition & 1 deletion ClassicalSharp/Entities/Entity.cs
Expand Up @@ -170,7 +170,7 @@ public abstract class Entity {
Vector3I min = Vector3I.Floor(bounds.Min);
Vector3I max = Vector3I.Floor(bounds.Max);

AABB blockBB = default(AABB);
AABB blockBB;
Vector3 v;
min.X = min.X < 0 ? 0 : min.X; max.X = max.X > game.World.MaxX ? game.World.MaxX : max.X;
min.Y = min.Y < 0 ? 0 : min.Y; max.Y = max.Y > game.World.MaxY ? game.World.MaxY : max.Y;
Expand Down

0 comments on commit bfe4bc0

Please sign in to comment.