| 33 | | |
| 34 | | images = [ImgNario00, ImgNario01, ImgNario02, ImgNario03, ImgNario04] |
| 35 | | |
| 36 | | |
| 37 | | |
| 38 | | -- �L�[�{�[�h���� |
| 39 | | |
| 40 | | data GameKey = |
| 41 | | GKUp | GKDown | GKLeft | GKRight | GKRotate |
| 42 | | deriving (Eq,Show,Enum) |
| 43 | | |
| 44 | | data KeyState = |
| 45 | | Pushed | Pushing | Released | Releasing |
| 46 | | deriving (Eq,Show) |
| 47 | | |
| 48 | | isPressed Pushed = True |
| 49 | | isPressed Pushing = True |
| 50 | | isPressed _ = False |
| 51 | | |
| 52 | | type KeyProc = GameKey -> KeyState |
| 53 | | |
| 54 | | keyProc bef cur gk |
| 55 | | | not bp && not cp = Releasing |
| 56 | | | not bp && cp = Pushed |
| 57 | | | bp && not cp = Released |
| 58 | | | bp && cp = Pushing |
| 59 | | where |
| 60 | | bp = (mapPhyKey gk) `elem` bef |
| 61 | | cp = (mapPhyKey gk) `elem` cur |
| 62 | | |
| 63 | | mapPhyKey GKUp = SDLK_UP |
| 64 | | mapPhyKey GKDown = SDLK_DOWN |
| 65 | | mapPhyKey GKLeft = SDLK_LEFT |
| 66 | | mapPhyKey GKRight = SDLK_RIGHT |
| 67 | | mapPhyKey GKRotate = SDLK_z |
| 68 | | |
| 69 | | -- ���Ԓ��� |
| 70 | | |
| 71 | | elapseTime :: Integer -> IO (IO (Int,Bool)) |
| 72 | | elapseTime fps = do |
| 73 | | let frametime = picosec `div` fps |
| 74 | | tm <- getClockTime |
| 75 | | st <- newIORef ((0,0,noTimeDiff), (1,tm)) |
| 76 | | return $ do |
| 77 | | ((bef,cur,fdt), (cnt,bt)) <- readIORef st |
| 78 | | ct <- getClockTime |
| 79 | | let dt = diffClockTimes ct bt |
| 80 | | ndt = diffClockTimes ct tm |
| 81 | | adj = frametime*cnt - toPsec dt |
| 82 | | nc = if cnt==fps then (1,ct) else (cnt+1,bt) |
| 83 | | (nbef,ncur) = if tdSec fdt /= tdSec ndt then (cur,0) else (bef,cur) |
| 84 | | if adj < 0 then do |
| 85 | | writeIORef st ((nbef,ncur,ndt), nc) |
| 86 | | return (bef, False) |
| 87 | | else do |
| 88 | | writeIORef st ((nbef,ncur+1,ndt), nc) |
| 89 | | threadDelay $ fromInteger $ min 16666 $ adj `div` 1000000 |
| 90 | | return (bef, True) |
| 91 | | where |
| 92 | | toPsec dt = toInteger (tdMin dt * 60 + tdSec dt) * picosec + tdPicosec dt |
| 93 | | picosec = 1000000000000 |
| | 35 | imageFn ImgNario10 = "nario10.bmp" |
| | 36 | imageFn ImgNario11 = "nario11.bmp" |
| | 37 | imageFn ImgNario12 = "nario12.bmp" |
| | 38 | imageFn ImgNario13 = "nario13.bmp" |
| | 39 | imageFn ImgNario14 = "nario14.bmp" |
| | 40 | imageFn ImgBlock1 = "block1.bmp" |
| | 41 | imageFn ImgBlock2 = "block2.bmp" |
| | 42 | imageFn ImgBlock3 = "block3.bmp" |
| | 43 | imageFn ImgBlock4 = "block4.bmp" |
| | 44 | imageFn ImgBlock5 = "block5.bmp" |
| | 45 | |
| | 46 | images = [ |
| | 47 | ImgNario00, ImgNario01, ImgNario02, ImgNario03, ImgNario04, |
| | 48 | ImgNario10, ImgNario11, ImgNario12, ImgNario13, ImgNario14, |
| | 49 | ImgBlock1, ImgBlock2, ImgBlock3, ImgBlock4, ImgBlock5 |
| | 50 | ] |
| | 51 | |
| | 52 | |
| | 53 | |
| | 54 | type ImageResource = [(ImageType, Surface)] |
| | 55 | |
| | 56 | |
| | 57 | |
| | 58 | |
| | 59 | fieldMap = [ |
| | 60 | " ", |
| | 61 | " ", |
| | 62 | " ", |
| | 63 | " ", |
| | 64 | " ", |
| | 65 | " ", |
| | 66 | " O?O ", |
| | 67 | " ", |
| | 68 | " ", |
| | 69 | " O?O?O ", |
| | 70 | " ", |
| | 71 | " ", |
| | 72 | " ", |
| | 73 | "@@@@@@@@@@@@@@@@", |
| | 74 | "@@@@@@@@@@@@@@@@" |
| | 75 | ] |
| | 76 | |
| | 77 | chr2img '@' = ImgBlock1 |
| | 78 | chr2img 'O' = ImgBlock2 |
| | 79 | chr2img '?' = ImgBlock4 |
| | 80 | |
| | 81 | renderMap sur imgres = sequence_ $ concatMap lineProc $ zip [0..] fieldMap |
| | 82 | where |
| | 83 | lineProc (y, ln) = map (cellProc y) $ zip [0..] ln |
| | 84 | cellProc y (x, c) = do |
| | 85 | if c == ' ' |
| | 86 | then return () |
| | 87 | else do |
| | 88 | blitSurface (getImageSurface imgres $ chr2img c) Nothing sur $ pt (x*16) (y*16) |
| | 89 | return () |
| | 90 | |
| | 91 | |
| | 92 | |
| | 93 | |
| | 94 | data Player = Player { |
| | 95 | x :: Int, |
| | 96 | y :: Int, |
| | 97 | lr :: Int |
| | 98 | } |
| | 99 | |
| | 100 | one = 256 |
| | 101 | |
| | 102 | newPlayer = Player { |
| | 103 | x = 1 * 16 * one, |
| | 104 | y = 12 * 16 * one, |
| | 105 | lr = 1 |
| | 106 | } |
| | 107 | |
| | 108 | updatePlayer :: Player -> KeyProc -> Player |
| | 109 | updatePlayer player kp = |
| | 110 | player { x = x', y = y', lr = lr' } |
| | 111 | where |
| | 112 | x' |
| | 113 | | isPressed (kp GKLeft) = (x player) - 1 * one |
| | 114 | | isPressed (kp GKRight) = (x player) + 1 * one |
| | 115 | | otherwise = x player |
| | 116 | y' |
| | 117 | | isPressed (kp GKUp) = (y player) - 1 * one |
| | 118 | | isPressed (kp GKDown) = (y player) + 1 * one |
| | 119 | | otherwise = y player |
| | 120 | lr' |
| | 121 | | isPressed (kp GKLeft) = 0 |
| | 122 | | isPressed (kp GKRight) = 1 |
| | 123 | | otherwise = lr player |
| | 124 | |
| | 125 | renderPlayer sur player imgres = |
| | 126 | blitSurface (getImageSurface imgres chr) Nothing sur pos |
| | 127 | where |
| | 128 | pos = pt ((x player) `div` one) ((y player) `div` one) |
| | 129 | chr = if (lr player) == 0 |
| | 130 | then ImgNario00 |
| | 131 | else ImgNario10 |
| | 132 | |
| | 133 | |
| | 134 | -- �摜���\�[�X�ǂݍ��� |
| | 135 | loadImageResource :: IO ImageResource |
| | 136 | loadImageResource = mapM load images |
| | 137 | where |
| | 138 | load imgtype = do |
| | 139 | sur <- loadBMP $ ("img/" ++) $ imageFn imgtype |
| | 140 | -- colorKey <- mapRGB (surfacePixelFormat sur) $ Color r g b a |
| | 141 | setColorKey sur [SRCCOLORKEY] 0 |
| | 142 | return (imgtype, sur) |
| | 143 | r = 0 |
| | 144 | g = 0 |
| | 145 | b = 0 |
| | 146 | a = 255 |
| | 147 | |
| | 148 | |
| | 149 | getImageSurface :: ImageResource -> ImageType -> Surface |
| | 150 | getImageSurface imgres t = fromJust $ lookup t imgres |