Commit ce8c9b40 authored by Cory Chamblin's avatar Cory Chamblin

Part 2

parent 33451dbd
......@@ -3,19 +3,115 @@
(:export #:main))
(in-package :cl-rogue)
;;;; global state --------------------------------------------------------------
(defparameter *width* nil)
(defparameter *height* nil)
(defparameter *scene* nil)
(defparameter *running* nil)
;;;; scenes ---------------------------------------------------------------------
(defclass <scene> ()
((window
:initarg :window
:initform (error ":window is required")
:reader window)))
(defgeneric display-scene (s))
(defgeneric handle-input (s key))
(defclass <start-scene> (<scene>) ())
(defmethod display-scene ((s <start-scene>))
(draw-string (window s) (- (floor *width* 2) 7) 2 "CORYS ROGUELIKE")
(draw-string (window s) (- (floor *width* 2) 11) 4 "Press [Space] to start")
(draw-string (window s) (- (floor *width* 2) 8) 5 "Press [Q] to quit"))
(defmethod handle-input ((s <start-scene>) key)
(when (char= #\Space key)
(setf *scene* (make-instance '<play-scene> :window (window s))))
(when (char= #\Q key)
(setf *running* nil)))
(defclass <play-scene> (<scene>) ())
(defmethod display-scene ((s <play-scene>))
(draw-string (window s) 0 0 " You are having fun.")
(draw-string (window s) 0 1 "-- press [Esc] to lose or [Enter] to win --"))
(defmethod handle-input ((s <play-scene>) key)
(when (char= #\Escape key)
(setf *scene* (make-instance '<lose-scene> :window (window s))))
(when (char= #\Newline key)
(setf *scene* (make-instance '<win-scene> :window (window s)))))
(defclass <win-scene> (<scene>) ())
(defmethod display-scene ((s <win-scene>))
(draw-string (window s) 0 0 " !! YOU WIN !!")
(draw-string (window s) 0 1 "-- press [Enter] to restart --"))
(defmethod handle-input ((s <win-scene>) key)
(when (char= #\Newline key)
(setf *scene* (make-instance '<start-scene> :window (window s)))))
(defclass <lose-scene> (<scene>) ())
(defmethod display-scene ((s <lose-scene>))
(draw-string (window s) 0 0 " You lose.")
(draw-string (window s) 0 1 "-- press [Enter] to restart --"))
(defmethod handle-input ((s <lose-scene>) key)
(when (char= #\Newline key)
(setf *scene* (make-instance '<start-scene> :window (window s)))))
;;;; drawing --------------------------------------------------------------------
(defun draw-string (window x y format-string &rest args)
(charms:write-string-at-point window
(apply #'format nil format-string args)
x
y))
(defun update-window-dimensions (window)
(multiple-value-bind (width height) (charms:window-dimensions window)
(setf *width* width)
(setf *height* height)))
;;;; main -----------------------------------------------------------------------
(defun initialize (window)
(update-window-dimensions window)
(setf *running* t)
(setf *scene* (make-instance '<start-scene>
:window window)))
(defun render (window)
(charms:clear-window window)
(charms:write-string-at-point window "CL-Rogue!" 0 0)
(charms:write-string-at-point window "press q to quit" 0 1))
(display-scene *scene*))
(defun gameloop ()
(let ((window charms:*standard-window*))
(initialize window)
(loop named :game-loop
while *running*
do (progn
(render window)
(handle-input *scene* (charms:get-char window :ignore-error t))))))
(defun main ()
(charms:with-curses ()
(charms:disable-echoing)
(charms:enable-raw-input :interpret-control-characters t)
(let ((window charms:*standard-window*))
(loop named :driver-loop
do (progn
(render window)
(when (char= #\q (charms:get-char window :ignore-error t))
(return-from :driver-loop)))))))
(gameloop)))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment