-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.cs
More file actions
87 lines (74 loc) · 2.56 KB
/
Copy pathInput.cs
File metadata and controls
87 lines (74 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using SharpEngine.Core.Entities.Views;
using Silk.NET.Input;
namespace Minecraft;
/// <summary>
/// Handles input for the game.
/// </summary>
public class Input
{
/// <summary>Gets or sets the mouse sensitivity of the camera.</summary>
public float MouseSensitivity
{
get => _camera.Settings.MouseSensitivity;
set => _camera.Settings.MouseSensitivity = value;
}
/// <summary>Gets or sets the movement speed of the camera.</summary>
public float MovementSpeed { get; set; } = 1.5f;
private readonly CameraView _camera;
/// <summary>
/// Initializes a new instance of <see cref="Input"/>.
/// </summary>
/// <param name="camera">The camera controlled by the input.</param>
public Input(CameraView camera)
{
_camera = camera;
}
/// <summary>
/// Handles the keyboard input.
/// </summary>
/// <param name="input">The button pressed.</param>
/// <param name="deltaTime">Time since the last frame.</param>
public void HandleKeyboard(IKeyboard input, float deltaTime)
{
if (input.IsKeyPressed(Key.W))
{
_camera.Position += _camera.Front * MovementSpeed * deltaTime; // Forward
}
if (input.IsKeyPressed(Key.S))
{
_camera.Position -= _camera.Front * MovementSpeed * deltaTime; // Backwards
}
if (input.IsKeyPressed(Key.A))
{
_camera.Position -= _camera.Right * MovementSpeed * deltaTime; // Left
}
if (input.IsKeyPressed(Key.D))
{
_camera.Position += _camera.Right * MovementSpeed * deltaTime; // Right
}
if (input.IsKeyPressed(Key.Space))
{
_camera.Position += _camera.Up * MovementSpeed * deltaTime; // Up
}
if (input.IsKeyPressed(Key.ShiftLeft))
{
_camera.Position -= _camera.Up * MovementSpeed * deltaTime; // Down
}
}
/// <summary>
/// Determines whether a number key was pressed.
/// </summary>
/// <param name="key">The key pressed.</param>
/// <param name="number">Output the number pressed. -1 if the key was not a number.</param>
/// <returns><see langword="true"/> if the key was a number key; otherwise, <see langword="false"/>.</returns>
public static bool IsInputNumber(Key key, out int number)
{
number = -1;
if (key is >= Key.Number0 and <= Key.Number9)
{
number = key - Key.Number0;
return true;
}
return false;
}
}