Skip to content

Commit

Permalink
add block mining
Browse files Browse the repository at this point in the history
  • Loading branch information
julesgrc0 committed Feb 13, 2024
1 parent 4f70a8b commit 617acc7
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 84 deletions.
Binary file added assets/blocks/break_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/blocks/break_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/blocks/break_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/blocks/break_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/blocks/break_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/blocks/break_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/core/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void *update_bridge(void *arg)
do {

if (td->keyboard->key != 0 || td->camera->target.x != td->camera_target.x ||
td->camera->target.y != td->camera_target.y) {
td->camera->target.y != td->camera_target.y || td->force_update) {

if (!update_chunkview(td->chunk_view, td->chunk_group,
get_camera_view(td->camera))) {
Expand All @@ -153,6 +153,7 @@ void *update_bridge(void *arg)
return EXIT_FAILURE;
}
update_chunkview_lighting(td->chunk_view, get_player_center(td->player));
td->force_update = false;
}

QueryPerformanceCounter(&time_end);
Expand Down
1 change: 1 addition & 0 deletions src/core/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

typedef struct w_bridge {
bool is_active;
bool force_update;

w_chunkgroup *chunk_group;
w_chunkview *chunk_view;
Expand Down
40 changes: 9 additions & 31 deletions src/core/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,14 @@ void smooth_rect(Rectangle *box, Rectangle target, float move) {
smooth_float(box->y, target.y, move);
}

Vector2 get_collision_resolution(w_collision *bc, Rectangle self_rect,
Rectangle other_rect) {
Vector2 resolution = VEC_ZERO;
float overlapX = 0.0f, overlapY = 0.0f;

bc->left = self_rect.x < other_rect.x + other_rect.width &&
self_rect.x + self_rect.width > other_rect.x;
bc->right = self_rect.x + self_rect.width > other_rect.x &&
self_rect.x < other_rect.x;
bc->top = self_rect.y < other_rect.y + other_rect.height &&
self_rect.y + self_rect.height > other_rect.y;
bc->bottom = self_rect.y + self_rect.height > other_rect.y &&
self_rect.y < other_rect.y;

if (bc->left && !bc->right) {
overlapX = other_rect.x + other_rect.width - self_rect.x;
} else if (!bc->left && bc->right) {
overlapX = other_rect.x - (self_rect.x + self_rect.width);
}
if (bc->top && !bc->bottom) {
overlapY = other_rect.y + other_rect.height - self_rect.y;
} else if (!bc->top && bc->bottom) {
overlapY = other_rect.y - (self_rect.y + self_rect.height);
}

if (fabsf(overlapX) < fabsf(overlapY)) {
resolution.x = overlapX;
} else {
resolution.y = overlapY;
}
Vector2 get_mouse_block_center(Camera2D *camera) {
return Vector2Subtract(GetScreenToWorld2D(GetMousePosition(), *camera),
(Vector2){(CUBE_W / 2), (CUBE_H / 2)});
}

return resolution;
Vector2 vec_block_round(Vector2 vec) {
return (Vector2){
.x = round(vec.x / CUBE_W) * CUBE_W,
.y = round(vec.y / CUBE_H) * CUBE_H,
};
}
4 changes: 2 additions & 2 deletions src/core/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ Rectangle get_camera_view(Camera2D *camera);
void smooth_vec(Vector2 *position, Vector2 target, float move);
void smooth_rect(Rectangle *box, Rectangle target, float move);

Vector2 get_collision_resolution(w_collision *bc, Rectangle box,
Rectangle target);
Vector2 get_mouse_block_center(Camera2D *camera);
Vector2 vec_block_round(Vector2 vec);
58 changes: 13 additions & 45 deletions src/screen/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ void game_screen(w_state *state) {
};

w_bridge *td = create_bridge();

w_blockbreaker *bb = create_blockbreaker(state, td->chunk_view, td->camera);
while (!WindowShouldClose() && td->is_active) {
update_keyboard(td->keyboard);

float speed = GetFrameTime() * PLAYER_SPEED * 0.95f;
float dt = GetFrameTime();
float speed = dt * PLAYER_SPEED * 0.95f;

BeginTextureMode(state->render);
ClearBackground(BLACK);
DrawRectangleGradientV(0, 0, RENDER_W, RENDER_H, (Color){66, 135, 245, 255},
(Color){142, 184, 250, 255});

BeginMode2D(*(td->camera));
smooth_vec(&td->camera->target, td->camera_target, speed);
Expand All @@ -37,11 +40,6 @@ void game_screen(w_state *state) {
#else
pthread_mutex_lock(&td->chunk_view->mutex);
#endif // _WIN32
Rectangle view = get_camera_view(td->camera);
DrawRectangleGradientV(view.x, view.y, view.width, view.height,
(Color){66, 135, 245, 255},
(Color){142, 184, 250, 255});

for (unsigned int i = 0; i < td->chunk_view->textures_len; i++) {
DrawTexturePro(block_textures[td->chunk_view->blocks[i].block.type - 1],
td->chunk_view->blocks[i].src,
Expand All @@ -54,46 +52,15 @@ void game_screen(w_state *state) {
pthread_mutex_unlock(&td->chunk_view->mutex);
#endif // _WIN32

#ifdef _DEBUG
if (td->chunk_view->target != NULL) {
DrawRectangleLinesEx(
(Rectangle){.x = td->chunk_view->target->position * FULL_CHUNK_W,
.y = 0,
.width = FULL_CHUNK_W,
.height = FULL_CHUNK_H},
5, RED);
}
if (td->chunk_view->next != NULL) {
DrawRectangleLinesEx(
(Rectangle){.x = td->chunk_view->next->position * FULL_CHUNK_W,
.y = 0,
.width = FULL_CHUNK_W,
.height = FULL_CHUNK_H},
5, GREEN);
}
#endif

#if 0
Vector2 mouse =
Vector2Subtract(GetScreenToWorld2D(GetMousePosition(), *td->camera),
(Vector2){(CUBE_W / 2), (CUBE_H / 2)});
DrawCircleV(GetScreenToWorld2D(GetMousePosition(), *td->camera), 5, RED);
w_breakstate bstate =
update_blockbreaker(bb, get_player_center(td->player), dt);

if (Vector2Distance(mouse, get_player_center(td->player)) < CUBE_W * 3) {
Rectangle mouse_block = {.x = round(mouse.x / CUBE_W) * CUBE_W,
.y = round(mouse.y / CUBE_H) * CUBE_H,
.width = CUBE_W,
.height = CUBE_H};

w_block *block = get_chunkview_block(
td->chunk_view, (Vector2){mouse_block.x, mouse_block.y});
if (block != NULL && block->type != BLOCK_AIR) {
DrawRectangleLinesEx(mouse_block, 2, RED);
}
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
}
if (bstate == BS_BREAKING) {
draw_blockbreaker(bb);
} else if (bstate == BS_BROKEN) {
td->force_update = true;
}
#endif

EndMode2D();
DrawTexturePro(player_textures[td->player->state], td->player->src,
td->player->dst, VEC_ZERO, 0, WHITE);
Expand All @@ -106,6 +73,7 @@ void game_screen(w_state *state) {
DrawFPS(0, 0);
EndDrawing();
}
destroy_blockbreaker(bb);
destroy_bridge(td);

#ifndef _DEBUG
Expand Down
1 change: 1 addition & 0 deletions src/screen/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../core/state.h"
#include "../entities/player.h"
#include "../stdafx.h"
#include "../terrain/break.h"
#include "../terrain/chunk.h"

#define DAY_DURATION 60.0f
Expand Down
80 changes: 80 additions & 0 deletions src/terrain/break.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "break.h"

w_blockbreaker *create_blockbreaker(w_state *state, w_chunkview *chunk_view,
Camera2D *camera) {
w_blockbreaker *bb = malloc(sizeof(w_blockbreaker));
if (bb == NULL) {
return NULL;
}
memset(bb, 0, sizeof(w_blockbreaker));

bb->textures = malloc(sizeof(Texture) * BREAKER_STAGES);
for (size_t i = 0; i < BREAKER_STAGES; i++) {
bb->textures[i] =
get_texture_by_id(state, (char *)TextFormat("blocks\\break_%i.png", i));
}

bb->time = 0;
bb->stage = 0;
bb->position = VEC_ZERO;
bb->chunk_view = chunk_view;
bb->camera = camera;

return bb;
}

w_breakstate update_blockbreaker(w_blockbreaker *bb, Vector2 player, float dt) {
Vector2 mouse = get_mouse_block_center(bb->camera);
if (Vector2Distance(mouse, player) >= BREAKER_DISTANCE ||
!IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
bb->time = 0;
return BS_NONE;
}

Vector2 bmouse = vec_block_round(mouse);

w_block *block = get_chunkview_block(bb->chunk_view, bmouse);
if (block == NULL || block->type == BLOCK_AIR) {
bb->time = 0;
return BS_NONE;
}

if (bb->time > 0) {
if (!Vector2Equals(bb->position, bmouse)) {
bb->time = 0;
return false;
}
bb->time -= dt;
bb->stage =
(BREAKER_STAGES - 1) - ((bb->time) / (BREAKER_TIME / BREAKER_STAGES));

if (bb->stage >= BREAKER_STAGES - 1 || bb->time <= 0) {
bb->time = 0;
bb->stage = 0;
if (block != NULL && block->type != BLOCK_AIR) {
block->type = BLOCK_AIR;
}
return BS_BROKEN;
}

return BS_BREAKING;
}
bb->time = BREAKER_TIME;
bb->stage = BREAKER_STAGES - 1;
bb->position = bmouse;
return BS_NONE;
}

void draw_blockbreaker(w_blockbreaker *bb) {
DrawTexturePro(bb->textures[bb->stage], CUBE_SRC_RECT,
(Rectangle){.x = bb->position.x,
.y = bb->position.y,
.width = CUBE_W,
.height = CUBE_H},
VEC_ZERO, 0, Fade(WHITE, 0.5));
}

void destroy_blockbreaker(w_blockbreaker *bb) {
free(bb->textures);
free(bb);
}
34 changes: 34 additions & 0 deletions src/terrain/break.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once
#include "../core/state.h"
#include "../stdafx.h"
#include "chunk_view.h"

#define BREAKER_TIME .8f
#define BREAKER_STAGES 6
#define BREAKER_DISTANCE (3 * CUBE_W)

typedef enum w_breakstate {
BS_NONE,
BS_BREAKING,
BS_BROKEN,
} w_breakstate;

typedef struct w_blockbreaker {
float time;
int stage;
Vector2 position;

w_chunkview *chunk_view;
Camera2D *camera;

Texture *textures;
} w_blockbreaker;

w_blockbreaker *create_blockbreaker(w_state *state, w_chunkview *chunk_view,
Camera2D *camera);

w_breakstate update_blockbreaker(w_blockbreaker *bb, Vector2 player, float dt);

void draw_blockbreaker(w_blockbreaker *bb);

void destroy_blockbreaker(w_blockbreaker *bb);
6 changes: 1 addition & 5 deletions src/terrain/chunk_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,11 @@ w_block *get_chunkview_block(w_chunkview *chunk_view, Vector2 position) {
w_chunk *chunk = (chunk_view->target->position == chunk_x)
? chunk_view->target
: chunk_view->next;
LOG("chunk: %d", chunk->position);
unsigned int block_x = (int)(position.x / CUBE_W) - chunk->position * CHUNK_W;
unsigned int block_y = (int)(position.y / CUBE_H);

if (block_x + block_y * CHUNK_W > CHUNK_W * CHUNK_H) {
return NULL;
}
LOG("(%d;%d) = %d", block_x, block_y,
chunk->blocks[block_x + block_y * CHUNK_W].type);
// chunk->blocks[block_x + block_y * CHUNK_W].type = BLOCK_AIR;
return &chunk->blocks[block_x + block_y * CHUNK_W];
return &(chunk->blocks[block_x + block_y * CHUNK_W]);
}
Binary file modified tools/resource.pack
Binary file not shown.
2 changes: 2 additions & 0 deletions wispy-c.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
<ClCompile Include="src\core\state.c" />
<ClCompile Include="src\main.c" />
<ClCompile Include="src\core\mainframe.c" />
<ClCompile Include="src\terrain\break.c" />
<ClCompile Include="src\terrain\chunk.c" />
<ClCompile Include="src\terrain\chunk_group.c" />
<ClCompile Include="src\terrain\chunk_view.c" />
Expand All @@ -368,6 +369,7 @@
<ClInclude Include="src\core\state.h" />
<ClInclude Include="src\stdafx.h" />
<ClInclude Include="src\terrain\block.h" />
<ClInclude Include="src\terrain\break.h" />
<ClInclude Include="src\terrain\chunk.h" />
<ClInclude Include="src\terrain\chunk_group.h" />
<ClInclude Include="src\terrain\chunk_view.h" />
Expand Down

0 comments on commit 617acc7

Please sign in to comment.