52 lines
1.6 KiB
Odin
52 lines
1.6 KiB
Odin
package ecs
|
|
|
|
import "core:math"
|
|
import "core:math/linalg"
|
|
|
|
CameraSystem :: struct {
|
|
using base: SystemBase,
|
|
}
|
|
|
|
|
|
camera_system_init :: proc(camera_system: ^CameraSystem) {
|
|
|
|
}
|
|
|
|
camera_system_update :: proc(camera_system: ^CameraSystem, coordinator: ^Coordinator, dt: f32) {
|
|
for entity in camera_system.entities {
|
|
camera := coordinator_get_component(CameraComponent, coordinator, entity)
|
|
input := coordinator_get_component(InputComponent, coordinator, entity)
|
|
|
|
move_input: Vec3
|
|
if input.key_down[.W] do move_input.y = 1
|
|
else if input.key_down[.S] do move_input.y = -1
|
|
|
|
if input.key_down[.D] do move_input.x = 1
|
|
else if input.key_down[.A] do move_input.x = -1
|
|
|
|
if input.key_down[.SPACE] do move_input.z = 1
|
|
else if input.key_down[.LEFT_SHIFT] do move_input.z = -1
|
|
|
|
look_input: Vec2 = -input.mouse_movement * camera.look_sensitivity
|
|
camera.look += look_input
|
|
camera.look.x = math.wrap(camera.look.x, 360)
|
|
camera.look.y = math.clamp(camera.look.y, -89.5, 89.5)
|
|
|
|
look_mat := linalg.matrix4_from_yaw_pitch_roll_f32(
|
|
linalg.to_radians(camera.look.x),
|
|
linalg.to_radians(camera.look.y),
|
|
0,
|
|
)
|
|
|
|
forward := (look_mat * Vec4{0, 0, -1, 1}).xyz
|
|
right := (look_mat * Vec4{1, 0, 0, 1}).xyz
|
|
up := (look_mat * Vec4{0, 1, 0, 1}).xyz
|
|
|
|
move_dir := forward * move_input.y + right * move_input.x + up * move_input.z
|
|
motion := linalg.normalize0(move_dir) * camera.movement_speed * dt
|
|
camera.position += motion
|
|
|
|
camera.target = camera.position + forward
|
|
}
|
|
|
|
}
|