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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
// author: dario
package game
import "core:strings"
import rl "vendor:raylib"
STICK_DEADZONE :: 0.15
// The first gamepad (Lars, 2026-11-21: "controller mode werkt niet"), GTA style: R2 throttle, L2 brake and
// reverse, left stick steers (and drives with up / down), R1 / L1 climb and sink, right stick looks around in the
// street view (climbs on the map), Triangle the street view, Options back. A trigger rests at -1 or at 0
// depending on the controller's mapping: until it has been seen below -0.5 only its upper half counts, so a
// resting trigger never drives. Connecting and disconnecting are logged with the name, so the log shows
// whether raylib knows the controller at all (without a mapping every axis stays 0).
Pad :: struct {
connected: bool,
full_range: [2]bool, // left, right trigger seen at the bottom of -1..1
}
pad_trigger :: proc(w: ^World, axis: rl.GamepadAxis, k: int) -> f32 {
v := rl.GetGamepadAxisMovement(0, axis)
if v < -0.5 do w.pad.full_range[k] = true
return w.pad.full_range[k] ? clamp((v + 1) / 2, 0, 1) : clamp(v, 0, 1)
}
// Once per frame before the controls are read: notices the gamepad coming and going.
pad_poll :: proc(w: ^World) {
on := !w.script.on && rl.IsGamepadAvailable(0)
if on == w.pad.connected do return
w.pad = {
connected = on,
}
if on {
name := strings.clone_from_cstring(rl.GetGamepadName(0), context.temp_allocator)
logf(w, "input", "gamepad 0 connected: %q, %d axes", name, rl.GetGamepadAxisCount(0))
} else {
logf(w, "input", "gamepad 0 disconnected")
}
}
pad_pressed :: proc(w: ^World, b: rl.GamepadButton) -> bool {
return w.pad.connected && rl.IsGamepadButtonPressed(0, b)
}
// Keyboard and the first gamepad add up, so either can be used at any time. Turn is positive to the right,
// stick Y is negative when pushed up, so throttle and climb flip it. In the street view Space and Shift climb
// and sink too (on the map Space fires the focused button).
read_controls :: proc(w: ^World) -> Controls {
c: Controls
if w.street.on {
if key_down(w, .SPACE) do c.climb += 1
if key_down(w, .LEFT_SHIFT) || key_down(w, .LEFT_CONTROL) do c.climb -= 1
}
if key_down(w, .W) || key_down(w, .UP) do c.throttle += 1
if key_down(w, .S) || key_down(w, .DOWN) do c.throttle -= 1
if key_down(w, .D) || key_down(w, .RIGHT) do c.turn += 1
if key_down(w, .A) || key_down(w, .LEFT) do c.turn -= 1
if key_down(w, .R) || key_down(w, .PAGE_UP) do c.climb += 1
if key_down(w, .F) || key_down(w, .PAGE_DOWN) do c.climb -= 1
if w.pad.connected {
c.throttle -= stick_axis(rl.GetGamepadAxisMovement(0, .LEFT_Y))
c.throttle += pad_trigger(w, .RIGHT_TRIGGER, 1) - pad_trigger(w, .LEFT_TRIGGER, 0)
c.turn += stick_axis(rl.GetGamepadAxisMovement(0, .LEFT_X))
if rl.IsGamepadButtonDown(0, .RIGHT_TRIGGER_1) do c.climb += 1
if rl.IsGamepadButtonDown(0, .LEFT_TRIGGER_1) do c.climb -= 1
if !w.street.on do c.climb -= stick_axis(rl.GetGamepadAxisMovement(0, .RIGHT_Y))
}
c.throttle = clamp(c.throttle, -1, 1)
c.turn = clamp(c.turn, -1, 1)
c.climb = clamp(c.climb, -1, 1)
return c
}
// Inside the deadzone is 0, outside it is rescaled so the output still reaches 1.
stick_axis :: proc(v: f32) -> f32 {
a := abs(v)
if a < STICK_DEADZONE do return 0
return clamp(a - STICK_DEADZONE, 0, 1 - STICK_DEADZONE) / (1 - STICK_DEADZONE) * (v < 0 ? -1 : 1)
}
// The game's keys and the street view's mouse look go through these, so a scripted run (debug.odin) drives them
// frame by frame and real keys or mouse moves cannot change its screenshots.
key_down :: proc(w: ^World, k: rl.KeyboardKey) -> bool {
if w.script.on do return w.script.down[k]
return rl.IsKeyDown(k)
}
key_pressed :: proc(w: ^World, k: rl.KeyboardKey) -> bool {
if w.script.on do return w.script.down[k] && !w.script.was[k]
return rl.IsKeyPressed(k)
}
mouse_delta :: proc(w: ^World) -> [2]f32 {
if w.script.on do return w.script.mouse
return rl.GetMouseDelta()
}
mouse_wheel :: proc(w: ^World) -> f32 {
if w.script.on do return w.script.wheel
return rl.GetMouseWheelMove()
}
|