Skip to content

Instantly share code, notes, and snippets.

@knusbaum
Created June 15, 2018 06:09
Show Gist options
  • Save knusbaum/5a500412d2e4c57911794950ea8409e5 to your computer and use it in GitHub Desktop.
Save knusbaum/5a500412d2e4c57911794950ea8409e5 to your computer and use it in GitHub Desktop.
(defvar *canvas-width* 800)
(defvar *canvas-height* 600)
(gamekit:defgame hello-gamekit ()
((universe :accessor universe)
(box :accessor box)
(box2 :accessor box2))
(:viewport-width *canvas-width*) ; window's width
(:viewport-height *canvas-height*) ; window's height
(:viewport-title "Hello Gamekit!")) ; window's title
(defvar *black* (gamekit:vec4 0 0 0 1))
(defvar *red* (gamekit:vec4 1 0 0 1))
(defparameter *b2-width* 1000)
(defparameter *b2-height* 100)
(defvar *box*)
(defmethod gamekit:post-initialize ((app hello-gamekit))
(setf (universe app)
(cl-bodge:make-universe :2d :on-pre-solve #'collide))
(setf (ge.phy:gravity (universe app)) (ge:vec2 0 -9.81))
(setf (box app)
(ge.phy:make-box-shape (universe app) 100 100
:body (ge.phy:make-rigid-body (universe app))))
(setf (ge.phy:body-position (ge.phy:shape-body (box app))) (gamekit:vec2 110 400))
(setf *box* (box app))
(ge.phy:apply-force (ge.phy:shape-body (box app)) (gamekit:vec2 0.0 -100.0))
(ge.phy:make-segment-shape (universe app) (ge:vec2 0 200) (ge:vec2 1000 0)))
(defmethod gamekit:act ((app hello-gamekit))
(loop for i below 10 do
(ge.phy:observe-universe (universe app) 0.01)))
(defun collide (this that)
(format t "~a ~a ~%" this that)
t)
(defun cpvrotate (v1 v2)
(ge:vec2 (- (* (ge:x v1) (ge:x v2)) (* (ge:y v1) (ge:y v2)))
(+ (* (ge:x v1) (ge:y v2)) (* (ge:y v1) (ge:x v2)))))
(defmethod gamekit:draw ((app hello-gamekit))
(gamekit:print-text (format nil "Box: ~a"
(ge.phy::body-rotation (ge.phy:shape-body (box app))))
10 500)
(let ((rot (ge.phy:body-rotation (ge.phy:shape-body (box app)))))
(flet ((convert-vec (vec)
(ge.math:add
(cpvrotate
(ge.math:subt vec (ge:vec2 50 50))
rot)
(ge.phy:body-position (ge.phy:shape-body (box app))))))
(gamekit:draw-polygon (list
(convert-vec (ge:vec2 0 0))
(convert-vec (ge:vec2 0 100))
(convert-vec (ge:vec2 100 100))
(convert-vec (ge:vec2 100 0)))
:fill-paint *red*)))
(gamekit:draw-line (ge:vec2 0 200) (ge:vec2 1000 0) *black*)
(gamekit:draw-circle (ge.phy:body-position (ge.phy:shape-body (box app))) 5 :fill-paint *black*))
(defvar *cursor-position* (gamekit:vec2 0 0))
(defun run ()
(gamekit:start 'hello-gamekit)
(gamekit:bind-cursor (lambda (x y)
"Save cursor position"
(setf (gamekit:x *cursor-position*) x
(gamekit:y *cursor-position*) y)))
(gamekit:bind-button :mouse-left :pressed
(lambda ()
"Copy saved cursor position into snake's head position vector"
(setf (ge.phy:body-position (ge.phy:shape-body *box*))
*cursor-position*)
(setf (ge.phy:body-angular-velocity (ge.phy:shape-body *box*))
0)
(setf (ge.phy:body-linear-velocity (ge.phy:shape-body *box*))
(ge:vec2 0 0)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment