From dee4ec086da43197b5527db4a21e21ed406a5d47 Mon Sep 17 00:00:00 2001 From: Sergey Date: Wed, 4 Jan 2017 14:33:42 +0600 Subject: [PATCH] Add level statistic --- src/Data.elm | 4 ++++ src/Game.elm | 26 ++++++++++++++++++-------- src/Render.elm | 9 +++++++++ src/Screen/Complete.elm | 1 + src/Screen/Level.elm | 1 + src/Style.elm | 25 ++++++++++++++++++------- src/Type.elm | 8 +++++++- 7 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/Data.elm b/src/Data.elm index 12b3137..b569888 100644 --- a/src/Data.elm +++ b/src/Data.elm @@ -69,6 +69,10 @@ generateLevel levelNumber levelData = player = { direction = Left, position = extractPlayerPosition positionsTuples + }, + statistic = { + moves = 0, + pushes = 0 } } diff --git a/src/Game.elm b/src/Game.elm index f378464..c763073 100644 --- a/src/Game.elm +++ b/src/Game.elm @@ -48,19 +48,25 @@ movePlayer: Direction -> Position -> Level -> Level movePlayer direction position level = let gamePlayer = level.player + levelStatistic = level.statistic in { - level | player = { - gamePlayer | - position = position, - direction = direction - } + level | + statistic = { + levelStatistic | moves = levelStatistic.moves + 1 + }, + player = { + gamePlayer | + position = position, + direction = direction + } } moveBox: Direction -> Position -> Level -> Level moveBox direction position level = let levelMap = level.map + levelStatistic = level.statistic mapBoxes = levelMap.boxes |> List.map ( \square -> @@ -71,9 +77,13 @@ moveBox direction position level = ) in { - level | map = { - levelMap | boxes = mapBoxes - } + level | + statistic = { + levelStatistic | pushes = levelStatistic.pushes + 1 + }, + map = { + levelMap | boxes = mapBoxes + } } move: Position -> Direction -> Position diff --git a/src/Render.elm b/src/Render.elm index 54a54de..4286e82 100644 --- a/src/Render.elm +++ b/src/Render.elm @@ -38,6 +38,15 @@ player player = Html.div [Style.player player.position player.direction] [] +statistic: Statistic -> Html msd +statistic statistic = + Html.div [Style.statistic] [ + text ( + "moves " ++ (toString statistic.moves) ++ ", " ++ + "pushes " ++ (toString statistic.pushes) + ) + ] + square: Class -> Position -> Html msg square class position = case class of diff --git a/src/Screen/Complete.elm b/src/Screen/Complete.elm index 7550abc..94a4a99 100644 --- a/src/Screen/Complete.elm +++ b/src/Screen/Complete.elm @@ -14,6 +14,7 @@ render model = Just level -> Render.layout [ Render.title ("Level " ++ (toString (level.number + 1)) ++ " complete"), + Render.statistic level.statistic, Render.menu "Press spacebar to play next level" ] diff --git a/src/Screen/Level.elm b/src/Screen/Level.elm index 7e28a78..e2758f2 100644 --- a/src/Screen/Level.elm +++ b/src/Screen/Level.elm @@ -13,6 +13,7 @@ render model = Just level -> Render.layout [ Render.title ("Level " ++ (toString (level.number + 1))), + Render.statistic level.statistic, Render.grid level.size [ Render.player level.player, Render.layer Floor level.map.floor, diff --git a/src/Style.elm b/src/Style.elm index 5e567b0..d4aab03 100644 --- a/src/Style.elm +++ b/src/Style.elm @@ -51,8 +51,8 @@ menu = style title: Attribute msg title = style [ - ("position", "absolute"), - ("top", "120px"), + ("position", "relative"), + ("top", "0px"), ("color", "#bac4c5"), ("font-family", "'Orbitron', sans-serif"), ("font-size", "40px"), @@ -61,16 +61,26 @@ title = style ("width", "100%") ] +statistic: Attribute msg +statistic = + style [ + ("position", "relative"), + ("top", "0px"), + ("color", "#bac4c5"), + ("font-family", "'Orbitron', sans-serif"), + ("font-size", "16px"), + ("line-heigth", "16px"), + ("text-align", "center"), + ("width", "100%") + ] + grid: Int -> Int -> Attribute msg grid height width = style [ - ("position", "absolute"), + ("position", "relative"), ("height", toPixels (height * squareSize)), ("width", toPixels (width * squareSize)), - ("left", "50%"), - ("top", "50%"), - ("margin-left", "-" ++ toPixels (width * squareSize // 2)), - ("margin-top", "-" ++ toPixels (height * squareSize // 2)) + ("margin", "auto 0") ] @@ -99,6 +109,7 @@ player position direction = ] ) + baseSquare: Position -> List (String, String) baseSquare position = [ diff --git a/src/Type.elm b/src/Type.elm index 7013e9b..e1cd432 100644 --- a/src/Type.elm +++ b/src/Type.elm @@ -53,11 +53,17 @@ type alias Map = { floor: Layer } +type alias Statistic = { + moves: Int, + pushes: Int +} + type alias Level = { number: Int, size: Size, map: Map, - player: Player + player: Player, + statistic: Statistic } type alias LevelRow = List Class