summaryrefslogtreecommitdiff
path: root/world.fnl
blob: f4a9cce129ff57fe004815631c660ab872dc5f91 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
;;;; 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 bump (require :lib.bump))
(local lume (require :lib.lume))

(local draw (require :draw))

;; Updates the position of the player contained in `world' to the world
;; coordinates, (`x', `y').
(fn position-player-at [world x y]
  (let [player (. world :player)
        collision-map (. world :collision-map)]
  (tset player :x-pos x)
  (tset player :y-pos y)
  (: collision-map :update player x y)))

;; Returns the tile at the given grid coordinates.
(fn tile-at [world x y]
  (if (and (>= x 0)
           (< x (. world :width))
           (>= y 0)
           (< y (. world :height)))
      (. world :tiles (+ 1 y) (+ 1 x))
      nil))

(local tile-sheet
       {:img (love.graphics.newImage "art/tiles.png")
        :width 32 :height 32
        :padding-x 8 :padding-y 8
        :offsets {:bricks 0}})

(local slime-sheet
       {:img (love.graphics.newImage "art/slime.png")
        :width 32 :height 32
        :padding-x 8 :padding-y 8
        :offsets {:top 0 :bottom 1 :left 2 :right 3}})

;; Draws the slime decal for a given orientation at the screen coordinates,
;; (`x', `y').
(fn draw-slime-decal [orientation x y]
  (let [tile-offset (. slime-sheet :offsets orientation)]
    (draw.tile x y slime-sheet tile-offset 0)))

;; Draws all slimed surfaces for a tile at screen coordinates, (`x', `y').
(fn draw-slime [tile x y]
  (each [orientation slimed (pairs (. tile :slimed))]
    (when slimed
      (draw-slime-decal orientation x y))))

;; Draws a single tile at the screen coordinates, (`x', `y').
(fn draw-tile [tile x y]
  (let [tile-offset (. tile-sheet :offsets (. tile :type))]
    (draw.tile x y tile-sheet tile-offset 0)
    (draw-slime tile x y)))

;; ;; Drawing routine for rendering the tiles visible from a given camera offset.
(fn draw-map [world camera-x camera-y screen-width screen-height]
  (let [tile-width (. tile-sheet :width)
        tile-height (. tile-sheet :height)
        how-many-x (+ 1 (/ screen-width tile-width))
        how-many-y (+ 1 (/ screen-height tile-height))
        start-x (math.floor (/ camera-x tile-width))
        start-y (math.floor (/ camera-y tile-height))
        width (. world :width)
        height (. world :height)]
    (for [x-offset 0 how-many-x]
      (for [y-offset 0 how-many-y]
        (when (and (and (>= (+ start-x x-offset) 0) (< (+ start-x x-offset) width))
                   (and (>= (+ start-y y-offset) 0) (< (+ start-y y-offset) height)))
          (let [tile (tile-at world (+ start-x x-offset) (+ start-y y-offset))
                screen-x (- (* x-offset tile-width) (% camera-x tile-width))
                screen-x (lume.round screen-x)
                screen-y (- (* y-offset tile-height) (% camera-y tile-height))
                screen-y (lume.round screen-y)]
            (when (~= :empty (. tile :type))
              (draw-tile tile screen-x screen-y))))))))

(fn draw-hud [world screen-width screen-height]
  (let [cur-health (. world :player :cur-health)
        max-health (. world :player :max-health)
        font-height (: (love.graphics.getFont) :getHeight)
        surfaces-slimed (. world :surfaces-slimed)
        surfaces-total (. world :surfaces-total)
        health-msg (string.format "h: %d/%d" cur-health max-health)
        slime-msg (string.format "s: %d/%d" surfaces-slimed surfaces-total)]
    (love.graphics.print health-msg 0 (- screen-height (* 2 font-height)))
    (love.graphics.print slime-msg 0 (- screen-height font-height))))

(fn draw-world [world camera-x camera-y screen-width screen-height]
  (love.graphics.clear 0.1 0.1 0.1)
  (draw-map world camera-x camera-y screen-width screen-height)
  (let [player (. world :player)
        player-x (. player :x-pos)
        player-y (. player :y-pos)
        x (- player-x camera-x)
        y (- player-y camera-y)]
    (: player :draw x y))
  (draw-hud world screen-width screen-height))

;; Returns whether or not `tile' exists in `checked'.
(fn tile-checked [tile checked]
  (var res false)
  (each [_ other (ipairs checked)]
    (when (and (= (. tile :x-pos) (. other :x-pos))
               (= (. tile :y-pos) (. other :y-pos)))
      (set res true)))
  res)

;; Modified implementation of <https://en.wikipedia.org/wiki/Flood_fill>.
(fn count-surfaces-recur [world tile checked]
  (var surfaces 0)

  (when (not (tile-checked tile checked))
    (table.insert checked tile)

    (let [x (/ (. tile :x-pos) (. tile-sheet :width))
          y (/ (. tile :y-pos) (. tile-sheet :height))
          width (. world :width)
          height (. world :height)]
      (when (>= x 0)
        (let [tile (tile-at world (- x 1) y)]
          (if (= :empty (. tile :type))
              (set surfaces (+ surfaces (count-surfaces-recur world tile checked)))
              (set surfaces (+ 1 surfaces)))))

      (when (< x width)
        (let [tile (tile-at world (+ x 1) y)]
          (if (= :empty (. tile :type))
              (set surfaces (+ surfaces (count-surfaces-recur world tile checked)))
              (set surfaces (+ 1 surfaces)))))

      (when (>= y 0)
        (let [tile (tile-at world x (- y 1))]
          (if (= :empty (. tile :type))
              (set surfaces (+ surfaces (count-surfaces-recur world tile checked)))
              (set surfaces (+ 1 surfaces)))))

      (when (< y height)
        (let [tile (tile-at world x (+ y 1))]
          (if (= :empty (. tile :type))
              (set surfaces (+ surfaces (count-surfaces-recur world tile checked)))
              (set surfaces (+ 1 surfaces)))))))

  surfaces)

;; Returns some tile in `world' of type `tile-type', or nil if no such tile is
;; present.
(fn find-any [tile-type world]
  (var res nil)
  (let [width (. world :width)
        height (. world :height)]
    (for [x 0 (- width 1)]
      (for [y 0 (- height 1)]
        (let [tile (tile-at world x y)]
          (when (= tile-type (. tile :type))
            (set res tile))))))
  res)

;; Returns the number of slime-able surfaces in the given grid of tiles.
(fn count-surfaces [world]
  (let [seed (find-any :empty world)]
    (when seed
      (count-surfaces-recur world seed []))))

(fn add-player-to-collision-map [collision-map player]
  (let [x (. player :x-pos)
        y (. player :y-pos)
        width (. player :width)
        height (. player :height)]
    (: collision-map :add player x y width height)))

(fn add-tile-to-collision-map [collision-map tile]
  (let [x (. tile :x-pos)
        y (. tile :y-pos)
        width (. tile :width)
        height (. tile :height)]
    (: collision-map :add tile x y width height)))

;; Returns a new collision map containing `player' and everything in `map'.
(fn new-collision-map [world player]
  (let [collision-map (bump.newWorld 32)
        width (. world :width)
        height (. world :height)]
    (add-player-to-collision-map collision-map player)
    (for [x 0 (- width 1)]
      (for [y 0 (- height 1)]
        (let [tile (tile-at world x y)]
          (when (~= :empty (. tile :type))
            (add-tile-to-collision-map collision-map tile)))))
    collision-map))

;; Creates a new tile object of the given `type-type' positioned at world
;; coordinates (`x', `y').
(fn new-tile [tile-type x y]
  {:width (. tile-sheet :width)
   :height (. tile-sheet :height)

   :x-pos x
   :y-pos y

   :type tile-type

   :slimed {:top false
            :bottom false
            :left false
            :right false}})

;; Returns the tile object for a given pixel in `image'.
(fn get-tile [image x y]
  (local tile-values
         {:empty [1 0 0 0]
          :bricks [0 0 0 1]})
  (var res (new-tile :empty (* x (. tile-sheet :width)) (* y (. tile-sheet :height))))
  (let [(r g b a) (: image :getPixel x y)]
    (each [tile-type colors (pairs tile-values)]
      (when (and (= (. colors 1) r)
                 (= (. colors 2) g)
                 (= (. colors 3) b)
                 (= (. colors 4) a))
        (tset res :type tile-type))))
  res)

;; Loads the tile layout of the level from the given ImageData, returning a
;; rectangular two-dimensional array.
(fn load-tiles [image]
  (var tiles [])
  (let [width (: image :getWidth)
        height (: image :getHeight)]
    (for [y 0 (- height 1)]
      (var row [])
      (for [x 0 (- width 1)]
        (table.insert row (get-tile image x y)))
      (table.insert tiles row))
    {:tiles tiles :width width :height height}))

;; Returns the metadata and objects stored in the metadata file at the given
;; path.
(fn load-meta [path]
  (fennel.dofile path))

(fn slime-tile [world tile orientation]
  (when (not (. tile :slimed orientation))
    (let [surfaces-slimed (. world :surfaces-slimed)]
      (tset world :surfaces-slimed (+ 1 surfaces-slimed))
      (tset tile :slimed orientation true))))

(fn update-world [world dt]
  (let [player (. world :player)
        collision-map (. world :collision-map)
        (goal-x goal-y) (: player :next-position dt)
        (x y collisions _) (: collision-map :move player goal-x goal-y)]
    (tset player :x-pos x)
    (tset player :y-pos y)

    (tset player :grounded false)
    (each [_ collision (ipairs collisions)]
      ;; Touching top of surface.
      (when (> 0 (. collision :normal :y))
        (tset player :grounded true)
        (slime-tile world (. collision :other) :top))

      ;; Touching bottom of surface.
      (when (< 0 (. collision :normal :y))
        (slime-tile world (. collision :other) :bottom))

      ;; Touching left of surface.
      (when (> 0 (. collision :normal :x))
        (slime-tile world (. collision :other) :left))

      ;; Touching right of surface.
      (when (< 0 (. collision :normal :x))
        (slime-tile world (. collision :other) :right)))

    (tset player :x-vel (: player :next-x-vel dt))
    (tset player :y-vel (: player :next-y-vel dt))))

;; Loads the map of the given name into a new world containing `player'. Will
;; error out if either 'maps/${name}.png' or 'maps/${name}.fnl' do not exist.
(fn new-world [name player]
  (let [tiles-path (.. "maps/" name ".png")
        meta-path (.. "maps/" name ".fnl")
        res (load-tiles (love.image.newImageData tiles-path))
        res (lume.extend res {:player player})
        res (lume.extend res {:surfaces-slimed 0 :meta (load-meta meta-path)})
        res (lume.extend res {:surfaces-total (count-surfaces res)})
        res (lume.extend res {:collision-map (new-collision-map res player)})
        res (lume.extend res {:position-player-at position-player-at})
        res (lume.extend res {:draw draw-world})
        res (lume.extend res {:update update-world})]
    res))

{:tile-sheet tile-sheet
 :tile-at tile-at
 :new new-world}