move imgui property code and Pysics Actor code to a single file

This commit is contained in:
2026-01-10 14:48:17 +08:00
parent 9e1474e643
commit f857d8e791
16 changed files with 442 additions and 210 deletions

View File

@ -7,9 +7,14 @@ namespace FPSExample
public float WalkingSpeed = 10.0f;
public float RunSpeed = 20.0f;
public float JumpForce = 50.0f;
[NonSerialized]
public float MouseSensitivity = 10.0f;
public float Distance = 5.0f;
public float CameraForwardOffset = 0.5f;
public float CameraYOffset = 0.5f;
private bool m_Colliding = false;
private float m_CurrentSpeed;
@ -21,7 +26,10 @@ namespace FPSExample
private Vec2 m_LastMousePosition;
private float m_CurrentYMovement = 0.0f;
private Vec2 m_MovementDirection = new Vec2(0.0f);
private bool m_ShouldJump = false;
void OnCreate()
{
m_Transform = GetComponent<TransformComponent>();
@ -40,7 +48,6 @@ namespace FPSExample
Input.SetCursorMode(Input.CursorMode.Locked);
}
Collider[] colliders = new Collider[10];
void OnUpdate(float ts)
{
if (Input.IsKeyPressed(KeyCode.Escape) && Input.GetCursorMode() == Input.CursorMode.Locked)
@ -51,32 +58,10 @@ namespace FPSExample
m_CurrentSpeed = Input.IsKeyPressed(KeyCode.LeftControl) ? RunSpeed : WalkingSpeed;
RaycastHit hitInfo;
if (Input.IsKeyPressed(KeyCode.H) &&
Physics.Raycast(m_CameraTransform.Position + (m_CameraTransform.Transform.Forward),
m_CameraTransform.Transform.Forward, 20.0f, out hitInfo))
{
FindEntityByID(hitInfo.EntityID).GetComponent<MeshComponent>().Mesh.GetMaterial(0).Set("u_AlbedoColor", new Vec3(1.0f ,0.0f, 0.0f));
}
if (Input.IsKeyPressed(KeyCode.I))
{
// NOTE: The NonAlloc version of Overlap functions should be used when possible since it doesn't allocate a new array
// whenever you call it. The normal versions allocates a brand new array every time.
int numColliders = Physics.OverlapBoxNonAlloc(m_Transform.Position, new Vec3(1.0f), colliders);
Console.WriteLine("Colliders: {0}", numColliders);
// When using NonAlloc it's not safe to use a foreach loop since some of the colliders may not exist
for (int i = 0; i < numColliders; i++)
{
Console.WriteLine(colliders[i]);
}
}
UpdateRayCasting();
UpdateMovementInput();
UpdateRotation(ts);
UpdateMovement();
UpdateCameraTransform();
}
@ -99,29 +84,82 @@ namespace FPSExample
m_LastMousePosition = currentMousePosition;
}
private void UpdateMovementInput()
{
if (Input.IsKeyPressed(KeyCode.W))
m_MovementDirection.Y += 1.0f;
else if (Input.IsKeyPressed(KeyCode.S))
m_MovementDirection.Y -= 1.0f;
else
m_MovementDirection.Y = 0.0f;
if(Input.IsKeyPressed(KeyCode.A))
m_MovementDirection.X -= 1.0f;
else if (Input.IsKeyPressed(KeyCode.D))
m_MovementDirection.X += 1.0f;
else
m_MovementDirection.X = 0.0f;
m_ShouldJump = Input.IsKeyPressed(KeyCode.Space) && !m_ShouldJump;
}
Collider[] colliders = new Collider[10];
private void UpdateRayCasting()
{
RaycastHit hitInfo;
if (Input.IsKeyPressed(KeyCode.H) &&
Physics.Raycast(m_CameraTransform.Position + (m_CameraTransform.Transform.Forward),
m_CameraTransform.Transform.Forward, 20.0f, out hitInfo))
{
FindEntityByID(hitInfo.EntityID).GetComponent<MeshComponent>().Mesh.GetMaterial(0).Set("u_AlbedoColor", new Vec3(1.0f ,0.0f, 0.0f));
}
if (Input.IsKeyPressed(KeyCode.I))
{
// NOTE: The NonAlloc version of Overlap functions should be used when possible since it doesn't allocate a new array
// whenever you call it. The normal versions allocates a brand new array every time.
int numColliders = Physics.OverlapBoxNonAlloc(m_Transform.Position, new Vec3(1.0f), colliders);
Console.WriteLine("Colliders: {0}", numColliders);
// When using NonAlloc it's not safe to use a foreach loop since some of the colliders may not exist
for (int i = 0; i < numColliders; i++)
{
Console.WriteLine(colliders[i]);
}
}
}
void OnPhysicsUpdate(float fixedTimeStep)
{
UpdateMovement();
}
private void UpdateMovement()
{
m_RigidBody.Rotate(new Vec3(0.0f, m_CurrentYMovement, 0.0f));
if (Input.IsKeyPressed(KeyCode.W))
m_RigidBody.AddForce(m_Transform.Transform.Forward * m_CurrentSpeed);
else if (Input.IsKeyPressed(KeyCode.S))
m_RigidBody.AddForce(m_Transform.Transform.Forward * -m_CurrentSpeed);
if (Input.IsKeyPressed(KeyCode.A))
m_RigidBody.AddForce(m_Transform.Transform.Right * -m_CurrentSpeed);
else if (Input.IsKeyPressed(KeyCode.D))
m_RigidBody.AddForce(m_Transform.Transform.Right * m_CurrentSpeed);
if (Input.IsKeyPressed(KeyCode.Space) && m_Colliding)
m_RigidBody.AddForce(Vec3.Up * JumpForce);
Vec3 movement = m_CameraTransform.Transform.Right * m_MovementDirection.X + m_CameraTransform.Transform.Forward * m_MovementDirection.Y;
if (m_MovementDirection.LengthSquared() != 0.0f)
{
movement.Normalize();
Vec3 velocity = movement * m_CurrentSpeed;
velocity.Y = m_RigidBody.GetLinearVelocity().Y;
m_RigidBody.SetLinearVelocity(velocity);
}
if (m_ShouldJump && m_Colliding)
{
m_RigidBody.AddForce(Vec3.Up * JumpForce, RigidBodyComponent.ForceMode.Impulse);
m_ShouldJump = false;
}
}
private void UpdateCameraTransform(){
Vec3 position = m_Transform.Position;
position.Y += m_Transform.Position.Y + 1.5f;
Vec3 position = m_Transform.Position + m_CameraTransform.Transform.Forward * CameraForwardOffset;
position.Y += m_Transform.Position.Y + CameraYOffset;
m_CameraTransform.Position = position;
}
}