Files
Hazel/Editor/SandboxProject/Source/Player.cs
2025-10-26 13:12:04 +08:00

55 lines
1.4 KiB
C#

using System;
using Hazel;
namespace Sandbox {
public class Player : Entity
{
private TransformComponent m_Transform;
private RigidBody2DComponent m_Rigidbody;
public float Force;
public float Time;
void OnCreate()
{
Console.WriteLine($"Player.OnCreate - {ID}");
m_Transform = GetComponent<TransformComponent>();
m_Rigidbody = GetComponent<RigidBody2DComponent>();
}
void OnUpdate(float ts)
{
Time += ts;
float speed = Force;
Vector3 velocity = Vector3.Zero;
if(Input.IsKeyDown(KeyCode.W))
velocity.Y = 1.0f;
else if(Input.IsKeyDown(KeyCode.S))
velocity.Y = -1.0f;
if(Input.IsKeyDown(KeyCode.A))
velocity.X = -1.0f;
else if(Input.IsKeyDown(KeyCode.D))
velocity.X = 1.0f;
Entity cameraEntity = FindEntityByName("Camera");
if(cameraEntity != null)
{
Camera camera = cameraEntity.As<Camera>();
if(Input.IsKeyDown(KeyCode.Q))
camera.DistanceFromPlayer -= speed * 2.0f * ts;
else if(Input.IsKeyDown(KeyCode.E))
camera.DistanceFromPlayer += speed * 2.0f * ts;
}
m_Rigidbody.ApplyLinearImpulseToCenter(velocity.XY * speed, true);
}
}
}