Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
borodust committed Apr 28, 2019
0 parents commit 08d2807
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
@@ -0,0 +1,18 @@
# lisp junk
*.FASL
*.fasl
*.lisp-temp

# emacs junk
\#*
*~
.\#*

# system dependent junk
local/

# macOS junk
**/.DS_Store

# build
build/
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Pavel Korolev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
@@ -0,0 +1,3 @@
# trivial-gamekit-fistmachine

Finite state machine for `trivial-gamekit`.
73 changes: 73 additions & 0 deletions fistmachine.lisp
@@ -0,0 +1,73 @@
(cl:defpackage :trivial-gamekit.fistmachine
(:nicknames :gamekit.fistmachine)
(:use :cl)
(:export #:fistmachine
#:initialize-state
#:discard-state
#:transition-to))
(cl:in-package :trivial-gamekit.fistmachine)


(defclass fistmachine ()
((game-state :initform nil)
(initial-state :initarg :initial-state :initform nil)))


(defgeneric initialize-state (game-state &key &allow-other-keys)
(:method (game-state &key &allow-other-keys)
(declare (ignore game-state))))


(defgeneric discard-state (game-state)
(:method (game-state) (declare (ignore game-state))))


(defun transition-to (state-class &rest args &key &allow-other-keys)
(let ((game (gamekit:gamekit)))
(with-slots (game-state) game
(labels ((%report-retry (stream)
(format stream "Try transitioning to ~A again" state-class))
(%transition-to ()
(tagbody
begin
(restart-case
(progn
(discard-state game-state)
(setf game-state (apply #'make-instance state-class args))
(apply #'initialize-state game-state args))
(retry-transition ()
:report %report-retry
(go begin))))))
(gamekit:push-action #'%transition-to)))))


(defgeneric button-pressed (game-state button)
(:method (game-state button) (declare (ignore game-state button))))


(defgeneric button-released (game-state button)
(:method (game-state button) (declare (ignore game-state button))))


(defmethod gamekit:post-initialize ((this fistmachine))
(call-next-method)
(with-slots (game-state initial-state) this
(flet ((process-button (button state)
(case state
(:pressed (button-pressed game-state button))
(:released (button-released game-state button)))))
(gamekit:bind-any-button #'process-button))
(when initial-state
(transition-to initial-state))))


(defmethod gamekit:act ((this fistmachine))
(call-next-method)
(with-slots (game-state) this
(gamekit:act game-state)))


(defmethod gamekit:draw ((this fistmachine))
(call-next-method)
(with-slots (game-state) this
(gamekit:draw game-state)))
8 changes: 8 additions & 0 deletions trivial-gamekit-fistmachine.asd
@@ -0,0 +1,8 @@
(asdf:defsystem :trivial-gamekit-fistmachine
:description "Finite state machine for trivial-gamekit"
:author "Pavel Korolev"
:mailto "dev@borodust.org"
:license "MIT"
:depends-on (trivial-gamekit)
:serial t
:components ((:file "fistmachine")))

0 comments on commit 08d2807

Please sign in to comment.