~octaspire/crates2

ref: 90662a000cf64099caf0beaeacd2fed962f68b2e crates2/src/crates.lisp -rw-r--r-- 3.3 KiB
90662a00octaspire Fix Makefile, implement initial movement 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
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
;; 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)

;; Functions

(defun find-at (x y z)
  "Get crate at (X,Y,Z) or NIL if location is empty."
  (find-if #'(lambda (crate)
               (if (and (= (x crate) x)
                        (= (y crate) y)
                        (= (z crate) z))
                   crate
                   nil))
           *level*))

;; Classes

(defclass crate ()
  ((x :initarg :x
      :accessor x)
   (y :initarg :y
      :accessor y)
   (z :initarg :z
      :accessor z)))

(defclass wall (crate)
  ())

(defclass moving (crate)
  ((v :initarg :v
      :accessor v)))

(defclass player (moving)
  ())

;; Generic functions

(defgeneric update (obj)
  (:documentation "Update a crate"))

(defgeneric east (obj)
  (:documentation "Move crate east"))

(defgeneric west (obj)
  (:documentation "Move crate west"))

(defgeneric north (obj)
  (:documentation "Move crate north"))

(defgeneric south (obj)
  (:documentation "Move crate south"))

(defgeneric visual (obj)
  (:documentation "Get visual representation for a crate"))

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

(defgeneric collide (target crate)
  (:documentation "Handle CRATE colliding into TARGET"))

;; Methods

(defmethod update ((obj crate))
  (format t "update crate "))

(defmethod update ((obj wall))
  (format t "update wall ")
  (call-next-method))

(defmethod update ((obj moving))
  (format t "update moving ")
  (let ((v (v obj)))
    (ecase v (:east  (east obj))
             (:west  (west obj))
             (:north (north obj))
             (:south (south obj))))
  (call-next-method))

(defmethod update ((obj player))
  (format t "update player ")
  (call-next-method))

(defmethod visual ((obj wall))
  #\x)

(defmethod visual ((obj player))
  #\o)

(defmethod west ((obj moving))
  (format t "west~%")
  (let* ((x (- (x obj) 1))
         (crate (find-at x (y obj) (z obj))))
    (if (< x 0)
        (escape obj)
        (if crate
            (collide obj crate)
            (setf (x obj) x)))))

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

(defmethod north ((obj moving))
  (let* ((y (+ (y obj) 1))
         (crate (find-at (x obj) y (z obj))))
    (if (>= y *level-height*)
        (escape obj)
        (if crate
            (collide obj crate)
            (setf (y obj) y)))))

(defmethod south ((obj moving))
  (let* ((y (- (y obj) 1))
         (crate (find-at (x obj) y (z obj))))
    (if (< y 0)
        (escape obj)
        (if crate
            (collide obj crate)
            (setf (y obj) y)))))

(defmethod escape ((crate player))
  (setf *running* nil))