summary refs log tree commit diff
path: root/lib/main.fnl
blob: 8185f10a47d50d919319e9edd1f13cb9aa540232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
(local lume (require :vendor.lume))
(local proto (require :lib.proto))
(local state (require :lib.state))
(local game (require :lib.game))
(local music (require :lib.music))
(local font (require :lib.font))

;; i am thinking we could actually do a really hacky thing (modules add themselves
;; to this list) with this later but
;; i'm not sure if it'd be worth it (it'd require those dependency loops maybe)
;; TODO: ^
(local hotswap-modules
  [:lib.player
   :lib.music
   :lib.translation
   :lib.bullet
   :lib.cells
   :lib.game
   :lib.transition
   :lib.main])

;; the
;; oh thats why it doesnt work lmao

(fn love.load []
  (music.load)
  (love.keyboard.setKeyRepeat true)
  (global the-state (state.init game))
  (global messages (or messages {})))

(fn love.draw []
  (match (pcall #(state.draw the-state))
    (true x) nil
    (false x) (do
                (love.graphics.reset)
                (print (.. "draw \n" x))
                (love.graphics.print (.. "draw: \n" x))))
  (love.graphics.reset)
  ;; (love.graphics.setFont font.small)
  ;; (love.graphics.print (love.timer.getFPS))
  ;; (love.graphics.print :soko font.big 100 100)
  (when true ;; debug stuff
    (love.graphics.print (table.concat
                           (lume.map messages #$.msg)
                           "\n")
                         0
                         40)
    (each [i v (lume.ripairs messages)]
      (if (= v.ticks 0)
          (table.remove messages i)
          (set v.ticks (- v.ticks 1))))))

(var dt-acc 0)

(fn love.update [dt]
  (set dt-acc (math.min (/ 20) (+ dt-acc dt)))
  (while (> dt-acc (/ 60))
    (set dt-acc (- dt-acc (/ 60)))
    (music.pre-update)
    ;; TODO: make state changes actually possible
    (match (pcall #(state.update the-state))
      (true next) (global the-state next)
      (true nil) nil
      (false x) (do
                  (print (.. "update: \n" x))
                  (table.insert messages
                                {:ticks 1
                                 :msg (.. "update: \n" x)})))
    (music.update)))

(fn love.mousepressed [x y button]
  (match (pcall #(state.mousepressed the-state x y button))
    (true x) nil
    (false x) (do
                (print (.. "mousepressed: \n" x))
                (table.insert messages
                              {:ticks 5
                               :msg (.. "mousepressed: \n" x)}))))

(fn love.keypressed [key scancode repeat]
  (when (and (= key "f") (not repeat))
    (love.window.setFullscreen (not (love.window.getFullscreen))))
  (match (pcall #(state.keypressed the-state key scancode repeat))
    (true x) nil
    (false x) (do
                (print (.. "keypressed: \n" x))
                (table.insert messages
                              {:ticks 5
                               :msg (.. "keypressed: \n" x)})))
  (when (and (= key "r") false)
    (each [k v (lume.ripairs messages)]
      (when (= v.type :reload-error)
        (table.remove messages k)))
    (print (.. (if (love.keyboard.isDown :lshift) :hard :soft)
               " reloading..."))
    (each [_ v (ipairs hotswap-modules)]
      (match (lume.hotswap v)
        (nil x) (table.insert messages
                             {:ticks -1
                              :type :reload-error
                              :msg (.. "can't reload module " v "\n" x)})))
    (when (love.keyboard.isDown :lshift)
      (love.load))))