2024-05-17 23:49:31 +02:00
|
|
|
;;; 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 (game main)
|
2024-05-19 21:54:30 +02:00
|
|
|
#:use-module (game level)
|
2024-05-17 23:49:31 +02:00
|
|
|
#:use-module (chickadee)
|
2024-05-19 18:48:18 +02:00
|
|
|
#:use-module (chickadee math rect)
|
2024-05-17 23:49:31 +02:00
|
|
|
#:use-module (chickadee math vector)
|
2024-05-19 18:48:18 +02:00
|
|
|
#:use-module (chickadee graphics color)
|
2024-05-17 23:49:31 +02:00
|
|
|
#:use-module (chickadee graphics sprite)
|
|
|
|
#:use-module (chickadee graphics texture)
|
2024-05-19 18:48:18 +02:00
|
|
|
#:use-module (chickadee graphics text)
|
|
|
|
#:use-module (chickadee graphics tile-map)
|
2024-05-17 23:49:31 +02:00
|
|
|
#:use-module (system repl coop-server)
|
2024-05-19 21:54:30 +02:00
|
|
|
#:use-module (srfi srfi-64)
|
2024-05-17 23:49:31 +02:00
|
|
|
#:export (launch-game))
|
|
|
|
|
2024-05-19 18:48:18 +02:00
|
|
|
(define sprite-position (vec2 156.0 176.0))
|
2024-05-17 23:49:31 +02:00
|
|
|
(define sprite-texture #f)
|
2024-05-19 21:54:30 +02:00
|
|
|
(define tile-texture #f)
|
2024-05-19 18:48:18 +02:00
|
|
|
(define tile-atlas #f)
|
2024-05-19 21:54:30 +02:00
|
|
|
(define hero-texture #f)
|
2024-05-19 18:48:18 +02:00
|
|
|
(define hero-atlas #f)
|
|
|
|
(define text-position (vec2 0.0 280.0))
|
|
|
|
(define tile-index 0)
|
|
|
|
(define tile-map #f)
|
2024-05-19 21:54:30 +02:00
|
|
|
(define level #f)
|
2024-05-17 23:49:31 +02:00
|
|
|
(define repl #f)
|
|
|
|
|
2024-05-19 21:54:30 +02:00
|
|
|
(define sprite-batch #f)
|
2024-05-19 18:48:18 +02:00
|
|
|
|
2024-05-17 23:49:31 +02:00
|
|
|
(define (load)
|
2024-05-19 21:54:30 +02:00
|
|
|
(set! tile-texture (load-image "assets/images/simples_pimples.png"
|
|
|
|
#:transparent-color black))
|
|
|
|
(set! tile-atlas (split-texture tile-texture 16 16))
|
|
|
|
(set! hero-texture (load-image "assets/images/lr_penguin2.png"))
|
|
|
|
(set! hero-atlas (split-texture hero-texture 32 32))
|
2024-05-19 18:48:18 +02:00
|
|
|
(set! tile-map (load-tile-map "assets/levels/level1.tmx"))
|
2024-05-19 21:54:30 +02:00
|
|
|
(set! sprite-batch (make-sprite-batch tile-texture #:capacity 1000))
|
|
|
|
(set! level (level-parse-file "assets/levels/level-1.map"))
|
2024-05-17 23:49:31 +02:00
|
|
|
(set! repl (spawn-coop-repl-server)))
|
|
|
|
|
2024-05-19 18:48:18 +02:00
|
|
|
(define (update dt)
|
|
|
|
(poll-coop-repl-server repl)
|
|
|
|
(set-vec2-y! sprite-position
|
|
|
|
(floor-remainder (+ (vec2-y sprite-position) (* 50.0 dt)) 400.0))
|
|
|
|
(set-vec2-x! text-position
|
2024-05-19 21:54:30 +02:00
|
|
|
(floor-remainder (+ (vec2-x text-position) (* 25.0 dt)) 640.0)))
|
2024-05-19 18:48:18 +02:00
|
|
|
|
|
|
|
(define (draw-tile i)
|
|
|
|
(let ((x (* 16 (remainder i 40)))
|
|
|
|
(y (* 16 (quotient i 40))))
|
|
|
|
(draw-sprite
|
|
|
|
(texture-atlas-ref tile-atlas (+ i 3000 ))
|
|
|
|
(vec2 x y)
|
2024-05-19 21:54:30 +02:00
|
|
|
#:scale (vec2 1.0 1.0))))
|
2024-05-19 18:48:18 +02:00
|
|
|
|
|
|
|
(define (draw-hero i)
|
|
|
|
(let ((x (* 64 (remainder i 8)))
|
|
|
|
(y (* 32 (quotient i 8)))
|
|
|
|
)
|
|
|
|
(draw-sprite
|
|
|
|
(texture-atlas-ref hero-atlas i)
|
|
|
|
(vec2 (+ x 16) y)
|
|
|
|
#:scale (vec2 1.0 1.0))
|
2024-05-19 21:54:30 +02:00
|
|
|
(draw-text (format #f "~A" i) (vec2 x y) #:color white))
|
2024-05-19 18:48:18 +02:00
|
|
|
)
|
|
|
|
|
2024-05-19 21:54:30 +02:00
|
|
|
(define (level-tile-index tile)
|
|
|
|
(case tile
|
|
|
|
((empty) 3800)
|
|
|
|
((brick) 3750)
|
|
|
|
((wall) 3709)
|
|
|
|
((ladder) 3350)
|
|
|
|
((goal) 3404)
|
|
|
|
(else 3326)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (draw-level level)
|
|
|
|
(sprite-batch-clear! sprite-batch)
|
|
|
|
(for-each
|
|
|
|
(lambda (row)
|
|
|
|
(for-each
|
|
|
|
(lambda (column)
|
|
|
|
(let*
|
|
|
|
((x (* 16 column))
|
|
|
|
(y (* 16 row))
|
|
|
|
(tile (level-tile-at level column row))
|
|
|
|
(texture-region (texture-atlas-ref tile-atlas (level-tile-index tile))))
|
|
|
|
(sprite-batch-add! sprite-batch (vec2 x y)
|
|
|
|
#:texture-region texture-region)))
|
|
|
|
(iota (level-width level) 0)))
|
|
|
|
|
|
|
|
(iota (level-height level) 0))
|
|
|
|
(draw-sprite-batch sprite-batch))
|
2024-05-17 23:49:31 +02:00
|
|
|
|
|
|
|
(define (draw _alpha)
|
2024-05-19 21:54:30 +02:00
|
|
|
(draw-level level)
|
2024-05-19 18:48:18 +02:00
|
|
|
(let ((hero-position (vec2+ text-position (vec2 -8.0 16.0)))
|
|
|
|
(hero-index (+ 56 (remainder (inexact->exact (floor (vec2-x text-position))) 8))))
|
|
|
|
(draw-sprite
|
|
|
|
(texture-atlas-ref hero-atlas hero-index)
|
|
|
|
hero-position))
|
|
|
|
|
2024-05-19 21:54:30 +02:00
|
|
|
;; (draw-tile-map tile-map)
|
2024-05-19 18:48:18 +02:00
|
|
|
|
|
|
|
;;(do ((i 0 (+ i 1)))
|
|
|
|
;; ((>= i 1000))
|
|
|
|
;; (draw-tile i))
|
2024-05-19 21:54:30 +02:00
|
|
|
;; (do ((i 0 (+ i 1)))
|
|
|
|
;; ((>= i 64))
|
|
|
|
;; (draw-hero i))
|
|
|
|
)
|
2024-05-17 23:49:31 +02:00
|
|
|
|
|
|
|
(define (launch-game args)
|
2024-05-19 18:48:18 +02:00
|
|
|
(run-game #:load (lambda () (load))
|
|
|
|
#:update (lambda (dt) (update dt))
|
|
|
|
#:draw (lambda (alpha) (draw alpha))))
|
2024-05-19 21:54:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
(test-begin "tile draw")
|
|
|
|
|
|
|
|
(test-equal "tile index"
|
|
|
|
(level-tile-index 'empty)
|
|
|
|
3800)
|
|
|
|
|
|
|
|
(test-equal (level-tile-index 'brick) 3750)
|
|
|
|
|
|
|
|
(test-end "tile draw")
|