update for 0.14

This commit is contained in:
Evan Czaplicki 2014-12-10 03:26:21 -08:00
parent 8af5771423
commit 51f7753641
4 changed files with 81 additions and 57 deletions

4
.gitignore vendored
View file

@ -1,5 +1,3 @@
build
cache
elm-stuff
.cabal-sandbox
cabal.sandbox.config
todo/elm_dependencies

View file

@ -1,29 +1,41 @@
-- Welcome to your first Elm program
{- Welcome to your first Elm program
-- Read up on syntax at http://elm-lang.org/learn/Syntax.elm
Read up on syntax:
http://elm-lang.org/learn/Syntax.elm
Learn about the Elm's core libraries:
http://package.elm-lang.org/packages/elm-lang/core/latest/
-}
import Graphics.Element (..)
import List
import Text (..)
main : Element
main =
flow down
[ helloWorld
, welcomeGraphics
]
-- Learn about Elm's functions in the Catalog:
-- http://library.elm-lang.org/catalog/elm-lang-Elm/0.13.0/
helloWorld : Element
helloWorld = asText "Hello, World!"
helloWorld =
asText "Hello, World!"
welcomeGraphics : Element
welcomeGraphics =
let dimensions = 90
imgSize = 30
elmLogo = image imgSize imgSize "http://elm-lang.org/logo.png"
elmLogo =
image imgSize imgSize "http://elm-lang.org/logo.png"
elmsPerSide = dimensions // imgSize
row = flow right (repeat elmsPerSide elmLogo)
row =
flow right (List.repeat elmsPerSide elmLogo)
in
flow down (repeat elmsPerSide row)
main : Element
main =
flow down [
helloWorld,
welcomeGraphics
]
flow down (List.repeat elmsPerSide row)

View file

@ -9,13 +9,11 @@ fun to play with in [Elm Reactor][reactor].
## Get Set Up
First make sure you have [Elm Platform][platform] 0.13 or higher. The next
step is to clone this repo:
[platform]: https://github.com/elm-lang/elm-platform/
After [installing Elm Platform](http://elm-lang.org/Install.elm), clone this
repo:
```shell
git clone https://github.com/michaelbjames/elm-examples.git
git clone https://github.com/evancz/elm-examples.git
cd elm-examples
```

View file

@ -1,21 +1,27 @@
import Keyboard
import Window
import Color (..)
import Debug
import Graphics.Collage (..)
import Graphics.Element (..)
import Keyboard
import Signal
import Time (..)
import Window
-- MODEL
type Model =
{ x : Float
, y : Float
, vx : Float
, vy : Float
type alias Model =
{ x : Float
, y : Float
, vx : Float
, vy : Float
, dir : Direction
}
data Direction = Left | Right
type Direction = Left | Right
type alias Keys = { x:Int, y:Int }
type Keys = { x:Int, y:Int }
mario : Model
mario =
@ -29,8 +35,8 @@ mario =
-- UPDATE
step : (Float, Keys) -> Model -> Model
step (dt, keys) mario =
update : (Float, Keys) -> Model -> Model
update (dt, keys) mario =
mario
|> gravity dt
|> jump keys
@ -38,16 +44,21 @@ step (dt, keys) mario =
|> physics dt
|> Debug.watch "mario"
jump : Keys -> Model -> Model
jump keys mario =
if keys.y > 0 && mario.vy == 0 then { mario | vy <- 6.0 } else mario
if keys.y > 0 && mario.vy == 0
then { mario | vy <- 6.0 }
else mario
gravity : Float -> Model -> Model
gravity dt mario =
{ mario |
vy <- if mario.y > 0 then mario.vy - dt/4 else 0
vy <- if mario.y > 0 then mario.vy - dt/40 else 0
}
physics : Float -> Model -> Model
physics dt mario =
{ mario |
@ -55,32 +66,35 @@ physics dt mario =
y <- max 0 (mario.y + dt * mario.vy)
}
walk : Keys -> Model -> Model
walk keys mario =
{ mario |
vx <- toFloat keys.x,
dir <- if | keys.x < 0 -> Left
| keys.x > 0 -> Right
| otherwise -> mario.dir
dir <-
if | keys.x < 0 -> Left
| keys.x > 0 -> Right
| otherwise -> mario.dir
}
-- DISPLAY
-- VIEW
display : (Int, Int) -> Model -> Element
display (w',h') mario =
view : (Int, Int) -> Model -> Element
view (w',h') mario =
let (w,h) = (toFloat w', toFloat h')
verb = if | mario.y > 0 -> "jump"
| mario.vx /= 0 -> "walk"
| otherwise -> "stand"
verb =
if | mario.y > 0 -> "jump"
| mario.vx /= 0 -> "walk"
| otherwise -> "stand"
dir = case mario.dir of
Left -> "left"
Right -> "right"
dir =
case mario.dir of
Left -> "left"
Right -> "right"
src = "imgs/mario/"++ verb ++ "/" ++ dir ++ ".gif"
--src = "http://i188.photobucket.com/albums/z137/DreamsInMotion/Video%20Game%20Pictures/Metroid/samus-1.gif"
src = "imgs/mario/"++ verb ++ "/" ++ dir ++ ".gif"
marioImage = image 35 35 src
@ -102,12 +116,14 @@ display (w',h') mario =
-- SIGNALS
main : Signal Element
main = lift2 display Window.dimensions (foldp step mario input)
main =
Signal.map2 view Window.dimensions (Signal.foldp update mario input)
input : Signal (Float, Keys)
input =
let delta = lift (\t -> t/20) (fps 25)
let delta = Signal.map (\t -> t/20) (fps 25)
deltaArrows =
lift2 (,) delta (Debug.watch "arrows" <~ Keyboard.arrows)
Signal.map2 (,) delta (Signal.map (Debug.watch "arrows") Keyboard.arrows)
in
sampleOn delta deltaArrows
Signal.sampleOn delta deltaArrows