summaryrefslogtreecommitdiff
path: root/heyllama.scm
blob: 9b08c459dfb44ce33a4492bbb99bec14ce316d45 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(use-modules (chickadee)
             (chickadee math vector)
             (chickadee render font)
             (chickadee render sprite)
             (chickadee render texture)
             (oop goops))

(define-class <animal> (<object>)
  (pos #:init-keyword #:initial-pos
       #:getter get-pos)

  (vel #:init-value #v(0.0 0.0)
       #:getter get-vel
       #:setter set-vel!)

  (walk-direction
   #:init-value 0
   #:setter set-walk-direction!
   #:getter get-walk-direction)

  (face-direction
   #:init-keyword #:initial-face-dir
   #:setter set-face-direction!
   #:getter get-face-direction)

  (animation-step #:init-value 0
                  #:getter get-animation-step
                  #:setter set-animation-step!)

  (sprite #:init-keyword #:sprite
          #:getter get-sprite
          #:setter set-sprite!))

(define (face-row dir)
  (if (> 0 dir)
      4
      12))

(define (draw-animal animal)
  (draw-sprite (texture-atlas-ref (get-sprite animal)
                                  (+ (face-row (get-face-direction animal))
                                     (truncate (/ (get-animation-step animal) 4))))
               (get-pos animal)))


(define (update-animal animal tstep)
  (let ((wd (get-walk-direction animal)))
    (unless (eq? wd 0)
      (set-face-direction! animal wd)
      (vec2-add! (get-pos animal) #v((* tstep wd 0.2) 0))
      (set-animation-step! animal
                           (floor-remainder
                            (+ (get-animation-step animal) wd)
                            16))))
  (vec2-add! (get-pos animal) (get-vel animal))
  (if (> (vec2-y (get-pos animal)) 100.0)
      (set-vel! animal
                (vec2+ (get-vel animal)
                       #v(0.0 -1.0)))
      (set-vel! animal #v(0.0 0.0))))



;; -------------- Initial game state --------------

(define llama
  (make <animal>
    #:initial-pos #v(100.0 100.0)
    #:initial-face-dir 1))


;; ------------------------------------------------

(define (load)
  (set-sprite! llama
               (split-texture (load-image "llama_walk.png")
                              128
                              128)))


(define (draw alpha)
  (draw-animal llama))


(define (update tstep)
  (update-animal llama tstep))


(define (key-press key scancode modifier repeat?)
  (case key
    ((q) (abort-game))
    ((right) (set-walk-direction! llama 1))
    ((left) (set-walk-direction! llama -1))))
    ;;((space) (accelerate llama #v(0.0 10.0)))))


(define (key-release key scancode modifier)
  (case key
    ((right) (set-walk-direction! llama 0))
    ((left) (set-walk-direction! llama 0))
    ((space) (set-vel! llama #v(0.0 20.0)))))


(run-game #:window-title "Hey Llama!"
          #:load load
          #:draw draw
          #:key-press key-press
          #:update-hz 60
          #:key-release key-release
          #:update update)