178 lines
5.4 KiB
C
178 lines
5.4 KiB
C
#include "include/raylib.h"
|
|
#include "include/raymath.h"
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
|
|
#define TEXTURES_BUF_SIZE 16
|
|
#define TARGET_FPS 120
|
|
#define ANIM_SPEED 8
|
|
#define MAX_ANIMATION_PLAYBACKS 64
|
|
|
|
#include "sprites.h"
|
|
#include "lib.h"
|
|
#include "config.h"
|
|
|
|
typedef struct {
|
|
int frame_count;
|
|
int anim_frames;
|
|
bool is_attacking;
|
|
Vector2 knight_pos;
|
|
Rectangle *sprite_rects;
|
|
SpriteAnimationPlayback *anim_playbacks;
|
|
int anim_playbacks_count;
|
|
} GameState;
|
|
|
|
typedef struct {
|
|
Texture2D *textures;
|
|
} Assets;
|
|
|
|
Assets Init() {
|
|
knight_anims[ANIM_KNIGHT_IDLE] = knight_idle;
|
|
knight_anims[ANIM_KNIGHT_RUN] = knight_run;
|
|
knight_anims[ANIM_KNIGHT_ATTACK_SIDE1] = knight_attack_side1;
|
|
knight_anims[ANIM_KNIGHT_ATTACK_SIDE2] = knight_attack_side2;
|
|
knight_anims[ANIM_KNIGHT_ATTACK_TOP1 ] = knight_attack_front1;
|
|
knight_anims[ANIM_KNIGHT_ATTACK_TOP2 ] = knight_attack_front2;
|
|
knight_anims[ANIM_KNIGHT_ATTACK_BACK1] = knight_attack_back1;
|
|
knight_anims[ANIM_KNIGHT_ATTACK_BACK2] = knight_attack_back2;
|
|
|
|
Assets assets = { 0 };
|
|
assets.textures = malloc(sizeof(Texture2D) * TEXTURES_BUF_SIZE);
|
|
assets.textures[0] = LoadTexture("assets/Terrain/Ground/Tilemap_Flat.png");
|
|
assets.textures[1] =
|
|
LoadTexture("assets/Factions/Knights/Troops/Warrior/Blue/Warrior_Blue.png");
|
|
return assets;
|
|
}
|
|
|
|
void Update(GameState *state, Camera2D cam, float dt) {
|
|
(void)cam;
|
|
|
|
TickSpriteAnimations(&knight_anims[0], state->anim_playbacks, 1);
|
|
|
|
float movement_speed = 250.0f * dt;
|
|
Vector2 input_vel = {0};
|
|
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) {
|
|
input_vel.x = 1;
|
|
}
|
|
if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) {
|
|
input_vel.x = -1;
|
|
}
|
|
if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W)) {
|
|
input_vel.y = -1;
|
|
}
|
|
if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)) {
|
|
input_vel.y = 1;
|
|
}
|
|
if (IsKeyPressed(KEY_SPACE)) {
|
|
state->is_attacking = true;
|
|
}
|
|
input_vel = Vector2Normalize(input_vel);
|
|
input_vel = Vector2Scale(input_vel, movement_speed);
|
|
state->knight_pos = Vector2Add(state->knight_pos, input_vel);
|
|
// if (state->is_attacking) {
|
|
// if (state->anim_frames - attacking_frames > 5) {
|
|
// state->is_attacking = false;
|
|
// state->knight_rect.y = 0;
|
|
// } else {
|
|
// state->knight_rect.y = 192 * 2;
|
|
// }
|
|
// } else {
|
|
if (input_vel.x != 0 || input_vel.y != 0) {
|
|
state->anim_playbacks[0].anim_id = ANIM_KNIGHT_RUN;
|
|
} else {
|
|
state->anim_playbacks[0].anim_id = ANIM_KNIGHT_IDLE;
|
|
}
|
|
// }
|
|
}
|
|
|
|
void Draw(GameState *state, Assets assets, Camera2D cam, float dt) {
|
|
(void)cam;
|
|
(void)dt;
|
|
ClearBackground((Color){ 100, 149, 237, 255 });
|
|
|
|
int size = 32;
|
|
int topx = 300;
|
|
int topy = 32;
|
|
for (int col = 0; col < size; col++) {
|
|
for (int row = 0; row < size; row++) {
|
|
int atlas_col = 0;
|
|
int atlas_row = 0;
|
|
if (col == size - 1) {
|
|
atlas_col = 5;
|
|
} else if (col > 0) {
|
|
atlas_col = (col % 4) + 1;
|
|
}
|
|
if (row == size - 1) {
|
|
atlas_row = 5;
|
|
} else if (row > 0) {
|
|
atlas_row = (row % 4) + 1;
|
|
}
|
|
Vector2 pos = {32 * col + topx, 32 * row + topy};
|
|
Rectangle src_rect = {32 * atlas_col, 32 * atlas_row, 32, 32};
|
|
DrawTextureRec(assets.textures[0], src_rect, pos, WHITE);
|
|
}
|
|
}
|
|
for (int i = 0; i < state->anim_playbacks_count; i++) {
|
|
SpriteAnimationPlayback *playback = &state->anim_playbacks[i];
|
|
SpriteAnimation *anim = &knight_anims[playback->anim_id];
|
|
Rectangle src_rect = anim->src_rect;
|
|
src_rect.x = playback->current_frame * anim->src_rect.width;
|
|
src_rect.y = anim->src_rect.y;
|
|
DrawTextureRec(assets.textures[1], src_rect, state->knight_pos, WHITE);
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
const int screen_width = 1600;
|
|
const int screen_height = 1080;
|
|
|
|
InitWindow(screen_width, screen_height, "raylib [core] example - basic window");
|
|
|
|
int monitor = GetCurrentMonitor();
|
|
int monitor_width = GetMonitorWidth(monitor);
|
|
int monitor_height = GetMonitorHeight(monitor);
|
|
int win_pos_x = monitor_width / 2 - screen_width / 2;
|
|
int win_pos_y = monitor_height / 2 - screen_height / 2;
|
|
SetWindowPosition(win_pos_x, win_pos_y);
|
|
|
|
SetTargetFPS(TARGET_FPS);
|
|
|
|
Camera2D cam = {0};
|
|
cam.zoom = 1.0f;
|
|
|
|
GameState state = {0};
|
|
state.anim_playbacks = malloc(sizeof(SpriteAnimationPlayback) * MAX_ANIMATION_PLAYBACKS);
|
|
state.anim_playbacks_count = 1;
|
|
// First one is idle
|
|
state.anim_playbacks[0] = (SpriteAnimationPlayback){0};
|
|
// state.anim_playbacks[0].pos = (Position){ knight_idle.src_rect.x, knight_idle.src_rect.y };
|
|
|
|
Assets assets = Init();
|
|
|
|
PlayAnimation(&state.anim_playbacks[0], state.anim_playbacks[0]);
|
|
// const int idle
|
|
while (!WindowShouldClose()) {
|
|
state.frame_count++;
|
|
float dt = GetFrameTime();
|
|
|
|
Update(&state, cam, dt);
|
|
|
|
BeginDrawing();
|
|
{
|
|
BeginMode2D(cam);
|
|
{
|
|
Draw(&state, assets, cam, dt);
|
|
}
|
|
EndMode2D();
|
|
}
|
|
EndDrawing();
|
|
}
|
|
|
|
for (int i = 0; i < TEXTURES_BUF_SIZE; i++) {
|
|
UnloadTexture(assets.textures[0]);
|
|
}
|
|
free(assets.textures);
|
|
CloseWindow();
|
|
return 0;
|
|
}
|