From 45791c1360c98957ebe27655d59a2ae9db6cd709 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sun, 4 Jun 2023 09:25:06 -0400 Subject: Giant code and assets drop. --- super-bloom/actor.scm | 99 ++++++++++++++++++ super-bloom/common.scm | 69 +++++++++++++ super-bloom/dirt-ball.scm | 81 +++++++++++++++ super-bloom/flower.scm | 146 ++++++++++++++++++++++++++ super-bloom/game.scm | 255 ++++++++++++++++++++++++++++++++++++++++++++++ super-bloom/main.scm | 75 ++++++++++---- super-bloom/player.scm | 170 +++++++++++++++++++++++++++++++ super-bloom/water.scm | 60 +++++++++++ 8 files changed, 938 insertions(+), 17 deletions(-) create mode 100644 super-bloom/actor.scm create mode 100644 super-bloom/common.scm create mode 100644 super-bloom/dirt-ball.scm create mode 100644 super-bloom/flower.scm create mode 100644 super-bloom/game.scm create mode 100644 super-bloom/player.scm create mode 100644 super-bloom/water.scm (limited to 'super-bloom') diff --git a/super-bloom/actor.scm b/super-bloom/actor.scm new file mode 100644 index 0000000..0ba495f --- /dev/null +++ b/super-bloom/actor.scm @@ -0,0 +1,99 @@ +(define-module (super-bloom actor) + #:use-module (catbird asset) + #:use-module (catbird node) + #:use-module (catbird node-2d) + #:use-module (chickadee audio) + #:use-module (chickadee data quadtree) + #:use-module (chickadee math rect) + #:use-module (chickadee math vector) + #:use-module (oop goops) + #:export ( + velocity + hitbox + world-hitbox + quadtree + change-velocity + direction-to + angle-to + after-move + on-collide + refresh-world-hitbox)) + +(define-class () + (velocity #:getter velocity #:init-keyword #:velocity #:init-form (vec2 0.0 0.0)) + (hitbox #:getter hitbox #:init-keyword #:hitbox) + (world-hitbox #:getter world-hitbox #:init-form (make-rect 0.0 0.0 0.0 0.0)) + (quadtree #:accessor quadtree #:init-keyword #:quadtree)) + +(define-method (initialize (actor ) initargs) + (next-method) + (refresh-world-hitbox actor)) + +(define-method (change-velocity (actor ) dx dy) + (set-vec2! (velocity actor) dx dy)) + +(define-method (direction-to (actor ) (other )) + (let ((dir (vec2- (position other) (position actor)))) + (vec2-normalize! dir) + dir)) + +(define-method (angle-to (actor ) (other )) + (let ((p1 (position actor)) + (p2 (position other))) + (atan (- (vec2-y p2) (vec2-y p1)) + (- (vec2-x p2) (vec2-x p1))))) + +(define-method (add-to-quadtree (actor )) + (quadtree-insert! (quadtree actor) (world-hitbox actor) actor)) + +(define-method (remove-from-quadtree (actor )) + (quadtree-delete! (quadtree actor) (world-hitbox actor) actor)) + +(define-method (refresh-world-hitbox (actor )) + (let ((p (position actor)) + (h (hitbox actor)) + (wh (world-hitbox actor))) + (set-rect-x! wh (+ (vec2-x p) (rect-x h))) + (set-rect-y! wh (+ (vec2-y p) (rect-y h))) + (set-rect-width! wh (rect-width h)) + (set-rect-height! wh (rect-height h)))) + +(define-method (on-collide a b) + #f) + +(define-method (on-enter (actor )) + (refresh-world-hitbox actor) + (add-to-quadtree actor)) + +(define-method (on-exit (actor )) + (remove-from-quadtree actor)) + +(define-method (collision-check (actor )) + (let ((p (position actor)) + (r (world-hitbox actor))) + (quadtree-find + (quadtree actor) r + (lambda (other) + (and (not (eq? other actor)) + (rect-intersects? r (world-hitbox other)) + (on-collide actor other)))))) + +(define-method (after-move (actor )) + #t) + +(define-method (update (actor ) dt) + (let ((p (position actor)) + (v (velocity actor))) + (if (and (= (vec2-x v) 0.0) + (= (vec2-y v) 0.0)) + (collision-check actor) + (begin + (remove-from-quadtree actor) + (set-vec2! p + (+ (vec2-x p) (vec2-x v)) + (+ (vec2-y p) (vec2-y v))) + (after-move actor) + (refresh-world-hitbox actor) + (collision-check actor) + (add-to-quadtree actor) + (expire-local-matrix actor))))) diff --git a/super-bloom/common.scm b/super-bloom/common.scm new file mode 100644 index 0000000..08d4eae --- /dev/null +++ b/super-bloom/common.scm @@ -0,0 +1,69 @@ +;;; Copyright 2023 David Thompson +;;; +;;; 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. + +(define-module (super-bloom common) + #:use-module (catbird asset) + #:use-module (chickadee) + #:use-module (chickadee audio) + #:use-module (chickadee graphics text) + #:use-module (oop goops) + #:export (%default-width + %default-height + %game-width + %game-height + %game-width:float + %game-height:float + scope-datadir + monogram-font + explosion-sound + absorb-sound + watered-sound + spray-sound + random:float + steps + water)) + +(define %default-width 640) +(define %default-height 480) +(define %game-width 320) +(define %game-height 240) +(define %game-width:float (exact->inexact %game-width)) +(define %game-height:float (exact->inexact %game-height)) + +(define (scope-datadir file-name) + (let ((prefix (or (getenv "SUPERBLOOM_DATADIR") (getcwd)))) + (string-append prefix "/" file-name))) + +(define-asset (monogram-font (file (scope-datadir "assets/fonts/monogram_extended.ttf"))) + (load-font file 12 #:smooth? #f)) + +(define-asset (explosion-sound (file (scope-datadir "assets/audio/explosion.wav"))) + (load-audio file)) + +(define-asset (absorb-sound (file (scope-datadir "assets/audio/absorb.wav"))) + (load-audio file)) + +(define-asset (watered-sound (file (scope-datadir "assets/audio/watered.wav"))) + (load-audio file)) + +(define-asset (spray-sound (file (scope-datadir "assets/audio/spray.wav"))) + (load-audio file)) + +(define (random:float n) + (* (random:uniform) n)) + +(define (steps n) + (* n (current-timestep))) + +(define-accessor water) diff --git a/super-bloom/dirt-ball.scm b/super-bloom/dirt-ball.scm new file mode 100644 index 0000000..fd86d5b --- /dev/null +++ b/super-bloom/dirt-ball.scm @@ -0,0 +1,81 @@ +;;; Copyright 2023 David Thompson +;;; +;;; 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. + +(define-module (super-bloom dirt-ball) + #:use-module (catbird asset) + #:use-module (catbird node) + #:use-module (catbird node-2d) + #:use-module (chickadee audio) + #:use-module (chickadee graphics particles) + #:use-module (chickadee graphics texture) + #:use-module (chickadee math) + #:use-module (chickadee math rect) + #:use-module (chickadee math vector) + #:use-module (chickadee scripting) + #:use-module (oop goops) + #:use-module (super-bloom actor) + #:use-module (super-bloom common) + #:use-module (super-bloom flower) + #:use-module (super-bloom water) + #:export ()) + +(define-asset (dirt-ball-tileset (file (scope-datadir "assets/images/dirt-ball.png"))) + (load-tileset file 32 32)) + +(define-class () + (flower #:accessor flower #:init-keyword #:flower) + (speed #:accessor speed #:init-keyword #:speed) + (dead? #:accessor dead? #:init-value #f) + (hitbox #:getter hitbox #:init-form (make-rect -8.0 -8.0 16.0 16.0)) + (explosion-particles #:accessor explosion-particles #:init-keyword #:explosion-particles)) + +(define-method (initialize (dirt-ball ) initargs) + (next-method) + (attach-to dirt-ball + (make + #:name 'sprite + #:atlas dirt-ball-tileset + #:origin (vec2 16.0 16.0) + #:animations `((default . ,(make + #:frames #(0 1) + #:frame-duration 0.3)) + (up . ,(make + #:frames #(2 3) + #:frame-duration 0.3))))) + (let ((dir (direction-to dirt-ball (flower dirt-ball)))) + (change-velocity dirt-ball + (* (vec2-x dir) (speed dirt-ball)) + (* (vec2-y dir) (speed dirt-ball)))) + (update-animation dirt-ball)) + +(define-method (update-animation (dirt-ball )) + (let ((dy (vec2-y (velocity dirt-ball)))) + (change-animation (& dirt-ball sprite) (if (> dy 0.0) 'up 'default)))) + +(define-method (on-splash (dirt-ball )) + (set! (dead? dirt-ball) #t)) + +(define-method (on-collide (dirt-ball ) (flower )) + (damage flower 5) + (set! (dead? dirt-ball) #t)) + +(define-method (update (dirt-ball ) _dt) + (next-method) + (when (dead? dirt-ball) + (let ((p (position dirt-ball))) + (audio-play (artifact explosion-sound)) + (add-particle-emitter (particles (explosion-particles dirt-ball)) + (make-particle-emitter (make-rect (vec2-x p) (vec2-y p) 0.0 0.0) + 40 3)) + (detach dirt-ball)))) diff --git a/super-bloom/flower.scm b/super-bloom/flower.scm new file mode 100644 index 0000000..428b158 --- /dev/null +++ b/super-bloom/flower.scm @@ -0,0 +1,146 @@ +;;; Copyright 2023 David Thompson +;;; +;;; 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. + +(define-module (super-bloom flower) + #:use-module (catbird asset) + #:use-module (catbird mixins) + #:use-module (catbird node) + #:use-module (catbird node-2d) + #:use-module (chickadee audio) + #:use-module (chickadee graphics color) + #:use-module (chickadee graphics particles) + #:use-module (chickadee graphics path) + #:use-module (chickadee graphics texture) + #:use-module (chickadee math) + #:use-module (chickadee math rect) + #:use-module (chickadee math vector) + #:use-module (chickadee scripting) + #:use-module (oop goops) + #:use-module (super-bloom actor) + #:use-module (super-bloom common) + #:use-module (super-bloom water) + #:export ( + water + damage + growth-goal + growth-progress)) + +(define %max-water 1) + +(define-asset (flower-tileset (file (scope-datadir "assets/images/flower.png"))) + (load-tileset file 48 48)) + +;; Every growth-interval, the flower gains 1 growth point (accumulated +;; in growth-progress) and consumes 1 unit of water. If there is no +;; water, the flower is thirsty and stops growing. +(define-class () + (water #:accessor water #:init-value 0) + (growth-goal #:accessor growth-goal #:init-keyword #:growth-goal) + (growth-progress #:accessor growth-progress #:init-value 1) + (growth-interval #:accessor growth-interval #:init-keyword #:growth-interval) + (growth-accumulator #:accessor growth-accumulator #:init-value 0.0) + (hitbox #:getter hitbox #:init-form (make-rect -8.0 -8.0 16.0 16.0))) + +(define-method (initialize (flower ) initargs) + (next-method) + (attach-to flower + (make + #:name 'sprite + #:atlas flower-tileset + #:origin (vec2 24.0 24.0) + #:animations `((default . ,(make + #:frames #(0 1) + #:frame-duration 0.3)) + (stage-2 . ,(make + #:frames #(2 3 4 5) + #:frame-duration 0.3)) + (stage-3 . ,(make + #:frames #(6 7 8 9) + #:frame-duration 0.3)) + (stage-4 . ,(make + #:frames #(10 11 12 13) + #:frame-duration 0.3)))) + (make + #:name 'progress-meter + #:position (vec2 -32.0 -28.0) + #:width 64.0 + #:height 8.0)) + (update-progress-meter flower)) + +(define-method (update-progress-meter (flower )) + (define w 64.0) + (define t (/ (growth-progress flower) (growth-goal flower))) + (set! (painter (& flower progress-meter)) + (superimpose + (with-style ((stroke-color (make-color 0.0 0.0 0.0 0.5)) + (stroke-width 2.0)) + (stroke + (line (vec2 0.0 0.0) (vec2 w 0.0)))) + (with-style ((stroke-color db32-tahiti-gold) + (stroke-width 2.0)) + (stroke + (line (vec2 0.0 0.0) (vec2 (* w t) 0.0)))))) + (resize (& flower progress-meter) + (default-width (& flower progress-meter)) + (default-height (& flower progress-meter)))) + + +(define-method (update-animation (flower )) + (define t (/ (growth-progress flower) (growth-goal flower))) + (change-animation (& flower sprite) + (cond + ((< t 1/3) 'default) + ((< t 2/3) 'stage-2) + ((< t 1) 'stage-3) + (else 'stage-4)))) + +(define-method (increment-water (flower ) amount) + (set! (water flower) (min (+ (water flower) amount) %max-water))) + +(define-method (decrement-water (flower ) amount) + (set! (water flower) (max (- (water flower) amount) 0))) + +(define-method (damage (flower ) amount) + (set! (growth-progress flower) (max (- (growth-progress flower) amount) 0)) + (update-animation flower) + (update-progress-meter flower)) + +(define-method (thirsty? (flower )) + (= (water flower) 0)) + +(define-method (reset-growth-accumulator (flower )) + (set! (growth-accumulator flower) 0)) + +(define-method (on-splash (flower )) + (when (thirsty? flower) + (increment-water flower 1) + (audio-play (artifact watered-sound)))) + +(define-method (update (flower ) dt) + (unless (thirsty? flower) + (let ((interval (growth-interval flower)) + (accum (+ (growth-accumulator flower) dt))) + (set! (growth-accumulator flower) accum) + (when (>= accum interval) + (set! (growth-progress flower) + (min (+ (growth-progress flower) 1) + (growth-goal flower))) + (set! (growth-accumulator flower) + (if (thirsty? flower) 0 (- accum interval))) + (decrement-water flower 1) + (update-animation flower) + (update-progress-meter flower)))) + (next-method) + (when (= (growth-progress flower) 0) + (detach flower))) diff --git a/super-bloom/game.scm b/super-bloom/game.scm new file mode 100644 index 0000000..f5f1d99 --- /dev/null +++ b/super-bloom/game.scm @@ -0,0 +1,255 @@ +;;; Copyright 2023 David Thompson +;;; +;;; 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. + +(define-module (super-bloom game) + #:use-module (catbird) + #:use-module (catbird asset) + #:use-module (catbird camera) + #:use-module (catbird kernel) + #:use-module (catbird mode) + #:use-module (catbird node) + #:use-module (catbird node-2d) + #:use-module (catbird region) + #:use-module (catbird scene) + #:use-module (chickadee data quadtree) + #:use-module (chickadee graphics color) + #:use-module (chickadee graphics particles) + #:use-module (chickadee graphics texture) + #:use-module (chickadee math rect) + #:use-module (chickadee math vector) + #:use-module (oop goops) + #:use-module (super-bloom actor) + #:use-module (super-bloom common) + #:use-module (super-bloom dirt-ball) + #:use-module (super-bloom flower) + #:use-module (super-bloom player) + #:use-module (super-bloom water) + #:export ( + reset-game!)) + +(define-asset (background-texture (file (scope-datadir "assets/images/background.png"))) + (load-image file)) + +(define-asset (chickadee-texture (file (scope-datadir "assets/images/chickadee.png"))) + (load-image file)) + +(define-asset (water-particle-texture (file (scope-datadir "assets/images/water-particle.png"))) + (load-image file)) + +(define-asset (trail-particle-texture (file (scope-datadir "assets/images/trail-particle.png"))) + (load-image file)) + +(define-asset (explosion-particle-texture (file (scope-datadir "assets/images/explosion-particle.png"))) + (load-image file)) + +(define (make-game-quadtree) + (make-quadtree (make-rect 0.0 0.0 %game-width %game-height))) + +(define-class () + (quadtree #:getter quadtree #:init-thunk make-game-quadtree)) + +(define-method (on-enter (mode )) + (let ((scene (parent mode)) + (water-particles + (make + #:name 'water-particles + #:rank 1 + #:particles + (make-particles 1024 + #:texture (artifact water-particle-texture) + #:start-color white + #:end-color white + #:speed-range (vec2 2.0 5.0) + #:acceleration-range (vec2 0.1 0.3) + #:lifetime 10))) + (trail-particles + (make + #:name 'trail-particles + #:rank 1 + #:particles + (make-particles 1024 + #:texture (artifact trail-particle-texture) + #:animation-columns 3 + #:start-color white + #:end-color white + #:speed-range (vec2 0.2 0.5) + #:acceleration-range (vec2 0.1 0.2) + #:lifetime 12))) + (explosion-particles + (make + #:name 'explosion-particles + #:rank 1 + #:particles + (make-particles 1024 + #:texture (artifact explosion-particle-texture) + #:lifetime 20 + #:end-color white)))) + (attach-to scene + (make + #:name 'background + #:rank 0 + #:texture background-texture) + water-particles + trail-particles + explosion-particles + (make + #:name 'player + #:rank 2 + #:position (vec2 (/ %game-width:float 2.0) + (/ %game-height:float 4.0)) + #:quadtree (quadtree mode) + #:water-particles water-particles + #:trail-particles trail-particles) + (make + #:name 'flower + #:rank 4 + #:position (vec2 (/ %game-width:float 2.0) + (/ %game-height:float 2.0)) + #:quadtree (quadtree mode) + #:growth-goal 10 + #:growth-interval 0.5)))) + +(define-method (player (mode )) + (& (parent mode) player)) + +(define-method (player-move-left (mode )) + (set! (move-left? (player mode)) #t)) +(define-method (player-move-left-stop (mode )) + (set! (move-left? (player mode)) #f)) +(define-method (player-move-right (mode )) + (set! (move-right? (player mode)) #t)) +(define-method (player-move-right-stop (mode )) + (set! (move-right? (player mode)) #f)) +(define-method (player-move-down (mode )) + (set! (move-down? (player mode)) #t)) +(define-method (player-move-down-stop (mode )) + (set! (move-down? (player mode)) #f)) +(define-method (player-move-up (mode )) + (set! (move-up? (player mode)) #t)) +(define-method (player-move-up-stop (mode )) + (set! (move-up? (player mode)) #f)) + +(define-method (add-random-water (mode )) + (let ((p (vec2 (random:float %game-width:float) (random:float %game-height:float)))) + (attach-to (parent mode) + (make + #:rank 3 + #:position p + #:quadtree (quadtree mode))))) + +(define-method (add-random-dirt-ball (mode )) + (let ((p (vec2 (if (= (random 2) 0) -32.0 (+ %game-width:float 32.0)) + (+ (/ %game-height 2.0) + (* (/ %game-height 2.0) + (- (random:float 2.0) 1.0)))))) + (attach-to (parent mode) + (make + #:rank 3 + #:position p + #:quadtree (quadtree mode) + #:flower (& (parent mode) flower) + #:speed 1.0 + #:explosion-particles (& (parent mode) explosion-particles))))) + +(define-method (do-spray-water (mode )) + (spray-water (player mode))) + +(define-method (do-quit (mode )) + (exit-catbird)) + +(define-method (update (mode ) dt) + (let* ((scene (parent mode)) + (flower (& scene flower))) + (if flower + (when (= (growth-progress flower) (growth-goal flower)) + (stop-moving (& scene player)) + (replace-major-mode scene (make ))) + (begin + (pk 'no-flower) + (stop-moving (& scene player)) + (replace-major-mode scene (make )))))) + +(bind-input (key-press 'escape) do-quit) +(bind-input (key-press 'left) player-move-left) +(bind-input (key-release 'left) player-move-left-stop) +(bind-input (key-press 'right) player-move-right) +(bind-input (key-release 'right) player-move-right-stop) +(bind-input (key-press 'down) player-move-down) +(bind-input (key-release 'down) player-move-down-stop) +(bind-input (key-press 'up) player-move-up) +(bind-input (key-release 'up) player-move-up-stop) +(bind-input (key-press 'space) add-random-water) +(bind-input (key-press 'return) add-random-dirt-ball) +(bind-input (key-press 'z) do-spray-water) + +(define-class ()) + +(define-method (on-enter (mode )) + (let ((scene (parent mode))) + (attach-to scene + (make