summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob L. Kreuze <jakob@memeware.net>2018-10-21 16:07:50 -0400
committerJakob L. Kreuze <jakob@memeware.net>2018-10-21 16:07:50 -0400
commitfdf869cdddf66b1c1cb27380e157d580a5e3fb82 (patch)
tree1cf24542a9dcfd73c64bf9a73512457b600b579c
parent069c31331795e6501fda64ef4bf1d15771a43cfd (diff)
Initial movement etc
-rw-r--r--art/swanky.pngbin4625 -> 5186 bytes
-rw-r--r--sandbox.fnl232
-rw-r--r--wrap.fnl14
3 files changed, 205 insertions, 41 deletions
diff --git a/art/swanky.png b/art/swanky.png
index 250d563..2342e45 100644
--- a/art/swanky.png
+++ b/art/swanky.png
Binary files differ
diff --git a/sandbox.fnl b/sandbox.fnl
index f633a0c..b5f84a6 100644
--- a/sandbox.fnl
+++ b/sandbox.fnl
@@ -16,32 +16,12 @@
;;;; with swanky. If not, see <http://www.gnu.org/licenses/>.
(local bump (require :lib.bump))
+(local lume (require :lib.lume))
+
(local map (require :map))
(local sandbox (map.load "sandbox"))
-;; (var swanky-frame 0)
-
-;; (fn load-sprite [name frame-width frame-height]
-;; (let [img (love.graphics.newImage name)]
-;; {:img img
-;; :frame-width frame-width
-;; :frame-height frame-height}))
-
-;; (fn draw-animated [x y sprite animation frame]
-;; (let [img (. sprite :img)
-;; width (. sprite :frame-width)
-;; height (. sprite :frame-height)]
-;; (let [quad (love.graphics.newQuad (* frame width)
-;; (* animation height)
-;; width
-;; height
-;; (: img :getWidth)
-;; (: img :getHeight))]
-;; (love.graphics.draw img quad x y 0 1 1 0 0))))
-
-;; (fn load-player [])
-
(local tiles (love.graphics.newImage "art/tiles.png"))
(local tile-width 32)
(local tile-height 32)
@@ -49,8 +29,8 @@
;; Drawing routine for rendering the tiles visible from a given camera offset.
(fn draw-tiles [map camera-x camera-y]
- (let [how-many-x (math.floor (/ canvas-width tile-width))
- how-many-y (math.floor (/ canvas-height tile-height))
+ (let [how-many-x (math.ceil (+ 3 (/ screen-width tile-width))) ; fixme: why +3?
+ how-many-y (math.ceil (+ 2 (/ screen-height tile-height))) ; fixme: why +2?
start-x (math.floor (/ camera-x tile-width))
start-y (math.floor (/ camera-y tile-height))
width (. map :width)
@@ -69,30 +49,208 @@
tile-height
(: tiles :getWidth)
(: tiles :getHeight))]
- (love.graphics.draw tiles quad x y 0 1 1 0 0))))))))
+ (love.graphics.draw tiles quad x y))))))))
+
+;; (local swanky-tile-width 32)
+;; (local swanky-tile-height 32)
+;; (local swanky-width 24)
+;; (local swanky-height 22)
+
+;; Okay, here are the things we need to keep track of:
+;; - Camera
+;; - camera-x
+;; - camera-y
+;; - camera-x-velocity
+;; - camera-y-velocity
+;;
+;; - Player
+;; - orientation (right vs left)
+;; - x
+;; - y
+;; - x-velocity
+;; - y-velocity
+;; - goal-x-velocity
+;; - goal-y-velocity
+;; - Whether or not is grounded
+;; - Jumping (?)
+;; - The current action (moving right, etc.)
+;;
+;; - Animations
+;; - offsets
+;; - whether or not to sustain
+;; - number of frames
+
+;; ---
+
+(local swanky-animation-offsets {:idle 0
+ :jump 2
+ :fall 3})
+(local swanky-animation-sustains {:idle false
+ :jump true
+ :fall false})
+(local swanky-animation-frame-counts {:idle 2
+ :jump 4
+ :fall 4})
+
+(local swanky (love.graphics.newImage "art/swanky.png"))
+(local swanky-width 32)
+(local swanky-height 32)
+
+(fn draw-swanky [x y animation frame orientation]
+ (let [x-offset (+ (* frame swanky-width)
+ (if (= orientation :left) (* 4 swanky-width) 0))
+ y-offset (* (. swanky-animation-offsets animation) swanky-height)]
+ (let [quad (love.graphics.newQuad x-offset
+ y-offset
+ swanky-width
+ swanky-height
+ (: swanky :getWidth)
+ (: swanky :getHeight))]
+ (love.graphics.draw swanky quad x y))))
+
+(var swanky-orientation :right)
+
+(local swanky-animation-frame-delay 16)
+(var swanky-animation-frame-timer swanky-animation-frame-delay)
+(var swanky-animation-frame 0)
+(var swanky-animation :idle)
+
+(var swanky-animation-stack [])
+
+(var swanky-x 128)
+(var swanky-y 128)
+(var swanky-x-vel 0)
+(var swanky-y-vel 0)
+
+(var swanky-moving-right false)
+(var swanky-moving-left false)
+
+(local swanky-x-vel-goal 128)
+(local swanky-y-vel-goal 128)
+
+(var swanky-jumping false)
+(var swanky-grounded false)
+(var swanky-should-play-grounding-animation true)
+
+(var gravity 128)
(var camera-x 64)
(var camera-y 64)
(var camera-x-vel 0)
(var camera-y-vel 0)
-(var camera-animation-theta 0)
+
+(local camera-lock-goal-x (math.floor (+ (/ screen-width 2) (/ swanky-width 2))))
+(local camera-lock-goal-y (math.floor (+ (/ screen-height 2) (/ swanky-height 2))))
+
+(local world (bump.newWorld 32))
+(: world :add :swanky swanky-x swanky-y swanky-width swanky-height)
+
+(for [x 0 (- (. sandbox :width) 1)]
+ (for [y 0 (- (. sandbox :height) 1)]
+ (let [tile (. sandbox :tiles (+ 1 y) (+ 1 x))]
+ (when (~= :empty tile)
+ (: world :add [tile] (* x tile-width) (* y tile-height) tile-width tile-height)))))
(fn draw [message]
- (love.graphics.clear 255 255 255)
+ (love.graphics.clear 1 1 1)
(draw-tiles sandbox camera-x camera-y)
- (love.graphics.print (.. "Welcome to " (. sandbox :meta :name)) 0 0))
+ (let [x (- swanky-x camera-x)
+ y (- swanky-y camera-y)]
+ (draw-swanky x y swanky-animation swanky-animation-frame swanky-orientation)))
(fn update [dt set-mode]
- (set camera-animation-theta (+ camera-animation-theta (/ math.pi 16)))
- (when (>= camera-animation-theta (* 2 math.pi))
- (set camera-animation-theta 0))
- (set camera-x-vel (* 8 (math.sin camera-animation-theta)))
- (set camera-y-vel (* 8 (math.cos camera-animation-theta)))
- (set camera-x (+ camera-x camera-x-vel))
- (set camera-y (+ camera-y camera-y-vel)))
+ ;; Update animations.
+ (when (< 0 (# swanky-animation-stack))
+ (set swanky-animation (table.remove swanky-animation-stack (# swanky-animation-stack)))
+ (set swanky-animation-frame 0))
+
+ (when (> 0 swanky-animation-frame-timer)
+ (let [frame-count (. swanky-animation-frame-counts swanky-animation)
+ sustain (. swanky-animation-sustains swanky-animation)]
+ (if (and (= frame-count (+ 1 swanky-animation-frame)) (not sustain))
+ (do
+ (set swanky-animation-frame 0)
+ (when (~= swanky-animation :idle)
+ (set swanky-animation :idle)))
+ (> frame-count (+ 1 swanky-animation-frame))
+ (set swanky-animation-frame (+ 1 swanky-animation-frame))))
+ (set swanky-animation-frame-timer swanky-animation-frame-delay))
+
+ (set swanky-animation-frame-timer (- swanky-animation-frame-timer (* 64 dt)))
+
+ ;; Update camera.
+ (set camera-x (lume.lerp camera-x (- swanky-x camera-lock-goal-x) dt))
+ (set camera-y (lume.lerp camera-y (- swanky-y camera-lock-goal-y) dt))
+
+ ;; Lock camera so that it doesn't go out of bounds.
+ (when (> 0 camera-x)
+ (set camera-x 0))
+
+ (when (> 0 camera-y)
+ (set camera-y 0))
+
+ (when (>= camera-x (- (* tile-width (- (. sandbox :width) 3)) screen-width))
+ (set camera-x (- (* tile-width (- (. sandbox :width) 3)) screen-width)))
+
+ (when (>= camera-y (- (* tile-height (- (. sandbox :height) 2)) screen-height))
+ (set camera-y (- (* tile-height (- (. sandbox :height) 2)) screen-height)))
+
+ ;; Update the swanky's velocities to reflect controls.
+ (let [swanky-x-vel-goal (if swanky-moving-right swanky-x-vel-goal
+ swanky-moving-left (- swanky-x-vel-goal)
+ 0)]
+ (set swanky-x-vel (lume.lerp swanky-x-vel swanky-x-vel-goal dt)))
+
+ (when (and swanky-jumping swanky-grounded)
+ (set swanky-y-vel (- swanky-y-vel swanky-y-vel-goal)))
+
+ ;; Update the y-velocity to reflect gravity.
+ (if (and swanky-grounded (not swanky-jumping))
+ (set swanky-y-vel 0)
+ (set swanky-y-vel (+ swanky-y-vel (* gravity dt))))
+
+ (when (and (> 0 swanky-y-vel) (not swanky-grounded))
+ (set swanky-should-play-grounding-animation true))
+
+ ;; Calculate the goal position for swanky.
+ (set swanky-x (+ swanky-x (* dt swanky-x-vel)))
+ (set swanky-y (+ swanky-y (* dt swanky-y-vel)))
+
+ ;; Perform collision detection and update swanky's position accordingly
+ (let [(actual-x actual-y collisions length) (: world :move :swanky swanky-x swanky-y)]
+ (set swanky-x actual-x)
+ (set swanky-y actual-y)
+ (set swanky-grounded false)
+ (each [_ collision (ipairs collisions)]
+ (when (> 0 (. collision :normal :y))
+ (set swanky-grounded true)
+ (when swanky-should-play-grounding-animation
+ (table.insert swanky-animation-stack :fall)
+ (set swanky-should-play-grounding-animation false))))))
+
+(fn keypressed [key set-mode]
+ (if (= key "d")
+ (do (set swanky-moving-right true)
+ (set swanky-orientation :right))
+ (= key "a")
+ (do (set swanky-moving-left true)
+ (set swanky-orientation :left))
+ (= key "w")
+ (do
+ (set swanky-jumping true)
+ (set swanky-animation :jump))))
-(fn keypressed [key set-mode])
+(fn keyreleased [key set-mode]
+ (if (= key "d")
+ (do (set swanky-x-vel (/ swanky-x-vel 2))
+ (set swanky-moving-right false))
+ (= key "a")
+ (do (set swanky-x-vel (/ swanky-x-vel 2))
+ (set swanky-moving-left false))
+ (= key "w")
+ (set swanky-jumping false)))
{:draw draw
:update update
- :keypressed keypressed}
+ :keypressed keypressed
+ :keyreleased keyreleased}
diff --git a/wrap.fnl b/wrap.fnl
index 3fe2cb1..04e6290 100644
--- a/wrap.fnl
+++ b/wrap.fnl
@@ -19,10 +19,14 @@
;;;; This module contains non-game-specific bits and mode-changing logic.
-(global canvas-width 640)
-(global canvas-height 480)
+(var scale 2)
+(local canvas-width 640)
+(local canvas-height 480)
+
+(global screen-width (math.floor (/ canvas-width scale)))
+(global screen-height (math.floor (/ canvas-height scale)))
-(local canvas (love.graphics.newCanvas 640 480))
+(local canvas (love.graphics.newCanvas canvas-width canvas-height))
(local font (love.graphics.newImageFont "art/font.png"
(.. " !\"#$%&*()*+,-./"
"0123456789"
@@ -31,7 +35,6 @@
"abcdefghijklmnopqrstuvwxyz"
"[\\]^_`{|}~")))
-(var scale 2)
(var mode (require :sandbox))
(fn set-mode [mode-name ...]
@@ -72,3 +75,6 @@
:else
(mode.keypressed key set-mode)))
+
+(fn love.keyreleased [key]
+ (mode.keyreleased key set-mode))