diff options
author | henriquelalves <henriquelalves@gmail.com> | 2024-05-26 14:39:55 +0300 |
---|---|---|
committer | henriquelalves <henriquelalves@gmail.com> | 2024-05-26 14:39:55 +0300 |
commit | 56a3d75bbbd80512f76817a05eebaabdcaf37ae2 (patch) | |
tree | f6e3b87c484ebe3df51d6e43d9e76b4ded9efb01 | |
parent | 0a89a84956311f17648bb443e429dab25d35f0cc (diff) | |
download | gamejam-slgj-2024-56a3d75bbbd80512f76817a05eebaabdcaf37ae2.tar.gz gamejam-slgj-2024-56a3d75bbbd80512f76817a05eebaabdcaf37ae2.tar.bz2 gamejam-slgj-2024-56a3d75bbbd80512f76817a05eebaabdcaf37ae2.zip |
Update for SLGJ 2024
-rw-r--r-- | scripts/gol.scm | 35 | ||||
-rw-r--r-- | scripts/main.scm | 308 | ||||
-rw-r--r-- | scripts/prompt.scm | 53 | ||||
-rw-r--r-- | scripts/stage.scm | 0 | ||||
-rw-r--r-- | scripts/structs.scm | 1 | ||||
-rw-r--r-- | sources/main.c | 4 | ||||
-rw-r--r-- | sources/rl/core.h | 12 | ||||
-rw-r--r-- | sources/rl/shapes.h | 14 | ||||
-rw-r--r-- | sources/rl/types.h | 4 |
9 files changed, 359 insertions, 72 deletions
diff --git a/scripts/gol.scm b/scripts/gol.scm index 7e10d2d..e0732a6 100644 --- a/scripts/gol.scm +++ b/scripts/gol.scm @@ -3,12 +3,6 @@ (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 '(2 . 1)) #t) -(set! (current-cells '(3 . 1)) #t) -(set! (current-cells '(3 . 0)) #t) -(set! (current-cells '(2 . -1)) #t) - (define sum-cells (lambda (c1 c2) (cons (+ (car c1) (car c2)) (+ (cdr c1) (cdr c2))))) @@ -37,8 +31,10 @@ (define list-neighbors (lambda (c) (map (lambda (n) - (sum-cells c n)) - neighbors))) + (sum-cells c n) + ) + neighbors) + )) (define sum-live-cells (lambda (lat n) @@ -76,3 +72,26 @@ ) ) ) + +(define (clear-cells) + (set! current-cells (make-hash-table)) + ) + +(define (toggle-cell c) + (if (current-cells c) + (set! (current-cells c) #f) + (set! (current-cells c) #t) + ) + ) + +(define (set-cells-array arr) + (map (lambda (c) + (set! (current-cells c) #t) + ) + arr + ) + ) + +(define (get-cells-list) + (hash-table-keys current-cells) + ) diff --git a/scripts/main.scm b/scripts/main.scm index 5dcbe10..9db17fc 100644 --- a/scripts/main.scm +++ b/scripts/main.scm @@ -3,53 +3,297 @@ (load "./scripts/prompt.scm") (load "./scripts/gol.scm") -(define camera-offset (make-point)) -(define space-pressed #f) +(define current-stage-number 0) +(define current-generation 0) +(define current-pressed-key #f) +(define starting-cells (list)) +(define trigger-level-reload #t) +(define overlay-fade-time 0.0) +(define is-advancing #f) -(define (update) - (prompt-update) - (cond ((not prompt-active) - (if (rl-is-key-down KEY_DOWN) - (set! (point-y camera-offset) (+ (point-y camera-offset) 2)) - ) - - (if (rl-is-key-down KEY_UP) - (set! (point-y camera-offset) (+ (point-y camera-offset) -2)) - ) +(define stages (list (list '(1 . 0) + '(1 . 1) + '(0 . 0) + '(0 . 1)) + (list '(0 . -1) + '(1 . 0) + '(0 . 1) + '(-1 . 0)) + (list '(0 . 0) + '(0 . 1) + '(0 . 2)) + (list '(0 . 0) + '(-1 . 0) + '(0 . -1)) + (list '(1 . 0) + '(0 . 1)) + (list '(2 . 0) + '(1 . 0) + '(1 . 1) + '(0 . 1) + '(0 . 2)) + (list '(-1 . -1) + '(0 . -1) + '(1 . 0) + '(0 . 1) + '(-1 . 1) + '(-2 . 0)) + (list '(-1 . -1) + '(0 . -1) + '(1 . -1) + '(-1 . 0) + '(1 . 0) + '(-1 . 1) + '(0 . 1) + '(1 . 1)) + (list '(0 . -1) + '(0 . 0) + '(0 . 1) + '(1 . -1) + '(1 . 0) + '(1 . 1) + '(2 . 0)) + )) - (if (rl-is-key-down KEY_LEFT) - (set! (point-x camera-offset) (+ (point-x camera-offset) -2)) +(define (update-generation) + (clear-cells) + (set-cells-array starting-cells) + + (let loop ((times 0)) + (if (< times current-generation) + (begin (run-step) + (loop (+ times 1))) + ) + ) + ) + +(define (is-solution? l1) + (define trigger #t) + (for-each (lambda (c) + (if (not (hash-table-ref current-cells c)) + (begin (set! trigger #f))) + ) + (stages current-stage-number)) + (and trigger (eq? (length (stages current-stage-number)) (length (hash-table-keys current-cells)))) + ) + +(define (update-game) + + (set! current-pressed-key (rl-get-key-pressed)) + + (prompt-update) + + (if trigger-level-reload + (begin (set! starting-cells (list)) + (clear-cells) + (set-cells-array starting-cells) + (set! trigger-level-reload #f))) + + (cond ((and (not prompt-active) (not is-advancing)) + (if (eq? current-pressed-key KEY_LEFT) + (cond ((> current-generation 0) + (set! current-generation (- current-generation 1)) + (update-generation))) ) - (if (rl-is-key-down KEY_RIGHT) - (set! (point-x camera-offset) (+ (point-x camera-offset) 2)) + (if (eq? current-pressed-key KEY_RIGHT) + (cond ((< current-generation 1) + (set! current-generation (+ current-generation 1)) + (update-generation))) ) - (cond ((and (rl-is-key-down KEY_SPACE) (not space-pressed)) - (set! space-pressed #t) - (run-step)) - ) - - (if (not (rl-is-key-down KEY_SPACE)) - (set! space-pressed #f) - ) )) + + (if (and (eq? current-generation 1) (not is-advancing)) + (if (is-solution? (stages current-stage-number)) + (begin (set! is-advancing #t) + (set! overlay-fade-time 0.0)) + ) + ) + + (if (and (rl-is-mouse-button-pressed) (eq? current-generation 0)) + (let ((mouse-pos (rl-get-mouse-position))) + (define tile-x (floor ( / (- (mouse-pos 0) 275) 50))) + (define tile-y (floor ( / (- (mouse-pos 1) 200) 50))) + (if (and (>= tile-x -1) + (<= tile-x 1) + (>= tile-y -1) + (<= tile-y 1)) + (begin (toggle-cell `(,tile-x . ,tile-y)) + (set! starting-cells (get-cells-list)))) + )) + + (if is-advancing + (set! overlay-fade-time (+ overlay-fade-time (rl-get-frame-time))) + (set! overlay-fade-time (- overlay-fade-time (rl-get-frame-time))) + ) + + (if (> overlay-fade-time 1.0) + (begin (set! is-advancing #f) + (clear-cells) + (set! starting-cells (list)) + (set! current-generation 0) + (set! current-stage-number (+ current-stage-number 1)) + ) + ) ) -(define (draw) +(define (update-end) + (if is-advancing + (set! overlay-fade-time (+ overlay-fade-time (rl-get-frame-time))) + (set! overlay-fade-time (- overlay-fade-time (rl-get-frame-time))) + ) + ) + +(define (update) + (if (>= current-stage-number (length stages)) + (update-end) + (update-game) + ) + ) + +(define (draw-game) + (rl-draw-rectangle 0 0 600 450 (make-color 255 255 255 255)) + (prompt-draw) + + (rl-draw-text (string-append "Level " (number->string current-stage-number)) 75 15 20 (make-color 0 0 0 255)) + + (draw-cells) + + (draw-grid) + + (draw-generation) + + (define lerped-alpha (* 255.0 overlay-fade-time) ) + (if (< lerped-alpha 0.0) + (set! lerped-alpha 0.0)) + + (rl-draw-rectangle 0 0 600 450 (make-color 255 255 255 (floor lerped-alpha))) + ) + +(define (draw-end) + (rl-draw-rectangle 0 0 600 450 (make-color 255 255 255 255)) + + (rl-draw-text "The end! Thank you for playing :)" 38 200 30 (make-color 0 0 0 255)) + + (rl-draw-text "Made with Raylib + S7 Scheme" 146 340 20 (make-color 0 0 0 255)) + + (define lerped-alpha (* 255.0 overlay-fade-time) ) + (if (< lerped-alpha 0.0) + (set! lerped-alpha 0.0)) + + (rl-draw-rectangle 0 0 600 450 (make-color 255 255 255 (floor lerped-alpha))) + ) + +(define (draw-generation) + + (let loop ((times 0)) + (if (< times 2) + (begin + (rl-draw-rectangle (+ 75 (* times 40)) + 410 + 30 + 30 + (make-color 190 190 190 255)) + (if (eq? times current-generation) + (rl-draw-rectangle (+ 80 (* times 40)) + 415 + 20 + 20 + (make-color 0 0 0 255))) + + (loop (+ times 1)) + ))) + + ) + +(define (draw) + (if (>= current-stage-number (length stages)) + (draw-end) + (draw-game) + ) + ) + +(define (draw-grid) + (define cell-width 50) + (define cell-height 50) + + (let loop ((times 10)) + (if (> times 0) + (begin (rl-draw-line (+ (* times cell-width) (* cell-width 0.5)) + 50 + (+ (* times cell-width) (* cell-width 0.5)) + 400 + (make-color 200 200 200 255)) + (loop (- times 1))) + ) + ) + + (let loop ((times 8)) + (if (> times 0) + (begin (rl-draw-line 75 + (* times cell-height) + 525 + (* times cell-height) + (make-color 200 200 200 255)) + (loop (- times 1))) + ) + ) + + (let loop ((times 4)) + (if (> times 0) + (begin (rl-draw-line (+ (* times cell-width) 175) + 150 + (+ (* times cell-width) 175) + 300 + (make-color 0 0 0 255)) + (loop (- times 1))) + ) + ) + + (let loop ((times 4)) + (if (> times 0) + (begin (rl-draw-line 225 + (+ (* times cell-height) 100) + 375 + (+ (* times cell-height) 100) + (make-color 0 0 0 255)) + (loop (- times 1))) + ) + ) + ) + +(define (draw-cells) + (define cell-width 50) + (define cell-height 50) + (define cell-color (if (eq? current-generation 0) + (make-color 190 100 255 255) + (make-color 255 190 100 255))) + + (for-each (lambda (cell) (rl-draw-rectangle - (+ (* (car cell) 30) (point-x camera-offset)) - (+ (* (cdr cell) 30) (point-y camera-offset)) - 30 - 30 - (make-color 190 100 255)) + (+ (* (car cell) cell-width) (- 300 (* cell-width 0.5)) ) + (+ (* (cdr cell) cell-height) (- 225 (* cell-height 0.5)) ) + cell-width + cell-height + cell-color) ) (hash-table-keys current-cells)) - - ) + (for-each (lambda (cell) + (rl-draw-rectangle + (+ (* (car cell) 50) 290 ) + (+ (* (cdr cell) 50) 215 ) + 20 + 20 + (make-color 255 190 100 255)) + + ) (stages current-stage-number)) + + + ) diff --git a/scripts/prompt.scm b/scripts/prompt.scm index 3440b28..32e7606 100644 --- a/scripts/prompt.scm +++ b/scripts/prompt.scm @@ -3,35 +3,32 @@ (define prompt-text "") (define (prompt-update) - (let ((key-pressed (rl-get-key-pressed))) - - - (if (eq? key-pressed KEY_F1) - (set! prompt-active (not prompt-active)) - ) - - (if prompt-active - (begin - (let ((key (rl-get-char-pressed))) - (cond ((and (>= key 32) (<= key 125)) - (set! prompt-text - (string-append prompt-text (string (integer->char key))))) - )) - - (if (rl-is-key-down KEY_ENTER) - (begin - (eval-string prompt-text) - (set! prompt-text "") + (if (eq? current-pressed-key KEY_F1) + (set! prompt-active (not prompt-active)) + ) + + (if prompt-active + (begin + (let ((key (rl-get-char-pressed))) + (cond ((and (>= key 32) (<= key 125)) + (set! prompt-text + (string-append prompt-text (string (integer->char key))))) )) - - (if (eq? key-pressed KEY_BACKSPACE) - (let ((n (string-length prompt-text))) - (cond ((>= n 1) - (set! prompt-text (substring prompt-text 0 (- n 1)))) - ))) - - )) - ) + + (if (rl-is-key-down KEY_ENTER) + (begin + (eval-string prompt-text) + (set! prompt-text "") + )) + + (if (eq? current-pressed-key KEY_BACKSPACE) + (let ((n (string-length prompt-text))) + (cond ((>= n 1) + (set! prompt-text (substring prompt-text 0 (- n 1)))) + ))) + + )) + ) (define (prompt-draw) diff --git a/scripts/stage.scm b/scripts/stage.scm new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/scripts/stage.scm diff --git a/scripts/structs.scm b/scripts/structs.scm index e6be071..c3fa6ea 100644 --- a/scripts/structs.scm +++ b/scripts/structs.scm @@ -77,3 +77,4 @@ (defstruct rect (x 0.0) (y 0.0) (width 0.0) (height 0.0)) (defstruct point (x 0.0) (y 0.0)) +(defstruct stage (width 0) (height 0) (generations 0) (final-state #f)) diff --git a/sources/main.c b/sources/main.c index 34f2eda..a7ae609 100644 --- a/sources/main.c +++ b/sources/main.c @@ -54,8 +54,8 @@ int main(int argc, char* argv[]) { rl_core_define_methods(s7); rl_shapes_define_methods(s7); - const int screen_width = 800; - const int screen_height = 600; + const int screen_width = 600; + const int screen_height = 450; InitWindow(screen_width, screen_height, "SLGJ - 2024"); SetTargetFPS(60); diff --git a/sources/rl/core.h b/sources/rl/core.h index 09c5555..14d8ba6 100644 --- a/sources/rl/core.h +++ b/sources/rl/core.h @@ -11,12 +11,22 @@ static s7_pointer rl_get_mouse_position(s7_scheme *s7, s7_pointer args) { return vec; } +static s7_pointer rl_is_mouse_button_pressed(s7_scheme *s7, s7_pointer args) { + bool isPressed = IsMouseButtonPressed(0); + return(s7_make_boolean(s7, isPressed)); +} + static s7_pointer rl_is_key_down(s7_scheme *s7, s7_pointer args) { int key = s7_integer(s7_car(args)); bool isKey = IsKeyDown(key); return(s7_make_boolean(s7, isKey)); } +static s7_pointer rl_get_frame_time(s7_scheme *s7, s7_pointer args) { + float dt = GetFrameTime(); + return(s7_make_real(s7, dt)); +} + static s7_pointer rl_get_char_pressed(s7_scheme *s7, s7_pointer args) { int key = GetCharPressed(); return(s7_make_integer(s7, key)); @@ -32,4 +42,6 @@ static void rl_core_define_methods(s7_scheme *s7) { s7_define_function(s7, "rl-get-mouse-position", rl_get_mouse_position, 0, 0, false, "(rl-get-mouse-position)"); s7_define_function(s7, "rl-get-char-pressed", rl_get_char_pressed, 0, 0, false, "(rl-get-char-pressed)"); s7_define_function(s7, "rl-get-key-pressed", rl_get_key_pressed, 0, 0, false, "(rl-get-key-pressed)"); + s7_define_function(s7, "rl-get-frame-time", rl_get_frame_time, 0, 0, false, "(rl-get-frame-time)"); + s7_define_function(s7, "rl-is-mouse-button-pressed", rl_is_mouse_button_pressed, 0, 0, false, "(rl_is_mouse_button_pressed)"); } diff --git a/sources/rl/shapes.h b/sources/rl/shapes.h index 10d38eb..47e736c 100644 --- a/sources/rl/shapes.h +++ b/sources/rl/shapes.h @@ -15,6 +15,20 @@ static s7_pointer rl_draw_rectangle(s7_scheme *s7, s7_pointer args) { return(NULL); } +static s7_pointer rl_draw_line(s7_scheme *s7, s7_pointer args) { + Color *c = (Color *)s7_c_object_value(s7_car(s7_cdr(s7_cdr(s7_cdr(s7_cdr(args)))))); + + DrawLine(s7_real(s7_car(args)), + s7_real(s7_car(s7_cdr(args))), + s7_real(s7_car(s7_cdr(s7_cdr(args)))), + s7_real(s7_car(s7_cdr(s7_cdr(s7_cdr(args))))), + *c); + + return(NULL); +} + + static void rl_shapes_define_methods(s7_scheme *s7) { s7_define_function(s7, "rl-draw-rectangle", rl_draw_rectangle, 5, 0, false, "test"); + s7_define_function(s7, "rl-draw-line", rl_draw_line, 5, 0, false, "test"); } diff --git a/sources/rl/types.h b/sources/rl/types.h index 4a7244f..da426ae 100644 --- a/sources/rl/types.h +++ b/sources/rl/types.h @@ -21,7 +21,7 @@ static s7_pointer make_color(s7_scheme *sc, s7_pointer args) c->r = s7_integer(s7_car(args)); c->g = s7_integer(s7_cadr(args)); c->b = s7_integer(s7_caddr(args)); - c->a = 255; + c->a = s7_integer(s7_cadddr(args)); return(s7_make_c_object(sc, color_type_tag, (void *)c)); } @@ -92,7 +92,7 @@ static void rl_register_types(s7_scheme *s7) { s7_c_type_set_gc_free(s7, color_type_tag, free_color); s7_c_type_set_to_string(s7, color_type_tag, color_to_string); - s7_define_function(s7, "make-color", make_color, 3, 0, false, "(make-color r g b) makes a new color"); + s7_define_function(s7, "make-color", make_color, 4, 0, false, "(make-color r g b a) makes a new color"); s7_define_function(s7, "color?", is_color, 1, 0, false, "(color? anything) returns #t if its argument is a color object"); |