|
@@ -2,5 +2,99 @@
|
2
|
2
|
|
3
|
3
|
(in-package #:crypts-and-corpses)
|
4
|
4
|
|
5
|
|
-;;; "crypts-and-corpses" goes here. Hacks and glory await!
|
|
5
|
+(defclass tile-entity ()
|
|
6
|
+ ((codepoint :initarg :codepoint :initform #x3f)
|
|
7
|
+ (location :initarg :location :initform 0)))
|
6
|
8
|
|
|
9
|
+(defclass mobile (tile-entity)
|
|
10
|
+ ((health :initarg :health)
|
|
11
|
+ (mana :initarg :mana)))
|
|
12
|
+
|
|
13
|
+(defclass player-character (mobile) ())
|
|
14
|
+
|
|
15
|
+(defclass enemy (mobile) ())
|
|
16
|
+
|
|
17
|
+(defclass minion (mobile) ())
|
|
18
|
+
|
|
19
|
+(defun calc-location (x y)
|
|
20
|
+ (+ (* 100 (cdr y)) (car x)))
|
|
21
|
+
|
|
22
|
+(defgeneric set-location (object x y))
|
|
23
|
+(defmethod set-location ((object tile-entity) x y)
|
|
24
|
+ (setf (slot-value object 'location) (calc-location x y)))
|
|
25
|
+
|
|
26
|
+(defgeneric move-left (object map))
|
|
27
|
+(defgeneric move-right (object map))
|
|
28
|
+(defgeneric move-up (object map))
|
|
29
|
+(defgeneric move-down (object map))
|
|
30
|
+
|
|
31
|
+(defun update-location (object map old-location new-location)
|
|
32
|
+ (setf (slot-value object 'location) new-location)
|
|
33
|
+ (setf (gethash new-location map) object)
|
|
34
|
+ (remhash old-location map))
|
|
35
|
+
|
|
36
|
+(defmethod move-left ((object mobile) map)
|
|
37
|
+ (let ((old-location (slot-value object 'location))
|
|
38
|
+ (new-location (1- (slot-value object 'location))))
|
|
39
|
+ (update-location object map old-location new-location)))
|
|
40
|
+
|
|
41
|
+(defmethod move-up ((object mobile) map)
|
|
42
|
+ (let ((old-location (slot-value object 'location))
|
|
43
|
+ (new-location (- (slot-value object 'location) 100)))
|
|
44
|
+ (update-location object map old-location new-location)))
|
|
45
|
+
|
|
46
|
+(defmethod move-down ((object mobile) map)
|
|
47
|
+ (let ((old-location (slot-value object 'location))
|
|
48
|
+ (new-location (+ (slot-value object 'location) 100)))
|
|
49
|
+ (update-location object map old-location new-location)))
|
|
50
|
+
|
|
51
|
+(defmethod move-right ((object mobile) map)
|
|
52
|
+ (let ((old-location (slot-value object 'location))
|
|
53
|
+ (new-location (1+ (slot-value object 'location))))
|
|
54
|
+ (update-location object map old-location new-location)))
|
|
55
|
+
|
|
56
|
+(defun intro-message ()
|
|
57
|
+ (loop as key = (terminal-read) do
|
|
58
|
+ (terminal-print 40 21 "Crypts and Corpses")
|
|
59
|
+ (terminal-print 39 22 "Press enter to start")
|
|
60
|
+ (terminal-refresh)
|
|
61
|
+ until (= key bearlibterminal-ffi:+tk-enter+)))
|
|
62
|
+
|
|
63
|
+(defun write-cell (key tile)
|
|
64
|
+ (terminal-put (mod key 100) (floor (/ key 100)) (slot-value tile 'codepoint)))
|
|
65
|
+
|
|
66
|
+(defun write-map-to-terminal (map)
|
|
67
|
+ (maphash #'write-cell map))
|
|
68
|
+
|
|
69
|
+(defun initialize-map (map player)
|
|
70
|
+ (setf (gethash (slot-value player 'location) map) player)
|
|
71
|
+ map)
|
|
72
|
+
|
|
73
|
+(defun handle-input (key map player)
|
|
74
|
+ (cond
|
|
75
|
+ ((= key bearlibterminal-ffi:+tk-up+) (move-up player map))
|
|
76
|
+ ((= key bearlibterminal-ffi:+tk-down+) (move-down player map))
|
|
77
|
+ ((= key bearlibterminal-ffi:+tk-right+) (move-right player map))
|
|
78
|
+ ((= key bearlibterminal-ffi:+tk-left+) (move-left player map))))
|
|
79
|
+
|
|
80
|
+(defun main-loop ()
|
|
81
|
+ (let ((player (make-instance
|
|
82
|
+ 'player-character
|
|
83
|
+ :codepoint #x263a
|
|
84
|
+ :location 0)))
|
|
85
|
+ (loop with map = (initialize-map (make-hash-table) player)
|
|
86
|
+ as key = (terminal-read) do
|
|
87
|
+ (handle-input key map player)
|
|
88
|
+ (terminal-clear)
|
|
89
|
+ (write-map-to-terminal map)
|
|
90
|
+ (terminal-refresh)
|
|
91
|
+ until (or (= key bearlibterminal-ffi:+tk-close+)
|
|
92
|
+ (= key bearlibterminal-ffi:+tk-escape+)))))
|
|
93
|
+
|
|
94
|
+(defun start ()
|
|
95
|
+ (terminal-open)
|
|
96
|
+ (terminal-set "window: size=100x50, title='Crypts and Corpses'")
|
|
97
|
+ (terminal-set "font: ./fonts/DejaVuSansMono.ttf, size=12")
|
|
98
|
+ (intro-message)
|
|
99
|
+ (main-loop)
|
|
100
|
+ (terminal-close))
|