WIP: Multiple knights after refactoring data structures and creating loops
This commit is contained in:
parent
9dd7727c92
commit
1dda2f9315
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@
|
|||||||
*.aseprite
|
*.aseprite
|
||||||
/.idea/
|
/.idea/
|
||||||
/sprites.o
|
/sprites.o
|
||||||
|
/dod
|
||||||
|
7
Makefile
7
Makefile
@ -1,9 +1,9 @@
|
|||||||
CFLAGS=-g -Wall -Wextra -pedantic -O0
|
CFLAGS=-g -fsanitize=address -Wall -Wextra -pedantic -O0
|
||||||
CC=gcc
|
CC=gcc
|
||||||
|
|
||||||
.PHONY: build clean run all
|
.PHONY: build clean run all
|
||||||
|
|
||||||
all: main
|
all: main dod
|
||||||
|
|
||||||
main: main.c sprites.o sprites.h game_data.h ./lib/libraylib.a
|
main: main.c sprites.o sprites.h game_data.h ./lib/libraylib.a
|
||||||
$(CC) $(CFLAGS) -Iinclude/ -lm main.c -o main sprites.o ./lib/libraylib.a
|
$(CC) $(CFLAGS) -Iinclude/ -lm main.c -o main sprites.o ./lib/libraylib.a
|
||||||
@ -11,6 +11,9 @@ main: main.c sprites.o sprites.h game_data.h ./lib/libraylib.a
|
|||||||
sprites.o: sprites.c sprites.h lib.h
|
sprites.o: sprites.c sprites.h lib.h
|
||||||
$(CC) $(CFLAGS) -c sprites.c -o sprites.o
|
$(CC) $(CFLAGS) -c sprites.c -o sprites.o
|
||||||
|
|
||||||
|
dod: dod.c ./lib/libraylib.a
|
||||||
|
$(CC) $(CFLAGS) -Iinclude/ -lm dod.c -o dod ./lib/libraylib.a
|
||||||
|
|
||||||
run: all
|
run: all
|
||||||
./main
|
./main
|
||||||
|
|
||||||
|
75
dod.c
Normal file
75
dod.c
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
#define ENTITY_COUNT 10000000
|
||||||
|
#define TEST_DOD
|
||||||
|
|
||||||
|
typedef struct DoDState
|
||||||
|
{
|
||||||
|
Vector2* positions;
|
||||||
|
Color* colors;
|
||||||
|
Vector2* outputs;
|
||||||
|
} DoDState;
|
||||||
|
|
||||||
|
typedef struct ObjState
|
||||||
|
{
|
||||||
|
Vector2 position;
|
||||||
|
Color color;
|
||||||
|
Vector2 output;
|
||||||
|
} ObjState;
|
||||||
|
|
||||||
|
ObjState objects[ENTITY_COUNT];
|
||||||
|
|
||||||
|
Vector2 positions[ENTITY_COUNT];
|
||||||
|
Color colors[ENTITY_COUNT];
|
||||||
|
Vector2 outputs[ENTITY_COUNT];
|
||||||
|
|
||||||
|
|
||||||
|
void AWESOME_FUNCTION(void)
|
||||||
|
{
|
||||||
|
#ifdef TEST_DOD
|
||||||
|
for (int i = 0; i < ENTITY_COUNT; i++)
|
||||||
|
{
|
||||||
|
const Vector2 output = (Vector2){
|
||||||
|
// positions[i] = (Vector2){
|
||||||
|
positions[i].x * 2,
|
||||||
|
positions[i].y * 2,
|
||||||
|
};
|
||||||
|
outputs[i] = output;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
for (int i = 0; i < ENTITY_COUNT; i++)
|
||||||
|
{
|
||||||
|
const Vector2 output = (Vector2){
|
||||||
|
objects[i].position.x * objects[i].color.r,
|
||||||
|
objects[i].position.y * objects[i].color.b,
|
||||||
|
};
|
||||||
|
objects[i].output = output;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// Populate the data
|
||||||
|
int r = rand();
|
||||||
|
for (int i = 0; i < ENTITY_COUNT; i++)
|
||||||
|
{
|
||||||
|
positions[i].x = (float)r / RAND_MAX;
|
||||||
|
positions[i].y = (float)r / RAND_MAX;
|
||||||
|
|
||||||
|
colors[i].r = r % 256;
|
||||||
|
colors[i].g = r % 256;
|
||||||
|
colors[i].b = r % 256;
|
||||||
|
colors[i].a = r % 256;
|
||||||
|
|
||||||
|
objects[i].position = positions[i];
|
||||||
|
objects[i].color = colors[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
AWESOME_FUNCTION();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
38
game_data.h
38
game_data.h
@ -9,78 +9,62 @@
|
|||||||
#define ANIM_KNIGHT_ATTACK_BACK1 6
|
#define ANIM_KNIGHT_ATTACK_BACK1 6
|
||||||
#define ANIM_KNIGHT_ATTACK_BACK2 7
|
#define ANIM_KNIGHT_ATTACK_BACK2 7
|
||||||
|
|
||||||
const u16 cell_size = 192;
|
// SpriteSheet knight_spritesheet = {
|
||||||
SpriteSheet knight = {
|
// .name = "blue_knight",
|
||||||
.name = "blue_knight",
|
// .texture = {0},
|
||||||
.texture = {0},
|
// // .anim_count = 8,
|
||||||
// .anim_count = 8,
|
// .size = { 1152, 1536 },
|
||||||
.size = { 1152, 1536 },
|
// };
|
||||||
};
|
|
||||||
|
|
||||||
const Vector2 knight_origin = {cell_size / 2, cell_size / 2};
|
const u16 knight_sprite_size = 192;
|
||||||
const Size knight_col_size = {75, 100};
|
const Vector2 knight_origin = {knight_sprite_size / 2, knight_sprite_size / 2 + 35};
|
||||||
|
const Size knight_col_size = {85, 95};
|
||||||
// TODO: We probably want this as a helper function somewhere
|
// TODO: We probably want this as a helper function somewhere
|
||||||
const Rectangle knight_colrect_select = {
|
const Rectangle knight_colrect_select = {
|
||||||
knight_origin.x - knight_col_size.width / 2,
|
-knight_col_size.width / 2,
|
||||||
knight_origin.y - knight_col_size.height / 2,
|
-knight_col_size.height / 2 - 40,
|
||||||
knight_col_size.width,
|
knight_col_size.width,
|
||||||
knight_col_size.height,
|
knight_col_size.height,
|
||||||
};
|
};
|
||||||
|
|
||||||
SpriteAnimation knight_idle = {
|
SpriteAnimation knight_idle = {
|
||||||
.name = "idle",
|
|
||||||
.total_frames = 6,
|
.total_frames = 6,
|
||||||
.loop = true,
|
.loop = true,
|
||||||
.src_rect = { 0, cell_size * 0, cell_size, cell_size },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SpriteAnimation knight_run = {
|
SpriteAnimation knight_run = {
|
||||||
.name = "run",
|
|
||||||
.total_frames = 6,
|
.total_frames = 6,
|
||||||
.loop = true,
|
.loop = true,
|
||||||
.src_rect = { 0, cell_size * 1, cell_size, cell_size },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SpriteAnimation knight_attack_side1 = {
|
SpriteAnimation knight_attack_side1 = {
|
||||||
.name = "attack_side1",
|
|
||||||
.total_frames = 6,
|
.total_frames = 6,
|
||||||
.loop = false,
|
.loop = false,
|
||||||
.src_rect = { 0, cell_size * 2, cell_size, cell_size },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SpriteAnimation knight_attack_side2 = {
|
SpriteAnimation knight_attack_side2 = {
|
||||||
.name = "attack_side2",
|
|
||||||
.total_frames = 6,
|
.total_frames = 6,
|
||||||
.loop = false,
|
.loop = false,
|
||||||
.src_rect = { 0, cell_size * 3, cell_size, cell_size },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SpriteAnimation knight_attack_front1 = {
|
SpriteAnimation knight_attack_front1 = {
|
||||||
.name = "attack_front1",
|
|
||||||
.total_frames = 6,
|
.total_frames = 6,
|
||||||
.loop = false,
|
.loop = false,
|
||||||
.src_rect = { 0, cell_size * 4, cell_size, cell_size },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SpriteAnimation knight_attack_front2 = {
|
SpriteAnimation knight_attack_front2 = {
|
||||||
.name = "attack_front2",
|
|
||||||
.total_frames = 6,
|
.total_frames = 6,
|
||||||
.loop = false,
|
.loop = false,
|
||||||
.src_rect = { 0, cell_size * 5, cell_size, cell_size },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SpriteAnimation knight_attack_back1 = {
|
SpriteAnimation knight_attack_back1 = {
|
||||||
.name = "attack_back1",
|
|
||||||
.total_frames = 6,
|
.total_frames = 6,
|
||||||
.loop = false,
|
.loop = false,
|
||||||
.src_rect = { 0, cell_size * 6, cell_size, cell_size },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SpriteAnimation knight_attack_back2 = {
|
SpriteAnimation knight_attack_back2 = {
|
||||||
.name = "attack_back2",
|
|
||||||
.total_frames = 6,
|
.total_frames = 6,
|
||||||
.loop = false,
|
.loop = false,
|
||||||
.src_rect = { 0, cell_size * 7, cell_size, cell_size },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SpriteAnimation knight_anims[8];
|
SpriteAnimation knight_anims[8];
|
||||||
|
21
lib.h
21
lib.h
@ -4,17 +4,15 @@
|
|||||||
#include "include/raylib.h"
|
#include "include/raylib.h"
|
||||||
|
|
||||||
typedef uint8_t u8;
|
typedef uint8_t u8;
|
||||||
// typedef char16_t c16;
|
|
||||||
typedef uint16_t u16;
|
typedef uint16_t u16;
|
||||||
typedef int16_t i16;
|
typedef int16_t i16;
|
||||||
typedef int32_t b32;
|
|
||||||
typedef int32_t i32;
|
typedef int32_t i32;
|
||||||
typedef uint32_t u32;
|
typedef uint32_t u32;
|
||||||
typedef uint64_t u64;
|
typedef uint64_t u64;
|
||||||
typedef float f32;
|
typedef float f32;
|
||||||
typedef double f64;
|
typedef double f64;
|
||||||
typedef uintptr_t uptr;
|
typedef uintptr_t uptr;
|
||||||
typedef char byte;
|
typedef char sbyte;
|
||||||
typedef ptrdiff_t size;
|
typedef ptrdiff_t size;
|
||||||
typedef size_t usize;
|
typedef size_t usize;
|
||||||
|
|
||||||
@ -35,18 +33,21 @@ typedef union {
|
|||||||
Rectangle rect;
|
Rectangle rect;
|
||||||
} RectU;
|
} RectU;
|
||||||
|
|
||||||
typedef struct PointOrNone {
|
#define OPTION(type, typeName) \
|
||||||
enum {NONE, POINT} type;
|
struct { \
|
||||||
union {
|
enum {NONE, SOME} tag; \
|
||||||
byte none;
|
union { \
|
||||||
Point point;
|
u8 none; \
|
||||||
} value;
|
type typeName; \
|
||||||
} PointOrNone;
|
} some; \
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef OPTION(Point, point) PointOption;
|
||||||
|
|
||||||
void D_DrawTextureRec(Texture2D texture, Rectangle source, Vector2 position, Color tint);
|
void D_DrawTextureRec(Texture2D texture, Rectangle source, Vector2 position, Color tint);
|
||||||
void D_DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint);
|
void D_DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint);
|
||||||
void D_DrawTextureV(Texture2D texture, Vector2 position, Color tint);
|
void D_DrawTextureV(Texture2D texture, Vector2 position, Color tint);
|
||||||
|
|
||||||
#ifdef ENNIX_LIB_IMPLEMENTATION
|
#ifdef ENNIX_LIB_IMPLEMENTATION
|
||||||
extern bool global_debug_mode;
|
extern bool global_debug_mode;
|
||||||
inline void D_DrawTextureV(Texture2D texture, Vector2 position, Color tint) {
|
inline void D_DrawTextureV(Texture2D texture, Vector2 position, Color tint) {
|
||||||
|
153
main.c
153
main.c
@ -6,9 +6,7 @@
|
|||||||
|
|
||||||
#define TEXTURES_BUF_SIZE 16
|
#define TEXTURES_BUF_SIZE 16
|
||||||
#define TARGET_FPS 60
|
#define TARGET_FPS 60
|
||||||
#define ANIM_SPEED 8
|
#define MAX_KNIGHTS 256
|
||||||
#define MAX_ANIMATION_PLAYBACKS 64
|
|
||||||
#define MAX_KNIGHTS 64
|
|
||||||
|
|
||||||
#define DEBUG_MODE_ENABLED
|
#define DEBUG_MODE_ENABLED
|
||||||
|
|
||||||
@ -46,11 +44,11 @@ typedef struct {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int frame_count;
|
int frame_count;
|
||||||
Knight knight;
|
|
||||||
// Rectangle* sprite_rects;
|
// Rectangle* sprite_rects;
|
||||||
Point camera_position;
|
Point camera_position;
|
||||||
Knight *selected_knight;
|
Knight *selected_knight;
|
||||||
PointOrNone *target_points;
|
PointOption *target_points;
|
||||||
|
Knight *knights;
|
||||||
SpriteAnimationPlayback* anim_playbacks;
|
SpriteAnimationPlayback* anim_playbacks;
|
||||||
int anim_playbacks_count;
|
int anim_playbacks_count;
|
||||||
} GameState;
|
} GameState;
|
||||||
@ -80,20 +78,18 @@ Assets Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Update(GameState *game, Camera2D *cam, float dt) {
|
void Update(GameState *game, Camera2D *cam, float dt) {
|
||||||
TickSpriteAnimations(&knight_anims[0], game->anim_playbacks, 1);
|
|
||||||
|
|
||||||
if (IsMouseButtonPressed(0)) {
|
if (IsMouseButtonPressed(0)) {
|
||||||
Point mouse_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
|
Point mouse_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
|
||||||
// TODO: This kind of sucks that we're doing all the math here to calculate
|
// TODO: This kind of sucks that we're doing all the math here to calculate
|
||||||
// the position of the collider, we need a helper that calculates origin offset
|
// the position of the collider, we need a helper that calculates origin offset
|
||||||
Rectangle mouse_select_area = {
|
Rectangle mouse_select_area = {
|
||||||
game->knight.position.x - knight_colrect_select.width / 2,
|
game->knights[0].position.x + knight_colrect_select.x,
|
||||||
game->knight.position.y - knight_colrect_select.height / 2,
|
game->knights[0].position.y + knight_colrect_select.y,
|
||||||
knight_colrect_select.width,
|
knight_colrect_select.width,
|
||||||
knight_colrect_select.height,
|
knight_colrect_select.height,
|
||||||
};
|
};
|
||||||
if (CheckCollisionPointRec(mouse_pos, mouse_select_area)) {
|
if (CheckCollisionPointRec(mouse_pos, mouse_select_area)) {
|
||||||
game->selected_knight = &game->knight;
|
game->selected_knight = &game->knights[0];
|
||||||
} else {
|
} else {
|
||||||
game->selected_knight = NULL;
|
game->selected_knight = NULL;
|
||||||
}
|
}
|
||||||
@ -102,35 +98,10 @@ void Update(GameState *game, Camera2D *cam, float dt) {
|
|||||||
if (IsMouseButtonPressed(1)) {
|
if (IsMouseButtonPressed(1)) {
|
||||||
Point mouse_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
|
Point mouse_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
|
||||||
if (game->selected_knight != NULL) {
|
if (game->selected_knight != NULL) {
|
||||||
game->target_points[0] = (PointOrNone){.type = POINT, .value.point = mouse_pos };
|
game->target_points[0] = (PointOption){ .tag = SOME, .some.point = mouse_pos };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// for (int i = 0; i < MAX_KNIGHTS; i++) {
|
|
||||||
Vector2 input_vel = {0};
|
|
||||||
if (game->target_points[0].type == POINT) {
|
|
||||||
Vector2 target = game->target_points[0].value.point;
|
|
||||||
if (Vector2DistanceSqr(target, game->knight.position) < 0.5f) {
|
|
||||||
game->target_points[0].type = NONE;
|
|
||||||
} else {
|
|
||||||
input_vel.x = game->knight.position.x - target.x < 0.0f ? 1 : -1;
|
|
||||||
game->knight.position = Vector2MoveTowards(game->knight.position, target, 4.0f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// const float movement_speed = 250.0f * dt;
|
|
||||||
// if (IsKeyDown(KEY_RIGHT)) {
|
|
||||||
// input_vel.x = 1;
|
|
||||||
// }
|
|
||||||
// if (IsKeyDown(KEY_LEFT)) {
|
|
||||||
// input_vel.x = -1;
|
|
||||||
// }
|
|
||||||
// if (IsKeyDown(KEY_UP)) {
|
|
||||||
// input_vel.y = -1;
|
|
||||||
// }
|
|
||||||
// if (IsKeyDown(KEY_DOWN)) {
|
|
||||||
// input_vel.y = 1;
|
|
||||||
// }
|
|
||||||
const float cam_move_speed = 1050.0f * dt;
|
const float cam_move_speed = 1050.0f * dt;
|
||||||
Vector2 cam_vel = {0};
|
Vector2 cam_vel = {0};
|
||||||
if (IsKeyDown(KEY_D)) {
|
if (IsKeyDown(KEY_D)) {
|
||||||
@ -148,32 +119,68 @@ void Update(GameState *game, Camera2D *cam, float dt) {
|
|||||||
cam_vel = Vector2Normalize(cam_vel);
|
cam_vel = Vector2Normalize(cam_vel);
|
||||||
game->camera_position = Vector2Scale(cam_vel, cam_move_speed);
|
game->camera_position = Vector2Scale(cam_vel, cam_move_speed);
|
||||||
cam->offset = Vector2Add(cam->offset, game->camera_position);
|
cam->offset = Vector2Add(cam->offset, game->camera_position);
|
||||||
if (IsKeyPressed(KEY_SPACE) && game->knight.state != KNIGHT_ATTACKING) {
|
if (IsKeyPressed(KEY_SPACE) && game->knights[0].state != KNIGHT_ATTACKING) {
|
||||||
game->knight.state = KNIGHT_ATTACKING;
|
game->knights[0].state = KNIGHT_ATTACKING;
|
||||||
game->anim_playbacks[0].anim_id = ANIM_KNIGHT_ATTACK_SIDE1;
|
PlayAnimation(ANIM_KNIGHT_ATTACK_SIDE1, knight_anims, &game->anim_playbacks[0]);
|
||||||
PlayAnimation(&game->anim_playbacks[0], game->anim_playbacks[0]);
|
|
||||||
}
|
}
|
||||||
if (IsKeyPressed(KEY_F1)) {
|
if (IsKeyPressed(KEY_F1)) {
|
||||||
global_debug_mode = !global_debug_mode;
|
global_debug_mode = !global_debug_mode;
|
||||||
}
|
}
|
||||||
if (game->knight.state == KNIGHT_ATTACKING) {
|
// const float movement_speed = 250.0f * dt;
|
||||||
game->anim_playbacks[0].anim_id = ANIM_KNIGHT_ATTACK_SIDE1;
|
// if (IsKeyDown(KEY_RIGHT)) {
|
||||||
if (game->anim_playbacks[0].is_finished) {
|
// input_vel.x = 1;
|
||||||
game->knight.state = KNIGHT_IDLE;
|
// }
|
||||||
|
// if (IsKeyDown(KEY_LEFT)) {
|
||||||
|
// input_vel.x = -1;
|
||||||
|
// }
|
||||||
|
// if (IsKeyDown(KEY_UP)) {
|
||||||
|
// input_vel.y = -1;
|
||||||
|
// }
|
||||||
|
// if (IsKeyDown(KEY_DOWN)) {
|
||||||
|
// input_vel.y = 1;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Process animation data
|
||||||
|
TickSpriteAnimations(game->anim_playbacks, 1);
|
||||||
|
|
||||||
|
// Handle knight movement if they are moving somewhere
|
||||||
|
// for (int i = 0; i < MAX_KNIGHTS; i++) {
|
||||||
|
for (int i = 0; i < 1; i++) {
|
||||||
|
Vector2 input_vel = {0};
|
||||||
|
Knight *knight = &game->knights[i];
|
||||||
|
if (game->target_points[0].tag == SOME) {
|
||||||
|
Vector2 target = game->target_points[0].some.point;
|
||||||
|
if (Vector2DistanceSqr(target, knight->position) < 2.5f) {
|
||||||
|
game->target_points[0].tag = NONE;
|
||||||
|
} else {
|
||||||
|
input_vel.x = knight->position.x - target.x < 0.0f ? 1 : -1;
|
||||||
|
knight->position = Vector2MoveTowards(knight->position, target, 4.0f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle the attacking state, if not handle state transitions
|
||||||
|
if (knight->state == KNIGHT_ATTACKING) {
|
||||||
|
if (game->anim_playbacks[i].is_finished) {
|
||||||
|
knight->state = KNIGHT_IDLE;
|
||||||
|
PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game->anim_playbacks[i]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (input_vel.x != 0) {
|
if (input_vel.x != 0) {
|
||||||
game->knight.look_dir = input_vel.x == -1 ? DIR_LEFT : DIR_RIGHT;
|
knight->look_dir = input_vel.x == -1 ? DIR_LEFT : DIR_RIGHT;
|
||||||
}
|
}
|
||||||
// game->knight.velocity = Vector2Normalize(input_vel);
|
|
||||||
// game->knight.velocity = Vector2Scale(game->knight.velocity, movement_speed);
|
|
||||||
// game->knight.position = Vector2Add(game->knight.position, game->knight.velocity);
|
|
||||||
if (input_vel.x != 0 || input_vel.y != 0) {
|
if (input_vel.x != 0 || input_vel.y != 0) {
|
||||||
game->anim_playbacks[0].anim_id = ANIM_KNIGHT_RUN;
|
if (knight->state == KNIGHT_IDLE) {
|
||||||
game->knight.state = KNIGHT_RUNNING;
|
PlayAnimation(ANIM_KNIGHT_RUN, knight_anims, &game->anim_playbacks[i]);
|
||||||
|
knight->state = KNIGHT_RUNNING;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
game->anim_playbacks[0].anim_id = ANIM_KNIGHT_IDLE;
|
if (knight->state == KNIGHT_RUNNING) {
|
||||||
game->knight.state = KNIGHT_IDLE;
|
PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game->anim_playbacks[i]);
|
||||||
|
knight->state = KNIGHT_IDLE;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -206,31 +213,36 @@ void Draw(const GameState *game, Assets assets, Camera2D cam, float dt) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (game->target_points[0].type == POINT) {
|
if (game->target_points[0].tag == SOME) {
|
||||||
Vector2 marker_pos = game->target_points[0].value.point;
|
Vector2 marker_pos = game->target_points[0].some.point;
|
||||||
marker_pos.x -= assets.textures[3].width / 2;
|
marker_pos.x -= assets.textures[3].width / 2;
|
||||||
// marker_pos.y -= assets.textures[3].height / 2;
|
marker_pos.y -= assets.textures[3].height / 2;
|
||||||
D_DrawTextureV(assets.textures[3], marker_pos, WHITE);
|
D_DrawTextureV(assets.textures[3], marker_pos, WHITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < game->anim_playbacks_count; i++) {
|
// for (int i = 0; i < game->anim_playbacks_count; i++) {
|
||||||
|
for (int i = 0; i < 1; i++) {
|
||||||
|
Knight *knight = &game->knights[i];
|
||||||
SpriteAnimationPlayback *playback = &game->anim_playbacks[i];
|
SpriteAnimationPlayback *playback = &game->anim_playbacks[i];
|
||||||
SpriteAnimation *anim = &knight_anims[playback->anim_id];
|
Rectangle src_rect = {
|
||||||
Rectangle src_rect = anim->src_rect;
|
playback->current_frame * knight_sprite_size,
|
||||||
src_rect.x = playback->current_frame * anim->src_rect.width;
|
playback->row * knight_sprite_size,
|
||||||
src_rect.y = anim->src_rect.y;
|
knight_sprite_size,
|
||||||
if (game->knight.look_dir == DIR_LEFT) {
|
knight_sprite_size,
|
||||||
|
};
|
||||||
|
// TODO: This might be a nice place to optimize
|
||||||
|
if (knight->look_dir == DIR_LEFT) {
|
||||||
src_rect.width = -abs((int)src_rect.width);
|
src_rect.width = -abs((int)src_rect.width);
|
||||||
}
|
}
|
||||||
// Vector2 pos = Vector2Subtract(game->knight.position, knight_origin);
|
// Vector2 pos = Vector2Subtract(game->knight.position, knight_origin);
|
||||||
Rectangle dest_rect = {game->knight.position.x, game->knight.position.y, 192, 192};
|
Rectangle dest_rect = {knight->position.x, knight->position.y, 192, 192};
|
||||||
D_DrawTexturePro(assets.textures[1], src_rect, dest_rect, knight_origin, 0.0f, WHITE);
|
D_DrawTexturePro(assets.textures[1], src_rect, dest_rect, knight_origin, 0.0f, WHITE);
|
||||||
|
|
||||||
if (game->selected_knight) {
|
if (game->selected_knight) {
|
||||||
Vector2 kpos = game->knight.position;
|
Vector2 kpos = game->knights[0].position;
|
||||||
Rectangle knight_col_area = {
|
Rectangle knight_col_area = {
|
||||||
kpos.x - knight_colrect_select.width / 2,
|
kpos.x + knight_colrect_select.x,
|
||||||
kpos.y - knight_colrect_select.height / 2,
|
kpos.y + knight_colrect_select.y,
|
||||||
knight_colrect_select.width,
|
knight_colrect_select.width,
|
||||||
knight_colrect_select.height,
|
knight_colrect_select.height,
|
||||||
};
|
};
|
||||||
@ -248,6 +260,8 @@ int main(void) {
|
|||||||
const int screen_width = 1600;
|
const int screen_width = 1600;
|
||||||
const int screen_height = 1080;
|
const int screen_height = 1080;
|
||||||
|
|
||||||
|
printf("SpriteAnimationPlayback Size: %ld \n", sizeof(SpriteAnimationPlayback));
|
||||||
|
|
||||||
InitWindow(screen_width, screen_height, "raylib [core] example - basic window");
|
InitWindow(screen_width, screen_height, "raylib [core] example - basic window");
|
||||||
|
|
||||||
int monitor = GetCurrentMonitor();
|
int monitor = GetCurrentMonitor();
|
||||||
@ -265,17 +279,18 @@ int main(void) {
|
|||||||
cam.zoom = 1.0f;
|
cam.zoom = 1.0f;
|
||||||
|
|
||||||
GameState game = {0};
|
GameState game = {0};
|
||||||
game.anim_playbacks = malloc(sizeof(SpriteAnimationPlayback) * MAX_ANIMATION_PLAYBACKS);
|
game.knights = malloc(sizeof(Knight) * MAX_KNIGHTS);
|
||||||
|
game.knights[0] = (Knight){0};
|
||||||
|
game.knights[0].position = (Vector2){100,100};
|
||||||
|
game.anim_playbacks = malloc(sizeof(SpriteAnimationPlayback) * MAX_KNIGHTS);
|
||||||
game.anim_playbacks_count = 1;
|
game.anim_playbacks_count = 1;
|
||||||
// First one is idle
|
// First one is idle
|
||||||
game.anim_playbacks[0] = (SpriteAnimationPlayback){0};
|
game.anim_playbacks[0] = (SpriteAnimationPlayback){0};
|
||||||
game.target_points = calloc(MAX_KNIGHTS, sizeof(Point));
|
game.target_points = calloc(MAX_KNIGHTS, sizeof(Point));
|
||||||
game.knight = (Knight){0};
|
|
||||||
game.knight.position = (Vector2){100,100};
|
|
||||||
|
|
||||||
Assets assets = Init();
|
Assets assets = Init();
|
||||||
|
|
||||||
PlayAnimation(&game.anim_playbacks[0], game.anim_playbacks[0]);
|
PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game.anim_playbacks[0]);
|
||||||
// const int idle
|
// const int idle
|
||||||
while (!WindowShouldClose()) {
|
while (!WindowShouldClose()) {
|
||||||
game.frame_count++;
|
game.frame_count++;
|
||||||
|
21
sprites.c
21
sprites.c
@ -1,26 +1,25 @@
|
|||||||
#include "sprites.h"
|
#include "sprites.h"
|
||||||
#include "include/raylib.h"
|
#include "include/raylib.h"
|
||||||
|
|
||||||
void PlayAnimation(SpriteAnimationPlayback *playbacks, SpriteAnimationPlayback playback) {
|
inline void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimationPlayback *playback) {
|
||||||
playback.time_elapsed = 0.0f;
|
playback->time_elapsed = 0.0f;
|
||||||
playback.current_frame = 0;
|
playback->current_frame = 0;
|
||||||
playback.is_finished = false;
|
playback->row = animation;
|
||||||
playbacks[0] = playback;
|
playback->is_finished = false;
|
||||||
|
playback->total_frames = anims[animation].total_frames;
|
||||||
|
playback->loop = anims[animation].loop;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TickSpriteAnimations(const SpriteAnimation *animations,
|
void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len) {
|
||||||
SpriteAnimationPlayback *playbacks,
|
|
||||||
int len) {
|
|
||||||
const f32 anim_speed = 1.0f / 10.0f;
|
const f32 anim_speed = 1.0f / 10.0f;
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
SpriteAnimationPlayback *playback = &playbacks[i];
|
SpriteAnimationPlayback *playback = &playbacks[i];
|
||||||
const SpriteAnimation *animation = &animations[playback->anim_id];
|
|
||||||
playback->time_elapsed += GetFrameTime();
|
playback->time_elapsed += GetFrameTime();
|
||||||
if (playback->time_elapsed >= anim_speed) {
|
if (playback->time_elapsed >= anim_speed) {
|
||||||
playback->time_elapsed = 0.0f;
|
playback->time_elapsed = 0.0f;
|
||||||
playback->current_frame++;
|
playback->current_frame++;
|
||||||
if (playback->current_frame >= animation->total_frames) {
|
if (playback->current_frame >= playback->total_frames) {
|
||||||
if (animation->loop) {
|
if (playback->loop) {
|
||||||
playback->current_frame = 0;
|
playback->current_frame = 0;
|
||||||
} else {
|
} else {
|
||||||
playback->is_finished = true;
|
playback->is_finished = true;
|
||||||
|
27
sprites.h
27
sprites.h
@ -4,25 +4,26 @@
|
|||||||
#include "lib.h"
|
#include "lib.h"
|
||||||
#include "include/raylib.h"
|
#include "include/raylib.h"
|
||||||
|
|
||||||
typedef struct {
|
// typedef struct {
|
||||||
Texture2D texture;
|
// Texture2D texture;
|
||||||
char *name;
|
// char *name;
|
||||||
Size size;
|
// Size size;
|
||||||
} SpriteSheet;
|
// } SpriteSheet;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* name;
|
u8 loop;
|
||||||
u8 total_frames;
|
u8 total_frames;
|
||||||
bool loop;
|
|
||||||
Rectangle src_rect;
|
|
||||||
} SpriteAnimation;
|
} SpriteAnimation;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u16 anim_id;
|
|
||||||
u8 current_frame;
|
|
||||||
bool is_finished;
|
|
||||||
f32 time_elapsed;
|
f32 time_elapsed;
|
||||||
|
u8 row;
|
||||||
|
u8 total_frames;
|
||||||
|
u8 current_frame;
|
||||||
|
u8 loop;
|
||||||
|
// TODO: Maybe we can get rid of this one
|
||||||
|
u8 is_finished;
|
||||||
} SpriteAnimationPlayback;
|
} SpriteAnimationPlayback;
|
||||||
|
|
||||||
void PlayAnimation(SpriteAnimationPlayback *playbacks, SpriteAnimationPlayback playback);
|
void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimationPlayback *playback);
|
||||||
void TickSpriteAnimations(const SpriteAnimation *animations, SpriteAnimationPlayback *playbacks, int len);
|
void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user