Changeset 20243 for lang/haskell

Show
Ignore:
Timestamp:
09/30/08 07:05:13 (2 months ago)
Author:
mokehehe
Message:

ブロックを叩けるようにしてみた(仮)

Location:
lang/haskell/nario
Files:
2 added
5 modified

Legend:

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

    r20233 r20243  
    2020        | ImgCloud00 | ImgCloud01 | ImgCloud02 | ImgCloud10 | ImgCloud11 | ImgCloud12 
    2121        | ImgDk00 | ImgDk01 | ImgDk10 | ImgDk11 
    22         | ImgGrass00 | ImgGrass01 | ImgGrass02 
     22        | ImgGrass0 | ImgGrass1 | ImgGrass2 
    2323        | ImgPole0 | ImgPole1 
    2424        | ImgFont 
     
    5959imageFn ImgDk10 = "dk10.bmp" 
    6060imageFn ImgDk11 = "dk11.bmp" 
    61 imageFn ImgGrass00 = "grass00.bmp" 
    62 imageFn ImgGrass01 = "grass01.bmp" 
    63 imageFn ImgGrass02 = "grass02.bmp" 
     61imageFn ImgGrass0 = "grass0.bmp" 
     62imageFn ImgGrass1 = "grass1.bmp" 
     63imageFn ImgGrass2 = "grass2.bmp" 
    6464imageFn ImgPole0 = "pole0.bmp" 
    6565imageFn ImgPole1 = "pole1.bmp" 
     
    7474        ImgCloud00, ImgCloud01, ImgCloud02, ImgCloud10, ImgCloud11, ImgCloud12, 
    7575        ImgDk00, ImgDk01, ImgDk10, ImgDk11, 
    76         ImgGrass00, ImgGrass01, ImgGrass02, 
     76        ImgGrass0, ImgGrass1, ImgGrass2, 
    7777        ImgPole0, ImgPole1, 
    7878        ImgFont, 
  • lang/haskell/nario/Field.hs

    r20233 r20243  
     1 
     2-- フィールド 
     3{- 
     4        ' '             空白 
     5        '*'             見えない壁(壁を下から叩いたときに一時的に見えない壁に置き換える) 
     6-} 
    17 
    28module Field ( 
     
    410        loadField, 
    511        fieldRef, 
     12        fieldSet, 
    613        isBlock, 
    714        renderField 
     
    4148chr2img '5' = ImgCloud11 
    4249chr2img '6' = ImgCloud12 
    43 chr2img '7' = ImgGrass00 
    44 chr2img '8' = ImgGrass01 
    45 chr2img '9' = ImgGrass02 
     50chr2img '7' = ImgGrass0 
     51chr2img '8' = ImgGrass1 
     52chr2img '9' = ImgGrass2 
    4653chr2img '[' = ImgDk00 
    4754chr2img ']' = ImgDk01 
     
    5259 
    5360 
     61 
    5462isBlock :: Cell -> Bool 
    55 isBlock c = c `elem` "@OX?[]l|" 
     63isBlock c = c `elem` "@OX?[]l|*" 
    5664 
    5765inField :: Field -> Int -> Int -> Bool 
     
    6371        | otherwise                     = ' ' 
    6472 
     73fieldSet :: Field -> Int -> Int -> Cell -> Field 
     74fieldSet fld x y c 
     75        | inField fld x y       = replace fld y $ replace (fld !! y) x c 
     76        | otherwise                     = fld 
     77 
    6578 
    6679renderField sur imgres scrx fld = 
     
    6881        where 
    6982                lineProc (y, ln) = map (cellProc y) $ zip [0..] $ window ln 
    70                 cellProc _ (_, ' ') = return () 
    71                 cellProc y (x, c) = putchr x y c >> return () 
     83                cellProc y (x, c) 
     84                        | c `elem` " *" = return () 
     85                        | otherwise             = putchr x y c >> return () 
    7286                putchr x y c = blitSurface (getImageSurface imgres $ chr2img c) Nothing sur $ pt (x*chrSize - rx) (y*chrSize - 8) 
    7387 
  • lang/haskell/nario/Main.hs

    r20233 r20243  
    33module Main where 
    44 
    5 import Multimedia.SDL 
     5import Multimedia.SDL hiding (Event) 
    66import System.IO.Unsafe (unsafeInterleaveIO) 
    77import Control.Concurrent (threadDelay) 
     
    1414import Const 
    1515import Font 
     16import Event 
     17import Actor 
    1618 
    1719wndTitle = "NARIO in Haskell" 
     
    7173---- 
    7274 
     75 
    7376-- 状態 
    7477data GameState = GameState { 
    7578        pl :: Player, 
    76         fld :: Field 
     79        fld :: Field, 
     80        actors :: [Actor] 
    7781        } 
    7882 
     
    8286        return GameState { 
    8387                pl = newPlayer, 
    84                 fld = fldmap 
     88                fld = fldmap, 
     89                actors = [] 
    8590                } 
    8691        where 
    8792                stage = 0 
     93 
    8894 
    8995-- キー入力を処理して描画コマンドを返す 
     
    99105                loop bef gs (ks:kss) = scr' : loop ks gs' kss 
    100106                        where 
    101                                 (scr', gs') = update kp gs 
     107                                (scr', gs') = updateProc kp gs 
    102108                                kp = keyProc bef ks 
    103109 
    104110-- 更新 
    105 update :: KeyProc -> GameState -> (ImageResource -> Scr, GameState) 
    106 update kp gs = (render gs', gs') 
     111updateProc :: KeyProc -> GameState -> (ImageResource -> Scr, GameState) 
     112updateProc kp gs = (renderProc gs', gs') 
    107113        where 
    108                 gs' = gs { pl = updatePlayer kp (fld gs) (pl gs) } 
     114                (pl', ev) = updatePlayer kp (fld gs) (pl gs) 
     115                actors_updates = map updateActor (actors gs) 
     116                actors' = map fst actors_updates 
     117                ev' = concatMap snd actors_updates 
     118 
     119                gstmp = gs { pl = pl', actors = actors' } 
     120                gs' = procEvent gstmp (ev ++ ev') 
     121 
     122-- イベントを処理 
     123procEvent :: GameState -> [Event] -> GameState 
     124procEvent gs ev = foldl f gs ev 
     125        where 
     126                f gs (EvHitBlock imgtype cx cy) = gs { fld = fld', actors = actors' } 
     127                        where 
     128                                fld' = fieldSet (fld gs) cx cy '*' 
     129                                actors' = (newAnimBlock cx cy) : actors gs 
     130                f gs (EvSetField cx cy c) = gs { fld = fld' } 
     131                        where 
     132                                fld' = fieldSet (fld gs) cx cy c 
     133 
    109134 
    110135-- 描画 
    111 render :: GameState -> ImageResource -> Scr 
    112 render gs imgres sur = do 
     136renderProc :: GameState -> ImageResource -> Scr 
     137renderProc gs imgres sur = do 
    113138        fillRect sur Nothing backColor 
    114139 
     
    117142        renderField sur imgres scrx (fld gs) 
    118143        renderPlayer sur imgres scrx (pl gs) 
     144 
     145        mapM_ (\act -> renderActor act imgres scrx sur) (actors gs) 
    119146 
    120147        renderInfo gs imgres sur 
  • lang/haskell/nario/Player.hs

    r20233 r20243  
    1010) where 
    1111 
    12 import Multimedia.SDL 
     12import Multimedia.SDL hiding (Event) 
    1313 
    1414import Util 
     
    1616import Const 
    1717import Field 
     18import Event 
    1819 
    1920 
     
    6364        [ImgNarioRStand, ImgNarioRWalk1, ImgNarioRWalk2, ImgNarioRWalk3, ImgNarioRJump, ImgNarioRSlip] 
    6465        ] 
    65  
    66  
    67 cellCrd :: Int -> Int 
    68 cellCrd x = x `div` (chrSize * one) 
    6966 
    7067 
     
    125122                        | otherwise                                             = player 
    126123                        where 
    127                                 cx = cellCrd (x player + dx * chrSize `div` 2 * one) 
     124                                cx = cellCrd (x player + ofsx dx) 
    128125                                cy = cellCrd (y player - chrSize `div` 2 * one) 
     126                                ofsx (-1) = -6 * one 
     127                                ofsx   1  =  5 * one 
    129128 
    130129 
     
    153152                isGround y = isBlock $ fieldRef fld (cellCrd $ x player) (cellCrd y) 
    154153 
    155  
    156154-- 上をチェック 
    157 checkCeil :: Field -> Player -> Player 
     155checkCeil :: Field -> Player -> (Player, [Event]) 
    158156checkCeil fld player 
    159         | stand player || vy player >= 0 || not isCeil  = player 
    160         | otherwise = player { vy = 0 } 
     157        | stand player || vy player >= 0 || not isCeil  = (player, []) 
     158        | otherwise = (player { vy = 0 }, [EvHitBlock ImgBlock2 cx cy]) 
    161159        where 
    162160                ytmp = y player - one * chrSize 
    163161 
    164                 isCeil = isBlock $ fieldRef fld (cellCrd $ x player) (cellCrd ytmp) 
     162                cx = cellCrd $ x player 
     163                cy = cellCrd ytmp 
     164                isCeil = isBlock $ fieldRef fld cx cy 
    165165                yground y = (cellCrd y) * (chrSize * one) 
    166166 
     
    174174 
    175175-- 更新処理 
    176 updatePlayer :: KeyProc -> Field -> Player -> Player 
     176updatePlayer :: KeyProc -> Field -> Player -> (Player, [Event]) 
    177177updatePlayer kp fld player = 
    178178        moveY $ checkX fld $ moveX kp player 
    179179        where 
    180                 moveY = doJump kp . checkFloor fld . checkCeil fld . fall kp 
     180                moveY = checkCeil fld . doJump kp . checkFloor fld . fall kp 
    181181 
    182182-- スクロール位置取得 
  • lang/haskell/nario/Util.hs

    r20174 r20243  
    1010 
    1111-- ユーティリティ関数 
     12 
     13-- |Replace i-th element of list to v. 
     14replace :: [a] -> Int -> a -> [a] 
     15replace ls i v = take i ls ++ [v] ++ drop  (i + 1) ls 
    1216 
    1317-- 符号を返す 
     
    9498getImageSurface :: ImageResource -> ImageType -> Surface 
    9599getImageSurface imgres t = fromJust $ lookup t imgres 
     100 
     101 
     102-- 固定座標系からセル座標系に 
     103cellCrd :: Int -> Int 
     104cellCrd x = x `div` (chrSize * one)