Changeset 20243 for lang/haskell
- Timestamp:
- 09/30/08 07:05:13 (2 months ago)
- Location:
- lang/haskell/nario
- Files:
-
- 2 added
- 5 modified
Legend:
- Unmodified
- Added
- Removed
-
lang/haskell/nario/Const.hs
r20233 r20243 20 20 | ImgCloud00 | ImgCloud01 | ImgCloud02 | ImgCloud10 | ImgCloud11 | ImgCloud12 21 21 | ImgDk00 | ImgDk01 | ImgDk10 | ImgDk11 22 | ImgGrass0 0 | ImgGrass01 | ImgGrass0222 | ImgGrass0 | ImgGrass1 | ImgGrass2 23 23 | ImgPole0 | ImgPole1 24 24 | ImgFont … … 59 59 imageFn ImgDk10 = "dk10.bmp" 60 60 imageFn ImgDk11 = "dk11.bmp" 61 imageFn ImgGrass0 0 = "grass00.bmp"62 imageFn ImgGrass 01 = "grass01.bmp"63 imageFn ImgGrass 02 = "grass02.bmp"61 imageFn ImgGrass0 = "grass0.bmp" 62 imageFn ImgGrass1 = "grass1.bmp" 63 imageFn ImgGrass2 = "grass2.bmp" 64 64 imageFn ImgPole0 = "pole0.bmp" 65 65 imageFn ImgPole1 = "pole1.bmp" … … 74 74 ImgCloud00, ImgCloud01, ImgCloud02, ImgCloud10, ImgCloud11, ImgCloud12, 75 75 ImgDk00, ImgDk01, ImgDk10, ImgDk11, 76 ImgGrass0 0, ImgGrass01, ImgGrass02,76 ImgGrass0, ImgGrass1, ImgGrass2, 77 77 ImgPole0, ImgPole1, 78 78 ImgFont, -
lang/haskell/nario/Field.hs
r20233 r20243 1 2 -- フィールド 3 {- 4 ' ' 空白 5 '*' 見えない壁(壁を下から叩いたときに一時的に見えない壁に置き換える) 6 -} 1 7 2 8 module Field ( … … 4 10 loadField, 5 11 fieldRef, 12 fieldSet, 6 13 isBlock, 7 14 renderField … … 41 48 chr2img '5' = ImgCloud11 42 49 chr2img '6' = ImgCloud12 43 chr2img '7' = ImgGrass0 044 chr2img '8' = ImgGrass 0145 chr2img '9' = ImgGrass 0250 chr2img '7' = ImgGrass0 51 chr2img '8' = ImgGrass1 52 chr2img '9' = ImgGrass2 46 53 chr2img '[' = ImgDk00 47 54 chr2img ']' = ImgDk01 … … 52 59 53 60 61 54 62 isBlock :: Cell -> Bool 55 isBlock c = c `elem` "@OX?[]l| "63 isBlock c = c `elem` "@OX?[]l|*" 56 64 57 65 inField :: Field -> Int -> Int -> Bool … … 63 71 | otherwise = ' ' 64 72 73 fieldSet :: Field -> Int -> Int -> Cell -> Field 74 fieldSet fld x y c 75 | inField fld x y = replace fld y $ replace (fld !! y) x c 76 | otherwise = fld 77 65 78 66 79 renderField sur imgres scrx fld = … … 68 81 where 69 82 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 () 72 86 putchr x y c = blitSurface (getImageSurface imgres $ chr2img c) Nothing sur $ pt (x*chrSize - rx) (y*chrSize - 8) 73 87 -
lang/haskell/nario/Main.hs
r20233 r20243 3 3 module Main where 4 4 5 import Multimedia.SDL 5 import Multimedia.SDL hiding (Event) 6 6 import System.IO.Unsafe (unsafeInterleaveIO) 7 7 import Control.Concurrent (threadDelay) … … 14 14 import Const 15 15 import Font 16 import Event 17 import Actor 16 18 17 19 wndTitle = "NARIO in Haskell" … … 71 73 ---- 72 74 75 73 76 -- 状態 74 77 data GameState = GameState { 75 78 pl :: Player, 76 fld :: Field 79 fld :: Field, 80 actors :: [Actor] 77 81 } 78 82 … … 82 86 return GameState { 83 87 pl = newPlayer, 84 fld = fldmap 88 fld = fldmap, 89 actors = [] 85 90 } 86 91 where 87 92 stage = 0 93 88 94 89 95 -- キー入力を処理して描画コマンドを返す … … 99 105 loop bef gs (ks:kss) = scr' : loop ks gs' kss 100 106 where 101 (scr', gs') = update kp gs107 (scr', gs') = updateProc kp gs 102 108 kp = keyProc bef ks 103 109 104 110 -- 更新 105 update :: KeyProc -> GameState -> (ImageResource -> Scr, GameState)106 update kp gs = (rendergs', gs')111 updateProc :: KeyProc -> GameState -> (ImageResource -> Scr, GameState) 112 updateProc kp gs = (renderProc gs', gs') 107 113 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 -- イベントを処理 123 procEvent :: GameState -> [Event] -> GameState 124 procEvent 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 109 134 110 135 -- 描画 111 render :: GameState -> ImageResource -> Scr112 render gs imgres sur = do136 renderProc :: GameState -> ImageResource -> Scr 137 renderProc gs imgres sur = do 113 138 fillRect sur Nothing backColor 114 139 … … 117 142 renderField sur imgres scrx (fld gs) 118 143 renderPlayer sur imgres scrx (pl gs) 144 145 mapM_ (\act -> renderActor act imgres scrx sur) (actors gs) 119 146 120 147 renderInfo gs imgres sur -
lang/haskell/nario/Player.hs
r20233 r20243 10 10 ) where 11 11 12 import Multimedia.SDL 12 import Multimedia.SDL hiding (Event) 13 13 14 14 import Util … … 16 16 import Const 17 17 import Field 18 import Event 18 19 19 20 … … 63 64 [ImgNarioRStand, ImgNarioRWalk1, ImgNarioRWalk2, ImgNarioRWalk3, ImgNarioRJump, ImgNarioRSlip] 64 65 ] 65 66 67 cellCrd :: Int -> Int68 cellCrd x = x `div` (chrSize * one)69 66 70 67 … … 125 122 | otherwise = player 126 123 where 127 cx = cellCrd (x player + dx * chrSize `div` 2 * one)124 cx = cellCrd (x player + ofsx dx) 128 125 cy = cellCrd (y player - chrSize `div` 2 * one) 126 ofsx (-1) = -6 * one 127 ofsx 1 = 5 * one 129 128 130 129 … … 153 152 isGround y = isBlock $ fieldRef fld (cellCrd $ x player) (cellCrd y) 154 153 155 156 154 -- 上をチェック 157 checkCeil :: Field -> Player -> Player155 checkCeil :: Field -> Player -> (Player, [Event]) 158 156 checkCeil fld player 159 | stand player || vy player >= 0 || not isCeil = player160 | otherwise = player { vy = 0 }157 | stand player || vy player >= 0 || not isCeil = (player, []) 158 | otherwise = (player { vy = 0 }, [EvHitBlock ImgBlock2 cx cy]) 161 159 where 162 160 ytmp = y player - one * chrSize 163 161 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 165 165 yground y = (cellCrd y) * (chrSize * one) 166 166 … … 174 174 175 175 -- 更新処理 176 updatePlayer :: KeyProc -> Field -> Player -> Player176 updatePlayer :: KeyProc -> Field -> Player -> (Player, [Event]) 177 177 updatePlayer kp fld player = 178 178 moveY $ checkX fld $ moveX kp player 179 179 where 180 moveY = doJump kp . checkFloor fld . checkCeilfld . fall kp180 moveY = checkCeil fld . doJump kp . checkFloor fld . fall kp 181 181 182 182 -- スクロール位置取得 -
lang/haskell/nario/Util.hs
r20174 r20243 10 10 11 11 -- ユーティリティ関数 12 13 -- |Replace i-th element of list to v. 14 replace :: [a] -> Int -> a -> [a] 15 replace ls i v = take i ls ++ [v] ++ drop (i + 1) ls 12 16 13 17 -- 符号を返す … … 94 98 getImageSurface :: ImageResource -> ImageType -> Surface 95 99 getImageSurface imgres t = fromJust $ lookup t imgres 100 101 102 -- 固定座標系からセル座標系に 103 cellCrd :: Int -> Int 104 cellCrd x = x `div` (chrSize * one)
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)