Skip to content

Instantly share code, notes, and snippets.

@mark-chimes
Created March 14, 2018 10:17
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 mark-chimes/9f936db41f685267c442feb4076fcb15 to your computer and use it in GitHub Desktop.
Save mark-chimes/9f936db41f685267c442feb4076fcb15 to your computer and use it in GitHub Desktop.
Example of the way the dialog is currently implemented. See full files here: https://github.com/mark-chimes/trading-post
# types
type alias Model =
{ viewState : ViewState
, gameState : GameState
}
type alias GameState =
{ gold : Int
, goldOfferGetVal : Int
, offerOrGet : OfferGet
...
, currentCustomer : Customer
, dialog : List String
, currentTrade : Trade
}
type alias Trade =
{ goldOffered : Int
}
type Msg
= Noop
...
| Speak String
| ChangeMoney Int
| ConfirmTradeMsg
# State
speakAndRespond : GameState -> String -> GameState
speakAndRespond gameState playerSpeech =
{ gameState
| dialog =
gameState.dialog
|> appendDialog "You" playerSpeech
|> appendDialog gameState.currentCustomer.name (npcDialog gameState)
, currentTrade = updateTradeGold gameState.offerOrGet gameState.goldOfferGetVal gameState.currentTrade
}
npcDialog : GameState -> String
npcDialog gameState =
case gameState.actionRadioState of
MoneyDiscussion ->
case gameState.offerOrGet of
Offer ->
"Sure, I'd love " ++ toString gameState.goldOfferGetVal ++ " gold!"
Get ->
"Well, if I'm going to add " ++ toString gameState.goldOfferGetVal ++ " gold, you'd better make it worth my while!"
_ ->
"Nah, not interested, thanks."
updateTradeGold : OfferGet -> Int -> Trade -> Trade
updateTradeGold offerGet gold trade =
{ trade | goldOffered = calculateChangeInGoldTrade offerGet trade.goldOffered gold }
calculateChangeInGoldTrade : OfferGet -> Int -> Int -> Int
calculateChangeInGoldTrade offerGet oldGold alteredGold =
case offerGet of
Offer ->
oldGold + alteredGold
Get ->
oldGold - alteredGold
appendDialog : String -> String -> List String -> List String
appendDialog personName string dialog =
(personName ++ ": " ++ string) :: dialog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment