From 0ae3490e688c9b6970dae4be4a42236303666d49 Mon Sep 17 00:00:00 2001 From: sabadev Date: Sun, 31 Jan 2021 14:33:01 -0500 Subject: [PATCH] Implemented collision between the player and the box. --- src/Game.hs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Game.hs b/src/Game.hs index e08898a..8aae8b7 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -1,4 +1,4 @@ -module Game (gameSettings, G.getStdGen, G.playGame, centreCoords) where +module Game (gameSettings, G.getStdGen, G.playGame) where import Data.Bifunctor (bimap) import Data.Char (toUpper) @@ -85,16 +85,19 @@ handleEvent state (G.KeyPress key) = handleKeyPress state $ toUpper key handleEvent state G.Tick = handleTick state handleTick :: State -> State -handleTick state = do - let player = statePlayer state - state { statePlayer = moveCharacter (entityDirection player) player } +handleTick state@(State { stateScreen = GameScreen }) = do + let oldPlayer = statePlayer state + let player = moveCharacter (entityDirection oldPlayer) oldPlayer + let box = handleCollision player (stateBox state) + state { statePlayer = player, stateBox = box } +handleTick state = state handleKeyPress :: State -> Char -> State handleKeyPress state@(State { stateScreen = TitleScreen }) 'Q' = state { stateIsQuitting = True } -handleKeyPress state@(State { stateScreen = GameScreen }) 'Q' = state { stateScreen = TitleScreen } -handleKeyPress state@(State { stateScreen = TitleScreen }) 'P' = state { stateScreen = GameScreen } +handleKeyPress state@(State { stateScreen = TitleScreen }) 'P' = state { statePlayer = initPlayer, stateBox = initBox, stateEnemy = initEnemies, stateScreen = GameScreen } handleKeyPress state@(State { stateScreen = TitleScreen }) 'H' = state { stateScreen = HelpScreen } handleKeyPress state@(State { stateScreen = HelpScreen }) _ = state { stateScreen = TitleScreen } +handleKeyPress state@(State { stateScreen = GameScreen }) 'Q' = state { stateScreen = TitleScreen } handleKeyPress state@(State { stateScreen = GameScreen }) 'W' = state { statePlayer = updateDirection U (statePlayer state) } handleKeyPress state@(State { stateScreen = GameScreen }) 'S' = state { statePlayer = updateDirection D (statePlayer state) } handleKeyPress state@(State { stateScreen = GameScreen }) 'A' = state { statePlayer = updateDirection L (statePlayer state) } @@ -145,8 +148,17 @@ limitCoords (a, b) = (limitRowCoord a, limitColumnCoord b) | otherwise = a updatePosition :: G.Coords -> Character t -> Character t -updatePosition (velocityX, velocityY) character = do +updatePosition (velocityRow, velocityColumn) character = do let (oldRow, oldColumn) = entityCoords character - let newCoords = (oldRow + velocityX, oldColumn + velocityY) + let newCoords = (oldRow + velocityRow, oldColumn + velocityColumn) let boundedCoords = limitCoords newCoords character { entityCoords = boundedCoords } + +willCollide :: Direction -> G.Coords -> G.Coords -> Bool +willCollide Stop _ _ = False +willCollide _ first second = first == second + +handleCollision :: Character t -> Character Box -> Character Box +handleCollision (Character { entityCoords = coords, entityDirection = direction }) box + | willCollide direction coords (entityCoords box) = moveCharacter direction box + | otherwise = box -- 2.20.1