Lily Carpenter hace 9 años
padre
commit
dcb7625d10
Se han modificado 2 ficheros con 39 adiciones y 17 borrados
  1. 2 1
      NOTES.org
  2. 37 16
      crypts-and-corpses.lisp

+ 2 - 1
NOTES.org

@@ -18,6 +18,7 @@ CLOSED: [2016-01-01 Fri 22:52]
18 18
 *** TODO Figure out an interesting way to integrate "Darkness"
19 19
 *** TODO Figure out how to get messages in exact center.
20 20
 ** Medium priority
21
-*** TODO Create map -> graphics layer so that isn't manual. (consider generalizing this)
21
+*** DONE Create map -> graphics layer so that isn't manual. (consider generalizing this)
22
+CLOSED: [2016-01-02 Sat 01:32]
22 23
 *** TODO Create generalized input command dispatch. (consider generalizing this)
23 24
 *** TODO Create configuration system that can control display and keybindings.

+ 37 - 16
crypts-and-corpses.lisp

@@ -7,9 +7,16 @@
7 7
   (height 40 :type fixnum :read-only t)
8 8
   (mobiles (make-hash-table) :type hash-table :read-only t))
9 9
 
10
-(defclass tile-entity ()
11
-  ((codepoint :initarg :codepoint :initform #x3f :type fixnum)
12
-   (location :initarg :location :initform 0 :type fixnum)))
10
+(defclass entity ()
11
+  ((location :initarg :location :initform 0 :type fixnum)))
12
+
13
+(defclass tile-entity (entity)
14
+  ((codepoint :initarg :codepoint :initform #x3f :type fixnum)))
15
+
16
+(defclass viewport (entity)
17
+  ((width :initarg :width :type fixnum :initform 100)
18
+   (height :initarg :height :type fixnum :initform 40)
19
+   (location :initarg :location :type fixnum :initform 0)))
13 20
 
14 21
 (defclass mobile (tile-entity)
15 22
   ((health :initarg :health :type fixnum)
@@ -50,7 +57,14 @@
50 57
 (defgeneric move-up (object map))
51 58
 (defgeneric move-down (object map))
52 59
 
53
-(defun update-location (object map old-location new-location)
60
+(defgeneric update-location (object map old-locaiton new-location))
61
+
62
+(defmethod update-location ((object entity) map old-location new-location)
63
+  (declare (ignore old-location))
64
+  (unless (out-of-bounds-p new-location map)
65
+    (setf (slot-value object 'location) new-location)))
66
+
67
+(defmethod update-location ((object mobile) map old-location new-location)
54 68
   (unless (out-of-bounds-p new-location map)
55 69
     (setf (slot-value object 'location) new-location)
56 70
     (setf (gethash new-location (game-map-mobiles map)) object)
@@ -58,30 +72,28 @@
58 72
 
59 73
 (defun out-of-bounds-p (new-location map)
60 74
   (multiple-value-bind (x y) (calc-coord new-location map)
61
-    (format t "~S~%" x)
62
-    (format t "~S~%" y)
63 75
     (or (> 0 x)
64 76
         (> 0 y)
65 77
         (>= x (game-map-width map))
66 78
         (>= y (game-map-height map)))))
67 79
 
68
-(defmethod move-left ((object mobile) map)
80
+(defmethod move-left ((object entity) map)
69 81
   (let ((old-location (slot-value object 'location))
70 82
         (new-location (1- (slot-value object 'location))))
71 83
     (unless (= 0 (calc-coord old-location map))
72 84
       (update-location object map old-location new-location))))
73 85
 
74
-(defmethod move-up ((object mobile) map)
86
+(defmethod move-up ((object entity) map)
75 87
   (let ((old-location (slot-value object 'location))
76 88
         (new-location (- (slot-value object 'location) (game-map-width map))))
77 89
     (update-location object map old-location new-location)))
78 90
 
79
-(defmethod move-down ((object mobile) map)
91
+(defmethod move-down ((object entity) map)
80 92
   (let ((old-location (slot-value object 'location))
81 93
         (new-location (+ (slot-value object 'location) (game-map-width map))))
82 94
     (update-location object map old-location new-location)))
83 95
 
84
-(defmethod move-right ((object mobile) map)
96
+(defmethod move-right ((object entity) map)
85 97
   (let ((old-location (slot-value object 'location))
86 98
         (new-location (1+ (slot-value object 'location))))
87 99
     (unless (= 0 (calc-coord new-location map))
@@ -95,8 +107,8 @@
95 107
     (terminal-refresh)
96 108
         until (= key bearlibterminal-ffi:+tk-enter+)))
97 109
 
98
-(defun write-map-to-terminal (map)
99
-  (labels ((write-cell (key tile) (multiple-value-bind (x y) (calc-coord key map)
110
+(defun write-map-to-terminal (map viewport)
111
+  (labels ((write-cell (key tile) (multiple-value-bind (x y) (calc-coord (- key (slot-value viewport 'location)) map)
100 112
                                     (terminal-put x y (slot-value tile 'codepoint)))))
101 113
     (maphash #'write-cell (game-map-mobiles map))))
102 114
 
@@ -104,12 +116,20 @@
104 116
   (setf (gethash (slot-value player 'location) (game-map-mobiles map)) player)
105 117
   map)
106 118
 
107
-(defun handle-input (key map player)
119
+(defun handle-input (key map viewport player)
108 120
   (cond
109 121
     ((= key bearlibterminal-ffi:+tk-up+) (move-up player map))
110 122
     ((= key bearlibterminal-ffi:+tk-down+) (move-down player map))
111 123
     ((= key bearlibterminal-ffi:+tk-right+) (move-right player map))
112
-    ((= key bearlibterminal-ffi:+tk-left+) (move-left player map))))
124
+    ((= key bearlibterminal-ffi:+tk-left+) (move-left player map))
125
+    ((= key bearlibterminal-ffi:+tk-w+) (move-up viewport map))
126
+    ((= key bearlibterminal-ffi:+tk-a+) (move-left viewport map))
127
+    ((= key bearlibterminal-ffi:+tk-d+) (move-right viewport map))
128
+    ((= key bearlibterminal-ffi:+tk-s+) (move-down viewport map))))
129
+
130
+(defun create-viewport ()
131
+  (multiple-value-bind (width height) (get-window-size)
132
+    (make-instance 'viewport :width width :height height)))
113 133
 
114 134
 (defun main-loop ()
115 135
   (let ((player (make-instance
@@ -117,10 +137,11 @@
117 137
                  :codepoint #x263a
118 138
                  :location 0)))
119 139
     (loop with map = (initialize-map (make-instance 'game-map) player)
140
+          with viewport = (create-viewport)
120 141
           as key = (terminal-read) do
121
-            (handle-input key map player)
142
+            (handle-input key map viewport player)
122 143
             (terminal-clear)
123
-            (write-map-to-terminal map)
144
+            (write-map-to-terminal map viewport)
124 145
             (terminal-refresh)
125 146
           until (or (= key bearlibterminal-ffi:+tk-close+)
126 147
                     (= key bearlibterminal-ffi:+tk-escape+)))))