From ed983faaf48b20d0091e6635245477887fc726cf Mon Sep 17 00:00:00 2001 From: Gladosse Date: Thu, 8 Jun 2023 23:16:00 +0200 Subject: [PATCH] Upload files to 'src' --- src/animation.c | 92 +++++++++++++++++++++++++++++ src/animation.h | 29 ++++++++++ src/engine.h | 39 +++++++++++++ src/main.c | 112 ++++++++++++++++++++++++++++++++++++ src/stalactites.c | 144 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 416 insertions(+) create mode 100644 src/animation.c create mode 100644 src/animation.h create mode 100644 src/engine.h create mode 100644 src/main.c create mode 100644 src/stalactites.c diff --git a/src/animation.c b/src/animation.c new file mode 100644 index 0000000..215f3c5 --- /dev/null +++ b/src/animation.c @@ -0,0 +1,92 @@ +#include +#include +#include "animation.h" +#include "engine.h" + +struct sheet +{ + bopti_image_t *img; + int frame_w; + int frame_h; +}; + +extern bopti_image_t img_spritesheet; +struct sheet const anim_player = { + .img = &img_spritesheet, + .frame_w = 64, + .frame_h = 64, +}; + +extern bopti_image_t img_wind; +struct sheet const anim_wind = { + .img = &img_wind, + .frame_w = 48, + .frame_h = 224, +}; + +static struct anim_frame anim_frame(struct sheet const *sheet, int col,int row) +{ + struct anim_frame f = { + .source = sheet->img, + .left = sheet->frame_w * col, + .top = sheet->frame_h * row, + .w = sheet->frame_w, + .h = sheet->frame_h, + }; + return f; +} + +int anim_player_idle(struct anim_data *data, int init) +{ + if(init) + { + data->function = anim_player_idle; + data->frame = 0; + } + data->img = anim_frame(&anim_player, 0, 0); + return 0; +} + +void dframe(int x, int y, struct anim_frame const frame) +{ + dsubimage(x, y, frame.source, frame.left, frame.top, frame.w, frame.h, DIMAGE_NONE); +} + +int anim_player_jump(struct anim_data *data, int init){ + if(init) + { + data->function = anim_player_jump; + data->frame = 0; + data->duration = 30; + } + else + { + if(data->frame >= 9) + { + return anim_player_idle(data, 0); + } + + data->frame = data->frame + 1; + data->duration += 30; + } + data->img = anim_frame(&anim_player, data->frame, 0); + return 1; +} + +int anim_wind_idle(struct anim_data *data, int init){ + if(init) + { + data->function = anim_wind_idle; + data->frame = 0; + data->duration = 100; + } + else + { + data->frame = (data->frame + 1) % 12; + data->duration += 100; + } + + data->img = anim_frame(&anim_wind, data->frame, 0); + return 0; +} + diff --git a/src/animation.h b/src/animation.h new file mode 100644 index 0000000..f244bf2 --- /dev/null +++ b/src/animation.h @@ -0,0 +1,29 @@ +#ifndef _IZI_ANIMATION_H +#define _IZI_ANIMATION_H +#include + +struct anim_data; + +typedef int (anim_function_t)(struct anim_data *data, int init); +anim_function_t anim_player_idle; +anim_function_t anim_player_jump; +anim_function_t anim_wind_idle; + +struct anim_frame +{ + bopti_image_t *source; + int left, top; + int w, h; +}; + +struct anim_data +{ + anim_function_t *function; + struct anim_frame img; + int frame; + int duration; +}; + +void dframe(int x, int y, struct anim_frame const frame); + +#endif \ No newline at end of file diff --git a/src/engine.h b/src/engine.h new file mode 100644 index 0000000..1325c0e --- /dev/null +++ b/src/engine.h @@ -0,0 +1,39 @@ +#ifndef _IZI_ENGINE_H +#define _IZI_ENGINE_H + +#define ENGINE_TICK 25 +#define DIR_UP 1 + +#include +#include +#include "animation.h" + +struct player{ + float y; + float yvelocity; + struct anim_data anim; + int idle; + int frame; +}; + +struct game{ + struct player* player; + int time; + struct background* background; + struct stalactite* stalactites; + struct wind* wind; + int score; +}; + +struct background{ + int x; +}; + +void e_draw_player(struct game const *game); +void e_jump(struct player *player); +void e_update_player(struct game *game); +void engine_tick(struct game *game, int dt); +void e_draw_background(struct background *background); +int e_is_dead(struct game const *game); + +#endif /* _IZI_ENGINE_H */ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..2db552b --- /dev/null +++ b/src/main.c @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include "engine.h" +#include "stalactites.h" +#include "wind.h" + +volatile int timeout = 500; + +static int callback_tick(volatile int *tick) +{ + *tick = 1; + return TIMER_CONTINUE; +} + +int main(void) +{ + int FIRSTTIME = 1; + +play: + static volatile int tick = 1; + int t = timer_configure(TIMER_ANY, ENGINE_TICK*1000, + GINT_CALL(callback_tick, &tick)); + if(t < 0) return false; + timer_start(t); + + struct player player = { + .y = 108, + .yvelocity = 0, + .anim.function = anim_player_idle + }; + + struct wind wind = { + .anim.function = anim_wind_idle + }; + + struct background background = { + .x = 0 + }; + + struct stalactite stalactites[2]; + + struct game game = { + .player = &player, + .time = 0, + .background = &background, + .stalactites = stalactites, + .wind = &wind, + .score = 0 + }; + + player.idle = !anim_player_idle(&player.anim, 1); + stalactites_init(stalactites); + + while(FIRSTTIME) + { //MENU + dclear(C_WHITE); + e_draw_background(&background); + e_draw_player(&game); + extern bopti_image_t img_startmenu; + dimage(0, 0, &img_startmenu); + dupdate(); + int key = getkey_opt(GETKEY_REP_ALL, &timeout).key; + if (key == KEY_SHIFT) break; + } + + FIRSTTIME = 0; + + while(1) //PLAYING + { + while(!tick) sleep(); + tick = 0; + + dclear(C_WHITE); + int key = getkey_opt(GETKEY_REP_ALL, &timeout).key; + if (key == KEY_UP) e_jump(&player); + if (key == KEY_MENU) goto stop; + e_draw_background(&background); + stalactites_draw(stalactites, &wind); + e_draw_player(&game); + e_update_player(&game); + dupdate(); + + if(e_is_dead(&game)) break; + + engine_tick(&game, ENGINE_TICK); + } + + while(1)//gameover + { + dclear(C_WHITE); + e_draw_background(&background); + stalactites_draw(stalactites, &wind); + e_draw_player(&game); + extern bopti_image_t img_gameover; + dimage(0, 0, &img_gameover); + dupdate(); + int key = getkey_opt(GETKEY_REP_ALL, &timeout).key; + if (key == KEY_SHIFT){ + timer_stop(t); + goto play; + }; + if (key == KEY_MENU) break; + } + +stop: + timer_stop(t); + + return 1; +} + diff --git a/src/stalactites.c b/src/stalactites.c new file mode 100644 index 0000000..49cf8f4 --- /dev/null +++ b/src/stalactites.c @@ -0,0 +1,144 @@ +#include +#include +#include +#include + +#include "stalactites.h" +#include "wind.h" + +void stalactites_init(struct stalactite* stalactites) +{ + stalactites[0].x = 600; + stalactites[1].x = 840; + int type1 = rand()%4; + srand(rtc_ticks()); + int type2; + do{ + type2 = rand()%4; + srand(rtc_ticks()); + } + while(type1==type2); + stalactites[0].type = type1; + stalactites[1].type = type2; + stalactites[0].wind = 0; + stalactites[1].wind = 0; +} + +void stalactites_update(struct stalactite* stalactites, struct game* game) +{ + for(int i=0; i<2; i++){ + stalactites[i].x -= 4; + if(stalactites[i].x <= -76){ + stalactites[i].x = stalactites[(i+1) % 2].x + 240; + int type = rand()%4; + while(type==stalactites[(i+1) % 2].type){ + srand(rtc_ticks()); + type = rand()%4; + } + stalactites[i].type = type; + game->score +=1; + if(game->score > 4){ + if(game->score % 5 == 0) + stalactites[i].wind = 1; + else + stalactites[i].wind = 0; + } + } + } +} + +void stalactites_draw(struct stalactite* const stalactites, struct wind* const wind) +{ + extern bopti_image_t img_stalactites; + for(int i=0;i<2;i++){ + dsubimage(stalactites[i].x, 0, &img_stalactites, 79*stalactites[i].type, 0, 79, 224, DIMAGE_NONE); + if(stalactites[i].wind == 1) + wind_draw(&(stalactites[i]), wind); + } + +} + +int stalactites_collide(struct game const *game) +{ + int testing_num = (game->stalactites[1].x < game->stalactites[0].x) ? 1 : 0; + int hitbox[8]; + int type = game->stalactites[testing_num].type; + + // GETTING HITBOX + if(type==TOP){ + hitbox[0] = 17; + hitbox[1] = 0; + hitbox[2] = 48; + hitbox[3] = 52; + hitbox[4] = 19; + hitbox[5] = 109; + hitbox[6] = 51; + hitbox[7] = 224; + /* { + 17, 0, //topstart + 52, 52,//topfinish + 13, 110, + 51, 224 + }; */ + } + else if(type==HIGH){ + hitbox[0] = 16; + hitbox[1] = 0; + hitbox[2] = 47; + hitbox[3] = 90; + hitbox[4] = 24; + hitbox[5] = 143; + hitbox[6] = 57; + hitbox[7] = 224; + /*int hitbox[] = { + 15, 0, + 52, 87, + 23, 141, + 50, 224 + };*/ + } + else if(type==LOW){ + hitbox[0] = 22; + hitbox[1] = 0; + hitbox[2] = 53; + hitbox[3] = 104; + hitbox[4] = 22; + hitbox[5] = 164; + hitbox[6] = 47; + hitbox[7] = 224; + /*int hitbox[] = { + 16, 0, + 61, 105, + 18, 163, + 55, 224 + };*/ + } + else if(type==BOTTOM){ + hitbox[0] = 25; + hitbox[1] = 0; + hitbox[2] = 58; + hitbox[3] = 137; + hitbox[4] = 26; + hitbox[5] = 197; + hitbox[6] = 59; + hitbox[7] = 224; + /*int hitbox[] = { + 23, 0, + 57, 141, + 16, 197, + 57, 224 + };*/ + } + + int playerx = 78; + int playery = game->player->y + 25; + int stalactitex = game->stalactites[testing_num].x; + for(int i=0; i<8; i+=2) + hitbox[i] = hitbox[i] + stalactitex; + if( + (!(playery>hitbox[3] && playeryhitbox[0] && playerxhitbox[4] && playerx