summaryrefslogtreecommitdiff
path: root/lisparuga/bullets.scm
blob: c16df8b1a02a30496fc84794ba62b6e6f766c743 (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
;;; Lisparuga
;;; Copyright © 2016 David Thompson <davet@gnu.org>
;;;
;;; 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/>.

(define-module (lisparuga bullets)
  #:use-module (sly records)
  #:use-module (sly math rect)
  #:use-module (sly math vector)
  #:use-module (lisparuga utils)
  #:export (%bullet-bounds
            make-bullet
            bullet?
            bullet-type
            bullet-polarity
            bullet-live?
            bullet-position
            bullet-direction
            bullet-hitbox
            kill-bullet
            move-bullet
            move-bullet-to
            bullet-in-bounds?
            forward
            bullet-world-hitbox))

(define %bullet-bounds (rect-inflate bounds 32 32))

(define-record-type* <bullet>
  %make-bullet make-bullet
  bullet?
  (type bullet-type 'generic)
  (polarity bullet-polarity 'light)
  (live? bullet-live? #t)
  (position bullet-position origin2)
  (direction bullet-direction 0)
  (hitbox bullet-hitbox (make-rect -1 -1 1 1)))

(define (kill-bullet bullet)
  (make-bullet #:inherit bullet #:live? #f))

(define (move-bullet bullet offset)
  (make-bullet #:inherit bullet
               #:position (v+ (bullet-position bullet) offset)))

(define (move-bullet-to bullet position)
  (make-bullet #:inherit bullet #:position position))

(define (bullet-in-bounds? bullet)
  (rect-contains? %bullet-bounds (bullet-position bullet)))

(define (forward speed)
  (lambda (world effects bullet)
    (values #f
            effects
            (move-bullet bullet
                         (polar2 speed (bullet-direction bullet))))))

(define (bullet-world-hitbox bullet)
  (rect-move (bullet-hitbox bullet) (bullet-position bullet)))