20220708 - On peut sauter, casser des blocs et poser des blocs. Structure pour le player.

This commit is contained in:
mibi88 2022-07-08 19:19:05 +02:00
parent b48e5e2063
commit 64cd3fffad
4 changed files with 172 additions and 32 deletions

23
src/itoa.h Normal file
View File

@ -0,0 +1,23 @@
// From https://www.planet-casio.com/Fr/forums/topic14992-1-programmer-en-c-sur-graph-3575-e.html
/* itoa: convert n to characters in s */
void itoa(int n, char s[])
{
int i, j, sign;
char c;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}

View File

@ -8,6 +8,7 @@
# include "itemsizes.h"
# include "gamesettings.h"
# include "worldgen.h"
# include "itoa.h"
extern bopti_image_t title_img;
extern bopti_image_t soil_tile;
@ -20,8 +21,13 @@ extern bopti_image_t skin1_player_left1;
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;
int x, y, pos, hour, animation, orient, timing, falling;
# include "player.h"
//int x, y, pos, hour, animation, orient, timing, falling, jumping, jumpheight;
int pos, hour;
char tmp_char[2];
void drawselectedgame(int selected) {
dclear(C_WHITE);
@ -133,6 +139,7 @@ int collisiononmap(int x, int y, unsigned short int * terrain, int testx, int te
}
return 0;
}
Player player;
int main(void) {
dclear(C_WHITE);
dimage(16, TITLE_IMAGE_MARGIN, &title_img);
@ -187,47 +194,54 @@ int main(void) {
///// Add trees /////
addtrees(terrain);
/////////////////////
x = 0;
player.x = 0;
/*for(i=0;i!=WORLD_HEIGHT;i++){
if(terrain[i*WORLD_WIDTH+(x>>3)] != 0){
y = i*8-8;
break;
}
} */
y = 0;
player.y = 0;
game = 3;
hour = 0;
timing = 0;
player.timing = 0;
player.jumping = 0;
player.jumpheight = 0;
player.falling = 0;
for(i=0;i!=INVENTORY_SIZE;i++){
player.inventoryitems[i] = 0;
player.inventorynum[i] = 0;
}
}else if(game == 3){
clearevents();
if(keydown(KEY_RIGHT) && (collisiononmap(x, y, terrain, 67, 35) == 0 && collisiononmap(x, y, terrain, 67, 25) == 0)){
x++;
if(animation == 0 && timing == 5){
animation = 1;
timing = 0;
}else if(animation == 1 && timing == 5){
animation = 0;
timing = 0;
if(keydown(KEY_RIGHT) && (collisiononmap(player.x, player.y, terrain, 67, 35) == 0 && collisiononmap(player.x, player.y, terrain, 67, 25) == 0)){
player.x++;
if(player.animation == 0 && player.timing == 5){
player.animation = 1;
player.timing = 0;
}else if(player.animation == 1 && player.timing == 5){
player.animation = 0;
player.timing = 0;
}
timing++;
orient = 1;
}else if(keydown(KEY_LEFT) && (collisiononmap(x, y, terrain, 61, 35) == 0 && collisiononmap(x, y, terrain, 61, 25) == 0)){
x--;
if(animation == 0 && timing == 5){
animation = 1;
timing = 0;
}else if(animation == 1 && timing == 5){
animation = 0;
timing = 0;
player.timing++;
player.orient = 1;
}else if(keydown(KEY_LEFT) && (collisiononmap(player.x, player.y, terrain, 61, 35) == 0 && collisiononmap(player.x, player.y, terrain, 61, 25) == 0)){
player.x--;
if(player.animation == 0 && player.timing == 5){
player.animation = 1;
player.timing = 0;
}else if(player.animation == 1 && player.timing == 5){
player.animation = 0;
player.timing = 0;
}
timing++;
orient = 3;
player.timing++;
player.orient = 3;
}
if(collisiononmap(x, y, terrain, 61, 40) == 0 && collisiononmap(x, y, terrain, 67, 40) == 0 && collisiononmap(x, y, terrain, 60, 40) == 0){
y++;
falling = 1;
if(collisiononmap(player.x, player.y, terrain, 62, 40) == 0 && collisiononmap(player.x, player.y, terrain, 66, 40) == 0 && collisiononmap(player.x, player.y, terrain, 60, 40) == 0 && player.jumping == 0){
player.y++;
player.falling = 1;
}else{
falling = 0;
player.falling = 0;
}
/* clearevents();
if(keydown(KEY_UP)){
@ -236,16 +250,102 @@ int main(void) {
y++;
} */
clearevents();
if(keydown(KEY_SHIFT)){
//
if(keydown(KEY_SHIFT) && player.jumping == 0 && player.falling == 0 && player.jumpheight == 0){
player.jumping = 1;
player.jumpheight = 1;
}
if(player.jumping == 1 && player.jumpheight == 12){
player.jumping = 0;
player.jumpheight = 0;
}else if(collisiononmap(player.x, player.y, terrain, 62, 23) || collisiononmap(player.x, player.y, terrain, 66, 23) || collisiononmap(player.x, player.y, terrain, 60, 23)){
player.jumping = 0;
player.jumpheight = 0;
}else if(player.jumping == 1){
player.jumpheight++;
player.y--;
}
if(exists(terrain, 5)==0){
addtrees(terrain);
}
if(player.y>>3>WORLD_HEIGHT){
player.y = 0;
}
clearevents();
if (keydown(KEY_8) && player.sely>-8){
player.sely--;
}else if (keydown(KEY_5) && player.sely<24){
player.sely++;
}
clearevents();
if (keydown(KEY_4) && player.selx>-8){
player.selx--;
}else if (keydown(KEY_6) && player.selx<16){
player.selx++;
}
pos = ((player.y+player.sely)>>3)*WORLD_WIDTH+((player.x+player.selx)>>3);
if(pos<=WORLD_WIDTH*WORLD_HEIGHT && pos>=0){
clearevents();
if (keydown(KEY_EXE) && terrain[pos] == 0 && player.inventorynum[player.invselect] != 0){
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){
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];
terrain[pos] = 0;
player.inventorynum[i]++;
break;
}
}
}
}
clearevents();
if (keydown(KEY_0)){
if(player.invselect<INVENTORY_SIZE-1){
player.invselect++;
}else{
player.invselect = 0;
}
}
clearevents();
if(keydown(KEY_MENU)){
game = 4;
}
// dtext(1, 1, C_BLACK, "test");
mappartdisplaying(x, y, terrain, orient+animation);
mappartdisplaying(player.x, player.y, terrain, player.orient+player.animation);
dimage(player.selx + 56, player.sely + 20, &select_tool);
dupdate();
sleep_ms(20);
}else if(game == 4){
dclear(C_WHITE);
for(i=0;i!=INVENTORY_SIZE;i++){
pos = i*8;
switch(player.inventoryitems[i]){
case 1:
dimage(5, pos, &soil_tile); break;
case 2:
dimage(5, pos, &stone_tile); break;
case 3:
dimage(5, pos, &coal_tile); break;
case 4:
dimage(5, pos, &steel_tile); break;
case 5:
dimage(5, pos, &wood_tile); break;
}
itoa(player.inventorynum[i], tmp_char);
dtext(20, pos, C_BLACK, tmp_char);
if(i == player.invselect){
dtext(60, pos, C_BLACK, SELECTED);
}
}
clearevents();
if(keydown(KEY_EXE)){
game = 3;
}
dupdate();
}
clearevents();
}

View File

@ -1,3 +1,4 @@
# define TITLE_START "[EXE] pour jouer" // Start screen text.
# define WORLDSEL_EMPTY "--- VIDE ---" // Empty slot in the game selection menu.
# define WORLDGEN_INFO "Generation ..." // Text when waiting for world generation.
# define WORLDGEN_INFO "Generation ..." // Text when waiting for world generation.
# define SELECTED "<" // Selected block info.

16
src/player.h Normal file
View File

@ -0,0 +1,16 @@
typedef struct Player Player;
struct Player{
int x;
int y;
int falling;
int jumping;
int jumpheight;
int selx;
int sely;
int orient;
int timing;
int animation;
int inventoryitems[INVENTORY_SIZE];
int inventorynum[INVENTORY_SIZE];
int invselect;
};