summaryrefslogtreecommitdiff
path: root/menu.fnl
blob: e6961614f50c4da919e3b147ff130a365743259a (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
;;;; Copyright (C) 2018 Jakob L. Kreuze, All Rights Reserved.
;;;;
;;;; This file is part of slime-the-world.
;;;;
;;;; slime-the-world is free software: you can redistribute it and/or modify it
;;;; under the terms of the GNU General Public License as published by the Free
;;;; Software Foundation, either version 3 of the License, or (at your option)
;;;; any later version.
;;;;
;;;; slime-the-world is distributed in the hope that it will be useful, but
;;;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
;;;; more details.
;;;;
;;;; You should have received a copy of the GNU General Public License along
;;;; with slime-the-world. If not, see <http://www.gnu.org/licenses/>.

(local camera (require :camera))
(local player (require :player))
(local world (require :world))

(local logo (love.graphics.newImage "art/logo.png"))

(local menu-music (love.audio.newSource "music/nesulos.it" "stream"))
(: menu-music :setVolume 0.5)
(: menu-music :setLooping true)
(: menu-music :play)

(fn button-width [button]
  (: (love.graphics.getFont) :getWidth (. button :text)))

(fn button-height [button]
  (: (love.graphics.getFont) :getHeight))

(fn new-button [text offset function]
  {:text text
   :offset offset
   :function function})

(fn draw-logo [screen-width screen-height]
  (let [x (/ (- screen-width (: logo :getWidth)) 2)
        y (/ (- screen-height (: logo :getHeight)) 8)]
    (love.graphics.draw logo x y)))

(fn draw-button [button]
  (let [text (. button :text)
        offset (. button :offset)
        width (button-width button)
        height (button-height button)
        x (/ (- screen-width width) 2)
        y (+ (* height offset)
               (/ (- screen-height height) 2))]
    (love.graphics.print text x y)))

(fn play-clicked [set-mode]
  (: menu-music :stop)
  (set-mode :game))

(fn sound-clicked []
  (love.audio.setVolume (if (= (love.audio.getVolume) 0) 1 0)))

(fn quit-clicked []
  (love.event.quit))

(local buttons [(new-button "Play" 0 play-clicked)
                (new-button "Toggle Sound" 1 sound-clicked)
                (new-button "Quit" 2 quit-clicked)])

;; Demo world.
(local current-player (player.new))
(local current-world (world.new "demo" current-player))
(local current-camera (camera.new current-world screen-width screen-height))

(local demo-actions [:move-right 1 :stop-right 1 :move-left 1 :jump])
(var demo-action-index 1)
(var demo-action-timer 0)

(fn draw []
  (let [camera-x (. current-camera :x-pos)
        camera-y (. current-camera :y-pos)]
    (: current-world :draw camera-x camera-y screen-width screen-height))
  (draw-logo screen-width screen-height)
  (each [_ button (ipairs buttons)]
    (draw-button button)))

(fn update [dt]
  (set demo-action-timer (- demo-action-timer dt))
  (when (> 0 demo-action-timer)
    (if (= "number" (type (. demo-actions demo-action-index)))
        (set demo-action-timer (. demo-actions demo-action-index))
        (: current-player (. demo-actions demo-action-index)))
    (set demo-action-index (+ 1 demo-action-index))
    (when (> demo-action-index (# demo-actions))
      (set demo-action-index 1)))

  (: current-world :update dt)
  (: current-player :update dt)
  (let [(camera-x camera-y) (: current-camera :focus-on-object current-player dt)]
    (tset current-camera :x-pos camera-x)
    (tset current-camera :y-pos camera-y)))

(fn keypressed [])

(fn keyreleased [])

(fn click [mouse-x mouse-y set-mode]
  (each [_ button (ipairs buttons)]
    (let [offset (. button :offset)
          width (button-width button)
          height (button-height button)
          x (/ (- screen-width width) 2)
          y (+ (* height offset)
               (/ (- screen-height height) 2))]
      (when (and (>= mouse-x x) (<= mouse-x (+ x width))
                 (>= mouse-y y) (<= mouse-y (+ y height)))
        ((. button :function) set-mode)))))

{:draw draw
 :update update
 :keypressed keypressed
 :keyreleased keyreleased
 :click click}