summaryrefslogtreecommitdiff
path: root/src/Game/Internal/Types.hs
blob: b8e7229dec543942dbbc516326ef57725f8e3356 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedRecordDot #-}

-- |
-- - Module      : Game.Internal.Types
-- - Description :
-- - Copyright   : 2025 Andromeda
-- - License     : BSD 3-clause
-- - Maintainer  : Matrix @Andromeda:tchncs.de
-- - Stability   : Experimental
module Game.Internal.Types
  ( Object (..),
    toGLMatrix,
    Model (camera, objects, cursorDeltaPos, cursorPos, program, keys, wprop),
    mkModel,
    Camera (camPos, camPitch, camYaw, camReference, mouseSensitivity, camVel, strafeStrength, jumpStrength, hasJumped, airTime),
    mkCamera,
    WorldProperties (g, friction, up),
    mkWorldProperties,
  )
where

import qualified Graphics.Rendering.OpenGL as GL
import qualified Graphics.UI.GLFW as GLFW
import Linear (V3 (..), V4 (..))
import qualified Linear as L

-- | represents a single draw call
data Object = Object
  { -- | vao of vertex buffer
    vao :: GL.VertexArrayObject,
    -- | number of vertices
    numIndicies :: GL.NumArrayIndices,
    -- | dimensionallity; vec3, vec4, etc.
    numComponents :: GL.NumComponents,
    -- | primitive mode to be drawn with
    primitiveMode :: GL.PrimitiveMode
  }
  deriving (Show)

-- | converts M44 to a 16array for OpenGL
toGLMatrix :: L.M44 GL.GLfloat -> [GL.GLfloat]
toGLMatrix (V4 (V4 c00 c01 c02 c03) (V4 c10 c11 c12 c13) (V4 c20 c21 c22 c23) (V4 c30 c31 c32 c33)) =
  [ c00,
    c01,
    c02,
    c03,
    c10,
    c11,
    c12,
    c13,
    c20,
    c21,
    c22,
    c23,
    c30,
    c31,
    c32,
    c33
  ]

-- | gamestate
data Model = Model
  { camera :: Camera,
    -- | frame-on-frame delta mouse position
    cursorDeltaPos :: (Double, Double),
    -- | current mouse position
    cursorPos :: (Double, Double),
    -- | currently pressed keys
    keys :: [GLFW.Key],
    -- | draw calls
    objects :: [Object],
    -- | shader program
    program :: GL.Program,
    wprop :: WorldProperties
  }
  deriving (Show)

-- | smart constructor for Model
mkModel :: Camera -> [Object] -> GL.Program -> WorldProperties -> Model
mkModel camera objects program wprop =
  Model camera (0, 0) (0, 0) [] objects program wprop

-- | camera
data Camera = Camera
  { -- | position in world space
    camPos :: V3 Float,
    -- | pitch in radians, up positive
    camPitch :: Float,
    -- | yaw in radians, right positive
    camYaw :: Float,
    -- | reference direction; orientation applied to
    camReference :: V3 Float,
    -- | velocity in world space
    camVel :: V3 Float,
    -- | scale factor for mouse movement
    mouseSensitivity :: Float,
    -- | scale factor for strafe
    strafeStrength :: Float,
    -- | scale factor for jump initial velocity
    jumpStrength :: Float,
    -- | whether the camera still has jumping state
    hasJumped :: Bool,
    -- | time since jumping state entered in seconds
    airTime :: Float
  }
  deriving (Show)

-- | smart constructor for Camera
mkCamera ::
  V3 Float ->
  Float ->
  Float ->
  V3 Float ->
  V3 Float ->
  Float ->
  Float ->
  Float ->
  Camera
mkCamera camPos camPitch camYaw camReference camVel mouseSensitivity strafeStrength jumpStrength =
  Camera
    camPos
    camPitch
    camYaw
    (L.normalize camReference)
    (L.normalize camVel)
    mouseSensitivity
    strafeStrength
    jumpStrength
    False
    0

-- | physical properties of the world
data WorldProperties = WorldProperties
  { -- | gravity `g`
    g :: Float,
    -- | scale factor for floor friction
    friction :: Float,
    -- | global up vector
    up :: V3 Float
  }
  deriving (Show)

-- | smart constructor for WorldProperties
mkWorldProperties :: Float -> Float -> V3 Float -> WorldProperties
mkWorldProperties g friction up = WorldProperties g friction (L.normalize up)