From fcb067f5bda5a450916494e752532588b59e89ae Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Mon, 18 Nov 2024 21:54:37 +0700 Subject: [PATCH] Trying out CLOS classes, knight follows left click --- game.lisp | 106 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 81 insertions(+), 25 deletions(-) diff --git a/game.lisp b/game.lisp index dc26d1e..7fc8f19 100644 --- a/game.lisp +++ b/game.lisp @@ -10,33 +10,86 @@ (defparameter *move-speed* 5.0) -(defstruct game-state - (textures (make-hash-table)) - (knight-pos (vec 150.0 250.0)) - (archer-pos (vec 50.0 122.0))) +(defclass health () + ((current-amount :initform 100.0 :accessor current-amount) + (max-amount :initform 100.0 :accessor max-amount))) + +(defclass animation () + ((current-amount :initform 100.0 :accessor current-amount) + (max-amount :initform 100.0 :accessor max-amount))) + +(defclass knight () + ((pos :initform (vec 270 360) :accessor pos) + (anim-state :initform 0 :accessor anim-state) + (velocity :initform (vec 0 0) :accessor velocity) + (health :initform 100.0 :accessor health))) + +(defclass archer () + ((pos :initform (vec 145.0 253.0) :accessor pos) + (velocity :initform (vec 0 0) :accessor velocity) + (health :initform 100.0 :accessor health))) + +(defclass goblin () + ((health :initform 100.0 :accessor health))) + +(defclass tower () + ((health :initform 100.0 :accessor health))) + +(defparameter knight1 (make-instance 'knight)) +(defparameter archer1 (make-instance 'archer)) +(defparameter goblin1 (make-instance 'goblin)) + +(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)) + +(defclass game-state () + ((click-pos :initform nil :accessor click-pos))) (defparameter *game-state* nil) +(defparameter *textures* (make-hash-table)) (defun bind-texture (key path) - (setf (gethash key (game-state-textures *game-state*)) (rl:load-texture (uiop:native-namestring path)))) + (setf (gethash key *textures*) (rl:load-texture (uiop:native-namestring path)))) (defun game-init () - (setf *game-state* (make-game-state)) + (setf *game-state* (make-instance 'game-state)) + ;; (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") (bind-texture 'archer "~/Development/tinyswords/assets/Factions/Knights/Troops/Archer/Blue/Archer_Blue.png") (bind-texture 'tower "~/Development/tinyswords/assets/Factions/Knights/Buildings/Tower/Tower_Blue.png")) (defun game-input () - (with-slots ((pos archer-pos)) *game-state* - (let ((dx 0.0) (dy 0.0)) - (when (rl:is-key-down :key-right) (incf dx 1)) - (when (rl:is-key-down :key-left) (decf dx 1)) - (when (rl:is-key-down :key-up) (decf dy 1)) - (when (rl:is-key-down :key-down) (incf dy 1)) - (setf pos (v+ pos (v* (vunit* (vec dx dy)) *move-speed*)))))) + (when (rl:is-mouse-button-pressed 0) + (print (rl:get-mouse-position)) + (setf (click-pos *game-state*) (rl:get-mouse-position)))) + ;; (with-slots ((pos archer-pos) (kpos knight-pos)) *game-state* + ;; (let ((dx 0.0) (dy 0.0)) + ;; (when (rl:is-key-down :key-right) (incf dx 1)) + ;; (when (rl:is-key-down :key-left) (decf dx 1)) + ;; (when (rl:is-key-down :key-up) (decf dy 1)) + ;; (when (rl:is-key-down :key-down) (incf dy 1)) + ;; (setf pos (v+ pos (v* (vunit* (vec dx dy)) *move-speed*)))))) -(defun game-update () '()) +(defun game-update () + (with-accessors ((click-pos click-pos)) *game-state* + (when click-pos + (setf (anim-state knight1) 1) + (with-accessors ((kpos pos)) knight1 + (let* ((dist (v- click-pos kpos)) + (mag (vsqrlength dist))) + (if (<= mag 10.0) + (progn + (setf (anim-state knight1) 0) + (setf kpos click-pos) + ;; (setf (velocity knight1) (vec 0 0)) + (setf click-pos nil)) + (let ((vel (v* 3.0 (vunit* dist)))) + (setf (velocity knight1) vel) + (setf kpos (v+ kpos vel))))))))) (defun get-tile-wrapped (n wrap-count) (if (> n 0) (1+ (mod (1- n) (- wrap-count 2))) 0)) @@ -52,24 +105,29 @@ (defun draw-ground () (let ((size 32) - (terrain-tex (gethash 'terrain (game-state-textures *game-state*)))) + (terrain-tex (gethash 'terrain *textures*))) (loop for row from 0 to 16 do (loop for col from 0 to 32 do (draw-tile terrain-tex size col row))))) -(defun animate-sprite (tex-key row pos) - (let ((time-interval (coerce (mod (truncate (/ (rl:get-time) 0.1)) 6) 'single-float))) - (rl:draw-texture-rec (gethash tex-key (game-state-textures *game-state*)) +(defun animate-sprite (tex-key row pos vel) + (let ((time-interval (coerce (mod (truncate (/ (rl:get-time) 0.1)) 6) 'single-float)) + (side (if (< (vx vel) 0) -1 1))) + (rl:draw-texture-pro (gethash tex-key *textures*) (rl:make-rectangle :x (* time-interval 192.0) :y (* row 192) + :width (* side 192.0) :height 192.0) + (rl:make-rectangle :x (vx pos) :y (vy pos) :width 192.0 :height 192.0) - pos :white))) + (vec 95.0 128.0) + 0.0 + :white))) (defun game-draw () (rl:clear-background (rl:make-rgba 71 171 169 1)) (draw-ground) - (rl:draw-texture-v (gethash 'tower (game-state-textures *game-state*)) (vec 80 150) :white) - (animate-sprite 'knight 0 (game-state-knight-pos *game-state*)) - (animate-sprite 'archer 0 (game-state-archer-pos *game-state*)) + (rl:draw-texture-v (gethash 'tower *textures*) (vec 80 150) :white) + (animate-sprite 'knight (anim-state knight1) (pos knight1) (velocity knight1)) + (animate-sprite 'archer 0 (pos archer1) (velocity archer1)) (rl:draw-fps 10 5)) (defun game () @@ -83,7 +141,5 @@ (game-update) (rl:with-drawing (game-draw))) - (loop for value being the hash-values of (game-state-textures *game-state*) + (loop for value being the hash-values of *textures* do (rl:unload-texture value))))) - -(game)