2024-05-17 13:08:43 -04:00
|
|
|
;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
|
|
|
|
;;;
|
|
|
|
;;; Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
;;; you may not use this file except in compliance with the License.
|
|
|
|
;;; You may obtain a copy of the License at
|
|
|
|
;;;
|
|
|
|
;;; http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
;;;
|
|
|
|
;;; Unless required by applicable law or agreed to in writing, software
|
|
|
|
;;; distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
;;; See the License for the specific language governing permissions and
|
|
|
|
;;; limitations under the License.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;;
|
|
|
|
;;; Example game showing off several common game programming things.
|
|
|
|
;;;
|
|
|
|
;;; Code:
|
|
|
|
|
2024-05-17 17:49:43 -04:00
|
|
|
(use-modules (dom canvas)
|
|
|
|
(dom document)
|
|
|
|
(dom element)
|
|
|
|
(dom event)
|
|
|
|
(dom image)
|
|
|
|
(dom media)
|
|
|
|
(dom window)
|
2024-05-18 14:04:35 -04:00
|
|
|
(game actors)
|
2024-05-22 21:30:06 -04:00
|
|
|
(game audio)
|
2024-05-23 11:43:59 -04:00
|
|
|
(game effects)
|
2024-05-20 12:15:39 -04:00
|
|
|
(game level)
|
2024-05-24 12:09:26 -04:00
|
|
|
(game levels tutorial-1)
|
|
|
|
(game levels tutorial-2)
|
|
|
|
(game levels tutorial-3)
|
|
|
|
(game levels tutorial-4)
|
|
|
|
(game levels tutorial-5)
|
|
|
|
(game levels tutorial-6)
|
2024-05-19 17:30:36 -04:00
|
|
|
(game levels level-1)
|
2024-05-20 13:34:59 -04:00
|
|
|
(game levels level-2)
|
|
|
|
(game levels level-3)
|
2024-05-22 12:07:41 -04:00
|
|
|
(game levels level-4)
|
2024-05-23 14:05:13 -04:00
|
|
|
(game levels credits)
|
2024-05-23 11:43:59 -04:00
|
|
|
(game scripts)
|
2024-05-18 18:51:45 -04:00
|
|
|
(game tileset)
|
2024-05-18 14:04:35 -04:00
|
|
|
(goblins core)
|
2024-05-19 17:30:36 -04:00
|
|
|
(hoot bytevectors)
|
2024-05-17 17:49:43 -04:00
|
|
|
(hoot ffi)
|
|
|
|
(hoot hashtables)
|
|
|
|
(ice-9 match)
|
2024-05-20 22:12:35 -04:00
|
|
|
(local-storage)
|
2024-05-17 17:49:43 -04:00
|
|
|
(math)
|
|
|
|
(math rect)
|
|
|
|
(math vector))
|
|
|
|
|
|
|
|
(define game-width 320.0)
|
|
|
|
(define game-height 240.0)
|
2024-05-18 14:04:35 -04:00
|
|
|
(define level-width (inexact->exact (floor (/ game-width tile-width))))
|
|
|
|
(define level-height (inexact->exact (floor (/ game-height tile-height))))
|
|
|
|
|
|
|
|
(define *canvas-scale* 0.0)
|
|
|
|
(define *canvas-width* 0)
|
|
|
|
(define *canvas-height* 0)
|
2024-05-17 17:58:43 -04:00
|
|
|
|
2024-05-19 06:15:27 -04:00
|
|
|
;; Assets
|
2024-05-18 18:51:45 -04:00
|
|
|
(define tileset
|
|
|
|
(make-tileset (make-image "assets/images/cirkoban.png")
|
|
|
|
320 240
|
|
|
|
(inexact->exact tile-width)
|
|
|
|
(inexact->exact tile-height)))
|
2024-05-20 12:15:39 -04:00
|
|
|
(define* (load-sound-effect name #:key (volume 0.25))
|
2024-05-22 21:30:06 -04:00
|
|
|
(make-sound-effect (string-append "assets/sounds/" name ".wav")))
|
2024-05-19 17:30:36 -04:00
|
|
|
(define audio:bump (load-sound-effect "bump"))
|
|
|
|
(define audio:push (load-sound-effect "push"))
|
|
|
|
(define audio:undo (load-sound-effect "undo"))
|
|
|
|
(define audio:no (load-sound-effect "no"))
|
2024-05-20 12:15:39 -04:00
|
|
|
(define audio:exit (load-sound-effect "exit"))
|
2024-05-21 14:10:57 -04:00
|
|
|
(define audio:pickup (load-sound-effect "pickup"))
|
2024-05-23 09:10:25 -04:00
|
|
|
(define audio:emit (load-sound-effect "emit"))
|
2024-05-23 15:07:48 -04:00
|
|
|
(define audio:emitter-on (load-sound-effect "emitter-on"))
|
|
|
|
(define audio:emitter-off (load-sound-effect "emitter-off"))
|
2024-05-22 09:29:44 -04:00
|
|
|
(define audio:die (load-sound-effect "die"))
|
2024-05-23 07:48:53 -04:00
|
|
|
(define audio:gate (load-sound-effect "gate"))
|
|
|
|
(define audio:warp (load-sound-effect "warp"))
|
2024-05-23 09:01:47 -04:00
|
|
|
(define audio:floor-switch (load-sound-effect "floor-switch"))
|
2024-05-22 21:53:17 -04:00
|
|
|
(define audio:electric-switch-on (load-sound-effect "electric-switch-on"))
|
|
|
|
(define audio:electric-switch-off (load-sound-effect "electric-switch-off"))
|
2024-05-17 13:08:43 -04:00
|
|
|
|
2024-05-18 14:04:35 -04:00
|
|
|
;; Game state
|
2024-05-20 13:34:59 -04:00
|
|
|
(define *state* #f)
|
|
|
|
|
2024-05-18 14:04:35 -04:00
|
|
|
(define *actormap* (make-whactormap))
|
|
|
|
(define (call-with-goblins thunk)
|
|
|
|
(actormap-churn-run! *actormap* thunk))
|
|
|
|
(define-syntax-rule (with-goblins body ...)
|
|
|
|
(call-with-goblins (lambda () body ...)))
|
|
|
|
|
2024-05-20 13:34:59 -04:00
|
|
|
(define levels
|
|
|
|
(vector
|
2024-05-24 12:09:26 -04:00
|
|
|
load-tutorial-1
|
|
|
|
load-tutorial-2
|
|
|
|
load-tutorial-3
|
|
|
|
load-tutorial-4
|
|
|
|
load-tutorial-5
|
|
|
|
load-tutorial-6
|
|
|
|
;; load-level-1
|
|
|
|
;; load-level-2
|
2024-05-22 12:07:41 -04:00
|
|
|
load-level-3
|
|
|
|
load-level-4))
|
2024-05-20 13:34:59 -04:00
|
|
|
(define *level-idx* #f)
|
2024-05-21 14:10:57 -04:00
|
|
|
(define *gems* #f)
|
2024-05-18 14:04:35 -04:00
|
|
|
(define *level* #f)
|
|
|
|
;; Latest representation of all actors in level
|
2024-05-20 22:12:35 -04:00
|
|
|
(define *objects* #f)
|
2024-05-18 14:04:35 -04:00
|
|
|
|
|
|
|
(define *snapshots* '())
|
|
|
|
(define (clear-snapshots!)
|
|
|
|
(set! *snapshots* '()))
|
|
|
|
(define (save-snapshot!)
|
|
|
|
(set! *snapshots* (cons (copy-whactormap *actormap*) *snapshots*)))
|
2024-05-18 18:51:45 -04:00
|
|
|
(define (rollback-snapshot!)
|
|
|
|
(match *snapshots*
|
2024-05-22 21:30:06 -04:00
|
|
|
(() (play-sound-effect audio:no))
|
2024-05-18 18:51:45 -04:00
|
|
|
((snapshot . older-snapshots)
|
|
|
|
(set! *actormap* snapshot)
|
2024-05-19 17:30:36 -04:00
|
|
|
(set! *snapshots* older-snapshots)
|
2024-05-23 12:06:42 -04:00
|
|
|
(play-sound-effect audio:undo)
|
2024-05-23 16:41:11 -04:00
|
|
|
(unless *current-effect*
|
|
|
|
(show-effect! (make-wipe-effect 0.15))))))
|
2024-05-18 14:04:35 -04:00
|
|
|
|
2024-05-22 06:52:36 -04:00
|
|
|
(define (sort lst compare)
|
2024-05-21 17:28:57 -04:00
|
|
|
(match lst
|
|
|
|
(() '())
|
|
|
|
((_) lst)
|
2024-05-22 06:52:36 -04:00
|
|
|
(_
|
|
|
|
;; Insertion sort because I am lazy!
|
|
|
|
(let ((vec (list->vector lst)))
|
|
|
|
(let outer ((i 1))
|
|
|
|
(when (< i (vector-length vec))
|
|
|
|
(let inner ((j i))
|
|
|
|
(when (> j 0)
|
|
|
|
(let ((a (vector-ref vec j))
|
|
|
|
(b (vector-ref vec (1- j))))
|
|
|
|
(when (compare a b)
|
|
|
|
(vector-set! vec j b)
|
|
|
|
(vector-set! vec (1- j) a)
|
|
|
|
(inner (1- j))))))
|
|
|
|
(outer (1+ i))))
|
|
|
|
(vector->list vec)))))
|
2024-05-21 17:28:57 -04:00
|
|
|
|
|
|
|
(define (filter-map proc lst)
|
|
|
|
(let lp ((lst lst))
|
|
|
|
(match lst
|
|
|
|
(() '())
|
|
|
|
((head . tail)
|
|
|
|
(let ((head* (proc head)))
|
|
|
|
(if head*
|
|
|
|
(cons head* (lp tail))
|
|
|
|
(lp tail)))))))
|
|
|
|
|
2024-05-20 22:12:35 -04:00
|
|
|
(define (update-objects!)
|
|
|
|
(set! *objects*
|
2024-05-21 22:12:21 -04:00
|
|
|
;; z-sort the list so we render in the correct order. Then
|
|
|
|
;; convert tile positions to vec2s of pixel coordinates for
|
|
|
|
;; more efficient rendering.
|
2024-05-20 22:12:35 -04:00
|
|
|
(map (match-lambda
|
2024-05-21 17:28:57 -04:00
|
|
|
((type #(x y _) . properties)
|
2024-05-21 22:12:21 -04:00
|
|
|
`(,type ,(vec2 (* x tile-width) (* y tile-height)) ,@properties)))
|
|
|
|
(sort ($ (level-actor *level*) 'describe)
|
2024-05-21 17:28:57 -04:00
|
|
|
(lambda (a b)
|
|
|
|
(match a
|
|
|
|
((_ #(_ _ az) . _)
|
|
|
|
(match b
|
|
|
|
((_ #(_ _ bz) . _)
|
|
|
|
(<= az bz))))))))))
|
|
|
|
|
|
|
|
(define (collected-gem? idx)
|
2024-05-22 18:34:29 -04:00
|
|
|
(memq idx *gems*))
|
2024-05-18 14:04:35 -04:00
|
|
|
|
2024-05-20 22:12:35 -04:00
|
|
|
(define (load-level! idx)
|
|
|
|
(set! *state* 'play)
|
|
|
|
(set! *actormap* (make-whactormap))
|
2024-05-20 13:34:59 -04:00
|
|
|
(clear-snapshots!)
|
2024-05-20 22:12:35 -04:00
|
|
|
(with-goblins
|
2024-05-21 17:28:57 -04:00
|
|
|
(set! *level* ((vector-ref levels idx) (collected-gem? idx)))
|
2024-05-20 22:12:35 -04:00
|
|
|
(update-objects!)))
|
|
|
|
|
2024-05-23 14:05:13 -04:00
|
|
|
(define (load-credits!)
|
|
|
|
(set! *state* 'win)
|
|
|
|
(set! *actormap* (make-whactormap))
|
|
|
|
(set-vec2-y! *credits-scroll* 0.0)
|
|
|
|
(clear-snapshots!)
|
|
|
|
(with-goblins
|
|
|
|
(set! *level* (load-credits #t))
|
|
|
|
(update-objects!)))
|
|
|
|
|
2024-05-20 22:12:35 -04:00
|
|
|
(define (next-level!)
|
2024-05-20 13:34:59 -04:00
|
|
|
(let ((idx (+ *level-idx* 1)))
|
2024-05-20 22:12:35 -04:00
|
|
|
(pk 'next-level idx)
|
2024-05-24 13:23:36 -04:00
|
|
|
;; TODO: Maybe show a little achievement popup when all gems
|
|
|
|
;; are collected?
|
|
|
|
(when (with-goblins ($ (level-actor *level*) 'gem-collected?))
|
|
|
|
(set! *gems* (cons *level-idx* *gems*)))
|
2024-05-20 13:34:59 -04:00
|
|
|
(set! *level-idx* idx)
|
|
|
|
(if (< idx (vector-length levels))
|
2024-05-20 22:12:35 -04:00
|
|
|
(begin
|
|
|
|
(save-game!)
|
2024-05-23 11:43:59 -04:00
|
|
|
(run-script
|
|
|
|
(lambda ()
|
2024-05-23 14:05:13 -04:00
|
|
|
(set! *state* 'interstitial)
|
2024-05-23 11:43:59 -04:00
|
|
|
(show-effect! (make-fade-out+in-effect 1.0))
|
2024-05-23 14:05:13 -04:00
|
|
|
(wait 30) ; ~half the effect time
|
2024-05-23 11:43:59 -04:00
|
|
|
(load-level! idx))))
|
2024-05-23 14:05:13 -04:00
|
|
|
(begin
|
|
|
|
(run-script
|
|
|
|
(lambda ()
|
|
|
|
(set! *level-idx* 0)
|
|
|
|
(save-game!)
|
|
|
|
(set! *state* 'interstitial)
|
|
|
|
(show-effect! (make-fade-out+in-effect 2.0))
|
|
|
|
(wait 60)
|
|
|
|
(load-credits!)))))))
|
2024-05-20 13:34:59 -04:00
|
|
|
|
2024-05-20 22:12:35 -04:00
|
|
|
;; Auto-save/load to local storage.
|
|
|
|
(define (save-game!)
|
|
|
|
(pk 'save)
|
2024-05-21 14:10:57 -04:00
|
|
|
(local-storage-set! "cirkoban-save"
|
|
|
|
(call-with-output-string
|
|
|
|
(lambda (port)
|
|
|
|
(write (list *level-idx* *gems*) port)))))
|
2024-05-20 22:12:35 -04:00
|
|
|
|
|
|
|
(define (load-game!)
|
2024-05-21 14:10:57 -04:00
|
|
|
(let ((saved
|
|
|
|
(match (local-storage-ref "cirkoban-save")
|
|
|
|
("" '(0 ())) ; initial save state
|
|
|
|
(str (call-with-input-string str read)))))
|
|
|
|
(match saved
|
|
|
|
((idx gems)
|
|
|
|
(set! *level-idx* idx)
|
|
|
|
(set! *gems* gems)
|
|
|
|
(pk 'load *level-idx*)
|
|
|
|
(load-level! *level-idx*)))))
|
2024-05-20 22:12:35 -04:00
|
|
|
|
2024-05-18 14:04:35 -04:00
|
|
|
(define (reset-game!)
|
2024-05-23 16:53:23 -04:00
|
|
|
(run-script
|
|
|
|
(lambda ()
|
|
|
|
(set! *level-idx* 0)
|
|
|
|
(save-game!)
|
|
|
|
(set! *state* 'interstitial)
|
|
|
|
(show-effect! (make-fade-out+in-effect 2.0))
|
|
|
|
(wait 60)
|
|
|
|
(load-level! 0))))
|
2024-05-18 14:04:35 -04:00
|
|
|
|
|
|
|
;; Update loop
|
|
|
|
(define (move-player dir)
|
2024-05-22 21:53:17 -04:00
|
|
|
(define level-complete? #f)
|
|
|
|
(with-goblins
|
|
|
|
(let ((player (level-player *level*))
|
|
|
|
(level (level-actor *level*)))
|
|
|
|
(cond
|
|
|
|
(($ player 'alive?)
|
|
|
|
(begin
|
|
|
|
($ player 'move dir)
|
|
|
|
($ level 'tick)
|
|
|
|
(let lp ((events ($ level 'flush-events)))
|
|
|
|
(match events
|
|
|
|
(() (values))
|
|
|
|
((event . rest)
|
|
|
|
(match (pk 'event event)
|
|
|
|
(('bump x y)
|
|
|
|
(play-sound-effect audio:bump))
|
|
|
|
(('push x y)
|
|
|
|
(play-sound-effect audio:push))
|
|
|
|
(('exit x y)
|
|
|
|
(play-sound-effect audio:exit)
|
|
|
|
(set! level-complete? #t))
|
|
|
|
(('player-death x y)
|
|
|
|
(play-sound-effect audio:die))
|
|
|
|
(('pickup x y)
|
2024-05-24 13:23:36 -04:00
|
|
|
(play-sound-effect audio:pickup))
|
2024-05-23 09:10:25 -04:00
|
|
|
(('emit x y)
|
|
|
|
(play-sound-effect audio:emit))
|
2024-05-23 15:07:48 -04:00
|
|
|
(('emitter-on x y)
|
|
|
|
(play-sound-effect audio:emitter-on))
|
|
|
|
(('emitter-off x y)
|
|
|
|
(play-sound-effect audio:emitter-off))
|
2024-05-23 11:43:59 -04:00
|
|
|
(('gate-open x y)
|
2024-05-23 07:48:53 -04:00
|
|
|
(play-sound-effect audio:gate))
|
2024-05-23 11:43:59 -04:00
|
|
|
(('gate-close x y)
|
|
|
|
(play-sound-effect audio:gate)
|
|
|
|
(show-effect! (make-screen-shake-effect 0.05)))
|
2024-05-23 09:01:47 -04:00
|
|
|
((or ('floor-switch-on x y) ('floor-switch-off x y))
|
|
|
|
(play-sound-effect audio:floor-switch))
|
2024-05-22 21:53:17 -04:00
|
|
|
(('electric-switch-on x y)
|
|
|
|
(play-sound-effect audio:electric-switch-on))
|
|
|
|
(('electric-switch-off x y)
|
|
|
|
(play-sound-effect audio:electric-switch-off))
|
2024-05-23 07:48:53 -04:00
|
|
|
(('receive-electron x y)
|
|
|
|
(play-sound-effect audio:warp 0.25))
|
2024-05-22 21:53:17 -04:00
|
|
|
(_ (values)))
|
|
|
|
(lp rest))))
|
|
|
|
(update-objects!)
|
|
|
|
(save-snapshot!)))
|
|
|
|
(else
|
|
|
|
(play-sound-effect audio:no)))))
|
|
|
|
(when level-complete? (next-level!)))
|
2024-05-18 14:04:35 -04:00
|
|
|
|
2024-05-17 13:08:43 -04:00
|
|
|
(define dt (/ 1000.0 60.0)) ; aim for updating at 60Hz
|
|
|
|
(define (update)
|
2024-05-23 11:43:59 -04:00
|
|
|
(scheduler-tick! (current-scheduler))
|
2024-05-17 13:08:43 -04:00
|
|
|
(timeout update-callback dt))
|
|
|
|
(define update-callback (procedure->external update))
|
|
|
|
|
2024-05-18 14:04:35 -04:00
|
|
|
;; Render loop
|
2024-05-23 11:43:59 -04:00
|
|
|
(define *current-effect* #f)
|
|
|
|
(define (show-effect! effect)
|
|
|
|
(set! *current-effect* effect)
|
|
|
|
(effect-start! effect))
|
|
|
|
(define (draw-current-effect type)
|
|
|
|
(when (and *current-effect*
|
|
|
|
(eq? type (effect-type *current-effect*)))
|
|
|
|
(draw-effect context *current-effect*)
|
|
|
|
(unless (effect-started? *current-effect*)
|
|
|
|
(set! *current-effect* #f))))
|
|
|
|
|
2024-05-17 13:08:43 -04:00
|
|
|
(define number->string*
|
2024-05-23 11:43:59 -04:00
|
|
|
(let ((cache (make-eq-hashtable))) ; assuming fixnums only
|
2024-05-17 13:08:43 -04:00
|
|
|
(lambda (x)
|
|
|
|
(or (hashtable-ref cache x)
|
|
|
|
(let ((str (number->string x)))
|
|
|
|
(hashtable-set! cache x str)
|
|
|
|
str)))))
|
|
|
|
|
2024-05-23 16:08:25 -04:00
|
|
|
(define (draw-rotated-tile id pos angle)
|
|
|
|
(let ((hw (* tile-width 0.5))
|
|
|
|
(hh (* tile-height 0.5)))
|
|
|
|
(save! context)
|
|
|
|
(translate! context (+ (vec2-x pos) hw) (+ (vec2-y pos) hh))
|
|
|
|
(rotate! context angle)
|
|
|
|
(draw-tile context tileset id (- hw) (- hh))
|
|
|
|
(restore! context)))
|
|
|
|
|
2024-05-22 18:21:45 -04:00
|
|
|
(define (draw-player pos alive?)
|
|
|
|
(draw-tile context tileset (if alive? 0 20) (vec2-x pos) (vec2-y pos)))
|
2024-05-20 22:12:35 -04:00
|
|
|
|
|
|
|
(define (draw-exit pos)
|
|
|
|
(draw-tile context tileset 27 (vec2-x pos) (vec2-y pos)))
|
|
|
|
|
2024-05-22 14:42:27 -04:00
|
|
|
(define (draw-wire-state pos state)
|
2024-05-20 22:12:35 -04:00
|
|
|
(let ((x (vec2-x pos))
|
|
|
|
(y (vec2-y pos)))
|
2024-05-22 14:42:27 -04:00
|
|
|
(match state
|
2024-05-20 22:12:35 -04:00
|
|
|
('electron-head
|
|
|
|
(draw-tile context tileset 4 x y))
|
|
|
|
('electron-tail
|
|
|
|
(draw-tile context tileset 5 x y))
|
|
|
|
(_ #f))))
|
|
|
|
|
2024-05-22 14:42:27 -04:00
|
|
|
(define (draw-wall pos type)
|
|
|
|
(draw-wire-state pos type))
|
|
|
|
|
2024-05-21 22:12:21 -04:00
|
|
|
(define (draw-block pos type)
|
2024-05-20 22:12:35 -04:00
|
|
|
(let ((x (vec2-x pos))
|
|
|
|
(y (vec2-y pos)))
|
|
|
|
(match type
|
|
|
|
('crate (draw-tile context tileset 29 x y))
|
|
|
|
(_ (draw-tile context tileset 3 x y)))
|
2024-05-22 14:42:27 -04:00
|
|
|
(draw-wire-state pos type)))
|
2024-05-20 22:12:35 -04:00
|
|
|
|
2024-05-24 12:46:15 -04:00
|
|
|
(define (draw-brick pos exploding?)
|
|
|
|
;; TODO use exploding for different sprite?
|
|
|
|
(draw-tile context tileset 22 (vec2-x pos) (vec2-y pos)))
|
|
|
|
|
2024-05-20 22:12:35 -04:00
|
|
|
(define (draw-clock-emitter pos)
|
|
|
|
(draw-tile context tileset 48 (vec2-x pos) (vec2-y pos)))
|
|
|
|
|
2024-05-22 15:28:42 -04:00
|
|
|
(define (draw-switched-emitter pos on?)
|
|
|
|
(draw-tile context tileset (if on? 48 47) (vec2-x pos) (vec2-y pos)))
|
|
|
|
|
2024-05-21 14:10:57 -04:00
|
|
|
(define (draw-floor-switch pos on?)
|
|
|
|
(draw-tile context tileset (if on? 25 24) (vec2-x pos) (vec2-y pos)))
|
|
|
|
|
2024-05-24 12:46:15 -04:00
|
|
|
(define (draw-bomb pos exploding?)
|
|
|
|
(draw-tile context tileset (if exploding? 51 50) (vec2-x pos) (vec2-y pos)))
|
|
|
|
|
2024-05-21 14:10:57 -04:00
|
|
|
(define (draw-gem pos)
|
|
|
|
(draw-tile context tileset 28 (vec2-x pos) (vec2-y pos)))
|
|
|
|
|
2024-05-22 18:34:29 -04:00
|
|
|
(define (draw-ghost-gem pos)
|
|
|
|
(set-global-alpha! context 0.5)
|
|
|
|
(draw-tile context tileset 49 (vec2-x pos) (vec2-y pos))
|
|
|
|
(set-global-alpha! context 1.0))
|
|
|
|
|
2024-05-21 17:28:57 -04:00
|
|
|
(define (draw-gate pos open?)
|
|
|
|
(draw-tile context tileset (if open? 46 45) (vec2-x pos) (vec2-y pos)))
|
|
|
|
|
2024-05-23 16:36:05 -04:00
|
|
|
(define (draw-logic-gate pos direction state id)
|
2024-05-22 14:42:27 -04:00
|
|
|
(let ((x (vec2-x pos))
|
|
|
|
(y (vec2-y pos)))
|
|
|
|
(draw-tile context tileset 2 x y)
|
2024-05-23 16:08:25 -04:00
|
|
|
(match direction
|
|
|
|
('right (draw-tile context tileset id x y))
|
|
|
|
('left (draw-rotated-tile id pos pi))
|
|
|
|
('up (draw-rotated-tile id pos (* pi 1.5)))
|
|
|
|
('down (draw-rotated-tile id pos (* pi 0.5))))
|
2024-05-22 14:42:27 -04:00
|
|
|
(draw-wire-state pos state)))
|
2024-05-22 12:07:41 -04:00
|
|
|
|
2024-05-22 08:00:39 -04:00
|
|
|
(define (draw-electric-switch pos on?)
|
|
|
|
(draw-tile context tileset (if on? 7 6) (vec2-x pos) (vec2-y pos)))
|
|
|
|
|
2024-05-22 12:35:56 -04:00
|
|
|
(define (draw-electron-warp pos state)
|
|
|
|
(draw-tile context tileset 71 (vec2-x pos) (vec2-y pos))
|
|
|
|
(draw-wire-state pos state))
|
|
|
|
|
2024-05-20 22:12:35 -04:00
|
|
|
(define (draw-object obj)
|
|
|
|
(match obj
|
|
|
|
(#f #f)
|
2024-05-22 18:21:45 -04:00
|
|
|
(('player pos alive?) (draw-player pos alive?))
|
2024-05-22 08:00:39 -04:00
|
|
|
(('exit pos) #t) ; drawn via background
|
2024-05-21 22:12:21 -04:00
|
|
|
(('wall pos type) (draw-wall pos type))
|
|
|
|
(('block pos type) (draw-block pos type))
|
2024-05-24 12:46:15 -04:00
|
|
|
(('brick pos exploding?) (draw-brick pos exploding?))
|
2024-05-23 16:28:53 -04:00
|
|
|
(('clock-emitter pos) (draw-clock-emitter pos))
|
2024-05-22 15:28:42 -04:00
|
|
|
(('switched-emitter pos on?) (draw-switched-emitter pos on?))
|
2024-05-21 14:10:57 -04:00
|
|
|
(('floor-switch pos on?) (draw-floor-switch pos on?))
|
2024-05-24 12:46:15 -04:00
|
|
|
(('bomb pos exploding?) (draw-bomb pos exploding?))
|
2024-05-21 17:28:57 -04:00
|
|
|
(('gem pos) (draw-gem pos))
|
2024-05-22 18:34:29 -04:00
|
|
|
(('ghost-gem pos) (draw-ghost-gem pos))
|
2024-05-22 08:00:39 -04:00
|
|
|
(('gate pos open?) (draw-gate pos open?))
|
2024-05-23 16:36:05 -04:00
|
|
|
(('and-gate pos direction state) (draw-logic-gate pos direction state 42))
|
|
|
|
(('or-gate pos direction state) (draw-logic-gate pos direction state 43))
|
|
|
|
(('xor-gate pos direction state) (draw-logic-gate pos direction state 44))
|
2024-05-22 12:35:56 -04:00
|
|
|
(('electric-switch pos on?) (draw-electric-switch pos on?))
|
|
|
|
(('electron-warp pos state) (draw-electron-warp pos state))))
|
2024-05-18 14:04:35 -04:00
|
|
|
|
2024-05-19 17:30:36 -04:00
|
|
|
(define (draw-background)
|
2024-05-22 14:22:27 -04:00
|
|
|
(let ((bg (level-background *level*))
|
|
|
|
(k (* level-width level-height)))
|
|
|
|
(do ((i 0 (1+ i)))
|
|
|
|
((= i k))
|
|
|
|
(let* ((tile (vector-ref bg i))
|
|
|
|
(pos (level-tile-position tile))
|
|
|
|
(id (level-tile-id tile)))
|
|
|
|
(draw-tile context tileset id (vec2-x pos) (vec2-y pos))))))
|
2024-05-19 17:30:36 -04:00
|
|
|
|
2024-05-18 14:04:35 -04:00
|
|
|
(define (draw-level)
|
2024-05-19 17:30:36 -04:00
|
|
|
(draw-background)
|
2024-05-22 18:21:45 -04:00
|
|
|
(for-each draw-object *objects*)
|
|
|
|
(let ((alive? (with-goblins ($ (level-player *level*) 'alive?))))
|
|
|
|
(unless alive?
|
2024-05-24 12:09:26 -04:00
|
|
|
(set-global-alpha! context 0.7)
|
|
|
|
(set-fill-color! context "#222034")
|
2024-05-22 18:21:45 -04:00
|
|
|
(fill-rect context 0.0 0.0 game-width game-height)
|
2024-05-24 12:09:26 -04:00
|
|
|
(set-global-alpha! context 1.0)
|
|
|
|
(set-font! context "normal 32px monogram")
|
2024-05-22 18:21:45 -04:00
|
|
|
(set-fill-color! context "#ffffff")
|
|
|
|
(set-text-align! context "center")
|
|
|
|
(fill-text context "OUCH... x_x" (/ game-width 2.0) (/ game-height 2.0)))))
|
2024-05-18 14:04:35 -04:00
|
|
|
|
2024-05-23 11:43:59 -04:00
|
|
|
(define (draw-interstitial)
|
|
|
|
(draw-level))
|
|
|
|
|
2024-05-23 14:05:13 -04:00
|
|
|
(define *credits-scroll* (vec2 0.0 0.0))
|
|
|
|
(define credits
|
|
|
|
#("Congratulations!"
|
|
|
|
#f
|
|
|
|
#f
|
|
|
|
"Cirkoban was made by the"
|
|
|
|
"Spritely Institute"
|
|
|
|
#f
|
|
|
|
"https://spritely.institute"
|
|
|
|
#f
|
|
|
|
"Programming"
|
|
|
|
#f
|
|
|
|
"David Thompson"
|
|
|
|
"Juliana Sims"
|
|
|
|
#f
|
|
|
|
"Art"
|
|
|
|
#f
|
|
|
|
"Christine Lemmer-Webber"
|
|
|
|
#f
|
|
|
|
"monogram font by datagoblin"
|
|
|
|
#f
|
|
|
|
#f
|
|
|
|
#f
|
|
|
|
#f
|
|
|
|
#f
|
|
|
|
#f
|
|
|
|
#f
|
|
|
|
#f
|
|
|
|
"Thank you for playing!"))
|
|
|
|
(define credits-line-spacing 16.0)
|
|
|
|
(define max-credits-scroll
|
|
|
|
(+ game-height (* (- (vector-length credits) 9) credits-line-spacing)))
|
2024-05-20 13:34:59 -04:00
|
|
|
(define (draw-win)
|
2024-05-23 14:05:13 -04:00
|
|
|
(draw-level)
|
|
|
|
(set-fill-color! context "#ffffff")
|
|
|
|
(set-text-align! context "center")
|
2024-05-23 16:48:46 -04:00
|
|
|
(set-font! context "normal 16px monogram")
|
2024-05-23 14:05:13 -04:00
|
|
|
(set-vec2-y! *credits-scroll*
|
2024-05-23 14:08:02 -04:00
|
|
|
(min (+ (vec2-y *credits-scroll*) 0.5)
|
2024-05-23 14:05:13 -04:00
|
|
|
max-credits-scroll))
|
|
|
|
(let* ((x (* game-width 0.7))
|
|
|
|
(lines-on-screen 15)
|
|
|
|
(scroll-y (vec2-y *credits-scroll*))
|
|
|
|
;; TODO: Only render the lines on screen.
|
|
|
|
(start 0)
|
|
|
|
(end (vector-length credits)))
|
|
|
|
(let lp ((i start) (y (- game-height scroll-y)))
|
|
|
|
(when (< i end)
|
|
|
|
(match (vector-ref credits i)
|
|
|
|
(#f #f)
|
|
|
|
(str (fill-text context str x y)))
|
|
|
|
(lp (1+ i) (+ y credits-line-spacing))))))
|
2024-05-20 13:34:59 -04:00
|
|
|
|
2024-05-18 14:04:35 -04:00
|
|
|
(define (draw prev-time)
|
|
|
|
(clear-rect context 0.0 0.0 *canvas-width* *canvas-height*)
|
|
|
|
(set-transform! context 1.0 0.0 0.0 1.0 0.0 0.0)
|
2024-05-23 11:43:59 -04:00
|
|
|
(scale! context *canvas-scale* *canvas-scale*)
|
|
|
|
(draw-current-effect 'pre)
|
2024-05-20 13:34:59 -04:00
|
|
|
(match *state*
|
2024-05-23 11:43:59 -04:00
|
|
|
((or 'play 'interstitial) (draw-level))
|
2024-05-20 13:34:59 -04:00
|
|
|
('win (draw-win)))
|
2024-05-23 11:43:59 -04:00
|
|
|
(draw-current-effect 'post)
|
2024-05-17 13:08:43 -04:00
|
|
|
(request-animation-frame draw-callback))
|
|
|
|
(define draw-callback (procedure->external draw))
|
|
|
|
|
|
|
|
;; Input
|
|
|
|
(define key:left "ArrowLeft")
|
|
|
|
(define key:right "ArrowRight")
|
2024-05-18 14:04:35 -04:00
|
|
|
(define key:down "ArrowDown")
|
|
|
|
(define key:up "ArrowUp")
|
2024-05-17 13:08:43 -04:00
|
|
|
(define key:confirm "Enter")
|
2024-05-18 18:51:45 -04:00
|
|
|
(define key:undo "KeyZ")
|
2024-05-17 13:08:43 -04:00
|
|
|
|
|
|
|
(define (on-key-down event)
|
|
|
|
(let ((key (keyboard-event-code event)))
|
2024-05-20 12:15:39 -04:00
|
|
|
(pk 'key-down key)
|
2024-05-20 13:34:59 -04:00
|
|
|
(match *state*
|
|
|
|
('play
|
|
|
|
(cond
|
|
|
|
((string=? key key:left)
|
|
|
|
(move-player 'left))
|
|
|
|
((string=? key key:right)
|
|
|
|
(move-player 'right))
|
|
|
|
((string=? key key:up)
|
|
|
|
(move-player 'up))
|
|
|
|
((string=? key key:down)
|
|
|
|
(move-player 'down))
|
|
|
|
((string=? key key:undo)
|
|
|
|
(rollback-snapshot!)
|
2024-05-21 14:10:57 -04:00
|
|
|
(with-goblins (update-objects!)))
|
|
|
|
;; REMOVE BEFORE RELEASE!!!!
|
|
|
|
((string=? key key:confirm)
|
|
|
|
(next-level!))))
|
2024-05-24 12:09:26 -04:00
|
|
|
;; Pressing any bound key resets the game.
|
|
|
|
('win
|
|
|
|
(when (or
|
|
|
|
(string=? key key:left)
|
|
|
|
(string=? key key:right)
|
|
|
|
(string=? key key:up)
|
|
|
|
(string=? key key:down)
|
|
|
|
(string=? key key:undo)
|
|
|
|
(string=? key key:confirm))
|
|
|
|
(reset-game!))))))
|
2024-05-17 13:08:43 -04:00
|
|
|
|
|
|
|
;; Canvas and event loop setup
|
|
|
|
(define canvas (get-element-by-id "canvas"))
|
|
|
|
(define context (get-context canvas "2d"))
|
2024-05-18 14:04:35 -04:00
|
|
|
|
|
|
|
(define (resize-canvas)
|
|
|
|
(let* ((win (current-window))
|
|
|
|
(w (window-inner-width win))
|
|
|
|
(h (window-inner-height win))
|
|
|
|
(gw (inexact->exact game-width))
|
|
|
|
(gh (inexact->exact game-height))
|
|
|
|
(scale (max (min (quotient w gw) (quotient h gh)) 1))
|
|
|
|
(cw (* gw scale))
|
|
|
|
(ch (* gh scale)))
|
|
|
|
(set-element-width! canvas cw)
|
|
|
|
(set-element-height! canvas ch)
|
|
|
|
(set-image-smoothing-enabled! context 0)
|
|
|
|
(set! *canvas-scale* (exact->inexact scale))
|
|
|
|
(set! *canvas-width* (* game-width *canvas-scale*))
|
|
|
|
(set! *canvas-height* (* game-height *canvas-scale*))
|
|
|
|
(pk 'resize-canvas *canvas-scale* *canvas-width* *canvas-height*)))
|
|
|
|
|
2024-05-17 17:49:43 -04:00
|
|
|
(set-element-width! canvas (inexact->exact game-width))
|
|
|
|
(set-element-height! canvas (inexact->exact game-height))
|
2024-05-18 14:04:35 -04:00
|
|
|
(add-event-listener! (current-window) "resize"
|
|
|
|
(procedure->external (lambda (_) (resize-canvas))))
|
2024-05-17 13:08:43 -04:00
|
|
|
(add-event-listener! (current-document) "keydown"
|
|
|
|
(procedure->external on-key-down))
|
2024-05-18 14:04:35 -04:00
|
|
|
(resize-canvas)
|
2024-05-17 13:08:43 -04:00
|
|
|
(request-animation-frame draw-callback)
|
|
|
|
(timeout update-callback dt)
|
2024-05-20 22:12:35 -04:00
|
|
|
(load-game!)
|