78 lines
1.5 KiB
Go
78 lines
1.5 KiB
Go
|
package fpscontrols
|
||
|
|
||
|
import (
|
||
|
"github.com/go-gl/glfw/v3.3/glfw"
|
||
|
"github.com/go-gl/mathgl/mgl32"
|
||
|
)
|
||
|
|
||
|
type FPSControls struct {
|
||
|
lookSpeed float32
|
||
|
moveSpeed float32
|
||
|
|
||
|
position mgl32.Vec3
|
||
|
rotation mgl32.Vec2
|
||
|
|
||
|
currentMove mgl32.Vec3
|
||
|
currentLook mgl32.Vec2
|
||
|
lastMousePos mgl32.Vec2
|
||
|
}
|
||
|
|
||
|
var instance *FPSControls
|
||
|
|
||
|
func Get(lookSpeed, moveSpeed float32, position mgl32.Vec3, rotation mgl32.Vec2, window *glfw.Window) *FPSControls {
|
||
|
if instance != nil {
|
||
|
return instance
|
||
|
}
|
||
|
|
||
|
instance = &FPSControls{
|
||
|
moveSpeed: moveSpeed,
|
||
|
lookSpeed: lookSpeed,
|
||
|
position: position,
|
||
|
rotation: rotation,
|
||
|
}
|
||
|
|
||
|
window.SetKeyCallback(func(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
|
||
|
var val float32
|
||
|
|
||
|
switch action {
|
||
|
case glfw.Repeat:
|
||
|
return
|
||
|
case glfw.Press:
|
||
|
val = 1
|
||
|
case glfw.Release:
|
||
|
val = 0
|
||
|
}
|
||
|
|
||
|
switch key {
|
||
|
case glfw.KeyW:
|
||
|
instance.currentMove[2] = -val
|
||
|
case glfw.KeyS:
|
||
|
instance.currentMove[2] = val
|
||
|
case glfw.KeyA:
|
||
|
instance.currentMove[0] = -val
|
||
|
case glfw.KeyD:
|
||
|
instance.currentMove[0] = val
|
||
|
case glfw.KeySpace:
|
||
|
instance.currentMove[1] = -val
|
||
|
case glfw.KeyLeftShift:
|
||
|
instance.currentMove[1] = val
|
||
|
}
|
||
|
})
|
||
|
|
||
|
window.SetCursorPosCallback(func(window *glfw.Window, x float64, y float64) {
|
||
|
pos := mgl32.Vec2{float32(x), float32(y)}
|
||
|
instance.currentLook = instance.lastMousePos.Sub(pos)
|
||
|
instance.lastMousePos = pos
|
||
|
})
|
||
|
|
||
|
return instance
|
||
|
}
|
||
|
|
||
|
func (c *FPSControls) forward() mgl32.Vec3 {
|
||
|
|
||
|
}
|
||
|
|
||
|
func (c *FPSControls) View() mgl32.Mat4 {
|
||
|
|
||
|
}
|