Getting a box to move around

This commit is contained in:
Joseph Ferano 2024-11-09 21:51:16 +07:00
commit f94ca11760

48
game.lisp Normal file
View File

@ -0,0 +1,48 @@
(ql:quickload :claw-raylib)
(ql:quickload :cffi)
(in-package :cffi)
(rename-package (find-package :raylib) :rl)
(defparameter *move-speed* 2.0)
(defmacro is-key-down (key)
`(rl:is-key-down (foreign-enum-value 'rl:keyboard-key ,key)))
(defun v2+ (v1 v2) (rl:vector2-add v1 v2))
(defun v2- (v1 v2) (rl:vector2-subtract v1 v2))
(defun v2* (v1 n) (rl:vector2-scale v1 n))
(defstruct game-state
(player-pos (rl:make-vector2 :x 100.0 :y 100.0)))
(defun game-input (state)
(with-slots ((pos player-pos)) state
(let ((dx 0.0) (dy 0.0))
(when (is-key-down :right) (incf dx 1))
(when (is-key-down :left) (decf dx 1))
(when (is-key-down :up) (decf dy 1))
(when (is-key-down :down) (incf dy 1))
(setf pos (v2+ pos (v2* (rl:vector2-normalize (rl:make-vector2 :x dx :y dy))
*move-speed*))))))
(defun game-update (state)
(rl:draw-rectangle-v (game-state-player-pos state)
(rl:make-vector2 :x 100.0 :y 100.0)
rl:+skyblue+)
(rl:draw-fps 10 5))
(defun basic-window ()
(let ((screen-width 800)
(screen-height 450)
(game-state (make-game-state)))
(rl:with-window ("CL Game" (screen-width screen-height))
(rl:set-target-fps 60)
(loop :until (rl:window-should-close)
:do (rl:with-drawing
(rl:clear-background rl:+raywhite+)
(game-input game-state)
(game-update game-state))))))
(basic-window)