From 1b174b77cfb7645fa1cda1eafb44d42497af8864 Mon Sep 17 00:00:00 2001
From: Gene Pasquet <dev@etenil.net>
Date: Wed, 22 May 2024 17:00:26 +0100
Subject: Engine sound and code clean-up

---
 turbo.scm | 107 ++++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 63 insertions(+), 44 deletions(-)

(limited to 'turbo.scm')

diff --git a/turbo.scm b/turbo.scm
index 15cae62..2611e19 100644
--- a/turbo.scm
+++ b/turbo.scm
@@ -36,6 +36,8 @@
 (define music-play? #t)
 (define music-res (load-audio "music.ogg"))
 (define music (make-source #:audio music-res #:loop? #t))
+(define engine-res (load-audio "engine-loop-1.ogg"))
+(define engine-sound (make-source #:audio engine-res #:loop? #t))
 
 (define (calculate-vehicle-position lane)
   (vec2 (window-width (current-window))
@@ -59,19 +61,18 @@
 
 (define vehicles (make-vehicle-list (list) 0))
 
+;; Important: set one vehicle active to bootstrap the game ????
+;; TODO: nope, bug here somewhere!
+(set-vehicle-active! (car vehicles) #t)
+
 (define game-over #f)
 
 (define (vehicle-has-spawn-space? vehicle)
-  (let loop ((vehicles vehicles))
-    (let ((other-vehicle (car vehicles))
-	  (other-vehicle-id (vehicle-id (car vehicles))))
-      (if (or (= other-vehicle-id (vehicle-id vehicle))
-	      (not (vehicle-active? other-vehicle))
-	      (vehicle-safety-distance? (car vehicles) vehicle))
-	  (if (null? (cdr vehicles))
-	      #t
-	      (loop (cdr vehicles)))
-	  #f))))
+  (define (vehicle-in-the-way? v)
+    (and (not (= (vehicle-id vehicle) (vehicle-id v)))
+	 (vehicle-inactive? v)
+	 (vehicle-safety-distance? v vehicle)))
+  (not (any vehicle-in-the-way? vehicles)))
 
 
 ;;;; Game functions
@@ -117,18 +118,29 @@
 		    (+ 1 player-min-speed)
 		    #t))
 
+(define (set-player-speed! new-speed)
+  (let* ((player-speed (max (min new-speed player-max-speed) player-min-speed))
+	(pitch-factor (* 0.10
+			 (- player-speed
+			    player-min-speed))))
+    (set-vehicle-speed! player-car player-speed)
+    (set-source-pitch! engine-sound (+ 1 pitch-factor))))
+
 (define (player-on-road?)
   (and (> (vec2-y (vehicle-position player-car)) road-start-y)
        (< (+ (vec2-y (vehicle-position player-car)) vehicle-height) road-end-y)))
 
+(define (crash-player top?)
+  (set! player-rotation (if top? 1 -1))
+  (set-vehicle-position! player-car (vec2+ (vehicle-position player-car) (vec2 0 (if top? 30 10))))
+  (set-vehicle-speed! player-car 0)
+  (source-stop engine-sound))
+
 (define (off-road-player-top)
-  (set! player-rotation 1)
-  (set-vehicle-position! player-car (vec2+ (vehicle-position player-car) (vec2 0 30)))
-  (set-vehicle-speed! player-car 0))
+  (crash-player #t))
+
 (define (off-road-player-bottom)
-  (set! player-rotation -1)
-  (set-vehicle-position! player-car (vec2+ (vehicle-position player-car) (vec2 0 -10)))
-  (set-vehicle-speed! player-car 0))
+  (crash-player #f))
 
 (define (off-road-player)
   "Moves the player off-road"
@@ -143,11 +155,24 @@
 	 vehicles)))
 
 
+(define (vehicle-off-screen? veh)
+  (and vehicle-active?
+       (>= (- 0 vehicle-length) (vec2-x (vehicle-position veh)))))
+
+(define (vehicle-reset-ahead veh)
+  (set-vehicle-position! veh (calculate-vehicle-position
+			      (+ 1 (random road-num-lanes))))
+  (set-vehicle-active! veh #f))
+
 ;;;; Chickadee hooks
 
+(set-source-volume! music 0.5)
 (if music-play?
     (source-play music))
 
+(if music-play?
+    (source-play engine-sound))
+
 (define (update interval)
   (if (not game-over)
       (begin
@@ -163,33 +188,27 @@
 	      (set! game-over #t)))
 
 	(set! road-section-position
-	      (if (< road-section-position (* lane-length -1))
+	      (if (< road-section-position (- 0 lane-length))
 		  (- (+ road-section-position lane-length) (vehicle-speed player-car))
 		  (- road-section-position (vehicle-speed player-car))))
 
 	(set-vehicle-position! player-car (vec2+ (vehicle-position player-car) player-velocity))
 
 	(for-each (lambda (veh)
-		    (if (vehicle-active? veh)
-			(let ((speed-diff (vehicle-speed-diff player-car veh)))
-			  (set-vehicle-position! veh
-						 (vec2+ (vehicle-position veh)
-							(vec2 (* speed-diff -1) 0))))))
-		  vehicles)
-
-	;; when van goes off screen
-	(for-each
-	 (lambda (veh)
-	   (if (>= (* vehicle-length -1) (vec2-x (vehicle-position veh)))
-	       (begin
-		 (set-vehicle-position! veh (calculate-vehicle-position
-					     (+ 1 (random road-num-lanes))))
-		 (set-vehicle-active! veh #f))
-	       (if (and (not (vehicle-active? veh))
-			(= 0 (random level-difficulty))
-			(vehicle-has-spawn-space? veh))
-		   (set-vehicle-active! veh #t))))
-	 vehicles))))
+		    (vehicle-move! veh (vec2 (- 0 (vehicle-speed-diff player-car veh)) 0)))
+		  (filter vehicle-active? vehicles))
+
+	;; when vehicle goes off screen
+	(for-each vehicle-reset-ahead (filter vehicle-off-screen? vehicles))
+
+	(if (= 0 (random level-difficulty))
+	    (begin
+	      (let* ((inactive-vehicles (filter vehicle-inactive? vehicles))
+		     (spawnable-vehicles (filter vehicle-has-spawn-space? inactive-vehicles)))
+		(if (not (null? spawnable-vehicles))
+		    (set-vehicle-active! (car spawnable-vehicles) #t)))))
+	;; End begin
+	)))
 
 (define (draw alpha)
   (draw-road road-section-position road-start-y road-num-lanes)
@@ -201,9 +220,8 @@
   (draw-sprite (vehicle-sprite player-car) (vehicle-position player-car) #:rotation player-rotation)
 
   (for-each (lambda (veh)
-	      (if (vehicle-active? veh)
-		  (draw-sprite (vehicle-sprite veh) (vehicle-position veh))))
-	    vehicles)
+	      (draw-sprite (vehicle-sprite veh) (vehicle-position veh)))
+	    (filter vehicle-active? vehicles))
 
   (draw-text (format #f "Speed: ~smph" (* (vehicle-speed player-car) 5))
 	     (vec2 450.0 20.0)
@@ -221,17 +239,18 @@
    ((eq? key 'w)
     (set! player-velocity (vec2 0 vehicle-steering-speed)))
    ((eq? key 's)
-    (set! player-velocity (vec2 0 (* vehicle-steering-speed -1))))
+    (set! player-velocity (vec2 0 (- 0 vehicle-steering-speed))))
    ((eq? key 'a)
-       (set-vehicle-speed! player-car (max (- (vehicle-speed player-car) 1) player-min-speed)))
+    (set-player-speed! (- (vehicle-speed player-car) 1)))
    ((eq? key 'd)
-       (set-vehicle-speed! player-car (min (+ (vehicle-speed player-car) 1) player-max-speed)))
+    (set-player-speed! (+ (vehicle-speed player-car) 1)))
    ((eq? key 'escape)
        (abort-game))
    ((and (eq? key 'return) game-over)
     (set-vehicle-position! player-car initial-player-position)
-    (set-vehicle-speed! player-car (+ 1 player-min-speed))
+    (set-player-speed! (+ 1 player-min-speed))
     (set! player-rotation 0)
+    (source-play engine-sound)
     ;; Reset all vehicles
     (set! game-over #f))))
 
-- 
cgit v1.2.3