Changeset 20534

Show
Ignore:
Timestamp:
10/03/08 01:04:54 (3 months ago)
Author:
mokehehe
Message:

Existence 型を使って書き換え

Location:
lang/haskell/nario
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lang/haskell/nario/Actor.hs

    r20397 r20534  
     1{-# OPTIONS_GHC -fglasgow-exts #-} 
    12 
    23module Actor where 
     
    1011import Event 
    1112 
    12  
    13 data AnimBlock = AnimBlock { startcy :: Int, x :: Int, y :: Int, vy :: Int } 
    14  
    15 data Actor = ActNull | ActAnimBlock AnimBlock 
     13class Actor a where 
     14        update :: a -> (a, [Event]) 
     15        render :: a -> ImageResource -> Int -> Surface -> IO () 
     16        bDead :: a -> Bool 
    1617 
    1718-- ============================================================================ 
     
    1920--      死亡 
    2021 
    21 updateNull = (ActNull, []) 
     22data AnimNull = AnimNull 
    2223 
    23 renderNull imgres scrx sur = return () 
    24  
    25 bDieNull = True 
     24instance Actor AnimNull where 
     25        update self = (self, []) 
     26        render self imgres scrx sur = return () 
     27        bDead self = True 
    2628 
    2729 
     
    3032--      ブロックを叩いたときのバウンド演出 
    3133 
    32 updateAnimBlock self = result' 
    33         where 
    34                 result' 
    35                         | not bEnd      = (ActAnimBlock $ self { vy = vy', y = y' }, []) 
    36                         | otherwise     = (ActNull, [EvSetField (cellCrd $ x self) (startcy self) '@']) 
     34data AnimBlock = AnimBlock { startcy :: Int, x :: Int, y :: Int, vy :: Int } 
    3735 
    38                 vy' = vy self + gravity 
    39                 y' = y self + vy' 
    40                 bEnd = y' >= startcy self * chrSize * one 
     36instance Actor AnimBlock where 
     37        update self = result' 
     38                where 
     39                        result' 
     40                                | not bEnd      = (self { vy = vy', y = y' }, []) 
     41                                | otherwise     = (self, [EvSetField (cellCrd $ x self) (startcy self) '@']) 
     42 
     43                        vy' = vy self + gravity 
     44                        y' = y self + vy' 
     45                        bEnd = y' >= startcy self * chrSize * one 
     46 
     47        render self imgres scrx sur = do 
     48                blitSurface (getImageSurface imgres ImgBlock2) Nothing sur (pt ((x self) `div` one - scrx) ((y self) `div` one - 8)) 
     49                return () 
     50 
     51        bDead self = False 
    4152 
    4253 
    43 renderAnimBlock self imgres scrx sur = do 
    44         blitSurface (getImageSurface imgres ImgBlock2) Nothing sur (pt ((x self) `div` one - scrx) ((y self) `div` one - 8)) 
    45         return () 
    46  
    47 bDieAnimBlock self = False 
    48  
    49 newAnimBlock cx cy = ActAnimBlock $ AnimBlock { startcy = cy, x = cx * chrSize * one, y = cy * chrSize * one, vy = -3 * one } 
    50  
     54newAnimBlock cx cy = AnimBlock { startcy = cy, x = cx * chrSize * one, y = cy * chrSize * one, vy = -3 * one } 
    5155 
    5256 
     
    5458-- ============================================================================ 
    5559 
     60{- 
    5661updateActor :: Actor -> (Actor, [Event]) 
    5762updateActor ActNull                             = updateNull 
     
    6570bDieActor ActNull                               = bDieNull 
    6671bDieActor (ActAnimBlock a)              = bDieAnimBlock a 
    67  
     72-} 
  • lang/haskell/nario/Images.hs

    r20397 r20534  
    11module Images (ImageType(..), imageTypes, imageFn) where 
    2 import Maybe 
     2import Maybe (fromJust) 
    33data ImageType = ImgBlock1 | ImgBlock2 | ImgBlock3 | ImgBlock4 | ImgBlock5 | ImgCloud00 | ImgCloud01 | ImgCloud02 | ImgCloud10 | ImgCloud11 | ImgCloud12 | ImgDk00 | ImgDk01 | ImgDk10 | ImgDk11 | ImgFlag | ImgFont | ImgGrass0 | ImgGrass1 | ImgGrass2 | ImgMt02 | ImgMt11 | ImgMt12 | ImgMt13 | ImgMt22 | ImgNarioDie | ImgNarioLJump | ImgNarioLSlip | ImgNarioLStand | ImgNarioLWalk1 | ImgNarioLWalk2 | ImgNarioLWalk3 | ImgNarioRJump | ImgNarioRSlip | ImgNarioRStand | ImgNarioRWalk1 | ImgNarioRWalk2 | ImgNarioRWalk3 | ImgPole0 | ImgPole1 | ImgTitle       deriving (Eq) 
    44imageTypes = [ImgBlock1, ImgBlock2, ImgBlock3, ImgBlock4, ImgBlock5, ImgCloud00, ImgCloud01, ImgCloud02, ImgCloud10, ImgCloud11, ImgCloud12, ImgDk00, ImgDk01, ImgDk10, ImgDk11, ImgFlag, ImgFont, ImgGrass0, ImgGrass1, ImgGrass2, ImgMt02, ImgMt11, ImgMt12, ImgMt13, ImgMt22, ImgNarioDie, ImgNarioLJump, ImgNarioLSlip, ImgNarioLStand, ImgNarioLWalk1, ImgNarioLWalk2, ImgNarioLWalk3, ImgNarioRJump, ImgNarioRSlip, ImgNarioRStand, ImgNarioRWalk1, ImgNarioRWalk2, ImgNarioRWalk3, ImgPole0, ImgPole1, ImgTitle] 
  • lang/haskell/nario/Main.hs

    r20453 r20534  
     1{-# OPTIONS_GHC -fglasgow-exts #-} 
     2 
    13-- Nario 
    24 
     
    7476 
    7577---- 
     78data ObjWrapper = forall a. Actor a => ObjWrapper a     -- 存在型aの動く範囲を型クラスDuckに制限 
     79 
     80updateActors :: [ObjWrapper] -> [(ObjWrapper, [Event])] 
     81updateActors = map (\(ObjWrapper x) -> let (x', ev') = update x in (ObjWrapper x', ev')) 
     82 
     83renderActors :: ImageResource -> Int -> Surface -> [ObjWrapper] -> IO () 
     84renderActors imgres ofsx sur = mapM_ (\(ObjWrapper x) -> render x imgres ofsx sur) 
    7685 
    7786 
     
    8190                pl :: Player, 
    8291                fld :: Field, 
    83                 actors :: [Actor], 
     92                actors :: [ObjWrapper], 
    8493                time :: Int 
    8594        } 
     
    134143                                time' = max 0 (time gs - one `div` 25) 
    135144                                (pl', ev) = updatePlayer kp (fld gs) (pl gs) 
    136                                 actors_updates = map updateActor (actors gs) 
     145                                actors_updates = updateActors (actors gs) 
    137146                                actors' = map fst actors_updates 
    138147                                ev' = concatMap snd actors_updates 
     
    153162                        where 
    154163                                fld' = fieldSet (fld gs) cx cy '*' 
    155                                 actors' = (newAnimBlock cx cy) : actors gs 
     164                                actors' = (ObjWrapper $ newAnimBlock cx cy) : actors gs 
    156165                f gs (EvSetField cx cy c) = gs { fld = fld' } 
    157166                        where 
     
    170179        renderPlayer sur imgres scrx (pl gs) 
    171180 
    172         mapM_ (\act -> renderActor act imgres scrx sur) (actors gs) 
     181        renderActors imgres scrx sur (actors gs) 
    173182        return () 
    174183 
  • lang/haskell/nario/tool/listup-imgs.hs

    r20453 r20534  
    4343 
    4444        putStrLn "module Images (ImageType(..), imageTypes, imageFn) where" 
    45         putStrLn "import Maybe" 
     45        putStrLn "import Maybe (fromJust)" 
    4646 
    4747        putStrLn $ "data ImageType = " ++ intercalate " | " symbols ++ "\tderiving (Eq)"