summaryrefslogtreecommitdiff
path: root/lisparuga/enemy.scm
blob: 06533eada2d989b6e8f830d5d7cb70f6210c6af8 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
;;; Lisparuga
;;; Copyright © 2020 David Thompson <dthompson2@worcester.edu>
;;;
;;; Lisparuga 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.
;;;
;;; Lisparuga 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 Lisparuga.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Enemy actors.
;;
;;; Code:

(define-module (lisparuga enemy)
  #:use-module (chickadee audio)
  #:use-module (chickadee math)
  #:use-module (chickadee math rect)
  #:use-module (chickadee math vector)
  #:use-module (chickadee scripting)
  #:use-module (chickadee render texture)
  #:use-module (lisparuga actor)
  #:use-module (lisparuga asset)
  #:use-module (lisparuga bullets)
  #:use-module (lisparuga config)
  #:use-module (lisparuga node)
  #:use-module (lisparuga node-2d)
  #:use-module (oop goops)
  #:export (<enemy>
            health
            points
            parting-shots
            dead?
            fire-parting-shots-maybe

            make-utatsugumi))


;;;
;;; Base Enemy
;;;

(define-asset explosion-sound (load-audio (scope-asset "sounds/explosion.wav")))
(define-asset hit-sound (load-audio (scope-asset "sounds/hit.wav")))

(define-class <enemy> (<actor>)
  (health #:accessor health #:init-keyword #:health)
  (points #:getter points #:init-keyword #:points)
  (parting-shots #:getter parting-shots #:init-keyword #:parting-shots)
  (fire-parting-shots? #:accessor fire-parting-shots? #:init-form #f))

(define-method (damage (enemy <enemy>) x)
  (let ((new-health (max (- (health enemy) x) 0)))
    (set! (health enemy) new-health)
    (if (zero? new-health)
        (audio-play (asset-ref explosion-sound)
                    #:volume 0.5)
        (audio-play (asset-ref hit-sound)
                    #:volume 0.5))))

(define-method (dead? (enemy <enemy>))
  (zero? (health enemy)))

(define (fire-parting-shots-maybe enemy player)
  (when (fire-parting-shots? enemy)
    (let* ((n (parting-shots enemy))
           (ep (position enemy))
           (pp (position player))
           (angle-to-player
            (atan (- (vec2-y pp) (vec2-y ep))
                  (- (vec2-x pp) (vec2-x ep)))))
      (let loop ((i 0))
        (when (< i n)
          (let ((theta (+ angle-to-player
                          (- (* (random:uniform) (/ pi 4.0))
                             (/ pi 8.0)))))
            (spawn-bullet (bullet-field enemy)
                          small-dot
                          (polarity enemy)
                          (+ (vec2-x ep)
                             (- (* (random:uniform) 16.0)
                                8.0))
                          (+ (vec2-y ep)
                             (- (* (random:uniform) 16.0)
                                8.0))
                          (* (cos theta) 4.0)
                          (* (sin theta) 4.0)))
          (loop (+ i 1)))))))

(define-method (on-collision (enemy <enemy>) bullet bullet-polarity hitbox)
  ;; TODO: Distinguish between normal play bullets and homing shots
  ;; that do more damage.
  ;;
  ;; Same polarity = 1 point of damage
  ;; Opposite polarity = 2 points of damage
  (let ((same-polarity? (eq? bullet-polarity (polarity enemy))))
    (damage enemy (if same-polarity? 1 2))
    (when (and same-polarity? (dead? enemy))
      (set! (fire-parting-shots? enemy) #t)))
  #t)

(define %enemy-tiles
  ;; 0: Utatsugumi - white
  `((0.0 0.0 24.0 24.0)
    ;; 1: Utatsugumi - black
    (24.0 0.0 24.0 24.0)))

(define (load-enemy-atlas file-name)
  (let ((texture (load-image file-name)))
    (list->texture-atlas texture %enemy-tiles)))

(define-asset enemy-atlas
  (load-enemy-atlas (scope-asset "images/enemies.png")))


;;;
;;; Utatsugumi
;;;

(define-class <utatsugumi> (<enemy>))

(define-method (on-boot (utatsugumi <utatsugumi>))
  (attach-to utatsugumi
             (make <atlas-sprite>
               #:atlas enemy-atlas
               #:index (if (eq? 'white (polarity utatsugumi)) 0 1)
               #:origin (vec2 12.0 12.0))))

(define (make-utatsugumi polarity x y)
  (make <utatsugumi>
    #:name (gensym "utatsugumi-")
    #:health 1
    #:points 20
    #:parting-shots 5
    #:polarity polarity
    #:hitboxes
    (list (make-hitbox 'utatsugumi (make-rect -10.0 -10.0 20.0 20.0)))
    #:position (vec2 x y)))