Player avatar; fix scroll-offset; explicit meta-level for sprites

This commit is contained in:
Tony Garnock-Jones 2015-10-23 21:58:55 -04:00
parent e28841f695
commit 69ba8d7a01
1 changed files with 92 additions and 47 deletions

View File

@ -1,6 +1,8 @@
#lang racket/base #lang racket/base
(require 2htdp/image) (require 2htdp/image)
(require 2htdp/planetcute)
(require racket/set) (require racket/set)
(require racket/match) (require racket/match)
(require plot/utils) ;; for vector utilities (require plot/utils) ;; for vector utilities
@ -102,7 +104,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ## Level Layer Protocols ;; ## Level Layer Protocols
;;
;;------------------------------------------------------------------------- ;;-------------------------------------------------------------------------
;; ### Movement and Physics ;; ### Movement and Physics
;; - message: JumpRequest ;; - message: JumpRequest
@ -135,7 +137,7 @@
;; started. May issue JumpRequests at any time. Represents both the player, ;; started. May issue JumpRequests at any time. Represents both the player,
;; enemies, the goal(s), and platforms and blocks in the environment. ;; enemies, the goal(s), and platforms and blocks in the environment.
;; Asserts a Sprite two layers out to render itself. ;; Asserts a Sprite two layers out to render itself.
;;
;;------------------------------------------------------------------------- ;;-------------------------------------------------------------------------
;; ### Player State ;; ### Player State
;; - message: Damage ;; - message: Damage
@ -145,6 +147,14 @@
;; Responds to Damage. ;; Responds to Damage.
;; When hitpoints drop low enough, removes the player from the board. ;; When hitpoints drop low enough, removes the player from the board.
;; ;;
;; A Damage is a (damage ID Number), a message indicating an event that should
;; consume the given number of health points of the named gamepiece.
(struct damage (id hit-points) #:transparent)
;;
;; A Health is a (health ID Number), an assertion describing the current hitpoints
;; of the named gamepiece.
(struct health (id hit-points) #:transparent)
;;------------------------------------------------------------------------- ;;-------------------------------------------------------------------------
;; ### World State ;; ### World State
;; - assertion: LevelSize ;; - assertion: LevelSize
@ -185,15 +195,10 @@
;; not only the *existence* but also the initial position (in World coordinates) ;; not only the *existence* but also the initial position (in World coordinates)
;; of the named gamepiece. ;; of the named gamepiece.
;; ;;
;; A Damage is a (damage ID Number), a message indicating an event that should
;; consume the given number of health points of the named gamepiece.
;;
;; A Health is a (health ID Number), an assertion describing the current hitpoints
;; of the named gamepiece.
;;
;; A LevelSize is a (level-size Vec), an assertion describing the right-hand and ;; A LevelSize is a (level-size Vec), an assertion describing the right-hand and
;; bottom edges of the level canvas (in World coordinates). ;; bottom edges of the level canvas (in World coordinates).
;;
;; ----------- ;; -----------
;; Interaction Diagrams (to be refactored into the description later) ;; Interaction Diagrams (to be refactored into the description later)
;; ;;
@ -271,24 +276,22 @@
(match-define (scroll-offset vec) o) (match-define (scroll-offset vec) o)
(struct-copy scene-manager-state s [offset vec]))) (struct-copy scene-manager-state s [offset vec])))
(parameterize ((2d-world-meta-level 1)) (spawn (lambda (e s)
(spawn (lambda (e s) (match e
(parameterize ((2d-world-meta-level 1)) [(? patch? p)
(match e (let* ((s (update-window-size s p))
[(? patch? p) (s (update-scroll-offset s p)))
(let* ((s (update-window-size s p)) (match-define (vector width height) (scene-manager-state-size s))
(s (update-scroll-offset s p))) (match-define (vector ofs-x ofs-y) (scene-manager-state-offset s))
(match-define (vector width height) (scene-manager-state-size s)) (transition s
(match-define (vector ofs-x ofs-y) (scene-manager-state-offset s)) (update-scene `((push-matrix
(transition s (scale ,width ,height)
(update-scene `((push-matrix (texture ,(rectangle 1 1 "solid" "white")))
(scale ,width ,height) (translate ,(- ofs-x) ,(- ofs-y)))
(texture ,(rectangle 1 1 "solid" "white"))) `())))]
(translate ,ofs-x ,ofs-y)) [_ #f]))
`())))] (scene-manager-state (vector 0 0) (vector 0 0))
[_ #f]))) (sub (window ? ?) #:meta-level 1)))
(scene-manager-state (vector 0 0) (vector 0 0))
(sub (window ? ?) #:meta-level 1))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ScoreKeeper ;; ScoreKeeper
@ -304,45 +307,87 @@
0 0
(sub (add-to-score ?)))) (sub (add-to-score ?))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Player
(define player-id 'player)
(define (spawn-player-avatar)
(struct player-state (x y hit-points) #:prefab)
(define initial-player-state (player-state 50 50 1))
(define icon character-cat-girl)
(define icon-width (/ (image-width icon) 2))
(define icon-height (/ (image-height icon) 2))
(define (sprite-update s)
(update-sprites #:meta-level game-level
(simple-sprite 0
(- (player-state-x s) (/ icon-width 2))
(- (player-state-y s) (* 13/16 icon-height))
icon-width
icon-height
icon)))
(spawn (lambda (e s)
(match-define (player-state x y hit-points) s)
(match e
[(message (damage _ amount))
(define new-hit-points (- hit-points amount))
(if (positive? new-hit-points)
(transition (struct-copy player-state s
[hit-points (- hit-points amount)])
'())
(quit))]
[_ #f]))
initial-player-state
(sub (damage player-id ?))
(assert (health player-id (player-state-hit-points initial-player-state)))
(assert (level-running) #:meta-level 1)
(sprite-update initial-player-state)
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; LevelSpawner ;; LevelSpawner
(define (spawn-level level-number) (define (spawn-level . actions)
(spawn-world (spawn (lambda (e s) (spawn-world
(and (not s) (spawn-player-avatar)
(transition #t (assert (level-running) #:meta-level 1)))) actions))
#f)
(spawn (lambda (e s) #f) (define (spawn-numbered-level level-number)
(void) (match level-number
(update-sprites (simple-sprite 0 50 50 50 50 (circle 50 "solid" "purple")))))) [0 (spawn-level (spawn (lambda (e s) #f)
(void)
(update-sprites #:meta-level game-level
(simple-sprite 0 50 50 50 50
(rectangle 50 50 "solid" "purple")))))]))
(define (spawn-level-spawner) (define (spawn-level-spawner)
(struct level-spawner-state (current-level level-complete?) #:prefab) (struct level-spawner-state (current-level level-complete?) #:prefab)
(list (spawn-level 0) (list (spawn (lambda (e s)
(spawn (lambda (e s)
(match-define (level-spawner-state current-level level-complete?) s) (match-define (level-spawner-state current-level level-complete?) s)
(match e (match e
[(? patch/removed?) [(? patch/removed?)
(define next-level (if level-complete? (+ current-level 1) current-level)) (define next-level (if level-complete? (+ current-level 1) current-level))
(transition (level-spawner-state next-level #f) (transition (level-spawner-state next-level #f)
(spawn-level next-level))] (spawn-numbered-level next-level))]
[(message (level-completed)) [(message (level-completed))
(transition (struct-copy level-spawner-state s [level-complete? #t]) '())] (transition (struct-copy level-spawner-state s [level-complete? #t]) '())]
[_ #f])) [_ #f]))
(level-spawner-state 0 #f) (level-spawner-state 0 #f)
(sub (level-running)) (sub (level-running))
(sub (level-completed))))) (sub (level-completed)))
(spawn-numbered-level 0)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(2d-world-meta-level 3) ;; TODO ack bleargh. See comment in prospect-gl/2d.rkt (define game-level 3) ;; used to specify meta-level to reach external I/O
(2d-world #:width 600 #:height 400 (2d-world #:width 600 #:height 400
(parameterize ((2d-world-meta-level 1)) ;; TODO ick yeughhh (spawn-keyboard-integrator)
(spawn-keyboard-integrator)) (spawn-scene-manager)
(spawn-scene-manager) (spawn-world (spawn-score-keeper)
(spawn-world (spawn-score-keeper) (spawn-level-spawner)
(spawn-level-spawner) )
) )
)