Refactored into multiple files. Still need to work on the structure.
This commit is contained in:
parent
dd5c07cc95
commit
997ac70c1d
14 changed files with 259 additions and 94 deletions
|
@ -4,7 +4,8 @@
|
|||
"repository": "https://github.com/user/project.git",
|
||||
"license": "BSD3",
|
||||
"source-directories": [
|
||||
"."
|
||||
".",
|
||||
"./src"
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
|
|
96
main.elm
96
main.elm
|
@ -1,98 +1,18 @@
|
|||
import Html exposing (Html, h1, div, text)
|
||||
import Time exposing (Time, inMilliseconds)
|
||||
import Svg exposing (Svg, svg, g, path)
|
||||
import Svg.Attributes exposing (stroke, fill, strokeLinejoin, strokeWidth, width, height, viewBox, d)
|
||||
import Svg.Path exposing (pathToString, arcBy, subpath, closed, lineBy, startAt, antiClockwise, clockwise, largestArc)
|
||||
import AnimationFrame exposing (times)
|
||||
module Main exposing (..)
|
||||
|
||||
import Html
|
||||
import Init.All
|
||||
import Subscriptions exposing (subscriptions)
|
||||
import Updates.All exposing (update)
|
||||
import Views.Body exposing (view)
|
||||
import Types exposing (Model, Msg)
|
||||
|
||||
mOVE_SPEED = 60 -- pixels per second
|
||||
cHAR_RAD = 25 --half-width of character
|
||||
wIDTH = "500"
|
||||
hEIGHT = "900"
|
||||
|
||||
main : Program Never Model Msg
|
||||
main =
|
||||
Html.program
|
||||
{ init = init
|
||||
{ init = ( Init.All.init, Cmd.none )
|
||||
, view = view
|
||||
, update = update
|
||||
, subscriptions = subscriptions
|
||||
}
|
||||
|
||||
|
||||
-- MODEL
|
||||
|
||||
type alias Model =
|
||||
{ time : Time }
|
||||
|
||||
init : (Model, Cmd msg)
|
||||
init =
|
||||
({ time = 0 }
|
||||
,Cmd.none
|
||||
)
|
||||
|
||||
-- UPDATE
|
||||
|
||||
type Msg = Tick Time
|
||||
|
||||
update : Msg -> Model -> (Model, Cmd Msg )
|
||||
update msg model =
|
||||
case msg of
|
||||
Tick newTime ->
|
||||
({ model | time = newTime }, Cmd.none)
|
||||
|
||||
-- SUBSCRIPTIONS
|
||||
|
||||
subscriptions : Model -> Sub Msg
|
||||
subscriptions model =
|
||||
times Tick
|
||||
|
||||
-- VIEW
|
||||
|
||||
type alias Point = (Float, Float)
|
||||
|
||||
render : String -> String -> Model -> Html msg
|
||||
render w h model =
|
||||
let
|
||||
-- Pull this out into a function to calculate a position in a range
|
||||
-- based off of a cycle time and current number in the cycle.
|
||||
cycleTime = 500
|
||||
maxOpen = 90
|
||||
currentCycleRatio = (model.time |> inMilliseconds |> round) % cycleTime
|
||||
openness = (toFloat currentCycleRatio) / (cycleTime / (2 * maxOpen)) - maxOpen |> abs
|
||||
in
|
||||
svg
|
||||
[ width w, height h, viewBox "0 0 500, 900"]
|
||||
[pacman (250, 450) (degrees openness)]
|
||||
|
||||
pacman : Point -> Float -> Svg msg
|
||||
pacman position openness =
|
||||
let
|
||||
opp = cHAR_RAD * sin (openness/2)
|
||||
adj = cHAR_RAD * cos (openness/2)
|
||||
in
|
||||
g []
|
||||
[ path
|
||||
[ d <|
|
||||
pathToString [(subpath (startAt position) closed
|
||||
[lineBy (adj, opp)
|
||||
,arcBy (cHAR_RAD, cHAR_RAD) 0 (largestArc, clockwise) (0, -(opp * 2))
|
||||
,lineBy (-adj, opp)
|
||||
]
|
||||
)]
|
||||
,fill "#ffff00"
|
||||
]
|
||||
[]
|
||||
]
|
||||
|
||||
|
||||
view : Model -> Html Msg
|
||||
view model =
|
||||
div []
|
||||
[h1 [] [text "ElmMan!"]
|
||||
,render wIDTH hEIGHT model
|
||||
]
|
||||
|
||||
|
||||
|
||||
|
|
15
src/AnimationHelpers.elm
Normal file
15
src/AnimationHelpers.elm
Normal file
|
@ -0,0 +1,15 @@
|
|||
module AnimationHelpers exposing (..)
|
||||
|
||||
import Types exposing (Range)
|
||||
|
||||
|
||||
calculateAnimation : Int -> Int -> Range -> Float
|
||||
calculateAnimation current cycle range =
|
||||
let
|
||||
( start, end ) =
|
||||
range
|
||||
|
||||
rangeDiff =
|
||||
end - start
|
||||
in
|
||||
(toFloat (current % cycle)) / (toFloat (cycle)) * rangeDiff + start
|
13
src/GLOBALS.elm
Normal file
13
src/GLOBALS.elm
Normal file
|
@ -0,0 +1,13 @@
|
|||
module GLOBALS exposing (..)
|
||||
|
||||
|
||||
move_speed =
|
||||
60
|
||||
|
||||
|
||||
character_radius =
|
||||
25
|
||||
|
||||
|
||||
playAreaSize =
|
||||
( "500", "900" )
|
11
src/Init/All.elm
Normal file
11
src/Init/All.elm
Normal file
|
@ -0,0 +1,11 @@
|
|||
module Init.All exposing (..)
|
||||
|
||||
import Types exposing (Model)
|
||||
import Init.Player
|
||||
|
||||
|
||||
init : Model
|
||||
init =
|
||||
{ time = 0
|
||||
, player = Init.Player.init
|
||||
}
|
10
src/Init/Player.elm
Normal file
10
src/Init/Player.elm
Normal file
|
@ -0,0 +1,10 @@
|
|||
module Init.Player exposing (..)
|
||||
|
||||
import Types exposing (PlayerModel, Direction(Right))
|
||||
|
||||
|
||||
init : PlayerModel
|
||||
init =
|
||||
{ direction = Right
|
||||
, location = ( 250, 450 )
|
||||
}
|
12
src/Subscriptions.elm
Normal file
12
src/Subscriptions.elm
Normal file
|
@ -0,0 +1,12 @@
|
|||
module Subscriptions exposing (..)
|
||||
|
||||
import AnimationFrame exposing (times, diffs)
|
||||
import Types exposing (Model, Msg(Tick, Tock))
|
||||
|
||||
|
||||
subscriptions : Model -> Sub Msg
|
||||
subscriptions model =
|
||||
Sub.batch
|
||||
[ times Tick
|
||||
, diffs Tock
|
||||
]
|
31
src/Types.elm
Normal file
31
src/Types.elm
Normal file
|
@ -0,0 +1,31 @@
|
|||
module Types exposing (..)
|
||||
|
||||
import Time exposing (Time)
|
||||
|
||||
|
||||
type Direction
|
||||
= Up
|
||||
| Down
|
||||
| Left
|
||||
| Right
|
||||
|
||||
|
||||
type alias Model =
|
||||
{ time : Time
|
||||
, player : PlayerModel
|
||||
}
|
||||
|
||||
|
||||
type alias PlayerModel =
|
||||
{ direction : Direction
|
||||
, location : ( Float, Float )
|
||||
}
|
||||
|
||||
|
||||
type alias Range =
|
||||
( Float, Float )
|
||||
|
||||
|
||||
type Msg
|
||||
= Tick Time
|
||||
| Tock Time
|
29
src/Updates/All.elm
Normal file
29
src/Updates/All.elm
Normal file
|
@ -0,0 +1,29 @@
|
|||
module Updates.All exposing (update)
|
||||
|
||||
import Updates.Player
|
||||
import Updates.Time
|
||||
import Types exposing (Msg(Tick, Tock), Model)
|
||||
import List
|
||||
import Time exposing (Time)
|
||||
|
||||
|
||||
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||
update msg model =
|
||||
let
|
||||
tuples =
|
||||
{ player = Updates.Player.update msg model.player
|
||||
, time = Updates.Time.update msg model.time
|
||||
}
|
||||
|
||||
returnModel =
|
||||
{ player = Tuple.first tuples.player
|
||||
, time = Tuple.first tuples.time
|
||||
}
|
||||
|
||||
commands =
|
||||
Cmd.batch
|
||||
[ Tuple.second tuples.player
|
||||
, Tuple.second tuples.time
|
||||
]
|
||||
in
|
||||
( returnModel, commands )
|
8
src/Updates/Player.elm
Normal file
8
src/Updates/Player.elm
Normal file
|
@ -0,0 +1,8 @@
|
|||
module Updates.Player exposing (update)
|
||||
|
||||
import Types exposing (PlayerModel, Msg)
|
||||
|
||||
|
||||
update : Msg -> PlayerModel -> ( PlayerModel, Cmd Msg )
|
||||
update msg model =
|
||||
( model, Cmd.none )
|
14
src/Updates/Time.elm
Normal file
14
src/Updates/Time.elm
Normal file
|
@ -0,0 +1,14 @@
|
|||
module Updates.Time exposing (update)
|
||||
|
||||
import Time exposing (Time)
|
||||
import Types exposing (Msg(Tick, Tock))
|
||||
|
||||
|
||||
update : Msg -> Time -> ( Time, Cmd Msg )
|
||||
update msg model =
|
||||
case msg of
|
||||
Tick newTime ->
|
||||
( newTime, Cmd.none )
|
||||
|
||||
Tock timeDiff ->
|
||||
( model, Cmd.none )
|
18
src/Views/Body.elm
Normal file
18
src/Views/Body.elm
Normal file
|
@ -0,0 +1,18 @@
|
|||
module Views.Body exposing (..)
|
||||
|
||||
import Html exposing (Html, h1, div, text)
|
||||
import GLOBALS exposing (playAreaSize)
|
||||
import Types exposing (Model, Range)
|
||||
import Views.PlayArea
|
||||
|
||||
|
||||
view : Model -> Html msg
|
||||
view model =
|
||||
let
|
||||
( width, height ) =
|
||||
playAreaSize
|
||||
in
|
||||
div []
|
||||
[ h1 [] [ text "ElmMan!" ]
|
||||
, Views.PlayArea.render width height model
|
||||
]
|
14
src/Views/PlayArea.elm
Normal file
14
src/Views/PlayArea.elm
Normal file
|
@ -0,0 +1,14 @@
|
|||
module Views.PlayArea exposing (..)
|
||||
|
||||
import Html exposing (Html)
|
||||
import Svg exposing (svg)
|
||||
import Svg.Attributes exposing (width, height, viewBox)
|
||||
import Types exposing (Model)
|
||||
import Views.Player
|
||||
|
||||
|
||||
render : String -> String -> Model -> Html msg
|
||||
render w h model =
|
||||
svg
|
||||
[ width w, height h, viewBox ("0 0 " ++ w ++ " " ++ h) ]
|
||||
[ Views.Player.render model ]
|
69
src/Views/Player.elm
Normal file
69
src/Views/Player.elm
Normal file
|
@ -0,0 +1,69 @@
|
|||
module Views.Player exposing (..)
|
||||
|
||||
import Time exposing (Time, inMilliseconds)
|
||||
import Svg exposing (Svg, g, path)
|
||||
import Svg.Attributes
|
||||
exposing
|
||||
( fill
|
||||
, d
|
||||
)
|
||||
import Svg.Path
|
||||
exposing
|
||||
( Point
|
||||
, pathToString
|
||||
, arcBy
|
||||
, subpath
|
||||
, closed
|
||||
, lineBy
|
||||
, startAt
|
||||
, clockwise
|
||||
, largestArc
|
||||
)
|
||||
import AnimationHelpers exposing (calculateAnimation)
|
||||
import Types exposing (Model)
|
||||
import GLOBALS exposing (character_radius)
|
||||
|
||||
|
||||
render : Model -> Svg msg
|
||||
render model =
|
||||
let
|
||||
cycleTime =
|
||||
500
|
||||
|
||||
maxOpen =
|
||||
90
|
||||
|
||||
openness =
|
||||
calculateAnimation (model.time |> inMilliseconds |> round) cycleTime ( 0, maxOpen )
|
||||
in
|
||||
pacman ( 250, 450 ) (degrees openness)
|
||||
|
||||
|
||||
pacman : Point -> Float -> Svg msg
|
||||
pacman position openness =
|
||||
let
|
||||
opp =
|
||||
character_radius * sin (openness / 2)
|
||||
|
||||
adj =
|
||||
character_radius * cos (openness / 2)
|
||||
|
||||
radiuses =
|
||||
( character_radius, character_radius )
|
||||
in
|
||||
g []
|
||||
[ path
|
||||
[ d <|
|
||||
pathToString
|
||||
[ (subpath (startAt position)
|
||||
closed
|
||||
[ lineBy ( adj, opp )
|
||||
, arcBy radiuses 0 ( largestArc, clockwise ) ( 0, -(opp * 2) )
|
||||
, lineBy ( -adj, opp )
|
||||
]
|
||||
)
|
||||
]
|
||||
, fill "#ffff00"
|
||||
]
|
||||
[]
|
||||
]
|
Loading…
Reference in a new issue