summaryrefslogtreecommitdiff
path: root/characters.scm
blob: e4f3e537bf03dc29cfc7fae94e455f460c4891b1 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
;;; The Inevitable Game
;;; Copyright © 2018, 2021 Ricardo Wurmus <rekado@elephly.net>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (characters)
  #:use-module (chickadee math rect)
  #:use-module (chickadee math vector)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee graphics font)
  #:use-module (chickadee graphics texture)
  #:use-module (chickadee graphics tiled)
  #:use-module (srfi srfi-1)
  #: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>
            conversations
            accepted-messages
            speaking?
            velocity
            walk-speed
            direction
            hitbox

            load-atlas

            walk))

(define-class <character> (<node-2d>)
  (conversations #:accessor conversations #:init-keyword #:conversations)
  (accepted-messages #:accessor accepted-messages #:init-form '((hello "Hello there!")))
  (speaking? #:accessor speaking? #:init-form #f)
  (velocity #:getter velocity #:init-form (vec2 0.0 0.0))
  (walk-speed #:accessor walk-speed #:init-form 0.8)
  (direction #:accessor direction #:init-form '(idle))
  (hitbox #:getter hitbox #:init-form (make-rect 8.0 0.0 16.0 16.0)))

(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)))))