Skip to content

Instantly share code, notes, and snippets.

@lorennorman
Created September 8, 2015 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lorennorman/6262b80037654d5f1372 to your computer and use it in GitHub Desktop.
Save lorennorman/6262b80037654d5f1372 to your computer and use it in GitHub Desktop.
first play with Elm
module Wizard where
import Debug
import StartApp.Simple as StartApp
import Signal exposing (Address)
import Time
import Maybe
import Html
import Color exposing (..)
import Graphics.Element exposing (Element, image)
import Graphics.Collage exposing (..)
import Mouse
import Keyboard
import Window
-- Main
main : Signal Element
main =
let
actions =
Signal.mailbox Nothing
address =
Signal.forwardTo actions.address Just
everyTick =
Signal.sampleOn (Time.fps 60)
feeds =
[ Signal.map WizardFace Mouse.position
, Signal.map CanvasDimensions Window.dimensions
, Signal.map WizardBlast Mouse.clicks
, Signal.map WizardWalk (everyTick (Signal.merge Keyboard.arrows Keyboard.wasd))
--, Signal.map BaddiesThink (everyTick )
]
model =
Signal.foldp
(\(Just action) model -> update action model)
initialModel
(Signal.mergeMany (actions.signal :: (List.map (Signal.map Just) feeds)))
in
Signal.map (view address) model
-- Model
type alias Model =
{ settings: Settings
, wizardFacing: Float
, wizardX: Int
, wizardY: Int
, wizardBlasting: Bool
}
type alias Settings =
{ canvasWidth: Int
, canvasHeight: Int
}
initialModel : Model
initialModel =
Model (Settings 800 600) 120 100 100 False
-- Update
type alias Keys = { x:Int, y:Int }
type Action
= CanvasDimensions (Int, Int)
| WizardFace (Int, Int)
| WizardWalk Keys
| WizardBlast ()
update : Action -> Model -> Model
update action model =
case action of
WizardFace (x, y) ->
let
originX = (round ((toFloat model.settings.canvasWidth) / 2))
originY = (round ((toFloat model.settings.canvasHeight) / 2))
newFacing = (positionToRadians x y originX originY)
in
{ model | wizardFacing <- newFacing }
CanvasDimensions (width, height) ->
{ model | settings <- (Settings width height) }
WizardBlast (blasting) ->
{ model | wizardBlasting <- True }
WizardWalk {x, y} ->
{ model | wizardX <- model.wizardX + (x * 10)
, wizardY <- model.wizardY + (y * 10) }
positionToRadians : Int -> Int -> Int -> Int -> Float
positionToRadians x1 y1 x2 y2 =
-(radians (atan2
(toFloat (x1 - x2))
(toFloat (-y1 + y2))))
-- View
view : Address Action -> Model -> Element
view address model =
let
canvasWidth = model.settings.canvasWidth
canvasHeight = model.settings.canvasHeight
in
collage canvasWidth canvasHeight
[ (backgroundForm model)
|> move ((toFloat -model.wizardX), (toFloat -model.wizardY))
, (wizardForm model)
]
backgroundForm : Model -> Form
backgroundForm model =
rect 500.0 500.0
|> filled (rgba 200 80 80 1.0)
wizardForm : Model -> Form
wizardForm model =
let
wizardImage = if model.wizardBlasting then "/images/wizard_blasting.png" else "/images/wizard.png"
width = if model.wizardBlasting then 106 else 100
height = if model.wizardBlasting then 98 else 68
in
toForm (image width height (wizardImage))
|> rotate model.wizardFacing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment