This is a system that adds a Smart Component script object to an entity.
This is a system that runs script update loops only for gameplay (not editors).
This is a system that runs script update loops only for editors.
Here is an example script that utilizes input features and the OnUpdate loop to create a moving player.
using System;
{
#region Public Fields
public float speed = 10.0f;
#endregion
#region Private Fields
private bool isFirstFrame = true;
#endregion
#region Event Methods
public override void OnUpdate() {
if (isFirstFrame)
{
Grindstone.Input.InputManager.CursorMode =
Grindstone.
Input.CursorMode.Disabled;
Grindstone.Input.InputManager.IsMouseRawMotion = true;
}
if (!isWindowFocused)
{
return;
}
{
window.Close();
}
Float2 halfWindowSize = window.Size / 2.0f;
if (isFirstFrame) {
Grindstone.Input.InputManager.MousePosition = halfWindowSize;
isFirstFrame = false;
return;
}
Float2 mousePos = Grindstone.Input.InputManager.MousePosition;
Float2 lookVec = new Float2(
(halfWindowSize.x - mousePos.x) / halfWindowSize.x,
(halfWindowSize.y - mousePos.y) / halfWindowSize.y
);
Grindstone.Input.InputManager.MousePosition = halfWindowSize;
bool w = Grindstone.Input.InputManager.IsKeyDown(Grindstone.Input.KeyboardKey.W);
bool s = Grindstone.Input.InputManager.IsKeyDown(Grindstone.Input.KeyboardKey.S);
bool a = Grindstone.Input.InputManager.IsKeyDown(Grindstone.Input.KeyboardKey.A);
bool d = Grindstone.Input.InputManager.IsKeyDown(Grindstone.Input.KeyboardKey.D);
float fwd = (w ? 1.0f : 0.0f) - (s ? 1.0f : 0.0f);
float rgt = (d ? 1.0f : 0.0f) - (a ? 1.0f : 0.0f);
var transf = entity.GetTransformComponent();
Float3 movementDirection =
transf.Forward * fwd +
transf.Right * rgt;
float dt = (float)Grindstone.Time.GetDeltaTime();
Float3 offset = movementDirection * dt * speed;
float mouseSensitivity = 20.0f;
lookEuler.pitch += mouseSensitivity * lookVec.x * dt;
lookEuler.yaw += mouseSensitivity * lookVec.y * dt;
const float maxViewAngle = (float)Math.PI * 0.45f;
if (lookEuler.yaw > maxViewAngle)
{
lookEuler.yaw = maxViewAngle;
}
if (lookEuler.yaw < -maxViewAngle)
{
lookEuler.yaw = -maxViewAngle;
}
transf.Rotation = new Quaternion(lookEuler);
transf.Position += offset;
}
#endregion
}
Definition SmartComponent.cs:2
Definition ArchiveContentFile.hpp:8
Definition InputManager.cpp:52
Definition EulerAngles.cs:6