aboutsummaryrefslogtreecommitdiff
path: root/Spear/Math/Entity.hs
blob: 4d29a95cb57f6ba5eca41f60f68c24b3787b9e2f (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
module Spear.Math.Entity
(
    Entity(..)
)
where


import qualified Spear.Math.Matrix3 as M
import qualified Spear.Math.Spatial2 as S
import qualified Spear.Math.Vector as V


-- | An entity in 2D space.
newtype Entity = Entity { transform :: M.Matrix3 }


instance S.Spatial2 Entity where
    move        v ent = ent { transform = M.translv v * transform ent }
    moveFwd     f ent = ent { transform = M.translv (V.scale f $ S.fwd ent) * transform ent }
    moveBack    f ent = ent { transform = M.translv (V.scale (-f) $ S.fwd ent) * transform ent }
    strafeLeft  f ent = ent { transform = M.translv (V.scale (-f) $ S.right ent) * transform ent }
    strafeRight f ent = ent { transform = M.translv (V.scale f $ S.right ent) * transform ent }
    rotate      a ent = ent { transform = transform ent * M.rot a }
    setRotation a ent =
        let t = transform ent
        in ent { transform = M.translation t * M.rot a }
    pos   = M.position . transform
    fwd   = M.forward  . transform
    up    = M.up       . transform
    right = M.right    . transform
    transform (Entity t) = t
    setTransform t (Entity _) = Entity t
    setPos pos (Entity t) = Entity $ M.transform (M.right t) (M.forward t) pos