group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
InputSampleSystem.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
9
10#include <SDL3/SDL.h>
11
12#include <algorithm>
13#include <cmath>
14#include <glm/trigonometric.hpp>
15
19namespace systems
20{
21
32inline void runMouseLook(Registry& registry, float mouseSensitivity)
33{
34 float mdx = 0.0f;
35 float mdy = 0.0f;
36 SDL_GetRelativeMouseState(&mdx, &mdy);
37
38 registry.view<InputSnapshot, LocalPlayer>().each([&](InputSnapshot& snap) {
39 // Negate: SDL mdx is positive when moving right, but positive yaw
40 // rotates toward +X which maps to screen-left via glm::lookAt's
41 // cross(forward, up) convention. Negating gives the standard
42 // "mouse right = look right" behaviour.
43 snap.yaw -= mdx * mouseSensitivity;
44
45 // Wrap yaw to [-π, π] to avoid float precision drift over time.
46 snap.yaw = std::remainder(snap.yaw, glm::radians(360.0f));
47
48 // Clamp pitch to avoid gimbal-lock at the poles.
49 snap.pitch = std::clamp(snap.pitch + mdy * mouseSensitivity, -glm::radians(89.0f), glm::radians(89.0f));
50 });
51}
52
60inline void runMovementKeys(Registry& registry)
61{
62 const bool* const kKeys = SDL_GetKeyboardState(nullptr);
63
64 registry.view<InputSnapshot, LocalPlayer>().each([&](InputSnapshot& snap) {
65 snap.forward = kKeys[SDL_SCANCODE_W];
66 snap.back = kKeys[SDL_SCANCODE_S];
67 snap.left = kKeys[SDL_SCANCODE_A];
68 snap.right = kKeys[SDL_SCANCODE_D];
69 snap.jump = kKeys[SDL_SCANCODE_SPACE];
70 snap.crouch = kKeys[SDL_SCANCODE_LCTRL];
71 snap.sprint = kKeys[SDL_SCANCODE_LSHIFT];
72 snap.grapple = kKeys[SDL_SCANCODE_E];
73 });
74}
75
83inline void runWeaponKeys(Registry& registry)
84{
85 const bool* const kKeys = SDL_GetKeyboardState(nullptr);
86 const SDL_MouseButtonFlags mouse = SDL_GetMouseState(nullptr, nullptr);
87
88 registry.view<InputSnapshot, LocalPlayer>().each([&](InputSnapshot& snap) {
89 snap.shooting =
90 (mouse & SDL_BUTTON_LMASK) != 0; // Apply bitmask to mouse input, true if left click is held down.
91 snap.switchToPrimary = kKeys[SDL_SCANCODE_1];
92 snap.switchToSecondary = kKeys[SDL_SCANCODE_2];
93 snap.switchToTertiary = kKeys[SDL_SCANCODE_3];
94 snap.switchToQuaternary = kKeys[SDL_SCANCODE_4];
95 snap.reload = kKeys[SDL_SCANCODE_R];
96 });
97}
98
102inline void runInputSample(Registry& registry, float mouseSensitivity = 0.001f)
103{
104 runMouseLook(registry, mouseSensitivity);
105 runMovementKeys(registry);
106 runWeaponKeys(registry);
107}
108
109} // namespace systems
Per-tick player input snapshot for networking and prediction.
Marker component identifying the locally controlled player entity.
Shared ECS registry type alias for the game engine.
entt::registry Registry
Shared ECS registry type alias.
Definition Registry.hpp:11
Client-only input sampling system — split into two halves so mouse look can run every iterate() (smoo...
Definition InputSampleSystem.hpp:20
void runInputSample(Registry &registry, float mouseSensitivity=0.001f)
Legacy combined sampler — calls both runMouseLook and runMovementKeys.
Definition InputSampleSystem.hpp:102
void runWeaponKeys(Registry &registry)
Sample keyboard state into the weapon flags.
Definition InputSampleSystem.hpp:83
void runMouseLook(Registry &registry, float mouseSensitivity)
Sample mouse delta and accumulate into yaw / pitch.
Definition InputSampleSystem.hpp:32
void runMovementKeys(Registry &registry)
Sample keyboard state into the movement flags.
Definition InputSampleSystem.hpp:60
One tick of player input, stamped with the tick it was sampled on.
Definition InputSnapshot.hpp:17
bool switchToSecondary
Switch to gun in secondary slot.
Definition InputSnapshot.hpp:32
bool grapple
Middle mouse button / E key.
Definition InputSnapshot.hpp:28
bool switchToPrimary
Switch to gun in primary slot.
Definition InputSnapshot.hpp:31
bool jump
Space key.
Definition InputSnapshot.hpp:25
bool switchToQuaternary
Switch to gun in quaternary slot.
Definition InputSnapshot.hpp:34
bool switchToTertiary
Switch to gun in tertiary slot.
Definition InputSnapshot.hpp:33
bool right
D key.
Definition InputSnapshot.hpp:24
bool forward
W key.
Definition InputSnapshot.hpp:21
bool left
A key.
Definition InputSnapshot.hpp:23
bool reload
Reload button.
Definition InputSnapshot.hpp:30
float yaw
Horizontal look angle in radians (accumulated from mouse X deltas).
Definition InputSnapshot.hpp:37
bool crouch
Left Ctrl key.
Definition InputSnapshot.hpp:26
bool shooting
Primary fire button.
Definition InputSnapshot.hpp:29
float pitch
Vertical look angle in radians, clamped to [-89°, +89°] by InputSampleSystem.
Definition InputSnapshot.hpp:38
bool back
S key.
Definition InputSnapshot.hpp:22
bool sprint
Left Shift key.
Definition InputSnapshot.hpp:27
Marker component that tags exactly one entity per client as the locally controlled player.
Definition LocalPlayer.hpp:12