about summary refs log tree commit diff stats
path: root/elm/cyoa/src/View.elm
diff options
context:
space:
mode:
Diffstat (limited to 'elm/cyoa/src/View.elm')
-rw-r--r--elm/cyoa/src/View.elm111
1 files changed, 111 insertions, 0 deletions
diff --git a/elm/cyoa/src/View.elm b/elm/cyoa/src/View.elm
new file mode 100644
index 0000000..090a89a
--- /dev/null
+++ b/elm/cyoa/src/View.elm
@@ -0,0 +1,111 @@
+module View exposing (..)
+
+import Html exposing (..)
+import Html.Attributes exposing (..)
+import Html.Events exposing (onClick)
+import Types exposing (..)
+import Game exposing (findScene)
+
+--| Main view function
+view : Model -> Html Msg
+view model =
+    div [ class "game-container" ]
+        [ viewScene model
+        , viewInventory model.inventory
+        ]
+
+--| View the current scene
+viewScene : Model -> Html Msg
+viewScene model =
+    case findScene model.currentScene model.scenes of
+        Just scene ->
+            div [ class "scene" ]
+                [ h1 [] [ text scene.title ]
+                , p [] [ text scene.description ]
+                , viewItems scene.items
+                , viewChoices scene.choices model.inventory
+                ]
+
+        Nothing ->
+            div [] [ text "Scene not found!" ]
+
+--| View available items in the current scene
+viewItems : List Item -> Html Msg
+viewItems items =
+    if List.isEmpty items then
+        div [] []
+    else
+        div [ class "items" ]
+            [ h2 [] [ text "Items in this area:" ]
+            , ul []
+                (List.map
+                    (\item ->
+                        li []
+                            [ button
+                                [ onClick (PickupItem item)
+                                , class "item-button"
+                                ]
+                                [ text ("Pick up " ++ item.name) ]
+                            ]
+                    )
+                    items
+                )
+            ]
+
+--| View available choices/actions
+viewChoices : List Choice -> List Item -> Html Msg
+viewChoices choices inventory =
+    div [ class "choices" ]
+        [ h2 [] [ text "What will you do?" ]
+        , ul []
+            (List.map
+                (\choice ->
+                    li []
+                        [ viewChoice choice inventory ]
+                )
+                choices
+            )
+        ]
+
+--| View a single choice/action
+viewChoice : Choice -> List Item -> Html Msg
+viewChoice choice inventory =
+    case choice.requiredItem of
+        Just requiredItemId ->
+            if Game.hasItem requiredItemId inventory then
+                button
+                    [ onClick (ChooseChoice choice)
+                    , class "choice-button"
+                    ]
+                    [ text choice.text ]
+            else
+                button
+                    [ class "choice-button disabled"
+                    , disabled True
+                    ]
+                    [ text (choice.text ++ " (requires an item)") ]
+
+        Nothing ->
+            button
+                [ onClick (ChooseChoice choice)
+                , class "choice-button"
+                ]
+                [ text choice.text ]
+
+--| View the player's inventory
+viewInventory : List Item -> Html Msg
+viewInventory items =
+    div [ class "inventory" ]
+        [ h2 [] [ text "Inventory" ]
+        , if List.isEmpty items then
+            p [] [ text "Your inventory is empty" ]
+          else
+            ul []
+                (List.map
+                    (\item ->
+                        li []
+                            [ text (item.name ++ " - " ++ item.description) ]
+                    )
+                    items
+                )
+        ] 
\ No newline at end of file