Items can have events

pull/2/head
bgiraudr 5 months ago
parent c594ed0d44
commit 8c3138209b
  1. 1
      CMakeLists.txt
  2. 1
      assets-cg/converters.py
  3. 5
      assets-cg/items/1.json
  4. 6
      assets-cg/items/2.json
  5. 22
      include/inventory.h
  6. 21
      include/item.h
  7. 3
      include/talkable.h
  8. 5
      src/engine.c
  9. 3
      src/event.c
  10. 35
      src/inventory.c
  11. 31
      src/item.c
  12. 37
      src/talkable.c

@ -46,6 +46,7 @@ set(SOURCES
src/monster.c
src/event.c
src/inventory.c
src/item.c
)
set(ASSETS_cg

@ -313,6 +313,7 @@ def convert_items(input, output, params, target):
item += fxconv.u32(data["id"])
item += fxconv.string(data["description"])
item += fxconv.ptr(f"img_{data['sprite']}")
item += fxconv.string(";".join(data["action"]))
items += fxconv.ptr(item)
except KeyError:
raise Exception(f"convert_items() : L'item {data['name']} est mal configuré")

@ -2,5 +2,8 @@
"name":"Test1",
"id":1,
"sprite":"item",
"description":"test1"
"description":"Redonne 20HP",
"action":[
"xp:20"
]
}

@ -2,5 +2,9 @@
"name":"Obj2",
"id":2,
"sprite":"item2",
"description":"test2"
"description":"test2",
"action":[
"xp:40",
"move:2"
]
}

@ -1,27 +1,15 @@
#pragma once
#include "define.h"
#include <gint/display.h>
struct Item {
char *name;
int id;
char *description;
bopti_image_t *sprite;
};
struct Items {
int nbItems;
struct Item *items[];
};
#include "game.h"
#include <stdbool.h>
struct Inventory {
int nbItems;
struct Item *items[NB_PLAYER_ITEMS];
};
struct Item *get_item_id(int id);
bool add_item_to_inventory(struct Inventory *inventory, struct Item *item);
bool add_item_to_inventory(struct Game *game, struct Inventory *inventory, struct Item *item);
int get_first_free_space(struct Inventory *inventory);
void remove_item_pos(struct Inventory *inventory, int pos);
void display_inventory(struct Inventory *inventory);
int open_inventory(struct Inventory *inventory, char* context);
int open_inventory(struct Game *game, struct Inventory *inventory, char* context, bool keep_open);

@ -0,0 +1,21 @@
#pragma once
#include "game.h"
#include "inventory.h"
#include <gint/display.h>
struct Item {
char *name;
int id;
char *description;
bopti_image_t *sprite;
char *action;
};
struct Items {
int nbItems;
struct Item *items[];
};
struct Item *get_item_id(int id);
void select_item(struct Game *game, int pos);
void remove_item_pos(struct Inventory *inventory, int pos);

@ -16,4 +16,5 @@ void draw_dialog(struct Game *game);
/*find the talkable tile using the player's position*/
struct Talkable* get_dialog_xy(struct Map *map, int x, int y);
char *word_boundary_before(char *str, char *limit);
char *skip_spaces(char *str);
char *skip_spaces(char *str);
void format_text(int x, int y, const int color, char const *format, ...);

@ -14,6 +14,7 @@
#include "capacite.h"
#include "util.h"
#include "inventory.h"
#include "item.h"
/*draw the current state of the game*/
void engine_draw(struct Game const *game) {
@ -127,10 +128,10 @@ void engine_action(struct Game *game, int action) {
if(action == ACTION_F1) {
/*game->player->sprint = game->player->sprint ? 0 : 1;
add_move(game->player, get_move_id(3));*/
open_inventory(&game->player->inventory, "Consultation");
open_inventory(game, &game->player->inventory, "Consultation", true);
}
if(action == ACTION_F2) {
add_item_to_inventory(&game->player->inventory, get_item_id(1));
add_item_to_inventory(game, &game->player->inventory, get_item_id(1));
}
if(action == ACTION_OPTN) {
draw_stats(game->player->stats);

@ -6,6 +6,7 @@
#include "player.h"
#include "capacite.h"
#include "inventory.h"
#include "item.h"
bool handle_event(struct Game *game, char const *event)
{
@ -15,7 +16,7 @@ bool handle_event(struct Game *game, char const *event)
int len=strlen(event), qty=1;
char *star = strchr(event, '*');
if(star) len=star-event, qty=atoi(star+1);
add_item_to_inventory(&game->player->inventory,get_item_id(2));
add_item_to_inventory(game, &game->player->inventory, get_item_id(2));
return true;
}
else if(!strncmp(event, "xp:", 3)) {

@ -1,3 +1,4 @@
#include <stdio.h>
#include <gint/display.h>
#include <gint/keyboard.h>
#include <stdbool.h>
@ -6,15 +7,10 @@
#include "util.h"
#include "define.h"
#include "vec2.h"
extern struct Items items;
struct Item *get_item_id(int id) {
for(int i = 0; i < items.nbItems; i++) {
if(items.items[i]->id == id) return items.items[i];
}
return items.items[0];
}
#include "talkable.h"
#include "event.h"
#include "item.h"
#include "game.h"
int get_first_free_space(struct Inventory *inventory) {
for(int i = 0; i < NB_PLAYER_ITEMS; i++) {
@ -25,24 +21,24 @@ int get_first_free_space(struct Inventory *inventory) {
return NB_PLAYER_ITEMS;
}
bool add_item_to_inventory(struct Inventory *inventory, struct Item *item) {
bool add_item_to_inventory(struct Game *game, struct Inventory *inventory, struct Item *item) {
int index = get_first_free_space(inventory);
extern bopti_image_t img_dialogue;
dimage(42,DHEIGHT-75,&img_dialogue);
if(index < NB_PLAYER_ITEMS) {
dprint(50,DHEIGHT-47,C_BLACK,"Vous ajoutez %s à votre inventaire !", item->name);
format_text(50, DHEIGHT-47, C_BLACK, "Vous ajoutez %s à votre inventaire !", item->name);
dupdate();
wait_for_input(KEY_SHIFT);
inventory->items[index] = item;
inventory->nbItems++;
return true;
} else {
dprint(50,DHEIGHT-47,C_BLACK,"Plus de place pour ajouter %s à votre inventaire !", item->name);
format_text(50, DHEIGHT-47, C_BLACK, "Plus de place pour ajouter %s à votre inventaire !", item->name);
dupdate();
wait_for_input(KEY_SHIFT);
int pos = open_inventory(inventory, "Remplacer");
int pos = open_inventory(game, inventory, "Remplacer", false);
if(pos != -1) {
inventory->items[pos] = item;
inventory->nbItems++;
@ -52,11 +48,6 @@ bool add_item_to_inventory(struct Inventory *inventory, struct Item *item) {
return false;
}
void remove_item_pos(struct Inventory *inventory, int pos) {
inventory->items[pos] = NULL;
inventory->nbItems--;
}
void display_inventory(struct Inventory *inventory) {
for(int i = 0 ; i < NB_PLAYER_ITEMS ; i++) {
int x = i%10;
@ -69,7 +60,7 @@ void display_inventory(struct Inventory *inventory) {
}
}
int open_inventory(struct Inventory *inventory, char* context) {
int open_inventory(struct Game *game, struct Inventory *inventory, char* context, bool keep_open) {
int buffer = keydown(KEY_SHIFT);
struct Vec2 cursor = VEC2(0,0);
int pos = 0;
@ -103,7 +94,11 @@ int open_inventory(struct Inventory *inventory, char* context) {
if(keydown(KEY_SHIFT)) {
if(buffer) buffer = 0;
else if(inventory->items[pos] != NULL) {
if(!suppression) break;
if(!suppression && !keep_open) break;
else if(!suppression) {
select_item(game, pos);
remove_item_pos(inventory, pos);
}
else remove_item_pos(inventory, pos);
}
}

@ -0,0 +1,31 @@
#include "item.h"
#include "player.h"
#include "util.h"
#include <gint/keyboard.h>
#include <gint/display.h>
#include <string.h>
#include "event.h"
extern struct Items items;
struct Item *get_item_id(int id) {
for(int i = 0; i < items.nbItems; i++) {
if(items.items[i]->id == id) return items.items[i];
}
return items.items[0];
}
void remove_item_pos(struct Inventory *inventory, int pos) {
inventory->items[pos] = NULL;
inventory->nbItems--;
}
void select_item(struct Game *game, int pos) {
const char *delim = ";";
char *str = strdup(game->player->inventory.items[pos]->action);
char *curr_line = strtok(str, delim);
while(curr_line != NULL) {
handle_event(game, curr_line);
curr_line = strtok(NULL, delim);
}
}

@ -3,6 +3,9 @@
#include <gint/cpu.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "talkable.h"
#include "util.h"
@ -58,17 +61,7 @@ void draw_dialog(struct Game *game) {
dimage(43,31,&img_dialogue);
dprint(50,40, C_BLACK, "%s", talk->name);
int const DIALOG_WIDTH = 295, LINE_HEIGHT = 13;
int y = 45 + LINE_HEIGHT;
while(*curr_line) {
char *end = (char *)drsize(curr_line, NULL, DIALOG_WIDTH, NULL);
char *last_word = word_boundary_before(curr_line, end);
dtext_opt(50, y, C_BLACK, C_NONE, DTEXT_LEFT, DTEXT_TOP,
curr_line, last_word - curr_line);
curr_line = skip_spaces(last_word);
y += LINE_HEIGHT;
}
format_text(50, 58, C_BLACK, curr_line);
dupdate();
wait_for_input(KEY_SHIFT);
@ -85,4 +78,26 @@ struct Talkable* get_dialog_xy(struct Map *map, int x, int y) {
i++;
}
return &default_value;
}
void format_text(int x, int y, const int color, char const *format, ...) {
int const DIALOG_WIDTH = 295, LINE_HEIGHT = 13;
char text_arg[512];
va_list args;
va_start(args, format);
vsnprintf(text_arg, 512, format, args);
va_end(args);
char *text = (char *)malloc(strlen(text_arg)+1);
strcpy(text,text_arg);
while(*text) {
char *end = (char *)drsize(text, NULL, DIALOG_WIDTH, NULL);
char *last_word = word_boundary_before(text, end);
dtext_opt(x, y, color, C_NONE, DTEXT_LEFT, DTEXT_TOP,
text, last_word - text);
text = skip_spaces(last_word);
y += LINE_HEIGHT;
}
}
Loading…
Cancel
Save