ソースを参照

Add basic game loop with movement

Lily Carpenter 9 年 前
コミット
55e100eb98
共有7 個のファイルを変更した113 個の追加2 個の削除を含む
  1. 18 1
      NOTES.org
  2. 95 1
      crypts-and-corpses.lisp
  3. BIN
      fonts/DejaVuSansMono.ttf
  4. BIN
      fonts/consolas_unicode_10x10.png
  5. BIN
      fonts/consolas_unicode_12x12.png
  6. BIN
      fonts/consolas_unicode_16x16.png
  7. BIN
      fonts/consolas_unicode_8x8.png

+ 18 - 1
NOTES.org

@@ -3,4 +3,21 @@ http://foo.wyrd.name/en:bearlibterminal:reference
3 3
 http://www.utf8-chartable.de/unicode-utf8-table.pl?utf8=0x
4 4
 
5 5
 * Tasks
6
-** TODO Get bearlibterminal autowrapped and basic window working.
6
+** DONE Get bearlibterminal autowrapped and basic window working.
7
+CLOSED: [2016-01-01 Fri 22:52]
8
+** High Priority
9
+*** TODO Create player character
10
+**** Health (no regeneration)
11
+**** Mana (regenerates per turn)
12
+**** Ranged attack (takes mana)
13
+**** Melee attack (weak, no mana cost)
14
+**** Ability to summon special corpses as undead
15
+*** TODO Create static map
16
+*** TODO Create Undead ally characters
17
+*** TODO Create enemy characters (human knights?) with basic UI
18
+*** TODO Figure out an interesting way to integrate "Darkness"
19
+*** TODO Figure out how to get messages in exact center.
20
+** Medium priority
21
+*** TODO Create map -> graphics layer so that isn't manual. (consider generalizing this)
22
+*** TODO Create generalized input command dispatch. (consider generalizing this)
23
+*** TODO Create configuration system that can control display and keybindings.

+ 95 - 1
crypts-and-corpses.lisp

@@ -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))

BIN
fonts/DejaVuSansMono.ttf


BIN
fonts/consolas_unicode_10x10.png


BIN
fonts/consolas_unicode_12x12.png


BIN
fonts/consolas_unicode_16x16.png


BIN
fonts/consolas_unicode_8x8.png