hero movement works now.

This commit is contained in:
Peter Tillemans 2024-05-21 20:59:48 +02:00
parent e130cac03b
commit ad4a4d118b
3 changed files with 26 additions and 22 deletions

View file

@ -6,7 +6,7 @@ WWWWGWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
W H W
W O H O W
W H W
W O BBBBBBBBBBBBBBBBBBBBBBBBBBBB O W -
W O BBBBBBBBBBBBBBBBBBBBBBBBBBHB O W -
W H W
W H W
W H W

View file

@ -26,7 +26,7 @@
(bloat hero-bloat hero-with-bloat) ;; 0.0 to 2.0, how much the hero is inflated
)
(define default-state 'falling)
(define default-state 'fall)
(define default-bloat 1.0)
(define (hero-load level)
@ -56,8 +56,8 @@
(tile-below (level-tile-at level (vec2 (hero-x hero) (1- (hero-y hero))))))
(cond
((equal? (level-tile-at level position) 'ladder) 'climb)
((> (floor-remainder (hero-y hero) level-cell-size) 0) 'falling)
((equal? tile-below 'empty) 'falling)
((> (floor-remainder (hero-y hero) level-cell-size) 0) 'fall)
((equal? tile-below 'empty) 'fall)
(else (cond
((member 'left inputs) 'go-left)
((member 'right inputs) 'go-right)
@ -73,16 +73,16 @@
(define (next-position position state inputs distance)
(pretty-print (list position state inputs distance))
(case state
((falling) (vec2- position (vec2 0 distance)))
((climb) (vec2+ position (input->movement (car inputs) distance)))
((fall) (vec2- position (vec2 0 distance)))
((go-left) (vec2- position (vec2 distance 0)))
((go-right) (vec2+ position (vec2 distance 0)))
((stationary) position)
((ladder) (fold (lambda (input movement)
(vec2+ movement (input->movement input distance)))
position
inputs))
((climb) (fold (lambda (input movement)
(vec2+ movement (input->movement input distance)))
position
inputs))
(else position)
))
@ -91,9 +91,10 @@
(define (hero-update hero level inputs dt)
(let* ((state (next-state hero level inputs))
(distance (* dt speed))
(position (next-position (hero-position hero) state inputs distance))
(position (next-position (hero-position hero) (hero-state hero) inputs distance))
(tile (level-tile-at level position))
)
(pretty-print (list state position tile))
(hero-with-state
(if (member tile '(empty ladder other))
(hero-with-position hero position)
@ -124,7 +125,7 @@
(test-equal 16 (hero-x (hero-with-position hero (vec2 16.34 20.78))))
(test-equal 20 (hero-y (hero-with-position hero (vec2 16.34 20.78))))
(test-equal 'falling (next-state hero level '()))
(test-equal 'fall (next-state hero level '()))
(let ((hero (hero-with-position hero (vec2 16 16))))
(test-equal 'stationary (next-state hero level '()))
(test-equal 'go-left (next-state hero level '(left)))
@ -134,9 +135,9 @@
(let ((hero (hero-with-position hero (vec2 48 31))))
(test-equal 'climb (next-state hero level '())))
(let ((hero (hero-with-position hero (vec2 32 31))))
(test-equal 'falling (next-state hero level '())))
(test-equal 'fall (next-state hero level '())))
(test-equal (vec2 32.0 31.0) (next-position (vec2 32.0 32.0) 'falling '() 1.0))
(test-equal (vec2 32.0 31.0) (next-position (vec2 32.0 32.0) 'fall '() 1.0))
(test-equal (vec2 31.0 32.0) (next-position (vec2 32.0 32.0) 'go-left '() 1.0))
(test-equal (vec2 33.0 32.0) (next-position (vec2 32.0 32.0) 'go-right '() 1.0))
@ -155,7 +156,7 @@
(hero-update
(hero-with-state
(hero-with-position hero (vec2 32 16.3))
'falling)
'fall)
level
'()
0.017

View file

@ -19,7 +19,7 @@
;; start index of the walk animation
(define hero-walking-offset 56)
(define hero-falling-offset 32)
(define hero-climbing-offset 40)
(define hero-climbing-offset 48)
(define (hero-sprite-walking hero)
(let* ((x (hero-x hero))
@ -47,17 +47,20 @@
(case (hero-state hero)
((stationary) (draw-sprite
(hero-sprite-stationary hero)
(vec2+ (hero-position hero) (vec2 -9.0 1.0))))
((falling) (draw-sprite
(hero-sprite-falling hero)
(vec2+ (hero-position hero) (vec2 -9.0 1.0))))
(vec2+ (hero-position hero) (vec2 -16.0 1.0))))
((fall) (draw-sprite
(hero-sprite-falling hero)
(vec2+ (hero-position hero) (vec2 -16.0 1.0))))
((climb) (draw-sprite
(hero-sprite-climbing hero)
(vec2+ (hero-position hero) (vec2 -16.0 1.0))))
((go-left) (draw-sprite
(hero-sprite-walking hero)
(vec2+ (hero-position hero) (vec2 23.0 1.0))
(vec2+ (hero-position hero) (vec2 16.0 1.0))
#:scale (vec2 -1.0 1.0)
))
((go-right) (draw-sprite
(hero-sprite-walking hero)
(vec2+ (hero-position hero) (vec2 -9.0 1.0))))
(vec2+ (hero-position hero) (vec2 -16.0 1.0))))
)
)