summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRicardo Wurmus <rekado@elephly.net>2018-07-22 14:05:59 +0200
committerRicardo Wurmus <rekado@elephly.net>2018-07-27 17:15:47 +0200
commit25061aee05d41254735b46298693326ca4d46a44 (patch)
treee8444f32544ef0bbf989af5766dacb9d02d32075
parent6f04ad781d5588b5f56490275b4d6ae0ad3a8a8b (diff)
Move "walk" procedure to characters module.
-rw-r--r--characters.scm33
-rw-r--r--scenes/game.scm26
2 files changed, 32 insertions, 27 deletions
diff --git a/characters.scm b/characters.scm
index c3f692b..03789f2 100644
--- a/characters.scm
+++ b/characters.scm
@@ -27,6 +27,7 @@
#:use-module (srfi srfi-26)
#:use-module (ice-9 match)
#:use-module (engine assets)
+ #:use-module (engine node)
#:use-module (engine node-2d)
#:use-module (oop goops)
#:export (<character>
@@ -38,7 +39,9 @@
direction
hitbox
- load-atlas))
+ load-atlas
+
+ walk))
(define-class <character> (<node-2d>)
(conversations #:accessor conversations #:init-keyword #:conversations)
@@ -51,3 +54,31 @@
(define (load-atlas file-name tile-width tile-height)
(split-texture (load-image file-name) tile-width tile-height))
+
+(define-method (walk (character <character>) directions . rest)
+ (let ((stop? (member 'stop rest)))
+ (unless (and (equal? (direction character) directions)
+ (not stop?))
+ (let ((sprite (child-ref character 'sprite))
+ (speed (if stop? 0.0 (walk-speed character))))
+ (change-animation sprite (last directions))
+ (for-each (lambda (dir)
+ (case dir
+ ((right)
+ (set-vec2-x! (velocity character) speed))
+ ((left)
+ (set-vec2-x! (velocity character) (* -1.0 speed)))
+ ((up)
+ (set-vec2-y! (velocity character) speed))
+ ((down)
+ (set-vec2-y! (velocity character) (* -1.0 speed)))
+ ((idle)
+ (set-vec2-x! (velocity character) 0.0)
+ (set-vec2-y! (velocity character) 0.0)
+ (change-animation sprite (case (last (direction character))
+ ((right) 'idle-right)
+ ((left) 'idle-left)
+ ((up) 'idle-back)
+ ((down) 'idle-front))))))
+ directions)
+ (set! (direction character) directions)))))
diff --git a/scenes/game.scm b/scenes/game.scm
index ea3da09..d1500ff 100644
--- a/scenes/game.scm
+++ b/scenes/game.scm
@@ -48,32 +48,6 @@
(define-class <stats> (<node-2d>)
(object #:accessor object #:init-form #f #:init-keyword #:object))
-(define-method (walk (character <character>) directions . rest)
- (unless (equal? (direction character) directions)
- (let ((sprite (child-ref character 'sprite))
- (speed (if (member 'stop rest) 0.0 (walk-speed character))))
- (change-animation sprite (last directions))
- (for-each (lambda (dir)
- (case dir
- ((right)
- (set-vec2-x! (velocity character) speed))
- ((left)
- (set-vec2-x! (velocity character) (* -1.0 speed)))
- ((up)
- (set-vec2-y! (velocity character) speed))
- ((down)
- (set-vec2-y! (velocity character) (* -1.0 speed)))
- ((idle)
- (set-vec2-x! (velocity character) 0.0)
- (set-vec2-y! (velocity character) 0.0)
- (change-animation sprite (case (last (direction character))
- ((right) 'idle-right)
- ((left) 'idle-left)
- ((up) 'idle-back)
- ((down) 'idle-front))))))
- directions)
- (set! (direction character) directions))))
-
(define-asset test-map (load-tile-map "assets/maps/01.tmx"))