Skip to content

Commit

Permalink
Game migrated to elm 0.18
Browse files Browse the repository at this point in the history
  • Loading branch information
tizpuppi committed Dec 6, 2016
1 parent 709ed89 commit 0898a51
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## What is this?

A minimalistic action game. Written in [Elm](http://elm-lang.org) version 0.17 as a programming exercise. Soundless and colorless.
A minimalistic action game. Written in [Elm](http://elm-lang.org) version 0.18 as a programming exercise. Soundless and colorless.

## Dependencies

Expand Down
10 changes: 5 additions & 5 deletions elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"exposed-modules": [],
"dependencies": {
"elm-lang/animation-frame": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "4.0.0 <= v < 5.0.0",
"elm-lang/html": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"elm-lang/keyboard": "1.0.0 <= v < 2.0.0",
"elm-lang/svg": "1.0.0 <= v < 2.0.0",
"elm-lang/virtual-dom": "1.0.0 <= v < 2.0.0",
"elm-lang/svg": "2.0.0 <= v < 3.0.0",
"elm-lang/virtual-dom": "2.0.0 <= v < 3.0.0",
"elm-lang/window": "1.0.0 <= v < 2.0.0"
},
"elm-version": "0.17.0 <= v < 0.18.0"
"elm-version": "0.18.0 <= v < 0.19.0"
}
3 changes: 1 addition & 2 deletions src/Main.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Main exposing (..)

import Html exposing (Html)
import Html.App as Html

import Model exposing (Model,initialModel)
import Update exposing (update)
Expand All @@ -10,7 +9,7 @@ import Subscription exposing (subscriptions, initialWindowSizeCommand)

--------------------------------------------------------------------------- MAIN

main : Program Never
main : Program Never Model Subscription.Msg
main =
Html.program
{ init = (initialModel, initialWindowSizeCommand)
Expand Down
4 changes: 2 additions & 2 deletions src/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ initialModel =
freshGame : Ui -> Model
freshGame ui =
let
ui' = { ui | screen = PlayScreen
ui_ = { ui | screen = PlayScreen
, pressedKeys = Set.empty }
in
{ initialModel | ui = ui' }
{ initialModel | ui = ui_ }


winScore : Int
Expand Down
2 changes: 1 addition & 1 deletion src/Subscription.elm
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ subscriptions {ui} =

initialWindowSizeCommand : Cmd Msg
initialWindowSizeCommand =
Task.perform (\_ -> NoOp) (\{width,height} -> ResizeWindow (width,height)) Window.size
Task.perform (\{width,height} -> ResizeWindow (width,height)) Window.size
98 changes: 49 additions & 49 deletions src/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ update action ({ui,scene} as model) =
player1 = scene.player1 |> steerAndGravity delta ui
player2 = scene.player2 |> steerAndGravity delta ui
round = scene.round
(player1', player2') = handleCollisions player1 player2
player1'' = player1' |> movePlayer delta
player2'' = player2' |> movePlayer delta
(player1_, player2_) = handleCollisions player1 player2
player1__ = player1_ |> movePlayer delta
player2__ = player2_ |> movePlayer delta
hasAnyPlayerFallen = hasFallen player1 || hasFallen player2
isRoundOver = hasAnyPlayerFallen && round.touchdownTime > 1400
(player1''', player2''') = applyScores player1'' player2'' isRoundOver
isGameOver = player1'''.score>=winScore || player2'''.score>=winScore
(round', screen') =
(player1___, player2___) = applyScores player1__ player2__ isRoundOver
isGameOver = player1___.score>=winScore || player2___.score>=winScore
(round_, screen_) =
if isGameOver then
(round, GameoverScreen)
else if isRoundOver then
Expand All @@ -41,12 +41,12 @@ update action ({ui,scene} as model) =
({ round | touchdownTime = round.touchdownTime + delta }, PlayScreen)
else
(round, PlayScreen)
scene' = { scene | player1 = player1'''
, player2 = player2'''
, round = round' }
ui' = { ui | screen = screen' }
scene_ = { scene | player1 = player1___
, player2 = player2___
, round = round_ }
ui_ = { ui | screen = screen_ }
in
({ model | scene = scene', ui = ui' }, Cmd.none)
({ model | scene = scene_, ui = ui_ }, Cmd.none)

KeyChange pressed keycode ->
(handleKeyChange pressed keycode model, Cmd.none)
Expand Down Expand Up @@ -77,12 +77,12 @@ applyScores player1 player2 isRoundOver =
payAndReset : Player -> Int -> Player
payAndReset player additionalPoints =
let
position' = Vector player.homePosX playerHomePosY
velocity' = Vector 0 0
position_ = Vector player.homePosX playerHomePosY
velocity_ = Vector 0 0
in
{ player | score = player.score + additionalPoints
, position = position'
, velocity = velocity' }
, position = position_
, velocity = velocity_ }


hasFallen : Player -> Bool
Expand All @@ -100,12 +100,12 @@ steerAndGravity delta {pressedKeys} ({velocity} as player) =
else
0
ax = directionX * 0.0000019
vx' = velocity.x + ax*delta |> friction delta
vy' = velocity.y |> (gravity delta)
velocity' = { velocity | x = vx'
, y = vy' }
vx_ = velocity.x + ax*delta |> friction delta
vy_ = velocity.y |> (gravity delta)
velocity_ = { velocity | x = vx_
, y = vy_ }
in
{ player | velocity = velocity' }
{ player | velocity = velocity_ }


handleCollisions : Player -> Player -> (Player,Player)
Expand All @@ -121,10 +121,10 @@ bounceOffEachOther player1 player2 =
let
v1 = deflect player1 player2
v2 = deflect player2 player1
player1' = { player1 | velocity = v1 }
player2' = { player2 | velocity = v2 }
player1_ = { player1 | velocity = v1 }
player2_ = { player2 | velocity = v2 }
in
(player1', player2')
(player1_, player2_)


movePlayer : Time -> Player -> Player
Expand All @@ -139,14 +139,14 @@ movePlayer delta ({velocity,position} as player) =
walk
else
fall
(x', y', vx', vy') = stepFn delta position.x position.y vx vy
position' = { position | x = x'
, y = y' }
velocity' = { velocity | x = vx'
, y = vy' }
(x_, y_, vx_, vy_) = stepFn delta position.x position.y vx vy
position_ = { position | x = x_
, y = y_ }
velocity_ = { velocity | x = vx_
, y = vy_ }
in
{ player | position = position'
, velocity = velocity' }
{ player | position = position_
, velocity = velocity_ }



Expand All @@ -158,25 +158,25 @@ fly delta x y vx vy =
walk : Time -> Float -> Float -> Float -> Float -> (Float,Float,Float,Float)
walk delta x y vx vy =
let
x = x + vx*delta
y = icePosY
x_ = x + vx*delta
y_ = icePosY
vy = 0
in
(x, y, vx, vy)
(x_, y_, vx, vy)


fall : Time -> Float -> Float -> Float -> Float -> (Float,Float,Float,Float)
fall delta x y vx vy =
let
y = y + vy*delta
x = x + vx*delta
y_ = y + vy*delta
x_ = x + vx*delta
isLeftSide = x<0.5
x' = if y<icePosY+playerRadius then
rollOffEdge x y isLeftSide
x__ = if y_<icePosY+playerRadius then
rollOffEdge x_ y_ isLeftSide
else
keepOutOfBounds x
keepOutOfBounds x_
in
(x', y, vx, vy)
(x__, y_, vx, vy)


inBounds : Float -> Bool
Expand All @@ -193,9 +193,9 @@ jump : Player -> Player
jump ({position,velocity} as player) =
let
vy = if position.y==icePosY then -0.001 else velocity.y
velocity' = { velocity | y = vy }
velocity_ = { velocity | y = vy }
in
{ player | velocity = velocity' }
{ player | velocity = velocity_ }


gravity : Time -> Float -> Float
Expand Down Expand Up @@ -227,25 +227,25 @@ handleKeyChange : Bool -> KeyCode -> Model -> Model
handleKeyChange pressed keycode ({scene,ui} as model) =
let
fn = if pressed then Set.insert else Set.remove
pressedKeys' = fn keycode ui.pressedKeys
pressedKeys_ = fn keycode ui.pressedKeys
in
case ui.screen of
PlayScreen ->
let
ui' = { ui | pressedKeys = pressedKeys' }
justPressed keycode = freshKeyPress keycode ui.pressedKeys pressedKeys'
ui_ = { ui | pressedKeys = pressedKeys_ }
justPressed keycode = freshKeyPress keycode ui.pressedKeys pressedKeys_
maybeJump player = if justPressed player.jumpKey then
jump player
else
player
player1' = maybeJump scene.player1
player2' = maybeJump scene.player2
scene' = { scene | player1 = player1', player2 = player2' }
player1_ = maybeJump scene.player1
player2_ = maybeJump scene.player2
scene_ = { scene | player1 = player1_, player2 = player2_ }
in
{ model | ui = ui', scene = scene' }
{ model | ui = ui_, scene = scene_ }

GameoverScreen ->
if freshKeyPress (Char.toCode ' ') ui.pressedKeys pressedKeys' then
if freshKeyPress (Char.toCode ' ') ui.pressedKeys pressedKeys_ then
freshGame ui
else
model
Expand Down
2 changes: 1 addition & 1 deletion src/View.elm
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,4 @@ renderTextLine xPos yPos fontSize anchor content extraAttrs =
]
|> List.append extraAttrs
in
Svg.text' attributes [ Svg.text content ]
Svg.text_ attributes [ Svg.text content ]

0 comments on commit 0898a51

Please sign in to comment.