Major refactor and organization, optimizations to chunk, world and renderer
This commit is contained in:
118
Entities/Entity.cs
Normal file
118
Entities/Entity.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using OpenTK.Mathematics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Voxel.Core;
|
||||
using Voxel.Physics;
|
||||
|
||||
namespace Voxel.Entities
|
||||
{
|
||||
public class Entity
|
||||
{
|
||||
public Vector3 Position;
|
||||
public Vector3 Velocity;
|
||||
public bool OnGround;
|
||||
public float Rotation;
|
||||
|
||||
public float Width;
|
||||
public float Height;
|
||||
|
||||
private float _gravity = 0.08f;
|
||||
private float _terminalVelocity = -3.92f;
|
||||
private float _airMultiplier = 0.91f;
|
||||
private float _yMultiplier = 0.98f;
|
||||
private float _groundMultiplier = 0.6f;
|
||||
|
||||
protected World _world;
|
||||
|
||||
public Entity(Vector3 position, float width, float height, World world)
|
||||
{
|
||||
Position = position;
|
||||
Width = width;
|
||||
Height = height;
|
||||
_world = world;
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
Move();
|
||||
|
||||
if (!OnGround)
|
||||
{
|
||||
Vector3 acceleration = new Vector3(0, -_gravity, 0);
|
||||
Velocity += acceleration;
|
||||
Velocity.X *= _airMultiplier;
|
||||
Velocity.Z *= _airMultiplier;
|
||||
Velocity.Y *= _yMultiplier;
|
||||
|
||||
|
||||
if (Velocity.Y < _terminalVelocity)
|
||||
{
|
||||
Velocity.Y = _terminalVelocity;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Velocity = new Vector3(Velocity.X * _groundMultiplier, 0f, Velocity.Z * _groundMultiplier);
|
||||
}
|
||||
}
|
||||
|
||||
public void Move()
|
||||
{
|
||||
Vector3 moveVector = Velocity;
|
||||
Vector3 originalVector = moveVector;
|
||||
|
||||
AABB body = AABB.FromCenter(Position, Width, Height, Width);
|
||||
|
||||
List<AABB> collisionChecks = _world.GetColliders(body.Expand(Velocity.X, Velocity.Y, Velocity.Z).Grow(1,1,1));
|
||||
|
||||
foreach (AABB collider in collisionChecks)
|
||||
{
|
||||
moveVector.X = body.GetClipX(collider, moveVector.X);
|
||||
}
|
||||
|
||||
body.Move(moveVector.X, 0, 0);
|
||||
|
||||
foreach (AABB collider in collisionChecks)
|
||||
{
|
||||
moveVector.Y = body.GetClipY(collider, moveVector.Y);
|
||||
}
|
||||
|
||||
body.Move(0, moveVector.Y, 0);
|
||||
|
||||
foreach (AABB collider in collisionChecks)
|
||||
{
|
||||
moveVector.Z = body.GetClipZ(collider, moveVector.Z);
|
||||
}
|
||||
|
||||
body.Move(0, 0, moveVector.Z);
|
||||
|
||||
if (moveVector.X != originalVector.X)
|
||||
{
|
||||
Velocity.X = 0;
|
||||
}
|
||||
|
||||
if (moveVector.Y != originalVector.Y)
|
||||
{
|
||||
Velocity.Y = 0;
|
||||
}
|
||||
|
||||
if (moveVector.Z != originalVector.Z)
|
||||
{
|
||||
Velocity.Z = 0;
|
||||
}
|
||||
|
||||
OnGround = moveVector.Y != originalVector.Y && originalVector.Y < 0;
|
||||
|
||||
Position = body.GetCenter();
|
||||
}
|
||||
|
||||
public void ApplyImpulse(Vector3 force)
|
||||
{
|
||||
Velocity += force;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user