module Update.Update ( update ) where import List import Keyboard (KeyCode) import Char (fromCode, toCode) import Maybe import Model.Player (..) import Model.Point (pointSpeed, pointMove, pointAwayDist) import Model.Vec2 (..) import Model.Config (otherConfig) import Model.Cloud (..) import Model.Game (..) import Model.Round (Round) import Utils.Geometry (..) import Utils.Physics (getNewPosAndSpeed) import Update.CloudUpdate (cloudUpdate) import Input (Input) update : Input -> Game -> Game update input game = let hostilePoints = game.cloud.points (otherConfig game.player.config) in if(playerPointsCollision game.time game.player hostilePoints) then { game | time <- 0 , currentScore <- 0 , cloud <- initCloud , rounds <- game.rounds `List.append` [Round game.time game.currentScore] } else let newTime = game.time + input.delta newPlayer = playerStep input.delta input.dir (newKeyCode game.keysDown input.inputKeysDown) game.player (newCloud, addScore, newSeed) = cloudUpdate game.time game.seed newPlayer game.cloud in { game | time <- newTime , keysDown <- input.inputKeysDown , currentScore <- game.currentScore + addScore , player <- newPlayer , cloud <- newCloud , seed <- newSeed } playerStep : Float -> Vec2 -> (KeyCode -> Bool) -> Player -> Player playerStep dt dir newKey player = let (pos, speed) = getNewPosAndSpeed dt dir playerSpeed (player.pos, player.speed) newConfig = if (newKey 69) then otherConfig player.config else player.config in { pos = inBoard playerSize pos , speed = speed , config = newConfig } newKeyCode : List KeyCode -> List KeyCode -> KeyCode -> Bool newKeyCode lastKeyCodes newKeyCodes keyCode = let contains = not << List.isEmpty << List.filter (\kc -> kc == keyCode) in not (contains lastKeyCodes) && (contains newKeyCodes)