Changeset 20673 for lang/haskell

Show
Ignore:
Timestamp:
10/04/08 14:32:30 (2 months ago)
Author:
mokehehe
Message:

プレーヤーとの当たり判定追加

Location:
lang/haskell/nario
Files:
5 modified

Legend:

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

    r20670 r20673  
    33module Actor ( 
    44        Actor(..), 
    5         ObjWrapper(..), 
     5        ActorWrapper(..), 
    66        updateActors, 
    77        filterActors, 
     
    1111import Multimedia.SDL (Surface) 
    1212 
    13 import Event 
    14 import AppUtil 
    15 import Field  
     13import Event (Event) 
     14import AppUtil (ImageResource, Rect) 
     15import Field (Field) 
    1616 
    1717 
     
    1919        update :: Field -> a -> (a, [Event]) 
    2020        render :: a -> ImageResource -> Int -> Surface -> IO () 
     21 
    2122        bDead :: a -> Bool 
    2223        bDead _ = False 
     24 
     25        getHitRect :: a -> Maybe Rect 
     26        getHitRect _ = Nothing 
    2327 
    2428-- ============================================================================ 
    2529 
    2630---- 
    27 data ObjWrapper = forall a. Actor a => ObjWrapper a     -- 存在型aの動く範囲を型クラスに制限 
     31data ActorWrapper = forall a. Actor a => ActorWrapper a -- 存在型aの動く範囲を型クラスに制限 
    2832 
    29 updateActors :: Field -> [ObjWrapper] -> [(ObjWrapper, [Event])] 
    30 updateActors fld = map (\(ObjWrapper x) -> let (x', ev') = update fld x in (ObjWrapper x', ev')) 
     33updateActors :: Field -> [ActorWrapper] -> [(ActorWrapper, [Event])] 
     34updateActors fld = map (\(ActorWrapper x) -> let (x', ev') = update fld x in (ActorWrapper x', ev')) 
    3135 
    32 filterActors :: [ObjWrapper] -> [ObjWrapper] 
    33 filterActors = filter (\(ObjWrapper x) -> not $ bDead x) 
     36filterActors :: [ActorWrapper] -> [ActorWrapper] 
     37filterActors = filter (\(ActorWrapper x) -> not $ bDead x) 
    3438 
    35 renderActors :: ImageResource -> Int -> Surface -> [ObjWrapper] -> IO () 
    36 renderActors imgres ofsx sur = mapM_ (\(ObjWrapper x) -> render x imgres ofsx sur) 
     39renderActors :: ImageResource -> Int -> Surface -> [ActorWrapper] -> IO () 
     40renderActors imgres ofsx sur = mapM_ (\(ActorWrapper x) -> render x imgres ofsx sur) 
  • lang/haskell/nario/Actor/Kinoko.hs

    r20670 r20673  
    55) where 
    66 
    7 import Multimedia.SDL hiding (Event) 
     7import Multimedia.SDL (blitSurface, pt) 
    88 
    99import Actor (Actor(..)) 
    1010import Const 
    1111import Util (sgn) 
    12 import AppUtil 
     12import AppUtil (getImageSurface, cellCrd, Rect(..)) 
    1313import Images 
    1414import Field 
     
    4747        bDead self = y self `div` one >= screenHeight + chrSize * 3 
    4848 
     49        getHitRect self = Just $ Rect (xx - 8) (yy - 16) (xx + 8) yy 
     50                where 
     51                        xx = x self `div` one 
     52                        yy = y self `div` one 
     53 
    4954 
    5055newKinoko :: Int -> Int -> Kinoko 
  • lang/haskell/nario/AppUtil.hs

    r20397 r20673  
    11module AppUtil where 
    22 
    3 import Multimedia.SDL 
     3import Multimedia.SDL (Surface, SDLKey(..), loadBMP, freeSurface, surfacePixelFormat, displayFormat, pfPalette, setColorKey, SurfaceFlag(..)) 
    44import Data.Maybe (fromJust) 
    55 
     
    7373cellCrd :: Int -> Int 
    7474cellCrd x = x `div` (chrSize * one) 
     75 
     76 
     77 
     78-- ======== 
     79data Rect = Rect Int Int Int Int 
     80 
     81 
     82ishit :: Rect -> Rect -> Bool 
     83ishit (Rect l1 t1 r1 b1) (Rect l2 t2 r2 b2) = 
     84        l1 < r2 && t1 < b2 && l2 < r1 && t2 < b1 
  • lang/haskell/nario/Main.hs

    r20670 r20673  
    7676                pl :: Player, 
    7777                fld :: Field, 
    78                 actors :: [ObjWrapper], 
     78                actors :: [ActorWrapper], 
    7979                time :: Int 
    8080        } 
     
    125125                        | otherwise             = Nothing 
    126126 
     127 
     128 
     129-- 当たり判定 
     130hitcheck :: Player -> [ActorWrapper] -> (Player, [ActorWrapper]) 
     131hitcheck player actors = foldl proc (player, []) actors 
     132        where 
     133                proc (pl, ac) (ActorWrapper a) = case getHitRect a of 
     134                        Nothing -> nothingHappened 
     135                        Just rc -> 
     136                                if not $ ishit plrc rc 
     137                                        then nothingHappened 
     138                                        else (pl', ac') 
     139                        where 
     140                                nothingHappened = (pl, ac ++ [ActorWrapper a]) 
     141                                plrc = getPlayerHitRect player 
     142                                pl' = pl 
     143                                ac' = ac 
     144 
     145 
    127146-- ゲーム 
    128147doGame :: Field -> [[SDLKey]] -> [ImageResource -> Scr] 
     
    152171                                ev' = concatMap snd actors_updates 
    153172 
    154                                 gstmp = gs { pl = pl', fld = fld', actors = actors', time = time' } 
     173                                (pl'', actors'') = hitcheck pl' actors' 
     174 
     175                                gstmp = gs { pl = pl'', fld = fld', actors = actors'', time = time' } 
    155176                                gs' = procEvent gstmp (plev ++ ev' ++ screv') 
    156177 
    157178                initialState = GameGame { pl = newPlayer, fld = fldmap, actors = [], time = 400 * one } 
    158179 
     180-- ゲームオーバー 
    159181doGameOver fldmap kss = doTitle fldmap kss 
    160182 
     
    170192                                c = fieldRef (fld gs) cx cy 
    171193                                items 
    172                                         | c == 'K'      = [ObjWrapper $ newKinoko cx cy] 
     194                                        | c == 'K'      = [ActorWrapper $ newKinoko cx cy] 
    173195                                        | otherwise     = [] 
    174                                 actors' = actors gs ++ [ObjWrapper $ newAnimBlock cx cy $ fieldRef (fld gs) cx cy] ++ items 
     196                                actors' = actors gs ++ [ActorWrapper $ newAnimBlock cx cy $ fieldRef (fld gs) cx cy] ++ items 
    175197                                fld' = fieldSet (fld gs) cx cy '*' 
    176198                proc gs (EvSetField cx cy c) = gs { fld = fieldSet (fld gs) cx cy c } 
     
    178200                        where 
    179201                                ene = case c of 
    180                                         'k'     -> ObjWrapper $ newKuribo cx cy 
    181                                         'n'     -> ObjWrapper $ newNokonoko cx cy 
     202                                        'k'     -> ActorWrapper $ newKuribo cx cy 
     203                                        'n'     -> ActorWrapper $ newNokonoko cx cy 
    182204 
    183205 
  • lang/haskell/nario/Player.hs

    r20670 r20673  
    88        getScrollPos, 
    99        getPlayerYPos, 
     10        getPlayerHitRect, 
    1011        getPlayerMedal, 
    1112        getPlayerScore 
    1213) where 
    1314 
    14 import Multimedia.SDL hiding (Event) 
     15import Multimedia.SDL (blitSurface, pt) 
    1516 
    1617import Util 
    17 import AppUtil 
     18import AppUtil (KeyProc, isPressed, PadBtn(..), cellCrd, KeyState(..), getImageSurface, Rect(..)) 
    1819import Const 
    1920import Images 
     
    200201getPlayerYPos = (`div` one) . y 
    201202 
     203-- 当たり判定用矩形 
     204getPlayerHitRect :: Player -> Rect 
     205getPlayerHitRect self = Rect (xx - 6) (yy - 16) (xx + 6) yy 
     206        where 
     207                xx = x self `div` one 
     208                yy = y self `div` one 
     209 
    202210-- メダル枚数取得 
    203211getPlayerMedal :: Player -> Int