For the lisp game jam http://itch.io/jam/january-2016-lisp-game-jam. Also practice for my future forever project game (not yet named). This is a roguelike where you play a necromancer able to summon skeletons and any other powers I have time for.

crypts-and-corpses.lisp 3.3KB

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