~octaspire/crates2

ref: 7254013ef0cc4ff6e2fc7ed9a7f57ef7be1e185d crates2/src/moving.lisp -rw-r--r-- 2.9 KiB
7254013eoctaspire Continue implementing 11 months ago
                                                                                
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
;; Octaspire Crates 2 - Puzzle Game
;; Copyright 2020 octaspire.com
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
(in-package :crates2)

;; Generic functions

(defgeneric movingp (self)
  (:documentation "Predicate telling whether moving SELF is in motion"))

(defgeneric stationaryp (self)
  (:documentation "Predicate telling whether moving SELF is not moving"))

(defgeneric escape (self)
  (:documentation "Handle crate SELF flying out of the level"))

;; Methods

(defmethod movingp ((self moving))
  (not (stationaryp self)))

(defmethod stationaryp ((self moving))
  (eq (velocity self) :zero))

(defmethod update ((self moving))
  (when (active self)
    (let ((v (velocity self)))
      (ecase v
        (:east  (east self))
        (:west  (west self))
        (:north (north self))
        (:south (south self))
        (:zero))))
  (call-next-method))

(defmethod west ((self moving))
  (let* ((x (- (crate-x self) 1))
         (crate (find-at x (crate-y self) (crate-z self))))
    (if (< x 0)
        (escape self)
        (if crate
            (handle-collision self crate)
            (setf (crate-x self) x)))))

(defmethod east ((self moving))
  (let* ((x (+ (crate-x self) 1))
         (crate (find-at x (crate-y self) (crate-z self))))
    (if (>= x *level-width*)
        (escape self)
        (if crate
            (handle-collision self crate)
            (setf (crate-x self) x)))))

(defmethod north ((self moving))
  (let* ((y (- (crate-y self) 1))
         (crate (find-at (crate-x self) y (crate-z self))))
    (if (< y 0)
        (escape self)
        (if crate
            (handle-collision self crate)
            (setf (crate-y self) y)))))

(defmethod south ((self moving))
  (let* ((y (+ (crate-y self) 1))
         (crate (find-at (crate-x self) y (crate-z self))))
    (if (>= y *level-height*)
        (escape self)
        (if crate
            (handle-collision self crate)
            (setf  (crate-y self) y)))))

(defmethod escape ((self moving))
  (setf (active self) nil)
  (when (typep self 'player)
    (setf (lamented self) t)
    (request-restart-level)))

(defmethod collide ((self moving) (target crate))
  (setf (velocity self) :zero))

(defmethod collide ((self moving) (target vacuum))
  (setf (crate-x target) (crate-x self))
  (setf (crate-y target) (crate-y self))
  (setf (crate-z target) (crate-z self)))

;; Functions

(defun handle-collision (c1 c2)
  (collide c1 c2)
  (collide c2 c1))