wolfadex / elm-text-adventure / Game

The following is a basic game with 2 rooms and connections to move between them.

import Game

{-| The most basic game possible
-}
main =
    let
        initialGame =
            Game.makeGame "Simple Game"

        ( bedroom, initialGame ) =
            Game.addRoom
                "Bedroom"
                "A simple bedroom. Just a bed and 3 drawer dresser."

        ( bathroom, initialGame ) =
            Game.addRoom
                "Bathroom"
                "A simple bathroom. Just a toilet and sink."
    in
    Game.makeGame "Sample Game"
        |> Game.addConnection
            { from = bedroom
            , to = bathroom
            , name = "Bathroom Door"
            , description = "Door to the bathroom."
            , message = "You walk to the bathroom."
            }
        |> Game.addConnection
            { from = bathroom
            , to = bedroom
            , name = "Bedroom Door"
            , description = "Door to the bedroom."
            , message = "You walk to the bedroom."
            }
        |> Game.finalize
            bedroom
            "You wake up in your bedroom."
        |> Game.program

Types for makeing your main function typed


type Game


type alias Msg =
Internal.Msg

Game building functions

General

update : Msg -> Game -> ( Game, Platform.Cmd.Cmd Msg )

Updates the game state.

view : View.ParentMsg msg -> Size -> Game -> Html msg

Displays the game, taking a parent Msg.

makeGame : Internal.Name -> Game

Takes a name and creates your new game.

makeGame "Your Cool Adventure"

finalize : Internal.RoomId -> Internal.Message -> Game -> Game

Finalizes your game by setting the initial room and the first message the player sees.

finalize startingRoom "Welcome message to set the scene" yourGame

endGame : String -> Game -> Game

Use this to end the game. Pass it your final message.

endGame "Final message" yourGame


type Size
    = Large
    | Small

Used for determining the way the game is displayed.

When Large, the log is displayed on the right and interactions on the left.

When Small, the log is displayed on the bottom and interactions on the top. Interactions are also collapsible so they take up less space.

encode : Game -> Json.Decode.Value

Encode the game so that it can be saved to localStorage or elsewhere.

decode : Dict String (Internal.ItemId -> Game -> ( Game, Internal.Message )) -> Json.Decode.Value -> Result Json.Decode.Error Game

Decode a game that was saved.

Also requires a Dict String ItemUse of the form

Dict.fromList
    [ ( "uniqueNameForItem", (\itemId, game -> ... ( modifiedGame, "Message about what happened." )) ) ]

This is used to rebuild the use function of the tools you've created.

Rooms

addRoom : Internal.Name -> Internal.Description -> Game -> ( Internal.RoomId, Game )

Adds a new room to your game. This only creates the room and not any connections between rooms.

addRoom "Name" "Description" yourGame

changeRoomName : Internal.Name -> Internal.RoomId -> Game -> Game

Change the name of a room.

changeRoomName "New Name" someRoom yourGame

changeRoomDescription : Internal.Description -> Internal.RoomId -> Game -> Game

Change the namedescription of a room.

changeRoomDescription "New Description" someRoom yourGame

getCurrentRoom : Game -> Internal.RoomId

Gets the current room the player is in. Useful for knowing where the player is when they use an item.

getCurrentRoom yourGame == someRoom

setRoom : Internal.RoomId -> Game -> Game

"Teleports" the player to the specified room.

setRoom someRoom yourGame

deleteRoom : Internal.RoomId -> Game -> Game

Removes a room from the game, as well as all of its contents and any connections going to the room.

deleteRoom someRoom yourGame

NOTE: If you delete the room your character is currently in, don't forget to send them to a new room with setRoom!

addConnection : { from : Internal.RoomId, to : Internal.RoomId, name : String, description : String, locked : Basics.Bool, message : String } -> Game -> Game

Creates a one-way connection between 2 rooms.

In order for to get from room A to room B and back, you need to create 2 connections.

existingGameWithRooms
    |> Game.addConnection
        { from = roomA
        , to = roomB
        , name = "Door"
        , description = "Door to Room B."
        , message = "You walk to Room B."
        }
    |> Game.addConnection
        { from = roomB
        , to = roomA
        , name = "Door"
        , description = "Door to Room A."
        , message = "You walk to Room A."
        }

deleteConnection : { from : Internal.RoomId, to : Internal.RoomId, name : Internal.Name } -> Game -> Game

Remove the specified connection from the game. A connection is identified by the room it's coming from, going to, and its name.

deleteConnection { from = roomId1, to = roomId2, name = "Name" } yourGame

Items

createTool : { name : Internal.Name, description : Internal.Description, use : Internal.ItemId -> Game -> ( Game, Internal.Message ), decoderKey : String } -> Game -> ( Internal.ItemId, Game )

Creates a tool item. A tool is any item that can be used such as a fork, sword, key, or potato.

createTool
    "Name"
    "Description"
    (\itemId gameState ->
        ( updatedGameState, "Message describing what happened" )
    )
    yourGame

ItemUse is how your item affects the game world, anything from opening a door to teleporting the player.

changeItemName : Internal.Name -> Internal.ItemId -> Game -> Game

Change the name of an item.

changeItemName "New Name" item game

changeItemDescription : Internal.Description -> Internal.ItemId -> Game -> Game

Change the description of an item.

changeItemDescription "New description." item game

changeItemUse : Internal.ItemUse -> Internal.ItemId -> Game -> Game

Change what happens when you use an item.

changeItemUse (\itemId game -> ...) item game

addItemToRoom : Internal.ItemId -> Internal.RoomId -> Game -> Game

Adds the specified item to the game.

addItemToRoom item room game

deleteItem : Internal.ItemId -> Game -> Game

Removes the specified item from the game.

deleteItem itemToDelete yourGame