Kaynağa Gözat

Add width and height based bound checking

Lily Carpenter 9 yıl önce
ebeveyn
işleme
3b2f3e3d46
1 değiştirilmiş dosya ile 17 ekleme ve 7 silme
  1. 17 7
      crypts-and-corpses.lisp

+ 17 - 7
crypts-and-corpses.lisp

@@ -3,12 +3,14 @@
3 3
 (in-package #:crypts-and-corpses)
4 4
 
5 5
 (defstruct game-map
6
-  (width 100 :type fixnum :read-only t)
7
-  (height 40 :type fixnum :read-only t)
6
+  (width 250 :type fixnum :read-only t)
7
+  (height 250 :type fixnum :read-only t)
8 8
   (mobiles (make-hash-table) :type hash-table :read-only t))
9 9
 
10 10
 (defclass entity ()
11
-  ((location :initarg :location :initform 0 :type fixnum)))
11
+  ((width :initarg :width :type fixnum :initform 1)
12
+   (height :initarg :height :type fixnum :initform 1)
13
+   (location :initarg :location :initform 0 :type fixnum)))
12 14
 
13 15
 (defclass tile-entity (entity)
14 16
   ((codepoint :initarg :codepoint :initform #x3f :type fixnum)))
@@ -57,11 +59,18 @@
57 59
 (defgeneric move-up (object map))
58 60
 (defgeneric move-down (object map))
59 61
 
60
-(defgeneric update-location (object map old-locaiton new-location))
62
+(defgeneric update-location (object map old-location new-location))
63
+
64
+(defun overrun-p (old-location new-location map)
65
+  (multiple-value-bind (old-x old-y) (calc-coord old-location map)
66
+    (multiple-value-bind (new-x new-y) (calc-coord new-location map)
67
+      (and (< old-y new-y)
68
+           (> old-x new-x)))))
61 69
 
62 70
 (defmethod update-location ((object entity) map old-location new-location)
63
-  (declare (ignore old-location))
64
-  (unless (out-of-bounds-p new-location map)
71
+  (unless (or (out-of-bounds-p new-location map)
72
+              (overrun-p old-location (+ new-location (slot-value object 'width)) map)
73
+              (out-of-bounds-p (+ new-location (* (slot-value object 'height) (game-map-width map))) map))
65 74
     (setf (slot-value object 'location) new-location)))
66 75
 
67 76
 (defmethod update-location ((object mobile) map old-location new-location)
@@ -125,7 +134,8 @@
125 134
     ((= key bearlibterminal-ffi:+tk-w+) (move-up viewport map))
126 135
     ((= key bearlibterminal-ffi:+tk-a+) (move-left viewport map))
127 136
     ((= key bearlibterminal-ffi:+tk-d+) (move-right viewport map))
128
-    ((= key bearlibterminal-ffi:+tk-s+) (move-down viewport map))))
137
+    ((= key bearlibterminal-ffi:+tk-s+) (move-down viewport map))
138
+    ((= key bearlibterminal-ffi:+tk-space+) (update-location viewport map (slot-value viewport 'location) (slot-value player 'location)))))
129 139
 
130 140
 (defun create-viewport ()
131 141
   (multiple-value-bind (width height) (get-window-size)