Generalize sprites and soldiers so we can iterate over a list
This commit is contained in:
parent
bdfe86438f
commit
1c4b0d6bc7
59
game.lisp
59
game.lisp
@ -18,49 +18,41 @@
|
||||
((current-amount :initform 100.0 :accessor current-amount)
|
||||
(max-amount :initform 100.0 :accessor max-amount)))
|
||||
|
||||
(defclass sprite ()
|
||||
(defclass sprite-class ()
|
||||
((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 soldier ()
|
||||
((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)
|
||||
(sprite :initarg :sprite :initform (make-instance 'sprite-class) :accessor sprite)
|
||||
(velocity :initform (vec 0 0) :accessor velocity)
|
||||
(health :initform 100.0 :accessor health)))
|
||||
(health :initarg :health :initform (make-instance 'health) :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)))
|
||||
|
||||
(defclass goblin ()
|
||||
((health :initform 100.0 :accessor health)))
|
||||
|
||||
(defclass tower ()
|
||||
((sprite :initarg :sprite :initform (make-instance 'sprite) :accessor sprite)
|
||||
(health :initform 100.0 :accessor health)))
|
||||
|
||||
(defparameter knight1 (make-instance 'knight
|
||||
(defparameter *knight* (make-instance 'soldier
|
||||
:pos (vec 270 360)
|
||||
:sprite (make-instance 'sprite
|
||||
:sprite (make-instance 'sprite-class
|
||||
: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 archer1 (make-instance 'archer))
|
||||
(defparameter goblin1 (make-instance 'goblin))
|
||||
(defparameter *archer* (make-instance 'soldier
|
||||
:pos (vec 145.0 253.0)
|
||||
:sprite (make-instance 'sprite-class
|
||||
:texture-key 'archer
|
||||
:src-rect (rl:make-rectangle :x 0.0 :y 0.0 :width 192.0 :height 192.0)
|
||||
:origin (vec 95.0 128.0))))
|
||||
|
||||
(defun deal-damage (obj damage)
|
||||
(with-accessors ((h health)) obj
|
||||
(setf h (- h damage))))
|
||||
|
||||
(mapcar (lambda (go) (deal-damage go 10)) (list knight1 archer1 goblin1))
|
||||
;; (mapcar (lambda (go) (deal-damage go 10)) (list knight1 archer1 goblin1))
|
||||
|
||||
(defclass game-state ()
|
||||
((click-pos :initform nil :accessor click-pos)))
|
||||
((click-pos :initform nil :accessor click-pos)
|
||||
(entities :initform '() :accessor entities)))
|
||||
|
||||
(defparameter *game-state* nil)
|
||||
(defparameter *textures* (make-hash-table))
|
||||
@ -70,6 +62,7 @@
|
||||
|
||||
(defun game-init ()
|
||||
(setf *game-state* (make-instance 'game-state))
|
||||
(setf (entities *game-state*) (list *knight* *archer*))
|
||||
;; (add-component (knight *game-state*) (make-instance 'transform))
|
||||
(bind-texture 'terrain "~/Development/tinyswords/assets/Terrain/Ground/Tilemap_Flat.png")
|
||||
(bind-texture 'knight "~/Development/tinyswords/assets/Factions/Knights/Troops/Warrior/Blue/Warrior_Blue.png")
|
||||
@ -91,20 +84,21 @@
|
||||
(defun game-update ()
|
||||
(with-accessors ((click-pos click-pos)) *game-state*
|
||||
(when click-pos
|
||||
(setf (anim-state knight1) 1)
|
||||
(with-accessors ((kpos pos)) knight1
|
||||
(setf (anim-state *knight*) 1)
|
||||
(with-accessors ((kpos pos)) *knight*
|
||||
(let* ((dist (v- click-pos kpos))
|
||||
(mag (vsqrlength dist)))
|
||||
(if (<= mag 10.0)
|
||||
(progn
|
||||
;; (setf (anim-state knight1) 0)
|
||||
;; (setf (anim-state *knight*) 0)
|
||||
(setf kpos click-pos)
|
||||
;; (setf (velocity knight1) (vec 0 0))
|
||||
(setf (velocity *knight*) (vec 0 0))
|
||||
(setf click-pos nil))
|
||||
(let ((vel (v* (* (rl:get-frame-time) 150.0) (vunit* dist))))
|
||||
(setf (velocity knight1) vel)
|
||||
(setf (velocity *knight*) vel)
|
||||
(setf kpos (v+ kpos vel))))))))
|
||||
(animate-sprite2 (sprite knight1)))
|
||||
(dolist (entity (entities *game-state*))
|
||||
(tick-sprite-animation (sprite entity))))
|
||||
|
||||
(defun get-tile-wrapped (n wrap-count) (if (> n 0) (1+ (mod (1- n) (- wrap-count 2))) 0))
|
||||
|
||||
@ -144,7 +138,7 @@
|
||||
(when col-p
|
||||
(setf (rl:rectangle-x src-rect) (* col (rl:rectangle-width src-rect))))))
|
||||
|
||||
(defun animate-sprite2 (sprite)
|
||||
(defun tick-sprite-animation (sprite)
|
||||
(with-slots (src-rect) sprite
|
||||
(update-sprite-anim sprite :col (coerce (mod (truncate (/ (rl:get-time) 0.1)) 6) 'single-float))))
|
||||
|
||||
@ -159,9 +153,8 @@
|
||||
(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 0 (pos knight1) (velocity knight1))
|
||||
(draw-sprite (sprite knight1) (pos knight1))
|
||||
(animate-sprite 'archer 0 (pos archer1) (velocity archer1))
|
||||
(draw-sprite (sprite *knight*) (pos *knight*))
|
||||
(draw-sprite (sprite *archer*) (pos *archer*))
|
||||
(rl:draw-fps 10 5))
|
||||
|
||||
(defun game ()
|
||||
|
Loading…
x
Reference in New Issue
Block a user