using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Prism; namespace Example { class PlayerSphere : Entity { public float HorizontalForce = 10.0f; public float JumpForce = 10.0f; private RigidBodyComponent m_PhysicsBody; private MaterialInstance m_MeshMaterial; public int m_CollisionCounter = 0; public Vec3 MaxSpeed = new Vec3(); private bool Colliding => m_CollisionCounter > 0; void OnCreate() { m_PhysicsBody = GetComponent(); MeshComponent meshComponent = GetComponent(); m_MeshMaterial = meshComponent.Mesh.GetMaterial(0); m_MeshMaterial.Set("u_Metalness", 0.0f); AddCollisionBeginCallback(OnPlayerCollisionBegin); AddCollisionEndCallback(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; } Vec3 forward = GetForwardDirection(); Vec3 right = GetRightDirection(); Vec3 up = GetUpDirection(); if (Input.IsKeyPressed(KeyCode.W)) m_PhysicsBody.AddForce(forward * movementForce); else if (Input.IsKeyPressed(KeyCode.S)) m_PhysicsBody.AddForce(forward * -movementForce); if (Input.IsKeyPressed(KeyCode.D)) m_PhysicsBody.AddForce(right * movementForce); else if (Input.IsKeyPressed(KeyCode.A)) m_PhysicsBody.AddForce(right * -movementForce); if (Colliding && Input.IsKeyPressed(KeyCode.Space)) m_PhysicsBody.AddForce(up * JumpForce); if (Colliding) 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)); Vec3 linearVelocity = m_PhysicsBody.GetLinearVelocity(); linearVelocity.Clamp(new Vec3(-MaxSpeed.X, -1000, -MaxSpeed.Z), MaxSpeed); m_PhysicsBody.SetLinearVelocity(linearVelocity); if (Input.IsKeyPressed(KeyCode.R)) { Mat4 transform = GetTransform(); transform.Translation = new Vec3(0.0f); SetTransform(transform); } } } }