Knight made to move independently, relative screen dimensions

This commit is contained in:
Joseph Ferano 2024-01-06 10:42:38 +07:00
parent 1ff06abdd4
commit 05c86f8284
2 changed files with 30 additions and 19 deletions

View File

@ -5,7 +5,7 @@ CC=gcc
all: main dod
main: main.c sprites.o sprites.h game_data.h ./lib/libraylib.a
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

47
main.c
View File

@ -6,7 +6,9 @@
#define TEXTURES_BUF_SIZE 16
#define TARGET_FPS 60
#define MAX_KNIGHTS 1024
#define MAX_KNIGHTS 512
#define SCREEN_WIDTH 1300
#define SCREEN_HEIGHT 1080
#define DEBUG_MODE_ENABLED
@ -36,9 +38,11 @@ typedef enum Direction {
typedef struct {
Point position;
Point move_target_point;
u8 look_dir;
u8 state;
u8 selected;
u8 ordered_to_move;
} Knight;
typedef struct {
@ -153,8 +157,18 @@ void Update(GameState *game, Camera2D *cam, float dt) {
}
if (IsMouseButtonPressed(1)) {
Point mouse_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
game->selected_point = (PointOption){ .tag = SOME, .some.point = mouse_pos };
Point target_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
bool any = false;
for (int i = 0; i < game->entity_count; i++) {
if (game->knights[i].selected) {
game->knights[i].move_target_point = target_pos;
game->knights[i].ordered_to_move = true;
any = true;
}
}
if (any) {
game->selected_point = (PointOption){ .tag = SOME, .some.point = target_pos };
}
}
const float cam_move_speed = 1050.0f * dt;
@ -192,10 +206,10 @@ void Update(GameState *game, Camera2D *cam, float dt) {
for (int i = 0; i < game->entity_count; i++) {
Vector2 input_vel = {0};
Knight *knight = &game->knights[i];
if (game->selected_point.tag == SOME && knight->selected) {
Vector2 target = game->selected_point.some.point;
if (knight->ordered_to_move) {
Vector2 target = knight->move_target_point;
if (Vector2DistanceSqr(target, knight->position) < 2.5f) {
game->selected_point.tag = NONE;
knight->ordered_to_move = false;
} else {
input_vel.x = knight->position.x - target.x < 0.0f ? 1 : -1;
knight->position = Vector2MoveTowards(knight->position, target, 4.0f);
@ -247,8 +261,8 @@ void Draw(GameState *game, Assets assets, Camera2D cam, float dt) {
ClearBackground((Color){100, 149, 237, 255});
int size = 32;
int topx = 300;
int topy = 32;
int topx = SCREEN_WIDTH / 2 - size * 32 / 2;
int topy = SCREEN_HEIGHT / 2 - size * 32 / 2;
for (int col = 0; col < size; col++) {
for (int row = 0; row < size; row++) {
int atlas_col = 0;
@ -323,14 +337,11 @@ void Draw(GameState *game, Assets assets, Camera2D cam, float dt) {
Vector2 pointer_pos = Vector2Subtract(world, (Vector2){24, 19});
D_DrawTextureV(assets.textures[TEX_MOUSE_CURSOR], pointer_pos, WHITE);
DrawRectangle(1492, 4, 88, 30, WHITE);
DrawFPS(1500, 10);
DrawRectangle(SCREEN_WIDTH - 106, 4, 88, 30, WHITE);
DrawFPS(SCREEN_WIDTH - 100, 10);
}
int main(void) {
const int screen_width = 1600;
const int screen_height = 1080;
printf("Knight Size: %ld \n", sizeof(Knight));
printf("SpriteAnimationPlayback Size: %ld \n", sizeof(SpriteAnimationPlayback));
printf("Point Size: %ld \n", sizeof(Point));
@ -338,13 +349,13 @@ int main(void) {
printf("Direction Size: %ld \n", sizeof(Direction));
printf("KnightState Size: %ld \n", sizeof(KnightState));
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_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;
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);
@ -362,8 +373,8 @@ int main(void) {
const int entities = MAX_KNIGHTS;
for (int i = 0; i < entities; i++) {
int rand_x = GetRandomValue(360, 1250);
int rand_y = 100 + ((float)930 / (float)entities) * i;
int rand_x = GetRandomValue(165, 1130);
int rand_y = 100 + ((float)950 / (float)entities) * i;
game.knights[i].position = (Vector2){rand_x,rand_y};
PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game.anim_playbacks[i]);