Skip to content

Commit

Permalink
Inventory with HUD.
Browse files Browse the repository at this point in the history
  • Loading branch information
twetzel59 committed May 1, 2017
1 parent 749e165 commit 3b44099
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 11 deletions.
29 changes: 29 additions & 0 deletions src/inventory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "inventory.h"

void Inventory_reset(Inventory *m) {
for(int i = 0; i < INVENTORY_COUNT_LEN; i++) {
m->count[i] = 0;
}
}

int Inventory_collect(Inventory *m, int w) {
if(m->count[w] < 255) {
m->count[w]++;
return 1;
} else {
return 0;
}
}

int Inventory_use(Inventory *m, int w) {
if(m->count[w]) {
m->count[w]--;
return 1;
} else {
return 0;
}
}

uint8_t Inventory_getCount(Inventory *m, int w) {
return m->count[w];
}
17 changes: 17 additions & 0 deletions src/inventory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef _inventory_h_
#define _inventory_h_

#include <stdint.h>
#include "item.h"

typedef struct {
#define INVENTORY_COUNT_LEN (Item_max + 1)
uint8_t count[INVENTORY_COUNT_LEN];
} Inventory;

void Inventory_reset(Inventory *m);
int Inventory_collect(Inventory *m, int w);
int Inventory_use(Inventory *m, int w);
uint8_t Inventory_getCount(Inventory *m, int w);

#endif // _inventory_h_
3 changes: 2 additions & 1 deletion src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ typedef enum {
Item_COLOR_29,
Item_COLOR_30,
Item_COLOR_31,
Item_NYANCAT
Item_NYANCAT,
Item_max
} Item;

extern const int items[];
Expand Down
50 changes: 41 additions & 9 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "config.h"
#include "cube.h"
#include "db.h"
#include "inventory.h"
#include "item.h"
#include "map.h"
#include "matrix.h"
Expand Down Expand Up @@ -162,6 +163,7 @@ typedef struct {
SkyColor sky_color;
SkyColor last_sky_color;
//int is_fullscreen;
Inventory inventory;
} Model;

static Model model;
Expand Down Expand Up @@ -1633,7 +1635,7 @@ int get_block(int x, int y, int z) {
}

void builder_block(int x, int y, int z, int w) {
if (y <= 0 || y >= 256) {
if (y <= 0 || y >= BUILD_HEIGHT_LIMIT) {
return;
}
if (is_destructable(get_block(x, y, z))) {
Expand Down Expand Up @@ -1839,6 +1841,7 @@ void render_item(Attrib *attrib) {

if(!i) {
set_matrix_item(matrix, g->width, g->height, g->scale);
matrix[12] += 0.08f;
matrix[13] += 0.1f;
}

Expand All @@ -1862,6 +1865,31 @@ void render_text(
del_buffer(buffer);
}

void render_item_count(Attrib *attrib, float ts) {
const int buf_len = 4;

float pos = 15.0f;
for(int i = 0; i < NUM_INVENTORY_VISIBLE; ++i) {
if(g->item_index + i >= item_count) {
break;
}

char buf[buf_len];
//snprintf(buf, buf_len, "%d\n", Inventory_getCount(&g->inventory, items[g->item_index + i]));
//snprintf(buf, buf_len, "%d\n", g->inventory.count[items[g->item_index]]);

render_text(attrib, ALIGN_CENTER, g->width - 20.0f, pos, ts, buf);
//printf("%d\n", g->width);

float ratio_to_hardcoded = (g->height / 768.0f);
if(i) {
pos += 96.0f * ratio_to_hardcoded;
} else {
pos += 140.0f * ratio_to_hardcoded;
}
}
}

void add_message(const char *text) {
printf("%s\n", text);
snprintf(
Expand Down Expand Up @@ -2202,7 +2230,7 @@ void on_left_click() {
State *s = &g->players->state;
int hx, hy, hz;
int hw = hit_test(0, s->x, s->y, s->z, s->rx, s->ry, &hx, &hy, &hz);
if (hy > 0 && hy < BUILD_HEIGHT_LIMIT && is_destructable(hw)) {
if (hy > 0 && hy < BUILD_HEIGHT_LIMIT && is_destructable(hw) && Inventory_collect(&g->inventory, hw)) {
set_block(hx, hy, hz, 0);
record_block(hx, hy, hz, 0);
if (is_plant(get_block(hx, hy + 1, hz))) {
Expand All @@ -2215,7 +2243,7 @@ void on_right_click() {
State *s = &g->players->state;
int hx, hy, hz;
int hw = hit_test(1, s->x, s->y, s->z, s->rx, s->ry, &hx, &hy, &hz);
if (hy > 0 && hy < BUILD_HEIGHT_LIMIT && is_obstacle(hw)) {
if (hy > 0 && hy < BUILD_HEIGHT_LIMIT && is_obstacle(hw) && Inventory_use(&g->inventory, items[g->item_index])) {
if (!player_intersects_block(2, s->x, s->y, s->z, hx, hy, hz)) {
set_block(hx, hy, hz, items[g->item_index]);
record_block(hx, hy, hz, items[g->item_index]);
Expand Down Expand Up @@ -2684,8 +2712,9 @@ void reset_model() {
glfwSetTime(g->day_length / 3.0);
g->time_changed = 1;

g->sky_color = (SkyColor){1.0f, 1.0f, 1.0f};
g->sky_color = (SkyColor){0.0f, 0.0f, 0.0f};
g->last_sky_color = g->sky_color;
Inventory_reset(&g->inventory);
}

int main(int argc, char **argv) {
Expand Down Expand Up @@ -2721,7 +2750,7 @@ int main(int argc, char **argv) {
glLogicOp(GL_INVERT);
glClearColor(0, 0, 0, 1);

parser_parse_all();
//parser_parse_all();

// LOAD TEXTURES //
GLuint texture;
Expand Down Expand Up @@ -2883,6 +2912,8 @@ int main(int argc, char **argv) {
// BEGIN MAIN LOOP //
double previous = glfwGetTime();
while (1) {
//printf("%d %d\n", items[g->item_index], (int) g->inventory.count[items[g->item_index]]);

// WINDOW SIZE AND SCALE //
g->scale = get_scale_factor();
glfwGetFramebufferSize(g->window, &g->width, &g->height);
Expand Down Expand Up @@ -2957,15 +2988,16 @@ int main(int argc, char **argv) {
if (SHOW_CROSSHAIRS) {
render_crosshairs(&line_attrib);
}
char text_buffer[1024];
float ts = 12 * g->scale;
float tx = ts / 2;
float ty = g->height - ts;
if (SHOW_ITEM) {
render_item(&block_attrib);
render_item_count(&text_attrib, ts);
}

// RENDER TEXT //
char text_buffer[1024];
float ts = 12 * g->scale;
float tx = ts / 2;
float ty = g->height - ts;
if (SHOW_INFO_TEXT) {
int hour = time_of_day() * 24;
char am_pm = hour < 12 ? 'a' : 'p';
Expand Down
2 changes: 1 addition & 1 deletion src/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void set_matrix_item(float *matrix, int width, int height, int scale) {
mat_multiply(a, b, a);
mat_ortho(b, -box * aspect, box * aspect, -box, box, -1, 1);
mat_multiply(a, b, a);
mat_translate(b, -xoffset, -yoffset, 0);
mat_translate(b, -xoffset + 1.75f, -yoffset, 0);
mat_multiply(a, b, a);
mat_identity(matrix);
mat_multiply(matrix, a, matrix);
Expand Down

0 comments on commit 3b44099

Please sign in to comment.