Sprite class so we can more easily generalize drawing them and eventually y-sorting
This commit is contained in:
parent
ebf769eeb7
commit
bdfe86438f
50
game.lisp
50
game.lisp
@ -18,14 +18,21 @@
|
|||||||
((current-amount :initform 100.0 :accessor current-amount)
|
((current-amount :initform 100.0 :accessor current-amount)
|
||||||
(max-amount :initform 100.0 :accessor max-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 ()
|
(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)
|
(anim-state :initform 0 :accessor anim-state)
|
||||||
|
(sprite :initarg :sprite :initform (make-instance 'sprite) :accessor sprite)
|
||||||
(velocity :initform (vec 0 0) :accessor velocity)
|
(velocity :initform (vec 0 0) :accessor velocity)
|
||||||
(health :initform 100.0 :accessor health)))
|
(health :initform 100.0 :accessor health)))
|
||||||
|
|
||||||
(defclass archer ()
|
(defclass archer ()
|
||||||
((pos :initform (vec 145.0 253.0) :accessor pos)
|
((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)
|
(velocity :initform (vec 0 0) :accessor velocity)
|
||||||
(health :initform 100.0 :accessor health)))
|
(health :initform 100.0 :accessor health)))
|
||||||
|
|
||||||
@ -33,9 +40,16 @@
|
|||||||
((health :initform 100.0 :accessor health)))
|
((health :initform 100.0 :accessor health)))
|
||||||
|
|
||||||
(defclass tower ()
|
(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 archer1 (make-instance 'archer))
|
||||||
(defparameter goblin1 (make-instance 'goblin))
|
(defparameter goblin1 (make-instance 'goblin))
|
||||||
|
|
||||||
@ -83,13 +97,14 @@
|
|||||||
(mag (vsqrlength dist)))
|
(mag (vsqrlength dist)))
|
||||||
(if (<= mag 10.0)
|
(if (<= mag 10.0)
|
||||||
(progn
|
(progn
|
||||||
(setf (anim-state knight1) 0)
|
;; (setf (anim-state knight1) 0)
|
||||||
(setf kpos click-pos)
|
(setf kpos click-pos)
|
||||||
;; (setf (velocity knight1) (vec 0 0))
|
;; (setf (velocity knight1) (vec 0 0))
|
||||||
(setf click-pos nil))
|
(setf click-pos nil))
|
||||||
(let ((vel (v* (* (rl:get-frame-time) 150.0) (vunit* dist))))
|
(let ((vel (v* (* (rl:get-frame-time) 150.0) (vunit* dist))))
|
||||||
(setf (velocity knight1) vel)
|
(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))
|
(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))
|
(rl:make-rectangle :x (* size (get-tile-wrapped col tile-count))
|
||||||
:y (* size (get-tile-wrapped row tile-count))
|
:y (* size (get-tile-wrapped row tile-count))
|
||||||
:width size :height size)
|
:width size :height size)
|
||||||
(v+ (vec 15 150) (vec (* col size) (* row size)))
|
(v+ (vec -15 150) (vec (* col size) (* row size)))
|
||||||
:white)))
|
:white)))
|
||||||
|
|
||||||
(defun draw-ground ()
|
(defun draw-ground ()
|
||||||
@ -122,11 +137,30 @@
|
|||||||
0.0
|
0.0
|
||||||
:white)))
|
: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 ()
|
(defun game-draw ()
|
||||||
(rl:clear-background (rl:make-rgba 71 171 169 1))
|
(rl:clear-background (rl:make-rgba 71 171 169 1))
|
||||||
(draw-ground)
|
(draw-ground)
|
||||||
(rl:draw-texture-v (gethash 'tower *textures*) (vec 80 150) :white)
|
(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))
|
(animate-sprite 'archer 0 (pos archer1) (velocity archer1))
|
||||||
(rl:draw-fps 10 5))
|
(rl:draw-fps 10 5))
|
||||||
|
|
||||||
@ -143,3 +177,5 @@
|
|||||||
(game-draw)))
|
(game-draw)))
|
||||||
(loop for value being the hash-values of *textures*
|
(loop for value being the hash-values of *textures*
|
||||||
do (rl:unload-texture value)))))
|
do (rl:unload-texture value)))))
|
||||||
|
|
||||||
|
(game)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user