2024-04-26 18:30:59 +00: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:
|
|
|
|
|
|
|
|
(import (scheme base)
|
|
|
|
(scheme inexact)
|
2024-05-25 03:57:06 +00:00
|
|
|
(hoot atomics)
|
2024-04-26 18:30:59 +00:00
|
|
|
(hoot debug)
|
|
|
|
(hoot ffi)
|
|
|
|
(hoot hashtables)
|
|
|
|
(hoot match)
|
|
|
|
(dom canvas)
|
2024-05-22 07:26:50 +00:00
|
|
|
(dom console)
|
2024-04-26 18:30:59 +00:00
|
|
|
(dom document)
|
|
|
|
(dom element)
|
|
|
|
(dom event)
|
|
|
|
(dom image)
|
|
|
|
(dom media)
|
|
|
|
(dom window)
|
|
|
|
(math)
|
|
|
|
(math rect)
|
2024-05-18 21:32:36 +00:00
|
|
|
(math vector)
|
|
|
|
(ces entity)
|
|
|
|
(ces system)
|
2024-05-23 19:54:07 +00:00
|
|
|
(ces system sprite-renderer)
|
2024-05-22 09:18:18 +00:00
|
|
|
(ces system keyboard-reader)
|
2024-05-22 22:41:35 +00:00
|
|
|
(ces system tilemap-renderer)
|
2024-05-18 21:32:36 +00:00
|
|
|
(ces system-manager)
|
|
|
|
(ces entity-manager)
|
2024-05-24 21:37:55 +00:00
|
|
|
(game-core)
|
2024-05-25 08:15:45 +00:00
|
|
|
(game game-manager)
|
2024-05-25 21:15:25 +00:00
|
|
|
(logging)
|
|
|
|
(media))
|
2024-04-26 18:30:59 +00:00
|
|
|
|
2024-05-25 03:57:06 +00:00
|
|
|
;; Canvas settings
|
2024-05-22 09:18:18 +00:00
|
|
|
(define canvas (get-element-by-id "canvas"))
|
|
|
|
(define context (get-context canvas "2d"))
|
2024-04-26 18:30:59 +00:00
|
|
|
|
2024-05-25 03:57:06 +00:00
|
|
|
(define game-width 640.0)
|
|
|
|
(define game-height 480.0)
|
2024-04-26 18:30:59 +00:00
|
|
|
|
2024-05-26 02:50:05 +00:00
|
|
|
(define game-images `((player . ,(make-image "assets/sprites/player.png"))
|
|
|
|
(barrel . ,(make-image "assets/sprites/barrel.png"))
|
|
|
|
(bricks . ,(make-image "assets/sprites/bricks.png"))
|
|
|
|
(stones . ,(make-image "assets/sprites/stones.png"))
|
2024-05-26 04:46:39 +00:00
|
|
|
(card-back . ,(make-image "assets/sprites/bricks.png"))
|
|
|
|
(tile-blank . ,(make-image "assets/tiles/blank.png"))
|
|
|
|
(tile-floor . ,(make-image "assets/tiles/floor-light.png"))
|
|
|
|
(tile-wall . ,(make-image "assets/tiles/bricks-light.png"))
|
|
|
|
(tile-door . ,(make-image "assets/tiles/door-light.png"))))
|
2024-05-25 21:52:39 +00:00
|
|
|
|
2024-05-26 04:29:10 +00:00
|
|
|
(parameterize ((*logger* #f))
|
2024-05-25 21:48:06 +00:00
|
|
|
(parameterize ((*media-library* (create-media-library)))
|
|
|
|
(add-images! (get-media-library) game-images)
|
|
|
|
|
2024-05-26 02:50:05 +00:00
|
|
|
(write-log! "Creating bootstrap managers")
|
|
|
|
(define bootstrap-entity-manager (create-entity-manager))
|
|
|
|
(define bootstrap-system-manager (create-entity-manager))
|
2024-05-25 21:48:06 +00:00
|
|
|
(write-log! "Creating entity manager")
|
2024-05-25 23:00:49 +00:00
|
|
|
(define e-manager (parameterize ((*entity-manager-parameter* bootstrap-entity-manager)
|
|
|
|
(*system-manager-parameter* bootstrap-system-manager))
|
2024-05-26 02:50:05 +00:00
|
|
|
(write-log! "Entering e-manager creation")
|
|
|
|
(let ((init-entities (init-game-entities)))
|
|
|
|
(write-log! "Got initial game entities.")
|
|
|
|
(create-entity-manager init-entities))))
|
2024-05-25 21:48:06 +00:00
|
|
|
|
|
|
|
(write-log! "Creating system manager")
|
2024-05-26 03:25:58 +00:00
|
|
|
(define s-manager (parameterize ((*entity-manager-parameter* e-manager))
|
|
|
|
(create-system-manager
|
|
|
|
(init-game-systems draw-image context))))
|
2024-05-25 21:48:06 +00:00
|
|
|
|
2024-05-26 17:10:48 +00:00
|
|
|
(pk "System information"
|
|
|
|
"Systems: " (system-manager-systems s-manager)
|
|
|
|
"System-manager proc table" (system-manager-proc-table s-manager))
|
|
|
|
|
2024-05-25 21:48:06 +00:00
|
|
|
(write-log! "Creating game")
|
|
|
|
(define game (make-game e-manager s-manager 0))
|
|
|
|
(define dt (/ 1000.0 60.0))
|
|
|
|
(define game-atom (make-atomic-box #f))
|
|
|
|
(atomic-box-set! game-atom (initialize-game! game))
|
|
|
|
(write-log! "Initialized game.")
|
|
|
|
|
|
|
|
(define number->string*
|
|
|
|
(let ((cache (make-eq-hashtable))) ; assuming fixnums only
|
|
|
|
(lambda (x)
|
|
|
|
(or (hashtable-ref cache x)
|
|
|
|
(let ((str (number->string x)))
|
|
|
|
(hashtable-set! cache x str)
|
|
|
|
str)))))
|
|
|
|
|
|
|
|
(write-log! "Defining game loop.")
|
|
|
|
(define (update-game-loop)
|
2024-05-26 02:50:05 +00:00
|
|
|
(parameterize ((*logger* #f))
|
2024-05-25 21:48:06 +00:00
|
|
|
(write-log! "Heartbeat")
|
|
|
|
(let* ((last-game-state (atomic-box-ref game-atom))
|
|
|
|
(next-game-state (step-game! last-game-state dt)))
|
|
|
|
(write-log! "Looped")
|
|
|
|
(atomic-box-set! game-atom next-game-state)
|
|
|
|
(timeout timeout-callback dt))))
|
|
|
|
|
|
|
|
(write-log! "Defining update callback")
|
|
|
|
(define timeout-callback (procedure->external update-game-loop))
|
|
|
|
|
|
|
|
(write-log! "Setting canvas size")
|
|
|
|
(set-element-width! canvas (exact game-width))
|
|
|
|
(set-element-height! canvas (exact game-height))
|
|
|
|
|
|
|
|
(write-log! "Starting game")
|
|
|
|
(timeout timeout-callback dt)))
|
2024-04-26 18:30:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
;; Canvas and event loop setup
|
2024-05-22 09:18:18 +00:00
|
|
|
;; Preserved for reference.
|
2024-05-25 03:57:06 +00:00
|
|
|
;;
|
2024-05-22 09:18:18 +00:00
|
|
|
;;(add-event-listener! (current-document) "keydown"
|
|
|
|
;; (procedure->external on-key-down))
|
|
|
|
;;(add-event-listener! (current-document) "keyup"
|
|
|
|
;; (procedure->external on-key-up))
|
|
|
|
;;(timeout update-callback dt)
|
|
|
|
;;(define draw-callback (procedure->external draw))
|