Skip to content

Instantly share code, notes, and snippets.

@rezich
Created May 12, 2017 20:11
Show Gist options
  • Save rezich/25079de2338c6b1d1a401a829017a4dc to your computer and use it in GitHub Desktop.
Save rezich/25079de2338c6b1d1a401a829017a4dc to your computer and use it in GitHub Desktop.
module App 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 Farmers =
Int
type alias Farms =
Int
type alias Model =
{ food : Food
, farmers : Farmers
, farms : Farms
}
-- BALANCE
priceFarmer : Food
priceFarmer =
5
priceFarm : Food
priceFarm =
10
farmersPerFarm : Farmers
farmersPerFarm =
5
-- INIT
init : ( Model, Cmd Msg )
init =
( Model 0 0 1, Cmd.none )
-- MESSAGES
type Msg
= Tick Time
| GatherFood
| HireFarmer
| BuildFarm
-- UPDATE
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Tick _ ->
let
food =
model.food + model.farmers
in
( { model | food = food }, Cmd.none )
GatherFood ->
( { model | food = model.food + 1 }, Cmd.none )
HireFarmer ->
if model.food >= priceFarmer then
( { model | farmers = model.farmers + 1, food = model.food - priceFarmer }, Cmd.none )
else
( model, Cmd.none )
BuildFarm ->
if model.food >= priceFarm then
( { model | farms = model.farms + 1, food = model.food - priceFarm }, Cmd.none )
else
( model, Cmd.none )
-- VIEW
viewHeader : Html Msg
viewHeader =
header []
[ h1 [] [ text "incremental game" ] ]
viewResources : Model -> Html Msg
viewResources model =
div []
[ h2 [] [ text "Resources" ]
, text ("Food: " ++ (toString model.food))
, button [ onClick GatherFood ] [ text "Gather" ]
]
viewUnits : Model -> Html Msg
viewUnits model =
div []
[ h2 [] [ text "Units" ]
, text ("Farmers: " ++ (toString model.farmers) ++ "/" ++ (toString (model.farms * farmersPerFarm)))
, button [ onClick HireFarmer, disabled (model.food < priceFarmer || model.farmers == model.farms * farmersPerFarm) ] [ text ("Hire (" ++ (toString priceFarmer) ++ ")") ]
]
viewStructures : Model -> Html Msg
viewStructures model =
if model.farmers >= farmersPerFarm then
div []
[ h2 [] [ text "Structures" ]
, text ("Farms: " ++ (toString model.farms))
, button [ onClick BuildFarm, disabled (model.food < priceFarm) ] [ text ("Build (" ++ (toString priceFarm) ++ ")") ]
]
else
div [] []
view : Model -> Html Msg
view model =
div [ class "content" ]
[ viewHeader
, viewResources model
, viewUnits model
, viewStructures model
]
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Time.every second Tick
-- MAIN
main : Program Never Model Msg
main =
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