-
Notifications
You must be signed in to change notification settings - Fork 3
/
Actor.hs
46 lines (32 loc) · 1.21 KB
/
Actor.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
{-# LANGUAGE ExistentialQuantification #-}
-- -*- mode: haskell; Encoding: UTF-8 -*-
-- Object that appear in the game
module Actor (
Actor(..),
ActorWrapper(..),
updateActors,
filterActors,
renderActors
) where
import Graphics.UI.SDL (Surface)
import AppUtil (ImageResource, Rect)
import Event (Event)
import Field (Field)
import {-# SOURCE #-} Player (Player)
class Actor a where
update :: Field -> a -> (a, [Event])
render :: a -> ImageResource -> Int -> Surface -> IO ()
bDead :: a -> Bool
bDead _ = False
getHitRect :: a -> Maybe Rect
getHitRect _ = Nothing
onHit :: Player -> a -> (Player, Maybe ActorWrapper, [Event])
onHit pl ac = (pl, Nothing, [])
----
data ActorWrapper = forall a. Actor a => ActorWrapper a -- 存在型aの動く範囲を型クラスに制限
updateActors :: Field -> [ActorWrapper] -> [(ActorWrapper, [Event])]
updateActors fld = map (\(ActorWrapper x) -> let (x', ev') = update fld x in (ActorWrapper x', ev'))
filterActors :: [ActorWrapper] -> [ActorWrapper]
filterActors = filter (\(ActorWrapper x) -> not $ bDead x)
renderActors :: ImageResource -> Int -> Surface -> [ActorWrapper] -> IO ()
renderActors imgres ofsx sur = mapM_ (\(ActorWrapper x) -> render x imgres ofsx sur)