Create a gist now

Instantly share code, notes, and snippets.

@hmans /Clicker.elm
Last active Apr 10, 2017

What would you like to do?
Very simple (and unfinished) incremental clicker game written in Elm.
module Clicker exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
import Time exposing (Time, second)
-- MODEL
type alias Food =
Int
type alias Wood =
Int
type alias Stone =
Int
type alias Gold =
Int
type alias GameState =
{ food : Food
, wood : Wood
, stone : Stone
, gold : Gold
, farmers : Int
}
farmerPrice : Food
farmerPrice =
20
init : ( GameState, Cmd Msg )
init =
let
state =
{ food = 0
, wood = 0
, stone = 0
, gold = 0
, farmers = 0
}
in
( state, Cmd.none )
-- UPDATE
type Msg
= Tick Time
| CollectFood
| CollectWood
| CollectStone
| HireFarmer
update : Msg -> GameState -> ( GameState, Cmd Msg )
update msg state =
case msg of
Tick _ ->
let
food =
state.food + state.farmers
in
( { state | food = food }, Cmd.none )
CollectFood ->
( { state | food = state.food + 1 }, Cmd.none )
CollectWood ->
( { state | wood = state.wood + 1 }, Cmd.none )
CollectStone ->
( { state | stone = state.stone + 1 }, Cmd.none )
HireFarmer ->
if state.food >= farmerPrice then
( { state | farmers = state.farmers + 1, food = state.food - farmerPrice }, Cmd.none )
else
( state, Cmd.none )
-- VIEW
viewHeader : String -> Html Msg
viewHeader title =
header []
[ h1 [] [ text title ] ]
viewGame : GameState -> Html Msg
viewGame state =
div [ id "game" ]
[ div []
[ text ("Food: " ++ (toString state.food))
, button [ onClick CollectFood ] [ text "Collect Food" ]
]
, div []
[ text ("Wood: " ++ (toString state.wood))
, button [ onClick CollectWood ] [ text "Collect Wood" ]
]
, div []
[ text ("Stone: " ++ (toString state.stone))
, button [ onClick CollectStone ] [ text "Collect Stone" ]
]
, div []
[ text ("Farmers: " ++ (toString state.farmers))
, button [ onClick HireFarmer, disabled (state.food < farmerPrice) ] [ text ("Hire (" ++ (toString farmerPrice) ++ " Food)") ]
]
]
view : GameState -> Html Msg
view state =
div [ class "content" ]
[ viewHeader "Clicky Wood!"
, viewGame state
]
subscriptions : GameState -> Sub Msg
subscriptions state =
Time.every second Tick
main : Program Never GameState Msg
main =
Html.program { init = init, view = view, update = update, subscriptions = subscriptions }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment