Compare commits
2 Commits
2f9e663523
...
5c2753dd8b
| Author | SHA1 | Date | |
|---|---|---|---|
| 5c2753dd8b | |||
| 1fc4628cd0 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,3 +9,4 @@
|
||||
/libboids.so
|
||||
/game.fasl
|
||||
/writeimage
|
||||
*.fasl
|
||||
|
||||
@ -5,11 +5,8 @@ CC=gcc
|
||||
|
||||
all: main dod boids_main
|
||||
|
||||
main: main.c lib.h sprites.o sprites.h game_data.h ./lib/libraylib.a
|
||||
$(CC) $(CFLAGS) -Iinclude/ -lm main.c -o main sprites.o ./lib/libraylib.a
|
||||
|
||||
sprites.o: sprites.c sprites.h lib.h
|
||||
$(CC) $(CFLAGS) -c sprites.c -o sprites.o
|
||||
main: main.c lib.h sprites.h game_data.h ./lib/libraylib.a Makefile
|
||||
$(CC) $(CFLAGS) -Iinclude/ -lm main.c -o main ./lib/libraylib.a
|
||||
|
||||
dod: dod.c ./lib/libraylib.a
|
||||
$(CC) $(CFLAGS) -Iinclude/ -lm dod.c -o dod ./lib/libraylib.a
|
||||
|
||||
@ -1,29 +0,0 @@
|
||||
#include "include/raylib.h"
|
||||
#include "include/raymath.h"
|
||||
#include <stdio.h>
|
||||
#include "lib.h"
|
||||
|
||||
#define SCREEN_WIDTH 1300
|
||||
#define SCREEN_HEIGHT 1000
|
||||
#define TARGET_FPS 60
|
||||
|
||||
|
||||
int main(void) {
|
||||
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Boids");
|
||||
|
||||
SetTargetFPS(TARGET_FPS);
|
||||
|
||||
while (!WindowShouldClose()) {
|
||||
float dt = GetFrameTime();
|
||||
|
||||
// Update
|
||||
|
||||
BeginDrawing();
|
||||
{
|
||||
ClearBackground(RAYWHITE);
|
||||
}
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
CloseWindow();
|
||||
}
|
||||
@ -60,6 +60,34 @@ inline void D_DrawTextureV(Texture2D texture, Vector2 position, Color tint) {
|
||||
#endif
|
||||
}
|
||||
|
||||
// Vector2 operations
|
||||
#define v2_add Vector2Add
|
||||
#define v2_sub Vector2Subtract
|
||||
#define v2_mul Vector2Scale
|
||||
#define v2_div Vector2Divide
|
||||
#define v2_dot Vector2DotProduct
|
||||
#define v2_len Vector2Length
|
||||
#define v2_norm Vector2Normalize
|
||||
#define v2_dist Vector2Distance
|
||||
#define v2_lerp Vector2Lerp
|
||||
|
||||
// Vector3 operations
|
||||
#define v3_add Vector3Add
|
||||
#define v3_sub Vector3Subtract
|
||||
#define v3_mul Vector3Scale
|
||||
#define v3_div Vector3Divide
|
||||
#define v3_dot Vector3DotProduct
|
||||
#define v3_cross Vector3CrossProduct
|
||||
#define v3_len Vector3Length
|
||||
#define v3_norm Vector3Normalize
|
||||
#define v3_dist Vector3Distance
|
||||
#define v3_lerp Vector3Lerp
|
||||
|
||||
// Constructor-style macros
|
||||
#define v2(x, y) ((Vector2){x, y})
|
||||
#define v3(x, y, z) ((Vector3){x, y, z})
|
||||
|
||||
|
||||
inline void D_DrawTextureRec(Texture2D texture, Rectangle source, Vector2 position, Color tint) {
|
||||
DrawTextureRec(texture, source, position, tint);
|
||||
#ifdef DEBUG_MODE_ENABLED
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
|
||||
#define TEXTURES_BUF_SIZE 16
|
||||
#define TARGET_FPS 60
|
||||
#define MAX_KNIGHTS 10000
|
||||
#define MAX_KNIGHTS 5000
|
||||
#define SCREEN_WIDTH 1300
|
||||
#define SCREEN_HEIGHT 1080
|
||||
|
||||
@ -75,7 +75,7 @@ Assets Init() {
|
||||
|
||||
Assets assets = {0};
|
||||
assets.textures = malloc(sizeof(Texture2D) * TEXTURES_BUF_SIZE);
|
||||
assets.textures[TEX_GROUND] = LoadTexture(../"assets/Terrain/Ground/Tilemap_Flat.png");
|
||||
assets.textures[TEX_GROUND] = LoadTexture("../assets/Terrain/Ground/Tilemap_Flat.png");
|
||||
assets.textures[TEX_KNIGHT] =
|
||||
LoadTexture("../assets/Factions/Knights/Troops/Warrior/Blue/Warrior_Blue.png");
|
||||
assets.textures[TEX_MOUSE_CURSOR] = LoadTexture("../assets/UI/Pointers/01.png");
|
||||
@ -349,6 +349,7 @@ int main(void) {
|
||||
printf("Direction Size: %ld \n", sizeof(Direction));
|
||||
printf("KnightState Size: %ld \n", sizeof(KnightState));
|
||||
|
||||
SetTraceLogLevel(4);
|
||||
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tiny Knights");
|
||||
|
||||
int monitor = GetCurrentMonitor();
|
||||
|
||||
@ -1,31 +0,0 @@
|
||||
#include "sprites.h"
|
||||
#include "include/raylib.h"
|
||||
|
||||
inline void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimationPlayback *playback) {
|
||||
playback->time_elapsed = 0.0f;
|
||||
playback->current_frame = 0;
|
||||
playback->row = animation;
|
||||
playback->total_frames = anims[animation].total_frames;
|
||||
playback->loop = anims[animation].loop;
|
||||
}
|
||||
|
||||
void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len) {
|
||||
const f32 anim_speed = 1.0f / 10.0f;
|
||||
for (int i = 0; i < len; i++) {
|
||||
SpriteAnimationPlayback *playback = &playbacks[i];
|
||||
playback->time_elapsed += GetFrameTime();
|
||||
if (playback->time_elapsed >= anim_speed) {
|
||||
playback->time_elapsed = 0.0f;
|
||||
playback->current_frame++;
|
||||
if (playback->current_frame >= playback->total_frames) {
|
||||
if (playback->loop) {
|
||||
playback->current_frame = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline bool IsAnimationFinished(SpriteAnimationPlayback playback) {
|
||||
return playback.current_frame == playback.total_frames;
|
||||
}
|
||||
@ -20,3 +20,32 @@ typedef struct {
|
||||
void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimationPlayback *playback);
|
||||
void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len);
|
||||
bool IsAnimationFinished(SpriteAnimationPlayback playback);
|
||||
|
||||
inline void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimationPlayback *playback) {
|
||||
playback->time_elapsed = 0.0f;
|
||||
playback->current_frame = 0;
|
||||
playback->row = animation;
|
||||
playback->total_frames = anims[animation].total_frames;
|
||||
playback->loop = anims[animation].loop;
|
||||
}
|
||||
|
||||
void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len) {
|
||||
const f32 anim_speed = 1.0f / 10.0f;
|
||||
for (int i = 0; i < len; i++) {
|
||||
SpriteAnimationPlayback *playback = &playbacks[i];
|
||||
playback->time_elapsed += GetFrameTime();
|
||||
if (playback->time_elapsed >= anim_speed) {
|
||||
playback->time_elapsed = 0.0f;
|
||||
playback->current_frame++;
|
||||
if (playback->current_frame >= playback->total_frames) {
|
||||
if (playback->loop) {
|
||||
playback->current_frame = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline bool IsAnimationFinished(SpriteAnimationPlayback playback) {
|
||||
return playback.current_frame == playback.total_frames;
|
||||
}
|
||||
|
||||
@ -2,6 +2,104 @@ You will find detailed installation instructions in the ECL manual
|
||||
https://common-lisp.net/project/ecl/static/manual/Building-ECL.html
|
||||
If you do not have access to the online version, follow the following recipies.
|
||||
|
||||
* Cross-compile for the WASM platform (via emscripten)
|
||||
|
||||
Emscripten target is a little fickle so keep in mind that:
|
||||
|
||||
- shared libraries are supported but come with some drawbacks (e.g.
|
||||
increased code size if used together with -sASYNCIFY), therefore the
|
||||
build instructions default to disable-shared
|
||||
|
||||
- disable-tcp is needed, because accept can't be found (lack of
|
||||
-lsockets or something in this spirit?)
|
||||
|
||||
- select for interactive streams does not work, because reading operations are
|
||||
blocking (as they should be!), so there is no EOF returned -- clear-input will
|
||||
hang without a proper file-cnt
|
||||
|
||||
- to build emscripten you need to use their SDK that provides the toolchain, and
|
||||
set the environment variable EMSDK_PATH
|
||||
|
||||
- for the garbage collector to be able to identify roots on the stack,
|
||||
you need to pass the -sBINARYEN_EXTRA_PASSES=--spill-pointers option
|
||||
to the linker for all of your code that might store pointer on the
|
||||
stack (for instance when embedding ECL)
|
||||
|
||||
- the optimization level -O0 is used because higher optimization
|
||||
levels seem to interfere with the binaryen options needed to get the
|
||||
garbage collector to work correctly and tend slow down the program
|
||||
(might be worth experimenting with the optimization options)
|
||||
|
||||
1. Build the host ECL
|
||||
|
||||
#+begin_src shell-script
|
||||
./configure ABI=32 CFLAGS="-m32 -g -O2 -DECL_C_COMPATIBLE_VARIADIC_DISPATCH" LDFLAGS="-m32 -g -O2" \
|
||||
--prefix=`pwd`/ecl-emscripten-host --disable-threads
|
||||
|
||||
make -j16 && make install
|
||||
rm -rf build/
|
||||
#+end_src
|
||||
|
||||
2. Configure the toolchain
|
||||
|
||||
Install the Emscripten SDK using the official instructions:
|
||||
|
||||
https://emscripten.org/docs/getting_started/downloads.html
|
||||
|
||||
These build instructions were tested against ~emsdk 3.1.41~. If things doesn't
|
||||
work try that version instead of ~latest~.
|
||||
|
||||
After that activate the toolchain and configure build flags:
|
||||
|
||||
#+begin_src shell-script
|
||||
source ${EMSDK_PATH}/emsdk_env.sh
|
||||
export ECL_TO_RUN=`pwd`/ecl-emscripten-host/bin/ecl
|
||||
# You may customize various emscripten flags here, i.e:
|
||||
# export LDFLAGS="-sASYNCIFY=1"
|
||||
#+end_src
|
||||
|
||||
3. Build the core environment and install it
|
||||
|
||||
#+begin_src shell-script
|
||||
emconfigure ./configure \
|
||||
--host=wasm32-unknown-emscripten \
|
||||
--build=x86_64-pc-linux-gnu \
|
||||
--with-cross-config=`pwd`/src/util/wasm32-unknown-emscripten.cross_config \
|
||||
--prefix=`pwd`/ecl-emscripten \
|
||||
--disable-shared \
|
||||
--with-tcp=no \
|
||||
--with-cmp=no
|
||||
|
||||
emmake make && emmake make install
|
||||
|
||||
# some files need to be copied manually
|
||||
cp build/bin/ecl.js build/bin/ecl.wasm ecl-emscripten/
|
||||
#+end_src
|
||||
|
||||
4. ECL may be hosted on a web page. Assuming that you have quicklisp installed:
|
||||
|
||||
#+begin_src shell-script
|
||||
export WEBSERVER=`pwd`/src/util/webserver.lisp
|
||||
pushd ecl-emscripten/
|
||||
lisp --load $WEBSERVER
|
||||
# After the server is loaded run:
|
||||
# firefox localhost:8888/ecl.html
|
||||
popd
|
||||
#+end_src
|
||||
|
||||
If the output does not show on the webpage then open the javascript console.
|
||||
This is a default html website produced by emscripten.
|
||||
|
||||
5. Build an external program linked against libecl.a
|
||||
|
||||
The default stack size proposed by emscripten is 64KB. This is too little for
|
||||
ECL, so when you build a program that is linked against libecl.a, then it is
|
||||
imoprtant to specify a different size. For example:
|
||||
|
||||
#+begin_src shell-script
|
||||
emcc program.c -sSTACK_SIZE=1048576 lib/*.a -I./include -o program.o
|
||||
#+end_src
|
||||
|
||||
* Unix and similar platforms.
|
||||
1. Type
|
||||
./configure --help
|
||||
@ -111,104 +209,6 @@ Hint provided by Pascal J. Bourguignon.
|
||||
4. Library and assets in the ecl-iOS directory are ready to run on
|
||||
the iOS system.
|
||||
|
||||
* Cross-compile for the WASM platform (via emscripten)
|
||||
|
||||
Emscripten target is a little fickle so keep in mind that:
|
||||
|
||||
- shared libraries are supported but come with some drawbacks (e.g.
|
||||
increased code size if used together with -sASYNCIFY), therefore the
|
||||
build instructions default to disable-shared
|
||||
|
||||
- disable-tcp is needed, because accept can't be found (lack of
|
||||
-lsockets or something in this spirit?)
|
||||
|
||||
- select for interactive streams does not work, because reading operations are
|
||||
blocking (as they should be!), so there is no EOF returned -- clear-input will
|
||||
hang without a proper file-cnt
|
||||
|
||||
- to build emscripten you need to use their SDK that provides the toolchain, and
|
||||
set the environment variable EMSDK_PATH
|
||||
|
||||
- for the garbage collector to be able to identify roots on the stack,
|
||||
you need to pass the -sBINARYEN_EXTRA_PASSES=--spill-pointers option
|
||||
to the linker for all of your code that might store pointer on the
|
||||
stack (for instance when embedding ECL)
|
||||
|
||||
- the optimization level -O0 is used because higher optimization
|
||||
levels seem to interfere with the binaryen options needed to get the
|
||||
garbage collector to work correctly and tend slow down the program
|
||||
(might be worth experimenting with the optimization options)
|
||||
|
||||
1. Build the host ECL
|
||||
|
||||
#+begin_src shell-script
|
||||
./configure ABI=32 CFLAGS="-m32 -g -O2 -DECL_C_COMPATIBLE_VARIADIC_DISPATCH" LDFLAGS="-m32 -g -O2" \
|
||||
--prefix=`pwd`/ecl-emscripten-host --disable-threads
|
||||
|
||||
make -j16 && make install
|
||||
rm -rf build/
|
||||
#+end_src
|
||||
|
||||
2. Configure the toolchain
|
||||
|
||||
Install the Emscripten SDK using the official instructions:
|
||||
|
||||
https://emscripten.org/docs/getting_started/downloads.html
|
||||
|
||||
These build instructions were tested against ~emsdk 3.1.41~. If things doesn't
|
||||
work try that version instead of ~latest~.
|
||||
|
||||
After that activate the toolchain and configure build flags:
|
||||
|
||||
#+begin_src shell-script
|
||||
source ${EMSDK_PATH}/emsdk_env.sh
|
||||
export ECL_TO_RUN=`pwd`/ecl-emscripten-host/bin/ecl
|
||||
# You may customize various emscripten flags here, i.e:
|
||||
# export LDFLAGS="-sASYNCIFY=1"
|
||||
#+end_src
|
||||
|
||||
3. Build the core environment and install it
|
||||
|
||||
#+begin_src shell-script
|
||||
emconfigure ./configure \
|
||||
--host=wasm32-unknown-emscripten \
|
||||
--build=x86_64-pc-linux-gnu \
|
||||
--with-cross-config=`pwd`/src/util/wasm32-unknown-emscripten.cross_config \
|
||||
--prefix=`pwd`/ecl-emscripten \
|
||||
--disable-shared \
|
||||
--with-tcp=no \
|
||||
--with-cmp=no
|
||||
|
||||
emmake make && emmake make install
|
||||
|
||||
# some files need to be copied manually
|
||||
cp build/bin/ecl.js build/bin/ecl.wasm ecl-emscripten/
|
||||
#+end_src
|
||||
|
||||
4. ECL may be hosted on a web page. Assuming that you have quicklisp installed:
|
||||
|
||||
#+begin_src shell-script
|
||||
export WEBSERVER=`pwd`/src/util/webserver.lisp
|
||||
pushd ecl-emscripten/
|
||||
lisp --load $WEBSERVER
|
||||
# After the server is loaded run:
|
||||
# firefox localhost:8888/ecl.html
|
||||
popd
|
||||
#+end_src
|
||||
|
||||
If the output does not show on the webpage then open the javascript console.
|
||||
This is a default html website produced by emscripten.
|
||||
|
||||
5. Build an external program linked against libecl.a
|
||||
|
||||
The default stack size proposed by emscripten is 64KB. This is too little for
|
||||
ECL, so when you build a program that is linked against libecl.a, then it is
|
||||
imoprtant to specify a different size. For example:
|
||||
|
||||
#+begin_src shell-script
|
||||
emcc program.c -sSTACK_SIZE=1048576 lib/*.a -I./include -o program.o
|
||||
#+end_src
|
||||
|
||||
* Build using Cosmopolitan toolchain (experimental)
|
||||
|
||||
Binaries built with cosmopolitan toolchain can be executed on numerous platforms
|
||||
@ -1,5 +1,5 @@
|
||||
(eval-when (:compile-toplevel :load-toplevel :execute)
|
||||
(load "~/.local/share/quicklisp/setup.lisp")
|
||||
;; (load "~/.local/share/quicklisp/setup.lisp")
|
||||
(ql:quickload :cl-raylib)
|
||||
(ql:quickload :livesupport))
|
||||
|
||||
@ -10,6 +10,19 @@
|
||||
(in-package :raylib-user)
|
||||
|
||||
(defparameter *move-speed* 5.0)
|
||||
(defparameter *tower-pos* (vec 80 150))
|
||||
(defparameter *tower-center* (vec 64 220))
|
||||
(defparameter *assets-path* "~/Development/tinyswords/assets/")
|
||||
(defparameter *camera* (rl:make-camera2d :offset (vec 0 0) :target (vec 0 0) :rotation 0.0 :zoom 1.0))
|
||||
|
||||
(defparameter *mouse-drag-start-pos* (vec 0 0))
|
||||
(defparameter *camera-drag-start-pos* (vec 0 0))
|
||||
(defparameter *unit-select-drag-start-pos* (vec 0 0))
|
||||
(defparameter *camera-dragging?* nil)
|
||||
(defparameter *unit-select-dragging?* nil)
|
||||
|
||||
(defparameter *game-state* nil)
|
||||
(defparameter *textures* (make-hash-table))
|
||||
|
||||
(defclass health ()
|
||||
((current-amount :initform 100.0 :accessor current-amount)
|
||||
@ -29,6 +42,7 @@
|
||||
(defclass soldier ()
|
||||
((pos :initarg :pos :initform (vec 0 0) :accessor pos)
|
||||
(anim-state :initform 0 :accessor anim-state)
|
||||
(entity-type :initarg :entity-type :initform nil :accessor entity-type)
|
||||
(sprite :initarg :sprite :initform (make-instance 'sprite-class) :accessor sprite)
|
||||
(velocity :initform (vec 0 0) :accessor velocity)
|
||||
(health :initarg :health :initform (make-instance 'health) :accessor health)))
|
||||
@ -36,6 +50,7 @@
|
||||
(defparameter *knight*
|
||||
(make-instance
|
||||
'soldier
|
||||
:entity-type ':soldier
|
||||
:pos (vec 270 360)
|
||||
:sprite (make-instance
|
||||
'sprite-class
|
||||
@ -46,6 +61,7 @@
|
||||
(defparameter *archer*
|
||||
(make-instance
|
||||
'soldier
|
||||
:entity-type :archer
|
||||
:pos (vec 145.0 253.0)
|
||||
:sprite (make-instance
|
||||
'sprite-class
|
||||
@ -53,6 +69,17 @@
|
||||
:src-rect (rl:make-rectangle :x 0.0 :y 0.0 :width 192.0 :height 192.0)
|
||||
:origin (vec 95.0 128.0))))
|
||||
|
||||
(defparameter *goblin*
|
||||
(make-instance
|
||||
'soldier
|
||||
:entity-type :goblin
|
||||
:pos (vec 445.0 300.0)
|
||||
:sprite (make-instance
|
||||
'sprite-class
|
||||
:texture-key 'goblin
|
||||
: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))))
|
||||
@ -63,26 +90,44 @@
|
||||
((click-pos :initform nil :accessor click-pos)
|
||||
(entities :initform '() :accessor entities)))
|
||||
|
||||
(defparameter *game-state* nil)
|
||||
(defparameter *textures* (make-hash-table))
|
||||
|
||||
(defun bind-texture (key path)
|
||||
(setf (gethash key *textures*) (rl:load-texture (uiop:native-namestring path))))
|
||||
(setf (gethash key *textures*) (rl:load-texture (uiop:native-namestring (merge-pathnames path *assets-path*)))))
|
||||
|
||||
(defun game-init ()
|
||||
(setf *game-state* (make-instance 'game-state))
|
||||
(setf (entities *game-state*) (list *knight* *archer*))
|
||||
(setf (entities *game-state*) (list *knight* *archer* *goblin*))
|
||||
;; (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"))
|
||||
(bind-texture 'terrain "Terrain/Ground/Tilemap_Flat.png")
|
||||
(bind-texture 'knight "Factions/Knights/Troops/Warrior/Blue/Warrior_Blue.png")
|
||||
(bind-texture 'archer "Factions/Knights/Troops/Archer/Blue/Archer_Blue.png")
|
||||
(bind-texture 'tower "Factions/Knights/Buildings/Tower/Tower_Blue.png")
|
||||
(bind-texture 'goblin "Factions/Goblins/Troops/Torch/Red/Torch_Red.png"))
|
||||
|
||||
(defun game-input ()
|
||||
(when (rl:is-mouse-button-pressed 0)
|
||||
(setf (click-pos *game-state*) (print (rl:get-mouse-position))))
|
||||
(when (rl:is-mouse-button-pressed 1)
|
||||
(setf (click-pos *game-state*) (print (rl:get-mouse-position)))))
|
||||
(if (rl:is-key-down :key-left-control)
|
||||
(progn
|
||||
(setf *camera-dragging?* t)
|
||||
(v<- *camera-drag-start-pos* (rl:camera2d-target *camera*)))
|
||||
(progn
|
||||
(setf *unit-select-dragging?* t)
|
||||
(v<- *unit-select-drag-start-pos* (rl:camera2d-target *camera*))))
|
||||
(v<- *mouse-drag-start-pos* (rl:get-mouse-position)))
|
||||
;; (princ (rl:get-mouse-wheel-move))
|
||||
(setf (rl:camera2d-zoom *camera*)
|
||||
(alexandria:clamp (exp (+ (log (rl:camera2d-zoom *camera*))
|
||||
(* (rl:get-mouse-wheel-move) 0.1)))
|
||||
1.0 2.0))
|
||||
(when (rl:is-mouse-button-released 0)
|
||||
(setf *camera-dragging?* nil)
|
||||
(setf *unit-select-dragging?* nil))
|
||||
(when (rl:is-key-down :key-r) (setf (rl:camera2d-target *camera*) (vec 0 0))))
|
||||
|
||||
;; (defun game-input ()
|
||||
;; (when (rl:is-mouse-button-pressed 0)
|
||||
;; (setf (click-pos *game-state*) (print (rl:get-mouse-position))))
|
||||
;; (when (rl:is-mouse-button-pressed 1)
|
||||
;; (setf (click-pos *game-state*) (print (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))
|
||||
@ -91,7 +136,22 @@
|
||||
;; (when (rl:is-key-down :key-down) (incf dy 1))
|
||||
;; (setf pos (v+ pos (v* (vunit* (vec dx dy)) *move-speed*))))))
|
||||
|
||||
(defun goblin-fsm ()
|
||||
(dolist (entity (entities *game-state*))
|
||||
(when (eq (entity-type entity) :goblin)
|
||||
(let* ((target-dist (v- (v+ *tower-pos* *tower-center*) (pos entity)))
|
||||
(dir (vunit* target-dist)))
|
||||
(setf (anim-idx (sprite entity)) 1)
|
||||
(setf (pos entity) (v+ (pos entity) (v* dir 1)))))))
|
||||
|
||||
(defun calculate-camera-drag ()
|
||||
(let* ((target-pos (v- (rl:get-mouse-position)
|
||||
*mouse-drag-start-pos*)))
|
||||
(setf (rl:camera2d-target *camera*) (v+ *camera-drag-start-pos* (v* -1 target-pos)))))
|
||||
|
||||
(defun game-update ()
|
||||
(when *camera-dragging?*
|
||||
(calculate-camera-drag))
|
||||
(with-accessors ((click-pos click-pos)) *game-state*
|
||||
(when click-pos
|
||||
(with-accessors ((kpos pos)) *knight*
|
||||
@ -103,10 +163,11 @@
|
||||
(setf kpos click-pos)
|
||||
;; (setf (velocity *knight*) (vec 0 0))
|
||||
(setf click-pos nil))
|
||||
(let ((vel (v* (* (rl:get-frame-time) 150.0) (vunit* dist))))
|
||||
(let ((vel (v* (* (rl:get-frame-time) 240.0) (vunit* dist))))
|
||||
(setf (anim-idx (sprite *knight*)) 1)
|
||||
(setf (velocity *knight*) vel)
|
||||
(setf kpos (v+ kpos vel))))))))
|
||||
(goblin-fsm)
|
||||
(dolist (entity (entities *game-state*))
|
||||
(tick-sprite-animation (sprite entity) (< (vx (velocity entity)) 0.0))))
|
||||
|
||||
@ -157,22 +218,36 @@
|
||||
:height (abs (rl:rectangle-height src-rect)))))
|
||||
(rl:draw-texture-pro (gethash texture-key *textures*) src-rect dst-rec origin 0.0 :white))))
|
||||
|
||||
(defun draw-unit-select-rect ()
|
||||
(let* ((start-x (vx *mouse-drag-start-pos*))
|
||||
(start-y (vy *mouse-drag-start-pos*))
|
||||
(end-x (rl:get-mouse-x))
|
||||
(end-y (rl:get-mouse-y))
|
||||
(x (min start-x end-x))
|
||||
(y (min start-y end-y))
|
||||
(rect (rl:make-rectangle :x x :y y :width (abs (- end-x start-x)) :height (abs (- end-y start-y)))))
|
||||
(rl:draw-rectangle-lines-ex rect 2.0 :raywhite)))
|
||||
|
||||
(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)
|
||||
(dolist (entity (entities *game-state*))
|
||||
(draw-sprite (sprite entity) (pos entity)))
|
||||
(rl:with-mode-2d (*camera*)
|
||||
(draw-ground)
|
||||
(rl:draw-texture-v (gethash 'tower *textures*) *tower-pos* :white)
|
||||
(dolist (entity (entities *game-state*))
|
||||
(draw-sprite (sprite entity) (pos entity))))
|
||||
(when *unit-select-dragging?*
|
||||
(draw-unit-select-rect))
|
||||
(rl:draw-fps 10 10))
|
||||
|
||||
(defun game ()
|
||||
(let* ((screen-width 900)
|
||||
(screen-height 500))
|
||||
(let* ((screen-width 1600)
|
||||
(screen-height 900))
|
||||
(rl:with-window (screen-width screen-height "RTS")
|
||||
(rl:set-target-fps 240)
|
||||
(rl:set-target-fps 60)
|
||||
(game-init)
|
||||
(loop :until (rl:window-should-close)
|
||||
:do (livesupport:continuable (game-input)
|
||||
:do (livesupport:continuable
|
||||
(game-input)
|
||||
(game-update)
|
||||
(rl:with-drawing
|
||||
(game-draw))))
|
||||
Loading…
x
Reference in New Issue
Block a user