add box2D colliders simple impl, some little tweaks, fixed camera bug
This commit is contained in:
@ -11,11 +11,17 @@ namespace Example
|
||||
class PlayerCube : Entity
|
||||
{
|
||||
public float HorizontalForce = 10.0f;
|
||||
public float VerticalForce = 10.0f;
|
||||
public float JumpForce = 10.0f;
|
||||
|
||||
private RigidBody2DComponent m_PhysicsBody;
|
||||
private MaterialInstance m_MeshMaterial;
|
||||
|
||||
int m_CollisionCounter = 0;
|
||||
|
||||
public Vec2 MaxSpeed = new Vec2();
|
||||
|
||||
private bool Colliding => m_CollisionCounter > 0;
|
||||
|
||||
void OnCreate()
|
||||
{
|
||||
m_PhysicsBody = GetComponent<RigidBody2DComponent>();
|
||||
@ -23,23 +29,46 @@ namespace Example
|
||||
MeshComponent meshComponent = GetComponent<MeshComponent>();
|
||||
m_MeshMaterial = meshComponent.Mesh.GetMaterial(0);
|
||||
m_MeshMaterial.Set("u_Metalness", 0.0f);
|
||||
|
||||
AddCollision2DBeginCallback(OnPlayerCollisionBegin);
|
||||
AddCollision2DEndCallback(OnPlayerCollisionEnd);
|
||||
}
|
||||
|
||||
|
||||
void OnPlayerCollisionBegin(float value)
|
||||
{
|
||||
m_CollisionCounter++;
|
||||
}
|
||||
|
||||
void OnPlayerCollisionEnd(float value)
|
||||
{
|
||||
m_CollisionCounter--;
|
||||
}
|
||||
|
||||
void OnUpdate(float ts)
|
||||
{
|
||||
float movementForce = HorizontalForce;
|
||||
|
||||
if (!Colliding)
|
||||
{
|
||||
movementForce *= 0.4f;
|
||||
}
|
||||
if (Input.IsKeyPressed(KeyCode.D))
|
||||
m_PhysicsBody.ApplyLinearImpulse(new Vec2(HorizontalForce, 0), new Vec2(), true);
|
||||
m_PhysicsBody.ApplyLinearImpulse(new Vec2(movementForce, 0), new Vec2(), true);
|
||||
else if (Input.IsKeyPressed(KeyCode.A))
|
||||
m_PhysicsBody.ApplyLinearImpulse(new Vec2(-HorizontalForce, 0), new Vec2(), true);
|
||||
m_PhysicsBody.ApplyLinearImpulse(new Vec2(-movementForce, 0), new Vec2(), true);
|
||||
|
||||
if (Input.IsKeyPressed(KeyCode.Space))
|
||||
m_PhysicsBody.ApplyLinearImpulse(new Vec2(0, VerticalForce), new Vec2(0, -10), true);
|
||||
if (Colliding && Input.IsKeyPressed(KeyCode.Space))
|
||||
m_PhysicsBody.ApplyLinearImpulse(new Vec2(0, JumpForce), new Vec2(0, 0), true);
|
||||
|
||||
Vec3 color = new Vec3(0.8f, 0.8f, 0.8f);
|
||||
if (Input.IsKeyPressed(KeyCode.Q))
|
||||
color = new Vec3(0.0f, 1.0f, 0.0f);
|
||||
if (m_CollisionCounter > 0)
|
||||
m_MeshMaterial.Set("u_AlbedoColor", new Vec3(1.0f, 0.0f, 0.0f));
|
||||
else
|
||||
m_MeshMaterial.Set("u_AlbedoColor", new Vec3(0.8f, 0.8f, 0.8f));
|
||||
|
||||
m_MeshMaterial.Set("u_AlbedoColor", color);
|
||||
Vec2 linearVelocity = m_PhysicsBody.GetLinearVelocity();
|
||||
linearVelocity.Clamp(new Vec2(-MaxSpeed.X, -1000), MaxSpeed);
|
||||
m_PhysicsBody.SetLinearVelocity(linearVelocity);
|
||||
|
||||
if (Input.IsKeyPressed(KeyCode.R))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user