Show
Ignore:
Timestamp:
10/04/08 18:09:41 (3 months ago)
Author:
mokehehe
Message:

敵との当たり判定追加

Files:
1 modified

Legend:

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

    r20680 r20691  
    99        getScrollPos, 
    1010        getPlayerYPos, 
     11        getPlayerVY, 
    1112        getPlayerHitRect, 
    1213        getPlayerMedal, 
    1314        getPlayerScore, 
    1415        getPlayerType, 
    15         setPlayerType 
     16        setPlayerType, 
     17        setPlayerDamage, 
     18        stampPlayer 
    1619) where 
    1720 
    1821import Multimedia.SDL (blitSurface, pt) 
     22import Data.Bits ((.&.)) 
    1923 
    2024import Util 
     
    3438scrollMaxX = 8 * chrSize 
    3539gravity2 = one `div` 4          -- Aを長押ししたときの重力 
    36  
     40stampVy = -8 * gravity 
     41undeadFrame = frameRate * 2 
     42 
     43-- 種類 
    3744data PlayerType = SmallNario | SuperNario | FireNario 
    3845        deriving (Eq) 
    3946 
     47-- 状態 
     48data PlayerState = Normal | Dead 
     49        deriving (Eq) 
     50 
     51-- 構造体 
    4052data Player = Player { 
    4153        pltype :: PlayerType, 
     54        plstate :: PlayerState, 
    4255        x :: Int, 
    4356        y :: Int, 
     
    4659        scrx :: Int, 
    4760        stand :: Bool, 
     61        undeadCount :: Int, 
    4862 
    4963        medal :: Int, 
     
    5771newPlayer = Player { 
    5872        pltype = SmallNario, 
     73        plstate = Normal, 
    5974        x = 3 * chrSize * one, 
    6075        y = 13 * chrSize * one, 
     
    6378        scrx = 0, 
    6479        stand = False, 
     80        undeadCount = 0, 
    6581 
    6682        medal = 0, 
     
    8096patSit = patSlip + 1 
    8197patShot = patSit + 1 
     98patDead = patShot + 2 
    8299 
    83100imgTableSmall = [ 
     
    102119                else self' 
    103120        where 
    104                 ax = (-padl + padr) * acc 
     121                ax = if padd then 0 else (-padl + padr) * acc 
    105122                vx' 
    106123                        | ax /= 0                       = rangeadd (vx self) ax (-maxspd) maxspd 
     
    114131                scrollPos = (max vx' 0) * (scrollMaxX - scrollMinX) `div` runVx + scrollMinX 
    115132 
     133                padd = if isPressed (kp PadD) then True else False 
    116134                padl = if isPressed (kp PadL) then 1 else 0 
    117135                padr = if isPressed (kp PadR) then 1 else 0 
     
    130148                                1       -> 1 
    131149                pat' 
     150                        | padd && pltype self /= SmallNario     = patSit 
    132151                        | vx' == 0                              = patStop 
    133152                        | vx' > 0 && lr' == 0   = patSlip 
     
    158177 
    159178-- 重力による落下 
    160 fall :: KeyProc -> Player -> Player 
    161 fall kp self 
     179fall :: Bool -> Player -> Player 
     180fall abtn self 
    162181        | stand self    = self 
    163182        | otherwise             = self { y = y', vy = vy' } 
    164183        where 
    165184                ay 
    166                         | vy self < 0 && isPressed (kp PadA)    = gravity2 
    167                         | otherwise                                                             = gravity 
     185                        | vy self < 0 && abtn   = gravity2 
     186                        | otherwise                             = gravity 
    168187                vy' = min maxVy $ vy self + ay 
    169188                y' = y self + vy' 
     
    211230updatePlayer :: KeyProc -> Field -> Player -> (Player, [Event]) 
    212231updatePlayer kp fld self = 
     232        case plstate self of 
     233                Normal  -> updateNormal kp fld self' 
     234                Dead    -> updateDead kp fld self' 
     235        where 
     236                self' = decUndead self 
     237                decUndead pl = pl { undeadCount = max 0 $ undeadCount pl - 1 } 
     238 
     239-- 通常時 
     240updateNormal :: KeyProc -> Field -> Player -> (Player, [Event]) 
     241updateNormal kp fld self = 
    213242        moveY $ checkX fld $ moveX kp self 
    214243        where 
    215                 moveY = checkCeil fld . doJump kp . checkFloor fld . fall kp 
     244                moveY = checkCeil fld . doJump kp . checkFloor fld . fall (isPressed $ kp PadA) 
     245 
     246-- 死亡時 
     247updateDead :: KeyProc -> Field -> Player -> (Player, [Event]) 
     248updateDead kp fld self = (fall False self, []) 
    216249 
    217250-- スクロール位置取得 
     
    223256getPlayerYPos = (`div` one) . y 
    224257 
     258-- Y速度取得 
     259getPlayerVY :: Player -> Int 
     260getPlayerVY = vy 
     261 
    225262-- 当たり判定用矩形 
    226263getPlayerHitRect :: Player -> Rect 
     
    246283setPlayerType t self = self { pltype = t } 
    247284 
     285-- ダメージを与える 
     286setPlayerDamage :: Player -> Player 
     287setPlayerDamage self 
     288        | undeadCount self > 0                  = self 
     289        | pltype self == SmallNario             = self { plstate = Dead, pat = patDead, vy = jumpVy, stand = False } 
     290        | otherwise                                             = self { pltype = SmallNario, undeadCount = undeadFrame } 
     291 
     292-- 敵を踏み潰した 
     293stampPlayer :: Player -> Player 
     294stampPlayer self = self { vy = stampVy } 
     295 
    248296-- 描画 
    249297renderPlayer sur imgres scrx self = do 
    250         blitSurface (getImageSurface imgres imgtype) Nothing sur pos 
     298        if undeadCount self == 0 || (undeadCount self .&. 1) /= 0 
     299                then blitSurface (getImageSurface imgres imgtype) Nothing sur pos >> return () 
     300                else return () 
    251301        where 
    252302                pos = case pltype self of 
    253303                        SmallNario      -> pt sx $ sy - chrSize + 1 
    254304                        otherwise       -> pt sx $ sy - chrSize * 2 + 1 
    255                 imgtype = imgtbl !! lr self !! pat self 
     305                imgtype 
     306                        | plstate self == Dead  = ImgNarioDead 
     307                        | otherwise                             = imgtbl !! lr self !! pat self 
    256308                imgtbl = case pltype self of 
    257309                        SmallNario      -> imgTableSmall