diff options
author | Henrique Alves <henrique.alves@itsjungle.xyz> | 2024-05-02 00:37:10 +0300 |
---|---|---|
committer | Henrique Alves <henrique.alves@itsjungle.xyz> | 2024-05-02 00:37:10 +0300 |
commit | 510a5d76180f30f1dd75a05927bbcb10bfae8da8 (patch) | |
tree | 4395294fd3fe9076169b467b22f34e2fff22a8b9 | |
parent | aeddacb8d88f41ba7bdf37d39de17d8cf3b6a258 (diff) | |
download | gamejam-slgj-2024-510a5d76180f30f1dd75a05927bbcb10bfae8da8.tar.gz gamejam-slgj-2024-510a5d76180f30f1dd75a05927bbcb10bfae8da8.tar.bz2 gamejam-slgj-2024-510a5d76180f30f1dd75a05927bbcb10bfae8da8.zip |
basic GoL
-rw-r--r-- | scripts/gol.scm | 76 | ||||
-rw-r--r-- | scripts/main.scm | 20 | ||||
-rw-r--r-- | scripts/prompt.scm | 2 | ||||
-rw-r--r-- | sources/main.c | 4 |
4 files changed, 97 insertions, 5 deletions
diff --git a/scripts/gol.scm b/scripts/gol.scm new file mode 100644 index 0000000..f95dc91 --- /dev/null +++ b/scripts/gol.scm @@ -0,0 +1,76 @@ +(define current-cells (make-hash-table)) + +(define neighbors + '((-1 . -1) (0 . -1) (1 . -1) (-1 . 0) (1 . 0) (-1 . 1) (0 . 1) (1 . 1))) + +(set! (current-cells '(1 . 1)) #t) +(set! (current-cells '(1 . 2)) #t) +(set! (current-cells '(1 . 3)) #t) + +(define sum-cells + (lambda (c1 c2) + (cons (+ (car c1) (car c2)) (+ (cdr c1) (cdr c2))))) + +(define diff-cells + (lambda (c1 c2) + (cons (- (car c1) (car c2)) (- (cdr c1) (cdr c2))))) + +(define sum-all-cells + (lambda (lat) + (cond ((null? (cdr lat)) (car lat)) + (else (let ((c1 (car lat)) (c2 (sum-all-cells (cdr lat)))) + (sum-cells c1 c2) + ))))) + +(define is-hash-neighbor? + (lambda (c1 c2) + (let ((d (diff-cells c1 c2))) + (and (<= (car d) 1) (>= (car d) -1) (<= (cdr d) 1) (>= (cdr d) -1))))) + +(define count-elements + (lambda (lat s) + (cond ((null? lat) s) + (else (count-elements (cdr lat) (+ s 1)))))) + +(define list-neighbors + (lambda (c) + (map (lambda (n) + (sum-cells c n)) + neighbors))) + +(define sum-live-cells + (lambda (lat n) + (cond ((null? lat) n) + (else (cond ((current-cells (car lat)) (sum-live-cells (cdr lat) (+ n 1))) + (else (sum-live-cells (cdr lat) n))))))) + +(define list-all-neighbors + (lambda (lat cells) + (cond ((null? lat) cells) + (else + (map (lambda (new-neighbor) + (set! (cells new-neighbor) #t)) + (cons (car lat) (list-neighbors (car lat)))) + (list-all-neighbors (cdr lat) cells))))) + +(define (hash-table-keys ht) + (map (lambda (kv) + (kv 0) + ) + ht) + ) + +(define run-step + (lambda () + (let ((temp-hash (make-hash-table)) (next-cells (make-hash-table))) + (map (lambda (temp-cell) + (let ((n (sum-live-cells (list-neighbors temp-cell) 0))) + (cond ((current-cells temp-cell) + (cond ((and (>= n 2) (<= n 3)) (set! (next-cells temp-cell) #t)))) + (else (cond ((eq? n 3) (set! (next-cells temp-cell) #t)))))) + ) + (hash-table-keys (list-all-neighbors (hash-table-keys current-cells) temp-hash))) + (set! current-cells next-cells) + ) + ) + ) diff --git a/scripts/main.scm b/scripts/main.scm index 1a5ebb1..f480053 100644 --- a/scripts/main.scm +++ b/scripts/main.scm @@ -2,17 +2,31 @@ (load "./scripts/structs.scm") (load "./scripts/utils.scm") (load "./scripts/prompt.scm") +(load "./scripts/gol.scm") -;; (define a 0) -;; (define b (rl-load-texture)) - +(define camera-offset (make-point)) (define (update) (prompt-update) + + (if (rl-is-key-down KEY_DOWN) + (run-step) + ) ) (define (draw) (prompt-draw) + + (for-each (lambda (cell) + (rl-draw-rectangle + (* (car cell) 30) + (* (cdr cell) 30) + 30 + 30 + (make-color 190 100 255)) + + ) (hash-table-keys current-cells)) + ) diff --git a/scripts/prompt.scm b/scripts/prompt.scm index 738cd42..f3fe6c9 100644 --- a/scripts/prompt.scm +++ b/scripts/prompt.scm @@ -6,7 +6,7 @@ (let ((key-pressed (rl-get-key-pressed))) - (if (eq? key-pressed 161) + (if (or (eq? key-pressed 161) (eq? key-pressed 96)) (set! prompt-active (not prompt-active)) ) diff --git a/sources/main.c b/sources/main.c index b248f24..3c2e1da 100644 --- a/sources/main.c +++ b/sources/main.c @@ -21,12 +21,14 @@ s7_pointer s7_draw_fn; #ifdef __EMSCRIPTEN__ EM_BOOL main_loop_web(double time, void* userData) { + s7_call(s7, s7_update_fn, s7_list(s7, 0)); - + BeginDrawing(); ClearBackground(BLUE); s7_call(s7, s7_draw_fn, s7_list(s7, 0)); EndDrawing(); + PollInputEvents(); // <- this is necessary because of a web specific bug. See https://github.com/raysan5/raylib/issues/2379 return EM_TRUE; } #else |