platform-with-crappy-collission-det.fnl

#744
Raw
Author
winny
Created
May 4, 2023, 6:05 a.m.
Expires
Never
Size
3.9 KB
Hits
150
Syntax
Fennel
Private
✗ No
;; title:   platform PoC
;; author:  game developer, email, etc.
;; desc:    short description
;; site:    website link
;; license: MIT License (change this to your license of choice)
;; version: 0.1
;; script:  fennel
;; strict:  true

(var UP 0)
(var DOWN 1)
(var LEFT 2)
(var RIGHT 3)
(var A 4)
(var B 5)
(var X 6)
(var Y 7)
(var PLATFORM-THICKNESS 3)
(var PLATFORM-COLOR 3)
(var BG 1)
(var AVATAR-SIZE 4)
(var AVATAR-COLOR 5)

(fn p [x y width]
  {:x x :y y :width width :height PLATFORM-THICKNESS})

(var platforms [(p 0 125 240)
                (p 20 110 40)
                (p 80 90 20)
                ])
(var pos-x 120)
(var pos-y 20)
(var jumping 0)
(var double-jump false)
(var show-debug false)
(var tic-collision nil)


(fn jumpingp []
  (and jumping (> jumping 0)))
;; Is the avator airborne?
(fn airbornep []
  (not tic-collision))

(macro trace2 [expr]
  `(let [expr# ,expr]
     (do
       (trace (string.format "%s -> %s"
                             ,(view expr)
                             expr#))
       expr#)))

(fn collides? [x y platform]
  (and (< platform.x (+ x AVATAR-SIZE))     ; From the left
       (< x (+ platform.x platform.width)) ; From the right
       (< y (+ platform.y platform.height)) ; From below
       (< platform.y (+ y AVATAR-SIZE))))  ; From Above

(fn find-collision [x y]
  (each [_ value (ipairs platforms) ]
    (when (collides? x y value)
      (trace (string.format "collision! %.2f" (time)))
      (lua "return value")))
  nil)

(fn move [dx dy]
  (var target-x (math.max 0 (+ dx pos-x)))
  (var target-y (math.max 0 (+ dy pos-y)))
  (match (find-collision target-x target-y)
    nil (do
          (trace (.. "ok " (time)))
            (set pos-x target-x)
            (set pos-y target-y)
            (set tic-collision nil))
    {:x px :y py :width pwidth :height pheight}
    (do
      (set tic-collision {:x px :y py :width pwidth :height pheight})
      (when (< pos-y target-y)
        (trace "moving...")
          (set target-x (- py AVATAR-SIZE 1))))))

(fn _G.TIC []
  (when (btn RIGHT) (move 1 0))
  (when (btn LEFT) (move -1 0))
  (when (and (btn UP) (not double-jump))
    (when (airbornep)
      (set double-jump true))
    (set jumping (+ jumping 4)))
  (when (and (btn LEFT) (btn RIGHT) (btn Y))
    (set show-debug (not show-debug)))
  (if (jumpingp)
      (do
        (move 0 (- (math.floor (* 1.5 AVATAR-SIZE))))
        (set jumping (- jumping 1)))
      (if (airbornep)
          (move 0 1)
          (set double-jump false)))
  (cls BG)
  (each [_ value (ipairs platforms)]
    (rect value.x value.y value.width value.height PLATFORM-COLOR))
  (rect pos-x pos-y AVATAR-SIZE AVATAR-SIZE AVATAR-COLOR)
  (when show-debug
    (print (string.format "Avatar:    (%f,%f)" pos-x pos-y) 1 1)
    (print (string.format "Jumping:   %d" jumping) 1 8)
    (print (string.format "Double:    %s" double-jump) 1 15)
    (print (let [c tic-collision]
             (if c
                 (string.format "Collision: x:%d y:%d width:%d height:%d"
                                c.x c.y c.width c.height)
                 "Collision: None"))
           1 22)))

;; <TILES>
;; 001:eccccccccc888888caaaaaaaca888888cacccccccacc0ccccacc0ccccacc0ccc
;; 002:ccccceee8888cceeaaaa0cee888a0ceeccca0ccc0cca0c0c0cca0c0c0cca0c0c
;; 003:eccccccccc888888caaaaaaaca888888cacccccccacccccccacc0ccccacc0ccc
;; 004:ccccceee8888cceeaaaa0cee888a0ceeccca0cccccca0c0c0cca0c0c0cca0c0c
;; 017:cacccccccaaaaaaacaaacaaacaaaaccccaaaaaaac8888888cc000cccecccccec
;; 018:ccca00ccaaaa0ccecaaa0ceeaaaa0ceeaaaa0cee8888ccee000cceeecccceeee
;; 019:cacccccccaaaaaaacaaacaaacaaaaccccaaaaaaac8888888cc000cccecccccec
;; 020:ccca00ccaaaa0ccecaaa0ceeaaaa0ceeaaaa0cee8888ccee000cceeecccceeee
;; </TILES>

;; <TRACKS>
;; 000:100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; </TRACKS>

;; <PALETTE>
;; 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
;; </PALETTE>