class.scm 1.87 KB
Newer Older
1 2 3
(define-module (class)
  :use-module (oop goops)
  :use-module (util)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
4
  :export (vector-length v3 make-player make-ray))
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

(define-class <v3> ()
  (x #:accessor x #:init-keyword #:x)
  (y #:accessor y #:init-keyword #:y)
  (z #:accessor z #:init-keyword #:z))
(export x y z)

(define* (v3 #:optional (x 0) (y 0) (z 0))
  (make <v3> #:x x #:y y #:z z))

(define-method (+ (v <v3>) (u <v3>))
  (v3 (+ (x v) (x u))
      (+ (y v) (y u))
      (+ (z v) (z u))))

(define-method (* (a <number>) (v <v3>))
  (v3 (* a (x v))
      (* a (y v))
      (* a (z v))))

(define (vector-length v)
  (sqrt (+ (* (x v) (x v))
           (* (y v) (y v))
           (* (z v) (z v)))))

(define-class <player> ()
Hugo Hörnquist's avatar
Hugo Hörnquist committed
31 32 33 34
  (pos #:accessor p
       #:init-form (v3 1 1)
       #:init-keyword #:p)
  (angle #:accessor a #:init-value 0 #:init-keyword #:a)
35
  (feild-of-view #:accessor fov #:init-value (/ tau 8)))
36 37 38
(export p a fov)

(define (make-player x y a)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
39
   (make <player> #:p (v3 x y) #:a a))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
40 41 42


(define-class <ray> ()
43
  (angle #:accessor a #:init-keyword #:a)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
44 45 46
  (length #:accessor length #:init-keyword #:length #:init-value #f)
  (vec #:accessor vec #:init-keyword #:v)
  (pos-on-wall #:accessor pos-on-wall)
47
  (hit-point #:getter hitf #:init-keyword #:hitf)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
48 49
  (hit-float-point #:getter hitf #:init-keyword #:hitf)
  (tile-type #:getter type
50 51
             #:initial-value #f
             #:init-keyword #:type)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
52 53
  )

54
(export a length vec pos-on-wall hitf type)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
55

Hugo Hörnquist's avatar
Hugo Hörnquist committed
56 57 58 59 60
(define-method (initialize (self <ray>) args)
  (next-method)
  (unless (length self)
    (set! (length self) (vector-length (vec self))))

Hugo Hörnquist's avatar
Hugo Hörnquist committed
61 62 63 64 65 66 67 68
  (let* ((xx (x (hitf self)))
         (yy (y (hitf self))))
    (set! (pos-on-wall self)
          (min
           (max (abs (decimals xx))
                (abs (decimals yy)))
           (max (- 1 (abs (decimals xx)))
                (- 1 (abs (decimals yy)))))
69

Hugo Hörnquist's avatar
Hugo Hörnquist committed
70
          ))
71 72

  )
Hugo Hörnquist's avatar
Hugo Hörnquist committed
73 74 75

(define (make-ray . args)
  (apply make <ray> args))