dialogue.fnl 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. (var _dialogue [])
  2. (var _button_dirty false)
  3. (fn dialogue? [] (not (empty? _dialogue)))
  4. (fn dialogue! [f] (add _dialogue f))
  5. (fn enter-btn? [] (or (btnp 4) (keyp 50)))
  6. (fn cancel-btn? [] (or (btnp 5)))
  7. (fn action-btn? [] (or (btnp 5)))
  8. (fn exit-dialogue []
  9. (set _dialogue [])
  10. (set _button_dirty true))
  11. (fn dialogue-back []
  12. (if (not (empty? _dialogue))
  13. (remove _dialogue (last _dialogue))))
  14. (fn draw-menu [title color bg]
  15. (clip 20 20 200 100)
  16. (cls)
  17. (rect 21 21 198 9 (or bg 2))
  18. (print title 24 23 (or color 15))
  19. (print "[x]" 205 23)
  20. (when (cancel-btn?)
  21. (sfx 61)
  22. (set _button_dirty true)
  23. (dialogue-back)))
  24. (fn option-list [v opts state colors]
  25. ;draw a list of text with a selected cursor
  26. ;handle keys for moving cursor or choosing something
  27. ;returns a value if something has been chosen
  28. (if (not state.idx)
  29. (if (not (empty? opts)) (tset state :idx 1)))
  30. (each [i s (ipairs opts)]
  31. (if colors
  32. (print s (+ v.x 8) (+ v.y (* (dec i) 8)) (. colors i))
  33. (print s (+ v.x 8) (+ v.y (* (dec i) 8))))
  34. (if (= i state.idx)
  35. (spr 240 (- v.x 2) (+ v.y (* (dec i) 8)))))
  36. (if (or (btnp 0) (btnp 1))
  37. (sfx 63))
  38. (if
  39. (btnp 0)
  40. (if (= 1 state.idx)
  41. (set state.idx (# opts))
  42. (set state.idx (dec state.idx)))
  43. (btnp 1)
  44. (if (= (# opts) state.idx)
  45. (set state.idx 1)
  46. (set state.idx (inc state.idx)))
  47. (enter-btn?)
  48. (do
  49. (sfx 63)
  50. state.idx)))
  51. (fn items-menu [col title filt f]
  52. ;show title and filtered list of item collection,
  53. ;selecting an item calls f on it
  54. (let [state {}
  55. inv (filter filt col)
  56. names (map (fn [o] o.name) inv)
  57. colors (map (fn [o] o.color) inv)]
  58. (dialogue! (fn []
  59. (draw-menu title 11)
  60. (let [cmd (option-list (point 30 40) names state colors)]
  61. (when cmd (sfx 62) (f (. inv cmd))))))))
  62. (fn inventory-menu [title filt f]
  63. (items-menu player.inventory title filt f))
  64. (fn equipment-menu [title filt f]
  65. (items-menu (vals player.equip) title filt f))
  66. (fn command-menu [s]
  67. (var state {})
  68. (dialogue! (fn []
  69. (draw-menu "commands" 6)
  70. (let [cmd (option-list (point 30 40) ["drop" "wear" "use" "remove"] state)]
  71. (if (= cmd 1) (inventory-menu "drop" identity (fn [o] (drop player o) (exit-dialogue)))
  72. (= cmd 3) (inventory-menu "use" identity (fn [o] (exit-dialogue)))
  73. (= cmd 2) (inventory-menu "wear" (fn [o] (if o.wear true)) (fn [o] (wear player o) (exit-dialogue)))
  74. (= cmd 4) (equipment-menu "remove" identity (fn [o] (unwear player o) (exit-dialogue)))
  75. )))))
  76. (fn _shop [k]
  77. (var state {})
  78. (dialogue! (fn []
  79. (draw-menu (.. k " store") 6)
  80. (let [cmd (option-list (point 30 40) ["buy" "sell"] state)]
  81. (if (= cmd 1) (inventory-menu "buy" identity (fn [o] (exit-dialogue)))
  82. (= cmd 2) (inventory-menu "sell" identity (fn [o] (exit-dialogue)))
  83. )))))
  84. (set shop _shop)