Render the archer, animation function

This commit is contained in:
Joseph Ferano 2024-11-15 21:46:35 +07:00
parent 3b0ee06adc
commit 0d97dd896f

View File

@ -1,6 +1,5 @@
(load "~/quicklisp/setup.lisp")
(eval-when (:compile-toplevel :load-toplevel :execute) (eval-when (:compile-toplevel :load-toplevel :execute)
(load "~/.local/share/quicklisp/setup.lisp")
(ql:quickload :cl-raylib)) (ql:quickload :cl-raylib))
(defpackage :raylib-user (defpackage :raylib-user
@ -9,11 +8,12 @@
(in-package :raylib-user) (in-package :raylib-user)
(defparameter *move-speed* 2.0) (defparameter *move-speed* 5.0)
(defstruct game-state (defstruct game-state
(textures (make-hash-table)) (textures (make-hash-table))
(player-pos (vec 100 100))) (knight-pos (vec 150.0 250.0))
(archer-pos (vec 50.0 122.0)))
(defparameter *game-state* nil) (defparameter *game-state* nil)
@ -24,10 +24,11 @@
(setf *game-state* (make-game-state)) (setf *game-state* (make-game-state))
(bind-texture 'terrain "~/Development/tinyswords/assets/Terrain/Ground/Tilemap_Flat.png") (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 '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")) (bind-texture 'tower "~/Development/tinyswords/assets/Factions/Knights/Buildings/Tower/Tower_Blue.png"))
(defun game-input (state) (defun game-input ()
(with-slots ((pos player-pos)) state (with-slots ((pos archer-pos)) *game-state*
(let ((dx 0.0) (dy 0.0)) (let ((dx 0.0) (dy 0.0))
(when (rl:is-key-down :key-right) (incf dx 1)) (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-left) (decf dx 1))
@ -35,7 +36,7 @@
(when (rl:is-key-down :key-down) (incf dy 1)) (when (rl:is-key-down :key-down) (incf dy 1))
(setf pos (v+ pos (v* (vunit* (vec dx dy)) *move-speed*)))))) (setf pos (v+ pos (v* (vunit* (vec dx dy)) *move-speed*))))))
(defun game-update (state) '()) (defun game-update () '())
(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))
@ -56,16 +57,19 @@
do (loop for col from 0 to 32 do (loop for col from 0 to 32
do (draw-tile terrain-tex size col row))))) do (draw-tile terrain-tex size col row)))))
(defun animate-sprite ()) (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*))
(rl:make-rectangle :x (* time-interval 192.0) :y (* row 192)
:width 192.0 :height 192.0)
pos :white)))
(defun game-draw (state) (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 (game-state-textures state)) (vec 80 150) :white) (rl:draw-texture-v (gethash 'tower (game-state-textures *game-state*)) (vec 80 150) :white)
(rl:draw-texture-rec (gethash 'knight (game-state-textures state)) (animate-sprite 'knight 0 (game-state-knight-pos *game-state*))
(rl:make-rectangle :x 0.0 :y 0.0 :width 192.0 :height 192.0) (animate-sprite 'archer 0 (game-state-archer-pos *game-state*))
(vec 0 0)
:white)
(rl:draw-fps 10 5)) (rl:draw-fps 10 5))
(defun game () (defun game ()
@ -75,10 +79,10 @@
(rl:set-target-fps 60) (rl:set-target-fps 60)
(game-init) (game-init)
(loop :until (rl:window-should-close) (loop :until (rl:window-should-close)
:do (game-input *game-state*) :do (game-input)
(game-update *game-state*) (game-update)
(rl:with-drawing (rl:with-drawing
(game-draw *game-state*))) (game-draw)))
(loop for value being the hash-values of (game-state-textures *game-state*) (loop for value being the hash-values of (game-state-textures *game-state*)
do (rl:unload-texture value))))) do (rl:unload-texture value)))))