Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9,337 changes: 4,603 additions & 4,734 deletions Assets/Scenes/SimulateKeyboardDemoScene.unity

Large diffs are not rendered by default.

101 changes: 101 additions & 0 deletions Assets/Tests/Demo/DemoKeyboardMover.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#if ULOOP_HAS_INPUT_SYSTEM
#nullable enable
using UnityEngine;
using UnityEngine.InputSystem;

namespace io.github.hatayama.UnityCliLoop
{
public class DemoKeyboardMover : MonoBehaviour
{
private const float MoveSpeed = 3f;
private const float SprintMultiplier = 2f;
private const float GroundY = 0.5f;
private const float JumpY = 1.5f;

private Renderer _renderer = null!;
private Vector3 _initialPosition;

private void Awake()
{
_renderer = GetComponent<Renderer>();
Debug.Assert(_renderer != null, "DemoKeyboardMover requires a Renderer");
_initialPosition = transform.position;
}

private void Update()
{
Keyboard? keyboard = Keyboard.current;
if (keyboard == null)
{
return;
}

Vector3 direction = ReadDirection(keyboard);
float speed = ReadSpeed(keyboard);
transform.position += direction * speed * Time.deltaTime;
ApplyVerticalInput(keyboard);
ApplyColor(direction);
}

public void ResetPosition()
{
transform.position = _initialPosition;
ApplyColor(Vector3.zero);
}

private static Vector3 ReadDirection(Keyboard keyboard)
{
Vector3 direction = Vector3.zero;

if (keyboard[Key.W].isPressed || keyboard[Key.UpArrow].isPressed)
{
direction.z += 1f;
}

if (keyboard[Key.S].isPressed || keyboard[Key.DownArrow].isPressed)
{
direction.z -= 1f;
}

if (keyboard[Key.A].isPressed || keyboard[Key.LeftArrow].isPressed)
{
direction.x -= 1f;
}

if (keyboard[Key.D].isPressed || keyboard[Key.RightArrow].isPressed)
{
direction.x += 1f;
}

if (direction.sqrMagnitude > 1f)
{
direction.Normalize();
}

return direction;
}

private static float ReadSpeed(Keyboard keyboard)
{
if (keyboard[Key.LeftShift].isPressed || keyboard[Key.RightShift].isPressed)
{
return MoveSpeed * SprintMultiplier;
}

return MoveSpeed;
}

private void ApplyVerticalInput(Keyboard keyboard)
{
Vector3 position = transform.position;
position.y = keyboard[Key.Space].isPressed || keyboard[Key.Enter].isPressed ? JumpY : GroundY;
transform.position = position;
}

private void ApplyColor(Vector3 direction)
{
_renderer.material.color = direction == Vector3.zero ? Color.white : Color.cyan;
}
}
}
#endif
11 changes: 11 additions & 0 deletions Assets/Tests/Demo/DemoKeyboardMover.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 41 additions & 1 deletion Assets/Tests/Demo/Editor/DemoKeyboardSceneBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class DemoKeyboardSceneBuilder
private const float KEY_GAP = 6f;
private const float ROW_GAP = 6f;
private const string SCENE_PATH = "Assets/Scenes/SimulateKeyboardDemoScene.unity";
private const string CUBE_NAME = "KeyboardInputCube";

private static bool IsMac =>
Application.platform == RuntimePlatform.OSXEditor ||
Expand Down Expand Up @@ -47,7 +48,13 @@ public static void Build()
GameObject cameraGo = new GameObject("Main Camera");
Camera camera = cameraGo.AddComponent<Camera>();
camera.clearFlags = CameraClearFlags.SolidColor;
camera.backgroundColor = new Color(0.1f, 0.1f, 0.1f, 1f);
camera.backgroundColor = new Color(0.08f, 0.09f, 0.12f, 1f);
camera.transform.position = new Vector3(0f, 4f, -7f);
camera.transform.rotation = Quaternion.Euler(30f, 0f, 0f);

CreateDirectionalLight();
CreateGround();
CreateKeyboardInputCube();

GameObject eventSystemGo = new GameObject("EventSystem");
eventSystemGo.AddComponent<EventSystem>();
Expand Down Expand Up @@ -77,6 +84,39 @@ public static void Build()
Debug.Log($"[DemoKeyboardSceneBuilder] Scene saved to {SCENE_PATH}");
}

private static void CreateDirectionalLight()
{
GameObject lightGo = new GameObject("Directional Light");
Light light = lightGo.AddComponent<Light>();
light.type = LightType.Directional;
light.color = new Color(1f, 0.96f, 0.84f);
light.intensity = 1.2f;
lightGo.transform.rotation = Quaternion.Euler(50f, -30f, 0f);
}

private static void CreateGround()
{
GameObject ground = GameObject.CreatePrimitive(PrimitiveType.Plane);
ground.name = "KeyboardInputGround";
ground.transform.position = Vector3.zero;
ground.transform.localScale = new Vector3(5f, 1f, 5f);

Renderer renderer = ground.GetComponent<Renderer>();
renderer.material.color = new Color(0.16f, 0.20f, 0.24f);
}

private static void CreateKeyboardInputCube()
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.name = CUBE_NAME;
cube.transform.position = new Vector3(0f, 0.5f, 0f);
cube.transform.localScale = Vector3.one;

Renderer renderer = cube.GetComponent<Renderer>();
renderer.material.color = Color.white;
cube.AddComponent<DemoKeyboardMover>();
}

private static void CreateTitle(Transform parent)
{
GameObject go = new GameObject("Title");
Expand Down
Loading