From bdfe86438f1f36e97a8c08718e2f967003625653 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Mon, 18 Nov 2024 23:06:59 +0700 Subject: [PATCH] Sprite class so we can more easily generalize drawing them and eventually y-sorting --- game.lisp | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/game.lisp b/game.lisp index fc32493..40805a0 100644 --- a/game.lisp +++ b/game.lisp @@ -18,14 +18,21 @@ ((current-amount :initform 100.0 :accessor current-amount) (max-amount :initform 100.0 :accessor max-amount))) +(defclass sprite () + ((src-rect :initarg :src-rect :initform (rl:make-rectangle :x 0 :y 0 :width 0 :height 0) :accessor src-rect) + (texture-key :initarg :texture-key :accessor texture-key) + (origin :initarg :origin :initform (vec 0 0) :accessor origin))) + (defclass knight () - ((pos :initform (vec 270 360) :accessor pos) + ((pos :initarg :pos :initform (vec 0 0) :accessor pos) (anim-state :initform 0 :accessor anim-state) + (sprite :initarg :sprite :initform (make-instance 'sprite) :accessor sprite) (velocity :initform (vec 0 0) :accessor velocity) (health :initform 100.0 :accessor health))) (defclass archer () ((pos :initform (vec 145.0 253.0) :accessor pos) + (sprite :initarg :sprite :initform (make-instance 'sprite) :accessor sprite) (velocity :initform (vec 0 0) :accessor velocity) (health :initform 100.0 :accessor health))) @@ -33,9 +40,16 @@ ((health :initform 100.0 :accessor health))) (defclass tower () - ((health :initform 100.0 :accessor health))) + ((sprite :initarg :sprite :initform (make-instance 'sprite) :accessor sprite) + (health :initform 100.0 :accessor health))) + +(defparameter knight1 (make-instance 'knight + :pos (vec 270 360) + :sprite (make-instance 'sprite + :texture-key 'knight + :src-rect (rl:make-rectangle :x 0.0 :y 0.0 :width 192.0 :height 192.0) + :origin (vec 95.0 128.0)))) -(defparameter knight1 (make-instance 'knight)) (defparameter archer1 (make-instance 'archer)) (defparameter goblin1 (make-instance 'goblin)) @@ -83,13 +97,14 @@ (mag (vsqrlength dist))) (if (<= mag 10.0) (progn - (setf (anim-state knight1) 0) + ;; (setf (anim-state knight1) 0) (setf kpos click-pos) ;; (setf (velocity knight1) (vec 0 0)) (setf click-pos nil)) (let ((vel (v* (* (rl:get-frame-time) 150.0) (vunit* dist)))) (setf (velocity knight1) vel) - (setf kpos (v+ kpos vel))))))))) + (setf kpos (v+ kpos vel)))))))) + (animate-sprite2 (sprite knight1))) (defun get-tile-wrapped (n wrap-count) (if (> n 0) (1+ (mod (1- n) (- wrap-count 2))) 0)) @@ -100,7 +115,7 @@ (rl:make-rectangle :x (* size (get-tile-wrapped col tile-count)) :y (* size (get-tile-wrapped row tile-count)) :width size :height size) - (v+ (vec 15 150) (vec (* col size) (* row size))) + (v+ (vec -15 150) (vec (* col size) (* row size))) :white))) (defun draw-ground () @@ -122,11 +137,30 @@ 0.0 :white))) +(defun update-sprite-anim (sprite &key (row 0 row-p) (col 0 col-p)) + (with-slots (src-rect) sprite + (when row-p + (setf (rl:rectangle-y src-rect) (* row (rl:rectangle-height src-rect)))) + (when col-p + (setf (rl:rectangle-x src-rect) (* col (rl:rectangle-width src-rect)))))) + +(defun animate-sprite2 (sprite) + (with-slots (src-rect) sprite + (update-sprite-anim sprite :col (coerce (mod (truncate (/ (rl:get-time) 0.1)) 6) 'single-float)))) + +(defun draw-sprite (sprite pos) + (with-slots (texture-key src-rect origin) sprite + (let ((dst-rec (rl:make-rectangle :x (vx pos) :y (vy pos) + :width (rl:rectangle-width src-rect) + :height (rl:rectangle-height src-rect)))) + (rl:draw-texture-pro (gethash texture-key *textures*) src-rect dst-rec origin 0.0 :white)))) + (defun game-draw () (rl:clear-background (rl:make-rgba 71 171 169 1)) (draw-ground) (rl:draw-texture-v (gethash 'tower *textures*) (vec 80 150) :white) - (animate-sprite 'knight (anim-state knight1) (pos knight1) (velocity knight1)) + ;; (animate-sprite 'knight 0 (pos knight1) (velocity knight1)) + (draw-sprite (sprite knight1) (pos knight1)) (animate-sprite 'archer 0 (pos archer1) (velocity archer1)) (rl:draw-fps 10 5)) @@ -143,3 +177,5 @@ (game-draw))) (loop for value being the hash-values of *textures* do (rl:unload-texture value))))) + +(game)