Unit movement by clicking on the ground, debug stuff
This commit is contained in:
parent
ceca65fb46
commit
631940a6eb
@ -18,6 +18,14 @@ SpriteSheet knight = {
|
||||
};
|
||||
|
||||
const Vector2 knight_origin = {cell_size / 2, cell_size / 2};
|
||||
const Size knight_col_size = {75, 100};
|
||||
// TODO: We probably want this as a helper function somewhere
|
||||
const Rectangle knight_colrect_select = {
|
||||
knight_origin.x - knight_col_size.width / 2,
|
||||
knight_origin.y - knight_col_size.height / 2,
|
||||
knight_col_size.width,
|
||||
knight_col_size.height,
|
||||
};
|
||||
|
||||
SpriteAnimation knight_idle = {
|
||||
.name = "idle",
|
||||
|
8
lib.h
8
lib.h
@ -34,3 +34,11 @@ typedef union {
|
||||
Rect components;
|
||||
Rectangle rect;
|
||||
} RectU;
|
||||
|
||||
typedef struct PointOrNone {
|
||||
enum {NONE, POINT} type;
|
||||
union {
|
||||
byte none;
|
||||
Point point;
|
||||
} value;
|
||||
} PointOrNone;
|
||||
|
111
main.c
111
main.c
@ -2,11 +2,13 @@
|
||||
#include "include/raymath.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define TEXTURES_BUF_SIZE 16
|
||||
#define TARGET_FPS 60
|
||||
#define ANIM_SPEED 8
|
||||
#define MAX_ANIMATION_PLAYBACKS 64
|
||||
#define MAX_KNIGHTS 64
|
||||
|
||||
#include "sprites.h"
|
||||
#include "game_data.h"
|
||||
@ -43,6 +45,8 @@ typedef struct {
|
||||
Knight knight;
|
||||
// Rectangle* sprite_rects;
|
||||
Point camera_position;
|
||||
Knight *selected_knight;
|
||||
PointOrNone *target_points;
|
||||
SpriteAnimationPlayback* anim_playbacks;
|
||||
int anim_playbacks_count;
|
||||
bool debug_mode;
|
||||
@ -67,27 +71,63 @@ Assets Init() {
|
||||
assets.textures[0] = LoadTexture("assets/Terrain/Ground/Tilemap_Flat.png");
|
||||
assets.textures[1] =
|
||||
LoadTexture("assets/Factions/Knights/Troops/Warrior/Blue/Warrior_Blue.png");
|
||||
assets.textures[2] = LoadTexture("assets/UI/Pointers/01.png");
|
||||
assets.textures[3] = LoadTexture("assets/UI/Pointers/02.png");
|
||||
return assets;
|
||||
}
|
||||
|
||||
void Update(GameState *game, Camera2D *cam, float dt) {
|
||||
|
||||
TickSpriteAnimations(&knight_anims[0], game->anim_playbacks, 1);
|
||||
|
||||
const float movement_speed = 250.0f * dt;
|
||||
if (IsMouseButtonPressed(0)) {
|
||||
Point mouse_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
|
||||
// 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
|
||||
Rectangle mouse_select_area = {
|
||||
game->knight.position.x - knight_colrect_select.width / 2,
|
||||
game->knight.position.y - knight_colrect_select.height / 2,
|
||||
knight_colrect_select.width,
|
||||
knight_colrect_select.height,
|
||||
};
|
||||
if (CheckCollisionPointRec(mouse_pos, mouse_select_area)) {
|
||||
game->selected_knight = &game->knight;
|
||||
} else {
|
||||
game->selected_knight = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (IsMouseButtonPressed(1)) {
|
||||
Point mouse_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
|
||||
if (game->selected_knight != NULL) {
|
||||
game->target_points[0] = (PointOrNone){.type = POINT, .value.point = mouse_pos };
|
||||
}
|
||||
}
|
||||
|
||||
// for (int i = 0; i < MAX_KNIGHTS; i++) {
|
||||
Vector2 input_vel = {0};
|
||||
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;
|
||||
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;
|
||||
Vector2 cam_vel = {0};
|
||||
if (IsKeyDown(KEY_D)) {
|
||||
@ -122,9 +162,9 @@ void Update(GameState *game, Camera2D *cam, float dt) {
|
||||
if (input_vel.x != 0) {
|
||||
game->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);
|
||||
// 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) {
|
||||
game->anim_playbacks[0].anim_id = ANIM_KNIGHT_RUN;
|
||||
game->knight.state = KNIGHT_RUNNING;
|
||||
@ -173,6 +213,14 @@ void Draw(const GameState *game, Assets assets, Camera2D cam, float dt) {
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (game->target_points[0].type == POINT) {
|
||||
Vector2 marker_pos = game->target_points[0].value.point;
|
||||
marker_pos.x -= assets.textures[3].width / 2;
|
||||
// marker_pos.y -= assets.textures[3].height / 2;
|
||||
DrawTextureV(assets.textures[3], marker_pos, WHITE);
|
||||
}
|
||||
|
||||
for (int i = 0; i < game->anim_playbacks_count; i++) {
|
||||
SpriteAnimationPlayback *playback = &game->anim_playbacks[i];
|
||||
SpriteAnimation *anim = &knight_anims[playback->anim_id];
|
||||
@ -193,9 +241,33 @@ void Draw(const GameState *game, Assets assets, Camera2D cam, float dt) {
|
||||
anim->src_rect.height,
|
||||
};
|
||||
DrawRectangleLinesEx(debug_frame, 2.0f, RED);
|
||||
Vector2 kpos = game->knight.position;
|
||||
Rectangle knight_col_area = {
|
||||
kpos.x - knight_colrect_select.width / 2,
|
||||
kpos.y - knight_colrect_select.height / 2,
|
||||
knight_colrect_select.width,
|
||||
knight_colrect_select.height,
|
||||
};
|
||||
Color color = game->selected_knight == NULL ? RED : GREEN;
|
||||
DrawRectangleLinesEx(knight_col_area, 2.0f, color);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Vector2 world = GetScreenToWorld2D(GetMousePosition(), cam);
|
||||
Vector2 pointer_pos = Vector2Subtract(world, (Vector2){24, 19});
|
||||
DrawTextureV(assets.textures[2], pointer_pos, WHITE);
|
||||
#ifdef DEBUG_MODE_ENABLED
|
||||
if (game->debug_mode) {
|
||||
Rectangle debug_frame = {
|
||||
pointer_pos.x,
|
||||
pointer_pos.y,
|
||||
assets.textures[2].width,
|
||||
assets.textures[2].height,
|
||||
};
|
||||
DrawRectangleLinesEx(debug_frame, 2.0f, RED);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
@ -213,15 +285,20 @@ int main(void) {
|
||||
|
||||
SetTargetFPS(TARGET_FPS);
|
||||
|
||||
HideCursor();
|
||||
|
||||
Camera2D cam = {0};
|
||||
cam.zoom = 1.0f;
|
||||
|
||||
GameState game = {0};
|
||||
game.debug_mode = true;
|
||||
game.anim_playbacks = malloc(sizeof(SpriteAnimationPlayback) * MAX_ANIMATION_PLAYBACKS);
|
||||
game.anim_playbacks_count = 1;
|
||||
// First one is idle
|
||||
game.anim_playbacks[0] = (SpriteAnimationPlayback){0};
|
||||
game.target_points = calloc(MAX_KNIGHTS, sizeof(Point));
|
||||
game.knight = (Knight){0};
|
||||
game.knight.position = (Vector2){100,100};
|
||||
|
||||
Assets assets = Init();
|
||||
|
||||
@ -244,6 +321,8 @@ int main(void) {
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
free(game.anim_playbacks);
|
||||
free(game.target_points);
|
||||
for (int i = 0; i < TEXTURES_BUF_SIZE; i++) {
|
||||
UnloadTexture(assets.textures[0]);
|
||||
}
|
||||
|
@ -12,7 +12,9 @@
|
||||
* DONE Debug mode to show sprite boundaries
|
||||
* DONE Sprite position needs to be set to the center
|
||||
* DONE Movement keys to move the camera
|
||||
* TODO Left click on knight to select, right click to move
|
||||
* DONE Implement basic collider system
|
||||
* DONE Left click on knight to select, right click to move
|
||||
* DONE Mouse cursor texture and target on the ground texture
|
||||
* TODO Handle multiple knights on the map at the same time
|
||||
* TODO Ability to attack an entity, like a dummy
|
||||
* TODO State to handle attacking when you right click a target
|
||||
|
Loading…
x
Reference in New Issue
Block a user