forked from mibi88/Builder
20220826 - La vitesse de destruction varie selon le type et l'outil, pioche ajoutée (premier outil !).
This commit is contained in:
parent
8e677b7b59
commit
1c4c1a11ae
Binary file not shown.
|
@ -1,8 +1,8 @@
|
|||
# define GAMESNUM 5 // How many games you can save (and play).
|
||||
|
||||
# define WORLDGEN_SOIL 8 // Blocks (y) of soil.
|
||||
# define WORLD_WIDTH 64 // World width.
|
||||
# define WORLD_WIDTH 256 // World width.
|
||||
# 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 2 // How many things you can craft.
|
||||
# define CRAFTINGS 3 // How many things you can craft.
|
121
src/main.c
121
src/main.c
|
@ -10,6 +10,16 @@
|
|||
# include "worldgen.h"
|
||||
# include "itoa.h"
|
||||
|
||||
# define ERROR(error, errorcode) dclear(C_WHITE); \
|
||||
dprint(1, 1, C_BLACK, "ERROR"); \
|
||||
dprint(1, 6, C_BLACK, "LINE : %d", __LINE__); \
|
||||
dprint(1, 12, C_BLACK, "FILE : %d", __FILE__); \
|
||||
dprint(1, 18, C_BLACK, "ERROR NUM : %d", errorcode); \
|
||||
dprint(1, 24, C_BLACK, "%c", error); \
|
||||
dupdate(); \
|
||||
getkey(); \
|
||||
return errorcode;
|
||||
|
||||
extern bopti_image_t title_img;
|
||||
extern bopti_image_t soil_tile;
|
||||
extern bopti_image_t grass_tile;
|
||||
|
@ -26,6 +36,7 @@ extern bopti_image_t skin1_player_left2;
|
|||
extern bopti_image_t skin1_player_right1;
|
||||
extern bopti_image_t skin1_player_right2;
|
||||
extern bopti_image_t select_tool;
|
||||
extern bopti_image_t pickaxe_tool;
|
||||
extern bopti_image_t invnormal;
|
||||
extern bopti_image_t invselected;
|
||||
extern bopti_image_t craftselection;
|
||||
|
@ -45,6 +56,10 @@ char tmp_char[2];
|
|||
|
||||
Player player;
|
||||
Crafting crafting;
|
||||
Tool hand;
|
||||
Tool wood_pickaxe;
|
||||
const int canbeplaced_lenght = 6;
|
||||
const int canbeplaced[6] = {1, 2, 3, 4, 5, 6};
|
||||
|
||||
void drawoverlay() {
|
||||
/*
|
||||
|
@ -83,7 +98,7 @@ void drawselectedgame(int selected) {
|
|||
void addtree(int pos, unsigned short int * terrain, int n){
|
||||
int i;
|
||||
srand(clock());
|
||||
for(i=pos-rand()%11;i!=pos;i++){
|
||||
for(i=pos-rand()%6;i!=pos;i++){
|
||||
terrain[i*WORLD_WIDTH+(n>>3)] = 5;
|
||||
}
|
||||
}
|
||||
|
@ -206,6 +221,8 @@ void drawinventory(){
|
|||
dimage(26+i*15+3, 52, &wood_tile); break;
|
||||
case 6:
|
||||
dimage(26+i*15+3, 52, &woodplanks_tile); break;
|
||||
case 7:
|
||||
dimage(26+i*15+3, 52, &pickaxe_tool); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -227,6 +244,8 @@ void drawdetailinv(){
|
|||
dimage(5, pos, &wood_tile); break;
|
||||
case 6:
|
||||
dimage(5, pos, &woodplanks_tile); break;
|
||||
case 7:
|
||||
dimage(5, pos, &pickaxe_tool); break;
|
||||
}
|
||||
itoa(player.inventorynum[i], tmp_char);
|
||||
dtext(20, pos, C_BLACK, tmp_char);
|
||||
|
@ -257,6 +276,8 @@ void drawcrafting(){
|
|||
dimage(36, 28, &wood_tile); break;
|
||||
case 6:
|
||||
dimage(36, 28, &woodplanks_tile); break;
|
||||
case 7:
|
||||
dimage(36, 28, &pickaxe_tool); break;
|
||||
}
|
||||
dtext(36, 38, C_BLACK, MULTIPLY);
|
||||
itoa(craftingnum[crafting.selected], tmp_char);
|
||||
|
@ -277,6 +298,8 @@ void drawcrafting(){
|
|||
dtext(58, pos, C_BLACK, WOOD_TILE); break;
|
||||
case 6:
|
||||
dtext(58, pos, C_BLACK, WOODPLANKS_TILE); break;
|
||||
case 7:
|
||||
dtext(58, pos, C_BLACK, WOODPICKAXE_TILE); break;
|
||||
}
|
||||
itoa(craftingsnum[i], tmp_char);
|
||||
dtext(46, pos, C_BLACK, tmp_char);
|
||||
|
@ -286,13 +309,66 @@ void drawcrafting(){
|
|||
}
|
||||
dfont(dfont_default());
|
||||
}
|
||||
bool is_in(int item, int* array, int len){
|
||||
for(int i=0;i!=len;i++){
|
||||
if(array[i] == item){
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int main(void) {
|
||||
// Tools //
|
||||
// Hand
|
||||
hand.easy_lenght = 1;
|
||||
hand.middle_lenght = 1;
|
||||
hand.hard_lenght = 1;
|
||||
hand.very_hard_lenght = 1;
|
||||
hand.breakable_middle = NULL;
|
||||
hand.breakable_hard = NULL;
|
||||
hand.breakable_very_hard = NULL;
|
||||
hand.breakable_middle = malloc(sizeof(int));
|
||||
hand.breakable_hard = malloc(sizeof(int));
|
||||
hand.breakable_very_hard = malloc(sizeof(int));
|
||||
if(hand.breakable_middle == NULL || hand.breakable_hard == NULL || hand.breakable_very_hard == NULL){
|
||||
ERROR("Need more ram !", 2)
|
||||
}
|
||||
hand.breakable_middle[0] = 1;
|
||||
hand.breakable_hard[0] = 6;
|
||||
hand.breakable_very_hard[0] = 5;
|
||||
hand.attack = 1;
|
||||
// Wooden pickaxe
|
||||
wood_pickaxe.easy_lenght = 1;
|
||||
wood_pickaxe.middle_lenght = 1;
|
||||
wood_pickaxe.hard_lenght = 2;
|
||||
wood_pickaxe.very_hard_lenght = 3;
|
||||
wood_pickaxe.breakable_middle = NULL;
|
||||
wood_pickaxe.breakable_hard = NULL;
|
||||
wood_pickaxe.breakable_very_hard = NULL;
|
||||
wood_pickaxe.breakable_middle = malloc(sizeof(int));
|
||||
wood_pickaxe.breakable_hard = malloc(wood_pickaxe.hard_lenght*sizeof(int));
|
||||
wood_pickaxe.breakable_very_hard = malloc(wood_pickaxe.very_hard_lenght*sizeof(int));
|
||||
if(wood_pickaxe.breakable_middle == NULL || wood_pickaxe.breakable_hard == NULL || wood_pickaxe.breakable_very_hard == NULL){
|
||||
ERROR("Need more ram !", 3)
|
||||
}
|
||||
wood_pickaxe.breakable_middle[0] = 1;
|
||||
wood_pickaxe.breakable_hard[0] = 6;
|
||||
wood_pickaxe.breakable_hard[1] = 2;
|
||||
wood_pickaxe.breakable_very_hard[0] = 5;
|
||||
wood_pickaxe.breakable_very_hard[1] = 3;
|
||||
wood_pickaxe.breakable_very_hard[2] = 4;
|
||||
wood_pickaxe.attack = 2;
|
||||
///////////
|
||||
dclear(C_WHITE);
|
||||
dimage(16, TITLE_IMAGE_MARGIN, &title_img);
|
||||
dtext(1, TITLE_MARGIN, C_BLACK, TITLE_START);
|
||||
dupdate();
|
||||
int key = 0, game = 0, selected = 0, i, n;
|
||||
unsigned short terrain[WORLD_WIDTH*WORLD_HEIGHT];
|
||||
unsigned short int* terrain = NULL;
|
||||
terrain = malloc(WORLD_WIDTH*WORLD_HEIGHT*sizeof(unsigned short int));
|
||||
if(terrain == NULL){
|
||||
ERROR("Need more ram !", 0)
|
||||
}
|
||||
while(keydown(KEY_EXIT) == 0){
|
||||
////////// TITLE SCREEN //////////
|
||||
if(game == 0){
|
||||
|
@ -331,12 +407,12 @@ int main(void) {
|
|||
terrain[i] = 0;
|
||||
}
|
||||
srand(clock());
|
||||
generateworld(terrain, WORLD_WIDTH, WORLD_HEIGHT, (int)((rand() % ((WORLD_HEIGHT - 40) - (WORLD_HEIGHT - 50) + 1)) + WORLD_HEIGHT - 50), WORLD_HEIGHT - 50, WORLD_HEIGHT - 40, 0, 1);
|
||||
generateworld(terrain, WORLD_WIDTH, WORLD_HEIGHT, (int)((rand() % ((WORLD_HEIGHT - 50) - (WORLD_HEIGHT - 60) + 1)) + WORLD_HEIGHT - 60), WORLD_HEIGHT - 60, WORLD_HEIGHT - 50, 0, 1);
|
||||
srand(clock());
|
||||
generateworld(terrain, WORLD_WIDTH, WORLD_HEIGHT, (int)((rand() % ((WORLD_HEIGHT - 30) - (WORLD_HEIGHT - 40) + 1)) + WORLD_HEIGHT - 40), WORLD_HEIGHT - 40, WORLD_HEIGHT - 30, 3, 2);
|
||||
generateworld(terrain, WORLD_WIDTH, WORLD_HEIGHT, (int)((rand() % ((WORLD_HEIGHT - 40) - (WORLD_HEIGHT - 50) + 1)) + WORLD_HEIGHT - 50), WORLD_HEIGHT - 50, WORLD_HEIGHT - 40, 3, 2);
|
||||
srand(clock());
|
||||
generateworld(terrain, WORLD_WIDTH, WORLD_HEIGHT, (int)((rand() % ((WORLD_HEIGHT - 20) - (WORLD_HEIGHT - 30) + 1)) + WORLD_HEIGHT - 30), WORLD_HEIGHT - 30, WORLD_HEIGHT - 20, 4, 2);
|
||||
dtext(1, 1, C_BLACK, "Adding trees");
|
||||
generateworld(terrain, WORLD_WIDTH, WORLD_HEIGHT, (int)((rand() % ((WORLD_HEIGHT - 30) - (WORLD_HEIGHT - 40) + 1)) + WORLD_HEIGHT - 40), WORLD_HEIGHT - 40, WORLD_HEIGHT - 30, 4, 2);
|
||||
// dtext(1, 1, C_BLACK, "Adding trees");
|
||||
///// Add trees /////
|
||||
addtrees(terrain);
|
||||
/////////////////////
|
||||
|
@ -366,7 +442,15 @@ int main(void) {
|
|||
player.inventoryitems[i] = 0;
|
||||
player.inventorynum[i] = 0;
|
||||
}
|
||||
player.tool = hand;
|
||||
}else if(game == 3){
|
||||
clock_t begin = clock();
|
||||
switch(player.inventoryitems[player.invselect]){
|
||||
case 7:
|
||||
player.tool = wood_pickaxe; break;
|
||||
default:
|
||||
player.tool = hand; break;
|
||||
};
|
||||
player.select_tx = ((player.x+player.selx)>>3);
|
||||
player.select_ty = ((player.y+player.sely)>>3);
|
||||
clearevents();
|
||||
|
@ -445,14 +529,26 @@ int main(void) {
|
|||
}
|
||||
pos = ((player.y+player.sely)>>3)*WORLD_WIDTH+((player.x+player.selx)>>3);
|
||||
if(pos<=WORLD_WIDTH*WORLD_HEIGHT && pos>=0){
|
||||
player.is_breakable = 1;
|
||||
if(is_in(terrain[pos], player.tool.breakable_easy, player.tool.easy_lenght)){
|
||||
player.destroytime = 4;
|
||||
}else if(is_in(terrain[pos], player.tool.breakable_middle, player.tool.middle_lenght)){
|
||||
player.destroytime = 8;
|
||||
}else if(is_in(terrain[pos], player.tool.breakable_hard, player.tool.hard_lenght)){
|
||||
player.destroytime = 16;
|
||||
}else if(is_in(terrain[pos], player.tool.breakable_very_hard, player.tool.very_hard_lenght)){
|
||||
player.destroytime = 32;
|
||||
}else{
|
||||
player.is_breakable = 0;
|
||||
}
|
||||
clearevents();
|
||||
if (keydown(KEY_EXE) && terrain[pos] == 0 && player.inventorynum[player.invselect] != 0){
|
||||
if (keydown(KEY_EXE) && terrain[pos] == 0 && player.inventorynum[player.invselect] != 0 && is_in(terrain[pos], (int*)canbeplaced, canbeplaced_lenght)){
|
||||
terrain[pos] = player.inventoryitems[player.invselect];
|
||||
player.inventorynum[player.invselect]--;
|
||||
if(player.inventorynum[player.invselect] == 0) {
|
||||
player.inventoryitems[player.invselect] = 0;
|
||||
}
|
||||
}else if (keydown(KEY_1) && terrain[pos] != 0 && player.overlaytimer == player.destroytime && player.overlayframe == 4 && (player.select_tx == player.old_select_tx && player.select_ty == player.old_select_ty)){
|
||||
}else if (keydown(KEY_1) && terrain[pos] != 0 && player.is_breakable && player.overlaytimer == player.destroytime && player.overlayframe == 4 && (player.select_tx == player.old_select_tx && player.select_ty == player.old_select_ty)){
|
||||
for(i=0;i!=INVENTORY_SIZE;i++){
|
||||
if((terrain[pos] == player.inventoryitems[i] && player.inventorynum[i] < 64 && terrain[pos] != 0) || (player.inventoryitems[i] == 0 && player.inventorynum[i] == 0 && terrain[pos] != 0)) {
|
||||
player.inventoryitems[i] = terrain[pos];
|
||||
|
@ -464,7 +560,7 @@ int main(void) {
|
|||
player.overlayframe = 0;
|
||||
player.overlaytimer = 0;
|
||||
player.wasdestroyingbefore = 0;
|
||||
}else if (keydown(KEY_1) && terrain[pos] != 0 && (player.select_tx == player.old_select_tx && player.select_ty == player.old_select_ty)){
|
||||
}else if (keydown(KEY_1) && terrain[pos] != 0 && player.is_breakable && (player.select_tx == player.old_select_tx && player.select_ty == player.old_select_ty)){
|
||||
if(player.overlaytimer == player.destroytime){
|
||||
player.overlaytimer = 0;
|
||||
player.overlayframe++;
|
||||
|
@ -508,7 +604,12 @@ int main(void) {
|
|||
player.old_select_tx = player.select_tx;
|
||||
player.old_select_ty = player.select_ty;
|
||||
dupdate();
|
||||
sleep_ms(20);
|
||||
clock_t end = clock();
|
||||
double time_spent = (double)(end-begin)/CLOCKS_PER_SEC;
|
||||
int ms = time_spent*1000;
|
||||
if(ms < 20){
|
||||
sleep_ms(20-ms);
|
||||
}
|
||||
}else if(game == 4){
|
||||
dclear(C_WHITE);
|
||||
drawdetailinv();
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
# define STEEL_TILE "M. aci."
|
||||
# define WOOD_TILE "Bois"
|
||||
# define WOODPLANKS_TILE "Planche"
|
||||
# define WOODPICKAXE_TILE "B. pio."
|
||||
# define MULTIPLY "x"
|
||||
# define ITEM_BUILT "Item construit."
|
||||
# define ITEM_NOT_BUILT "Peut pas construire."
|
||||
|
|
|
@ -1,3 +1,17 @@
|
|||
|
||||
typedef struct Tool Tool;
|
||||
struct Tool{
|
||||
int* breakable_easy;
|
||||
int easy_lenght;
|
||||
int* breakable_middle;
|
||||
int middle_lenght;
|
||||
int* breakable_hard;
|
||||
int hard_lenght;
|
||||
int* breakable_very_hard;
|
||||
int very_hard_lenght;
|
||||
int attack;
|
||||
};
|
||||
|
||||
typedef struct Player Player;
|
||||
struct Player{
|
||||
int x;
|
||||
|
@ -22,6 +36,8 @@ struct Player{
|
|||
int old_select_ty;
|
||||
int wasdestroyingbefore;
|
||||
int destroytime;
|
||||
bool is_breakable;
|
||||
Tool tool;
|
||||
};
|
||||
|
||||
typedef struct Crafting Crafting;
|
||||
|
@ -33,17 +49,21 @@ struct Crafting{
|
|||
};
|
||||
const int craftingsitems[CRAFTINGS*CRAFTSIZE] = {
|
||||
5, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
6, 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
|
||||
4, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
5, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
const int craftingitem[CRAFTINGS] = {
|
||||
6,
|
||||
5
|
||||
5,
|
||||
7
|
||||
};
|
||||
const int craftingnum[CRAFTINGS] = {
|
||||
4,
|
||||
1,
|
||||
1
|
||||
};
|
Loading…
Reference in New Issue