-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHitBox.hs
48 lines (40 loc) · 1.01 KB
/
HitBox.hs
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
{-# LANGUAGE TemplateHaskell #-}
module GameEngine.HitBox
(HitBox(..)
,hitBoxRectangle
,collidesHitBox
,hitBoxBoundaries
,rectPos
,rectSize
)
where
import GameEngine.Position
import GameEngine.Size
import GameEngine.Rectangle
import Control.Lens
import Data.Maybe
import Foreign.C.Types
import Linear
import Linear.Affine
import SDL
-- A hit box
data HitBox
= HitBoxRect
{_hitBoxRectangle :: Rectangle CFloat
}
| NoHitBox
deriving (Eq,Show)
makeLenses ''HitBox
collidesHitBox :: HitBox -> HitBox -> Bool
collidesHitBox h0 h1 = fromMaybe False $ do
V4 left0 right0 top0 bottom0 <- hitBoxBoundaries h0
V4 left1 right1 top1 bottom1 <- hitBoxBoundaries h1
return $ and [left0 < right1
,right0 > left1
,top0 < bottom1
,bottom0 > top1
]
-- leftX,rightX,topY and bottomY positions
hitBoxBoundaries :: HitBox -> Maybe (V4 CFloat)
hitBoxBoundaries NoHitBox = Nothing
hitBoxBoundaries (HitBoxRect r) = Just $ rectBoundaries r