20220728 - Crafting fonctionnel.

This commit is contained in:
mibi88 2022-07-28 13:50:16 +02:00
parent 02a36f236c
commit a25ff1878e
6 changed files with 80 additions and 3 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 496 B

After

Width:  |  Height:  |  Size: 577 B

Binary file not shown.

View File

@ -5,4 +5,4 @@
# define WORLD_HEIGHT 64 // World height.
# define INVENTORY_SIZE 5 // Size of the inventory.
# define CRAFTSIZE 9 // How many different blocks you can use to craft something.
# define CRAFTINGS 1 // How many things you can craft.
# define CRAFTINGS 2 // How many things you can craft.

View File

@ -258,6 +258,9 @@ void drawcrafting(){
case 6:
dimage(36, 28, &woodplanks_tile); break;
}
dtext(36, 38, C_BLACK, MULTIPLY);
itoa(craftingnum[crafting.selected], tmp_char);
dtext(36, 44, C_BLACK, tmp_char);
pos = 4;
for(i=crafting.selected*CRAFTSIZE;i!=(crafting.selected+1)*CRAFTSIZE;i++){
if(craftingsnum[i]!=0){
@ -288,7 +291,7 @@ int main(void) {
dimage(16, TITLE_IMAGE_MARGIN, &title_img);
dtext(1, TITLE_MARGIN, C_BLACK, TITLE_START);
dupdate();
int key = 0, game = 0, selected = 0, i;
int key = 0, game = 0, selected = 0, i, n;
unsigned short terrain[WORLD_WIDTH*WORLD_HEIGHT];
while(keydown(KEY_EXIT) == 0){
////////// TITLE SCREEN //////////
@ -604,6 +607,7 @@ int main(void) {
}else{
crafting.selected=CRAFTINGS-1;
}
clearevents();
while(keydown(KEY_LEFT)){
clearevents();
}
@ -614,10 +618,71 @@ int main(void) {
}else{
crafting.selected=0;
}
clearevents();
while(keydown(KEY_RIGHT)){
clearevents();
}
}
if(keydown(KEY_SHIFT)){
crafting.ispossible = 0;
pos = 0;
for(i=0;i!=5;i++){
if((player.inventoryitems[i]==craftingitem[crafting.selected] || player.inventoryitems[i]==0) && player.inventorynum[i]<=64-craftingnum[crafting.selected]){
crafting.ispossible = 1;
pos = i;
break;
}
}
crafting.hasanything = 0;
for(n=crafting.selected*CRAFTSIZE;n!=(crafting.selected+1)*CRAFTSIZE;n++){
crafting.hasitem = 0;
for(i=0;i!=5;i++){
if(craftingsitems[n]==player.inventoryitems[i] && craftingsnum[n]<=player.inventorynum[i]){
crafting.hasitem = 1;
}
}
crafting.hasanything += crafting.hasitem;
}
if(crafting.hasanything == CRAFTSIZE && crafting.ispossible == 1){
player.inventoryitems[pos] = craftingitem[crafting.selected];
player.inventorynum[pos] += craftingnum[crafting.selected];
for(n=crafting.selected*CRAFTSIZE;n!=(crafting.selected+1)*CRAFTSIZE;n++){
for(i=0;i!=5;i++){
if(craftingsitems[n]==player.inventoryitems[i] && craftingsnum[n]<=player.inventorynum[i]){
player.inventorynum[i]-=craftingsnum[n];
if(player.inventorynum[i]==0){
player.inventoryitems[i]=0;
}
}
}
}
dfont(&microfont);
dclear(C_WHITE);
dtext(10, 29, C_BLACK, ITEM_BUILT);
dtext(10, 35, C_BLACK, PRESS_SHIFT);
dfont_default();
dupdate();
clearevents();
while(keydown(KEY_SHIFT)){
clearevents();
}
}else{
dfont(&microfont);
dclear(C_WHITE);
dtext(10, 29, C_BLACK, ITEM_NOT_BUILT);
dtext(10, 35, C_BLACK, PRESS_SHIFT);
dfont_default();
dupdate();
clearevents();
while(keydown(KEY_SHIFT)){
clearevents();
}
}
clearevents();
while(keydown(KEY_SHIFT)){
clearevents();
}
}
}
clearevents();
}

View File

@ -10,4 +10,7 @@
# define STEEL_TILE "M. aci."
# define WOOD_TILE "Bois"
# define WOODPLANKS_TILE "Planche"
# define MULTIPLY "x"
# define MULTIPLY "x"
# define ITEM_BUILT "Item construit."
# define ITEM_NOT_BUILT "Peut pas construire."
# define PRESS_SHIFT "Appuyez sur SHIFT"

View File

@ -28,13 +28,22 @@ typedef struct Crafting Crafting;
struct Crafting{
int selected;
int ispossible;
int hasanything;
int hasitem;
};
const int craftingsitems[CRAFTINGS*CRAFTSIZE] = {
5, 0, 0, 0, 0, 0, 0, 0, 0,
6, 0, 0, 0, 0, 0, 0, 0, 0
};
const int craftingsnum[CRAFTINGS*CRAFTSIZE] = {
1, 0, 0, 0, 0, 0, 0, 0, 0,
4, 0, 0, 0, 0, 0, 0, 0, 0
};
const int craftingitem[CRAFTINGS] = {
6,
5
};
const int craftingnum[CRAFTINGS] = {
4,
1
};