yath_ex/lib/yath_ex.ex

122 lines
2.2 KiB
Elixir
Raw Normal View History

2021-01-03 16:49:38 +01:00
defmodule YathEx do
@behaviour Ratatouille.App
import Ratatouille.View
import Ratatouille.Constants, only: [key: 1]
2021-01-04 19:55:25 +01:00
alias YathEx.Update
2021-01-03 16:49:38 +01:00
alias YathEx.Views.{
Encounter,
Equipment,
Help,
Logbook,
2021-01-04 19:55:25 +01:00
Menu,
2021-01-03 16:49:38 +01:00
Savegame,
Spells
}
@arrow_up key(:arrow_up)
@arrow_down key(:arrow_down)
@arrow_right key(:arrow_right)
@arrow_left key(:arrow_left)
2021-01-04 19:55:25 +01:00
@escape key(:esc)
2021-01-03 16:49:38 +01:00
@view_keymap %{
?c => :encounter,
?C => :encounter,
?l => :logbook,
?L => :logbook,
?i => :equipment,
?I => :equipment,
?h => :help,
?H => :help,
?? => :help,
?k => :spells,
?K => :spells,
?s => :savegame,
?S => :savegame
}
@view_keys Map.keys(@view_keymap)
@impl true
def init(_) do
model = %{
2021-01-04 19:55:25 +01:00
encounter: %{
name: :none,
stamina: 0,
skill: 0
2021-01-03 16:49:38 +01:00
},
hero: %{
stamina: %{max: :none, current: :none},
skill: %{max: :none, current: :none},
luck: %{max: :none, current: :none},
equipment: [],
spells: []
},
logbook: [],
2021-01-04 19:55:25 +01:00
encounters: [],
savegame: [],
overlay: nil
2021-01-03 16:49:38 +01:00
}
model
end
2021-01-04 19:55:25 +01:00
# bind to overlay scene
# TODO: implement for each overlay
2021-01-03 16:49:38 +01:00
@impl true
2021-01-04 19:55:25 +01:00
def update(%{overlay: overlay} = model, msg) do
2021-01-03 16:49:38 +01:00
2021-01-04 19:55:25 +01:00
end
2021-01-03 16:49:38 +01:00
2021-01-04 19:55:25 +01:00
# catch all other updates
@impl true
def update(model, msg) do
2021-01-03 16:49:38 +01:00
case {model, msg} do
2021-01-04 19:55:25 +01:00
# set overlay
{_,{:event, %{ch: ch}}} when ch in @view_keys ->
Update.set_overlay(model, @view_keymap[ch])
2021-01-03 16:49:38 +01:00
end
end
2021-01-04 19:55:25 +01:00
@impl true
def render(model) do
menu = Menu.render()
2021-01-03 16:49:38 +01:00
2021-01-04 19:55:25 +01:00
# top level view
view(bottom_bar: menu) do
2021-01-03 16:49:38 +01:00
2021-01-04 19:55:25 +01:00
# TODO: declare main view
2021-01-03 16:49:38 +01:00
2021-01-04 19:55:25 +01:00
# render overlay when not nil
render_overlay(model)
2021-01-03 16:49:38 +01:00
end
end
2021-01-04 19:55:25 +01:00
def render_overlay(model) do
overlay(padding: 10) do
case model.overlay do
:encounter -> Encounter.render(model.encounter)
:equipment -> Equipment.render(model.hero.equipment)
:help -> Help.render()
2021-01-03 16:49:38 +01:00
2021-01-04 19:55:25 +01:00
:logbook -> Logbook.render(model.logbook)
:savegame -> Savegame.render(model.savegame)
:spells -> Spells.render(model.hero.spells)
_ -> model # or nil?
end
end
2021-01-03 16:49:38 +01:00
end
end