From ab5aac9c5907adf55320b5e8396689b971b7361b Mon Sep 17 00:00:00 2001
From: Gene Pasquet <dev@etenil.net>
Date: Wed, 22 May 2024 04:57:10 +0100
Subject: Spawn vehicles orderly, enable collisions

---
 turbo.scm | 52 +++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 39 insertions(+), 13 deletions(-)

diff --git a/turbo.scm b/turbo.scm
index 400146b..9e16e1f 100644
--- a/turbo.scm
+++ b/turbo.scm
@@ -44,8 +44,9 @@
 (define music (make-source #:audio music-res #:loop? #t))
 
 (define-record-type <vehicle>
-  (make-vehicle sprite position speed active?)
+  (make-vehicle id sprite position speed active?)
   vechicle?
+  (id vehicle-id)
   (sprite vehicle-sprite)
   (position vehicle-position set-vehicle-position!)
   (speed vehicle-speed set-vehicle-speed!)
@@ -57,13 +58,15 @@
 	    (* 65 (- lane 1))
 	   (/ (- lane-height vehicle-height) 2))))
 
-(define (make-inactive-vehicle tile)
-  (make-vehicle tile (calculate-vehicle-position (random 5)) 4 #f))
+(define (make-inactive-vehicle id tile)
+  (make-vehicle id tile (calculate-vehicle-position (+ 1 (random 5))) 4 #f))
 
 (define (make-vehicle-list vehicle-list tile-ref)
   (if (< tile-ref (texture-atlas-size vehicles-sprites))
-      (cons (make-inactive-vehicle (texture-atlas-ref vehicles-sprites tile-ref))
-	    (cons (make-inactive-vehicle (texture-atlas-ref vehicles-sprites tile-ref))
+      (cons (make-inactive-vehicle (* 2 tile-ref)
+				   (texture-atlas-ref vehicles-sprites tile-ref))
+	    (cons (make-inactive-vehicle (+ 1 (* 2 tile-ref))
+					 (texture-atlas-ref vehicles-sprites tile-ref))
 		  (make-vehicle-list vehicle-list (+ tile-ref 1))))
       vehicle-list))
 
@@ -71,6 +74,26 @@
 
 (define game-over #f)
 
+(define (vehicle-safety-distance? vehicle1 vehicle2)
+  (let ((vehicle1-x (vec2-x (vehicle-position vehicle1)))
+	(vehicle2-x (vec2-x (vehicle-position vehicle2))))
+    (> vehicle2-x
+       (+ (* 2 vehicle-length)
+	  vehicle1-x))))
+
+(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))))
+
+
 ;;;; Game functions
 
 (define (draw-lanes x y num-lanes)
@@ -129,13 +152,15 @@
 (define (vehicle-hitbox vehicle-pos)
   (rect (vec2-x vehicle-pos) (vec2-y vehicle-pos) vehicle-length vehicle-height))
 
-;; (define (player-collides?)
-;;   (let ((player-hitbox (vehicle-hitbox player-position))
-;; 	(van-hitbox (vehicle-hitbox red-van-position)))
-;;     (rect-intersects? player-hitbox van-hitbox)))
-
 (define (player-collides?)
-  #f)
+  (let ((player-hitbox (vehicle-hitbox player-position)))
+    (let loop ((vehicles vehicles))
+      (if (rect-intersects? player-hitbox (vehicle-hitbox (vehicle-position (car vehicles))))
+	  #t
+	  (if (null? (cdr vehicles))
+	      #f
+	      (loop (cdr vehicles)))))))
+
 
 ;;;; Chickadee hooks
 
@@ -179,10 +204,11 @@
 		 (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 200)))
+	       (if (and (not (vehicle-active? veh))
+			(= 0 (random 200))
+			(vehicle-has-spawn-space? veh))
 		   (set-vehicle-active! veh #t))))
 	 vehicles))))
-	
 
 (define (draw alpha)
   (draw-road road-section-position road-start-y road-num-lanes)
-- 
cgit v1.2.3