bomberpac/game/entities/bomberman.fnl

73 lines
3 KiB
Plaintext
Raw Normal View History

(local util (require :lib.util))
(local dim (require :game.dim))
2021-04-11 03:39:42 +00:00
(local {: direct : draw : update} (util.require :game.entity))
2021-04-04 02:36:12 +00:00
(local {: edge : edge-crosses : vec*} (util.require :game.helpers))
2021-04-11 03:39:42 +00:00
(local {: defmethod} (util.require :lib.multimethod))
(local map (require :game.tilemap))
(local bomberman (util.hot-table ...))
(set bomberman.keymap {:up :w :down :s :left :a :right :d :bomb :x})
2021-04-11 03:39:42 +00:00
(defmethod draw :bomberman (fn [{:pos [x y]}]
(love.graphics.setColor 0.2 0.2 0.2)
2021-04-11 03:39:42 +00:00
(love.graphics.circle :fill x y dim.halftile)))
2021-04-04 02:36:12 +00:00
(fn tile-at [x y rules]
(-?> [x y]
(map.world-to-tile)
2021-04-04 02:36:12 +00:00
(rules.tile-at)))
(fn bomberman.collides? [[x y] rules]
(let [tile (tile-at x y rules)]
(and tile (or tile.bomb tile.wall))))
(fn bomberman.tile-edge [c dc]
(let [tc (math.floor (/ c dim.tilesize))
cfloor (* tc dim.tilesize)]
(if (= dc 0) c
(> dc 0) (- cfloor dim.halftile)
(+ cfloor dim.tilesize dim.halftile))))
; Collision model for Bomberman works like this (in order):
; * if the moving edge is NOT crossing over a tile boundary, allow the movement
; * if there is no solid tile in in front of Bomberman, allow the movement
; * if there are two solid tiles in front of Bomberman, push Bomberman back onto
; the tile boundary
; * if there is one solid and one empty tile in front of Bomberman, push Bomberman
; back onto the tile boundary, and push Bomberman towards the empty tile in the
; other axis
; Movement only happens along one axis; diagonal movement is not allowed.
(fn horizontal [c copp] [c copp])
(fn vertical [c copp] [copp c])
(fn bomberman.axis-movement [make-xy c copp dc rules]
(let [(edge-prev edge-next) (edge c dc dim.halftile)
2021-04-04 02:36:12 +00:00
crosses (edge-crosses edge-prev edge-next dim.tilesize)
neg-edge (- copp dim.halftile)
neg-collides (bomberman.collides? (make-xy edge-next neg-edge) rules)
pos-edge (+ copp dim.halftile -0.01)
pos-collides (bomberman.collides? (make-xy edge-next pos-edge) rules)
dc-edge (- (bomberman.tile-edge edge-next dc) c)
dcopp (math.abs dc)
[cn coppn]
(if (not crosses) [dc 0]
(and (not neg-collides) (not pos-collides)) [dc 0]
(and neg-collides pos-collides) [dc-edge 0]
neg-collides [dc-edge (math.min dcopp (- (bomberman.tile-edge (+ pos-edge dim.tilesize) 1) copp))]
pos-collides [dc-edge (math.max (- dcopp) (- (bomberman.tile-edge (- neg-edge dim.tilesize) -1) copp))])]
(make-xy cn coppn)))
2021-04-11 03:39:42 +00:00
(defmethod update :bomberman (fn [entity dt rules]
(set entity.vel (direct bomberman.keymap (* dim.tilesize 3)))
2021-04-04 03:13:33 +00:00
(let [[x y] entity.pos
[dx dy] (vec* entity.vel dt)
[dx dy] (if (not= dx 0) (bomberman.axis-movement horizontal x y dx rules)
(bomberman.axis-movement vertical y x dy rules))]
2021-04-04 03:13:33 +00:00
(set entity.pos [(+ x dx) (+ y dy)]))
(when (love.keyboard.isDown bomberman.keymap.bomb)
2021-04-11 03:39:42 +00:00
(rules.place-bomb (map.world-to-tile entity.pos)))))
(fn bomberman.new [pos]
2021-04-11 03:39:42 +00:00
{: pos :vel [0 0] :entity :bomberman})
bomberman.hot