bomberpac/game/entities/bomberman.fnl

78 lines
3.2 KiB
Plaintext
Raw Normal View History

(local util (require :lib.util))
(local dim (require :game.dim))
(local {: direct : move} (util.require :game.entity))
2021-04-04 02:36:12 +00:00
(local {: edge : edge-crosses : vec*} (util.require :game.helpers))
(local map (require :game.tilemap))
(local bomberman (util.hot-table ...))
(set bomberman.keymap {:up :w :down :s :left :a :right :d :bomb :x})
(fn bomberman.draw [entity]
(love.graphics.setColor 0.2 0.2 0.2)
(love.graphics.circle :fill (. entity.pos 1) (. entity.pos 2) (/ dim.tilesize 2)))
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 wall-collides? [x y rules]
(-?> (tile-at x y rules)
(. :wall)))
(fn bomb-collides? [x y rules]
(-?> (tile-at x y rules)
(. :bomb)))
(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
; repeat for both x and y axis
; TODO: do we add the two vectors? is it acceptable that diagonal movement is faster?
(fn horizontal [x y] (fn [dx dy] [(+ x dx) (+ y dy)]))
(fn vertical [x y] (fn [dy dx] [(+ x dx) (+ y dy)]))
(fn bomberman.axis-movement [off c copp dc rules]
(let [d (fn [cnew] (- cnew c))
(edge-prev edge-next) (edge c dc dim.halftile)
crosses (edge-crosses edge-prev edge-next dim.tilesize)
neg-collides (bomberman.collides? (off (d edge-next) (- dim.halftile)) rules)
pos-collides (bomberman.collides? (off (d edge-next) (- dim.halftile 0.01)) rules)
dc-edge (d (bomberman.tile-edge edge-next dc))
dcopp (math.abs dc)]
(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 (+ copp dim.tilesize (- dim.halftile 0.01)) 1) copp))]
pos-collides [dc-edge (math.max (- dcopp) (- (bomberman.tile-edge (- copp dim.tilesize dim.halftile) -1) copp))])))
(fn bomberman.update [entity dt rules]
(set entity.vel (direct bomberman.keymap (* dim.tilesize 3)))
(local [x y] entity.pos)
(local [dx dy] (vec* entity.vel dt))
2021-04-04 02:36:12 +00:00
(let [[dx1 dy1] (bomberman.axis-movement (horizontal x y) x y dx rules)
[dy2 dx2] (bomberman.axis-movement (vertical x y) y x dy rules)]
(set entity.pos [(+ x dx1 dx2) (+ y dy1 dy2)]))
(when (love.keyboard.isDown bomberman.keymap.bomb)
(rules.place-bomb (map.world-to-tile entity.pos))))
(fn bomberman.new [pos]
{: pos :vel [0 0] :draw #(bomberman.draw $...) :update #(bomberman.update $...)})
bomberman.hot