Bomberman movement cleanup, notes on pacman movement

This commit is contained in:
Jeremy Penner 2021-04-04 13:26:47 -04:00
parent 238dcee158
commit 47230144bb
2 changed files with 40 additions and 25 deletions

View file

@ -35,31 +35,32 @@
; * 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)
; 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)
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))])))
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)))
(fn bomberman.update [entity dt rules]
(set entity.vel (direct bomberman.keymap (* dim.tilesize 3)))
(let [[x y] entity.pos
[dx dy] (vec* entity.vel dt)
[dx1 dy1] (bomberman.axis-movement (horizontal x y) x y dx rules)
[dy2 dx2] (bomberman.axis-movement (vertical x y) y x dy rules)
[dx dy] (if (not= dx 0) [dx1 dy1] [dx2 dy2])]
[dx dy] (if (not= dx 0) (bomberman.axis-movement horizontal x y dx rules)
(bomberman.axis-movement vertical y x dy rules))]
(set entity.pos [(+ x dx) (+ y dy)]))
(when (love.keyboard.isDown bomberman.keymap.bomb)
(rules.place-bomb (map.world-to-tile entity.pos))))

View file

@ -1,9 +1,23 @@
(local Entity (require :game.entity))
(local rules (require :game.rules))
(local util (require :lib.util))
(local dim (require :game.dim))
(local Pacman (Entity:extend))
(set Pacman.keymap {:up :up :down :down :left :left :right :right})
(set Pacman.color [1 1 0])
(local map (require :game.tilemap))
Pacman
(local pacman (util.hot-table ...))
; Pacman movement and collision rules
; * Pacman moves in the direction he was told until stopped by a wall
; * If the player tells Pacman to change direction but there is a wall there,
; Pacman's direction does not change
; * Turning a corner is funky:
; https://www.gamasutra.com/view/feature/132330/the_pacman_dossier.php?page=4
; "Whenever Pac-Man makes a pre-turn or post-turn, his orientation changes, and he starts to
; move one pixel in his new direction for every pixel traveled in his old direction, effectively
; doubling his speed as he moves at a 45 degree angle. Once he reaches the centerline of the new
; direction's path, he starts moving purely in that direction and his speed returns to normal."
; The simplest way to implement this appears to be to treat turns as having a "fudge factor" -
; Pacman enters a state where he changes direction _and_ is being pulled towards the center of
; the track. (The "post-turn" correction means we can't simply treat this as the corners having
; their edges filed off!)
; I think Bomberman actually does this too
pacman.hot