From ab692600912886f13e08bb937dbe6c06393e4a8e Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Sun, 11 Apr 2021 22:01:34 -0400 Subject: [PATCH] animate pacman --- game/entities/pacman.fnl | 23 ++++++++++++++++++++--- game/init.fnl | 3 ++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/game/entities/pacman.fnl b/game/entities/pacman.fnl index 05a32de..71c2f84 100644 --- a/game/entities/pacman.fnl +++ b/game/entities/pacman.fnl @@ -2,14 +2,24 @@ (local dim (require :game.dim)) (local {: defmethod} (util.require :lib.multimethod)) (local {: direct : draw : update} (util.require :game.entity)) +(local {: vec*} (util.require :game.helpers)) (local map (require :game.tilemap)) (local pacman (util.hot-table ...)) (set pacman.keymap {:up :up :down :down :left :left :right :right}) -(defmethod draw :pacman (fn [{:pos [x y]}] - (love.graphics.setColor 1 1 0) - (love.graphics.circle :fill x y dim.halftile))) +(defmethod draw :pacman (fn [{:pos [x y] :vel [dx dy]}] + (let [mouthsize-max (if (and (= dx 0) (= dy 0)) 0 (/ math.pi 4)) + mouthsize-ratio (math.abs (math.sin (* (love.timer.getTime) 16))) + mouthsize (* mouthsize-max mouthsize-ratio) + anglestart (if (< dx 0) math.pi + (> dx 0) 0 + (> dy 0) (/ math.pi 2) + (+ math.pi (/ math.pi 2))) + angle1 (+ anglestart mouthsize) + angle2 (+ anglestart math.pi math.pi (- mouthsize))] + (love.graphics.setColor 1 1 0) + (love.graphics.arc :fill x y dim.halftile angle1 angle2)))) ; Pacman movement and collision rules ; * Pacman moves in the direction he was told until stopped by a wall @@ -27,6 +37,13 @@ ; their edges filed off!) ; I think Bomberman actually does this too +(defmethod update :pacman (fn [entity dt rules] + (let [[dx dy] entity.vel + [dxkey dykey] (direct pacman.keymap (* dim.tilesize 4)) + turn-attempted (and (or (not= dxkey 0) (not= dykey 0)) + (or (not= dxkey dx) (not= dykey dy)))] + (when turn-attempted + (set entity.vel [dxkey dykey]))))) (fn pacman.new [pos] {: pos :vel [0 0] :entity :pacman}) diff --git a/game/init.fnl b/game/init.fnl index 9ebbf7e..12efa60 100644 --- a/game/init.fnl +++ b/game/init.fnl @@ -10,11 +10,12 @@ (local map (require :game.tilemap)) (local tile (require :game.tiles)) (local bomberman (require :game.entities.bomberman)) +(local pacman (require :game.entities.pacman)) (local rules (require :game.rules)) (modes:register :game gamemode) -(set state.entities [(bomberman.new [48 48])]) +(set state.entities [(bomberman.new [48 48]) (pacman.new [112 112])]) (set state.map (map.new-tilemap 28 31 tile.bombertile (tile.itile-named tile.bombertile :empty))) (set state.bombs (map.new-entitymap state.map.w state.map.h)) (rules.generate-maze state.map)