diff options
Diffstat (limited to 'elm/cyoa/src/Game.elm')
-rw-r--r-- | elm/cyoa/src/Game.elm | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/elm/cyoa/src/Game.elm b/elm/cyoa/src/Game.elm new file mode 100644 index 0000000..22b1e8a --- /dev/null +++ b/elm/cyoa/src/Game.elm @@ -0,0 +1,145 @@ +module Game exposing (..) + +import Types exposing (..) + +--| Initial game state with a simple adventure +init : Model +init = + { currentScene = SceneId "start" + , inventory = [] + , scenes = + [ { id = SceneId "start" + , title = "The Mysterious Mansion" + , description = "You stand before an old mansion. The door is locked, but you notice a rusty key on the ground nearby." + , choices = + [ { text = "Pick up the key" + , nextScene = SceneId "start" + , requiredItem = Nothing + , consumesItem = False + } + , { text = "Try the door" + , nextScene = SceneId "inside" + , requiredItem = Just (ItemId "rusty_key") + , consumesItem = True + } + ] + , items = [ { id = ItemId "rusty_key", name = "Rusty Key", description = "An old, rusty key that might fit the mansion's door." } ] + } + , { id = SceneId "inside" + , title = "Inside the Mansion" + , description = "You're inside the mansion. The air is musty and old. There's a dusty book on a table and a locked chest in the corner." + , choices = + [ { text = "Examine the book" + , nextScene = SceneId "book" + , requiredItem = Nothing + , consumesItem = False + } + , { text = "Try to open the chest" + , nextScene = SceneId "chest" + , requiredItem = Nothing + , consumesItem = False + } + ] + , items = [] + } + , { id = SceneId "book" + , title = "The Dusty Book" + , description = "The book contains a map of the mansion. You notice a small key drawn in the corner of one page." + , choices = + [ { text = "Take the key" + , nextScene = SceneId "inside" + , requiredItem = Nothing + , consumesItem = False + } + , { text = "Return to the main room" + , nextScene = SceneId "inside" + , requiredItem = Nothing + , consumesItem = False + } + ] + , items = [ { id = ItemId "small_key", name = "Small Key", description = "A small key that might fit the chest." } ] + } + , { id = SceneId "chest" + , title = "The Locked Chest" + , description = "The chest is locked with a small lock. You need a key to open it." + , choices = + [ { text = "Use the small key" + , nextScene = SceneId "treasure" + , requiredItem = Just (ItemId "small_key") + , consumesItem = True + } + , { text = "Return to the main room" + , nextScene = SceneId "inside" + , requiredItem = Nothing + , consumesItem = False + } + ] + , items = [] + } + , { id = SceneId "treasure" + , title = "The Treasure" + , description = "Inside the chest, you find a valuable gem! Your adventure has been successful." + , choices = + [ { text = "Start a new adventure" + , nextScene = SceneId "start" + , requiredItem = Nothing + , consumesItem = False + } + ] + , items = [ { id = ItemId "gem", name = "Valuable Gem", description = "A beautiful gem that sparkles in the light." } ] + } + ] + } + +--| Find a scene by its ID +findScene : SceneId -> List Scene -> Maybe Scene +findScene targetId scenes = + List.filter (\scene -> scene.id == targetId) scenes + |> List.head + +--| Find an item by its ID +findItem : ItemId -> List Item -> Maybe Item +findItem targetId items = + List.filter (\item -> item.id == targetId) items + |> List.head + +--| Check if player has an item +hasItem : ItemId -> List Item -> Bool +hasItem itemId inventory = + List.any (\item -> item.id == itemId) inventory + +--| Update the game state based on messages +update : Msg -> Model -> Model +update msg model = + case msg of + ChooseChoice choice -> + case choice.requiredItem of + Just requiredItemId -> + if hasItem requiredItemId model.inventory then + let + newInventory = + if choice.consumesItem then + List.filter (\item -> item.id /= requiredItemId) model.inventory + else + model.inventory + in + { model + | currentScene = choice.nextScene + , inventory = newInventory + } + else + model + + Nothing -> + { model | currentScene = choice.nextScene } + + PickupItem item -> + { model | inventory = item :: model.inventory } + + UseItem item _ -> + if hasItem item.id model.inventory then + { model + | inventory = List.filter (\i -> i.id /= item.id) model.inventory + } + else + model \ No newline at end of file |