diff --git a/inc/attack.h b/inc/attack.h index ef6f81e..d7506bc 100644 --- a/inc/attack.h +++ b/inc/attack.h @@ -2,19 +2,19 @@ #include "type.h" struct Attack { - struct Vec2 pos; - struct FVec2 dir; - int type; - int active; - int w, h; - int life; - int ice; - int dam; + struct Vec2 pos; + struct FVec2 dir; + int type; + int active; + int w, h; + int life; + int ice; + int dam; }; struct Attack_table { - int n; - struct Attack *attacks; + int n; + struct Attack *attacks; }; void attack_table_init(void); @@ -23,4 +23,4 @@ void attack_table_draw(int timer); void attack_destroy(struct Attack *attack); int attack(int x, int y, int dir_x, int dir_y, int type); int attack_nb(void); -struct Attack * attack_table_collide(int x, int y, int w, int h, int hitbox); +struct Attack *attack_table_collide(int x, int y, int w, int h, int hitbox); diff --git a/inc/bullet.h b/inc/bullet.h index ced25cc..13ed393 100644 --- a/inc/bullet.h +++ b/inc/bullet.h @@ -2,15 +2,15 @@ #include "type.h" struct Bullet { - struct Vec2 pos; - struct Vec2 dir; - int v; - int active; + struct Vec2 pos; + struct Vec2 dir; + int v; + int active; }; struct Bullet_table { - int n; - struct Bullet *bullets; + int n; + struct Bullet *bullets; }; void bullet_table_init(void); diff --git a/inc/conf.h b/inc/conf.h index 7c6278e..ba530c4 100644 --- a/inc/conf.h +++ b/inc/conf.h @@ -7,14 +7,14 @@ #define PLAYER_ID 2 #define STAGE_ID 1 -#define PLAYER_ACC 3 -#define PLAYER_FRIC 0.5 -#define AIR_FRIC 0.2 +#define PLAYER_ACC 3 +#define PLAYER_FRIC 0.5 +#define AIR_FRIC 0.2 #define BASE_COOLDOWN 10 -#define GRAVITY 1 -#define JUMP_H 10 +#define GRAVITY 1 +#define JUMP_H 10 -#define TILE_S 24 +#define TILE_S 24 #define TILESET_W 23 #define TILESET_H 15 diff --git a/inc/level.h b/inc/level.h index ebb969f..792628b 100644 --- a/inc/level.h +++ b/inc/level.h @@ -3,8 +3,8 @@ #include "type.h" struct Level { - int w, h, s; /* width, height, size */ - tile_t *data; + int w, h, s; /* width, height, size */ + tile_t *data; }; void level_init(int lvl_id, int chara); diff --git a/inc/lzy.h b/inc/lzy.h index 9e1b7d5..e6b8341 100644 --- a/inc/lzy.h +++ b/inc/lzy.h @@ -35,12 +35,12 @@ extern "C" { typedef void LZY_Music; typedef void LZY_Sound; enum LZY_ScanCode { - LZYK_LEFT = KEY_LEFT, - LZYK_RIGHT = KEY_RIGHT, - LZYK_UP = KEY_UP, - LZYK_DOWN = KEY_DOWN, - LZYK_O = KEY_SHIFT, - LZYK_X = KEY_OPTN, + LZYK_LEFT = KEY_LEFT, + LZYK_RIGHT = KEY_RIGHT, + LZYK_UP = KEY_UP, + LZYK_DOWN = KEY_DOWN, + LZYK_O = KEY_SHIFT, + LZYK_X = KEY_OPTN, }; #else /* end FXCG50, begin SDL2 */ #ifndef LZY_SDL_MIXER_INCLUDE @@ -48,36 +48,36 @@ enum LZY_ScanCode { #endif #include LZY_SDL_MIXER_INCLUDE enum LZY_ScanCode { - LZYK_LEFT, - LZYK_RIGHT, - LZYK_UP, - LZYK_DOWN, - LZYK_O, - LZYK_X, - LZYK_COUNT, + LZYK_LEFT, + LZYK_RIGHT, + LZYK_UP, + LZYK_DOWN, + LZYK_O, + LZYK_X, + LZYK_COUNT, }; typedef Mix_Music LZY_Music; typedef Mix_Chunk LZY_Sound; #endif /* SDL2 */ enum LZY_EventType { - LZY_QUIT, - LZY_KEYDOWN, - LZY_KEYUP, + LZY_QUIT, + LZY_KEYDOWN, + LZY_KEYUP, }; typedef struct LZY_KeyEvent { - unsigned int scancode; - int down; + unsigned int scancode; + int down; } LZY_KeyEvent; typedef union LZY_EventUnion { - LZY_KeyEvent key; + LZY_KeyEvent key; } LZY_EventUnion; typedef struct LZY_Event { - int type; - LZY_EventUnion u; + int type; + LZY_EventUnion u; } LZY_Event; int LZY_Init(int argc, const char **argv, const char *title, int target_fps, @@ -148,41 +148,41 @@ const char *LZY_GetError(void); #endif int LZY_DrawLine(int x0, int y0, int x1, int y1) { - int dx, dy, sx, sy, err, e2; - int rc = 0; + int dx, dy, sx, sy, err, e2; + int rc = 0; - dx = x1 - x0; - dx = (dx < 0) ? (-dx) : (dx); - dy = y1 - y0; - dy = (dy < 0) ? (dy) : (-dy); - sx = (x0 < x1) ? (1) : (-1); - sy = (y0 < y1) ? (1) : (-1); - err = dx + dy; + dx = x1 - x0; + dx = (dx < 0) ? (-dx) : (dx); + dy = y1 - y0; + dy = (dy < 0) ? (dy) : (-dy); + sx = (x0 < x1) ? (1) : (-1); + sy = (y0 < y1) ? (1) : (-1); + err = dx + dy; - for (;;) { - if (LZY_DrawPoint(x0, y0)) - rc = -1; + for (;;) { + if (LZY_DrawPoint(x0, y0)) + rc = -1; - if (x0 == x1 && y0 == y1) - break; + if (x0 == x1 && y0 == y1) + break; - e2 = 2 * err; - if (e2 >= dy) { - err += dy; - x0 += sx; - } - if (e2 <= dx) { - err += dx; - y0 += sy; - } - } + e2 = 2 * err; + if (e2 >= dy) { + err += dy; + x0 += sx; + } + if (e2 <= dx) { + err += dx; + y0 += sy; + } + } - return rc; + return rc; } #ifdef FXCG50 -#include #include +#include #include #include #include @@ -205,282 +205,268 @@ static int timer_callback(volatile int *); int LZY_Init(int argc, const char **argv, const char *title, int target_fps, const char *tset_path, const char *font_path) { #ifdef LZY_GINT_TILESET - extern bopti_image_t LZY_GINT_TILESET; - tset_width = LZY_GINT_TILESET.width / LZY_TILE_SIZE; - tset_height = LZY_GINT_TILESET.height / LZY_TILE_SIZE; + extern bopti_image_t LZY_GINT_TILESET; + tset_width = LZY_GINT_TILESET.width / LZY_TILE_SIZE; + tset_height = LZY_GINT_TILESET.height / LZY_TILE_SIZE; #endif #ifdef LZY_GINT_FONT - extern bopti_image_t LZY_GINT_FONT; - font_width = LZY_GINT_FONT.width / LZY_CHR_WIDTH; - font_height = LZY_GINT_FONT.height / LZY_CHR_HEIGHT; + extern bopti_image_t LZY_GINT_FONT; + font_width = LZY_GINT_FONT.width / LZY_CHR_WIDTH; + font_height = LZY_GINT_FONT.height / LZY_CHR_HEIGHT; #endif - LZY_UNUSED(argc); - LZY_UNUSED(argv); - LZY_UNUSED(title); - LZY_UNUSED(tset_path); - LZY_UNUSED(font_path); + LZY_UNUSED(argc); + LZY_UNUSED(argv); + LZY_UNUSED(title); + LZY_UNUSED(tset_path); + LZY_UNUSED(font_path); - if (target_fps > 0) { - timer = timer_configure(TIMER_ANY, 1000000 / target_fps, - GINT_CALL(timer_callback, &has_ticked)); - timer_start(timer); - } - return 0; + if (target_fps > 0) { + timer = timer_configure(TIMER_ANY, 1000000 / target_fps, + GINT_CALL(timer_callback, &has_ticked)); + timer_start(timer); + } + return 0; } static int timer_callback(volatile int *arg) { - *arg += 1; - return 0; + *arg += 1; + return 0; } void LZY_Quit(void) { - if (timer) { - timer_stop(timer); - timer = 0; - } + if (timer) { + timer_stop(timer); + timer = 0; + } } -int LZY_DrawBegin(void) { - return 0; -} +int LZY_DrawBegin(void) { return 0; } int LZY_DrawEnd(void) { - dupdate(); - while (timer && !has_ticked) - sleep(); - has_ticked = 0; - return 0; + dupdate(); + while (timer && !has_ticked) + sleep(); + has_ticked = 0; + return 0; } void LZY_DrawSetColor(uint8_t r, uint8_t g, uint8_t b) { - draw_color = C_RGB(r / 8, g / 8, b / 8); + draw_color = C_RGB(r / 8, g / 8, b / 8); } -void LZY_DrawSetColorNone(void) { - draw_color = C_NONE; -} +void LZY_DrawSetColorNone(void) { draw_color = C_NONE; } int LZY_DrawClear(void) { - dclear(draw_color); - return 0; + dclear(draw_color); + return 0; } int LZY_DrawPoint(int x, int y) { - dpixel(x + draw_off_x, y + draw_off_y, draw_color); - return 0; + dpixel(x + draw_off_x, y + draw_off_y, draw_color); + return 0; } int LZY_DrawRect(int x, int y, unsigned int w, unsigned int h) { - if (w < 1 || h < 1) - return -1; - x += draw_off_x; - y += draw_off_y; + if (w < 1 || h < 1) + return -1; + x += draw_off_x; + y += draw_off_y; - drect_border(x, y, x + w - 1, y + h - 1, C_NONE, 1, draw_color); - return 0; + drect_border(x, y, x + w - 1, y + h - 1, C_NONE, 1, draw_color); + return 0; } int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h) { - if (w < 1 || h < 1) - return -1; - x += draw_off_x; - y += draw_off_y; + if (w < 1 || h < 1) + return -1; + x += draw_off_x; + y += draw_off_y; - drect(x, y, x + w - 1, y + h - 1, draw_color); - return 0; + drect(x, y, x + w - 1, y + h - 1, draw_color); + return 0; } int LZY_DrawTile(unsigned int id, int x, int y) { #ifndef LZY_GINT_TILESET - LZY_UNUSED(id); - LZY_UNUSED(x); - LZY_UNUSED(y); - return -1; + LZY_UNUSED(id); + LZY_UNUSED(x); + LZY_UNUSED(y); + return -1; #else - extern bopti_image_t LZY_GINT_TILESET; - int ix, iy; + extern bopti_image_t LZY_GINT_TILESET; + int ix, iy; - if (id >= tset_width * tset_height) - return -1; - x += draw_off_x; - y += draw_off_y; + if (id >= tset_width * tset_height) + return -1; + x += draw_off_x; + y += draw_off_y; - ix = (id % tset_width) * LZY_TILE_SIZE; - iy = (id / tset_width) * LZY_TILE_SIZE; - dsubimage(x, y, &LZY_GINT_TILESET, ix, iy, LZY_TILE_SIZE, LZY_TILE_SIZE, - DIMAGE_NONE); + ix = (id % tset_width) * LZY_TILE_SIZE; + iy = (id / tset_width) * LZY_TILE_SIZE; + dsubimage(x, y, &LZY_GINT_TILESET, ix, iy, LZY_TILE_SIZE, LZY_TILE_SIZE, + DIMAGE_NONE); - return 0; + return 0; #endif } int LZY_DrawTileEx(unsigned int id, int x, int y, unsigned int w, unsigned int h) { #ifndef LZY_GINT_TILESET - LZY_UNUSED(id); - LZY_UNUSED(x); - LZY_UNUSED(y); - LZY_UNUSED(w); - LZY_UNUSED(h); - return -1; + LZY_UNUSED(id); + LZY_UNUSED(x); + LZY_UNUSED(y); + LZY_UNUSED(w); + LZY_UNUSED(h); + return -1; #else - extern bopti_image_t LZY_GINT_TILESET; - int ix, iy; + extern bopti_image_t LZY_GINT_TILESET; + int ix, iy; - if (w * h == 0) - return -1; + if (w * h == 0) + return -1; - x += draw_off_x; - y += draw_off_y; + x += draw_off_x; + y += draw_off_y; - ix = id % tset_width; - iy = id / tset_width; + ix = id % tset_width; + iy = id / tset_width; - if (ix >= (int)tset_width || ix + w - 1 >= tset_width || - iy >= (int)tset_height || iy + h - 1 >= tset_height) - return -1; + if (ix >= (int)tset_width || ix + w - 1 >= tset_width || + iy >= (int)tset_height || iy + h - 1 >= tset_height) + return -1; - ix *= LZY_TILE_SIZE; - iy *= LZY_TILE_SIZE; - dsubimage(x, y, &LZY_GINT_TILESET, ix, iy, w * LZY_TILE_SIZE, - h * LZY_TILE_SIZE, DIMAGE_NONE); + ix *= LZY_TILE_SIZE; + iy *= LZY_TILE_SIZE; + dsubimage(x, y, &LZY_GINT_TILESET, ix, iy, w * LZY_TILE_SIZE, + h * LZY_TILE_SIZE, DIMAGE_NONE); - return 0; + return 0; #endif } int LZY_DrawChar(unsigned char chr, int x, int y) { #ifndef LZY_GINT_FONT - LZY_UNUSED(chr); - LZY_UNUSED(x); - LZY_UNUSED(y); - return -1; + LZY_UNUSED(chr); + LZY_UNUSED(x); + LZY_UNUSED(y); + return -1; #else - extern bopti_image_t LZY_GINT_FONT; - const unsigned int id = (unsigned int)chr - LZY_FIRST_CHR; - int ix, iy; + extern bopti_image_t LZY_GINT_FONT; + const unsigned int id = (unsigned int)chr - LZY_FIRST_CHR; + int ix, iy; - if (id >= font_width * font_height) - return -1; - x += draw_off_x; - y += draw_off_y; + if (id >= font_width * font_height) + return -1; + x += draw_off_x; + y += draw_off_y; - ix = (id % font_width) * LZY_CHR_WIDTH; - iy = (id / font_width) * LZY_CHR_HEIGHT; - dsubimage(x, y, &LZY_GINT_FONT, ix, iy, LZY_CHR_WIDTH, LZY_CHR_HEIGHT, - DIMAGE_NONE); + ix = (id % font_width) * LZY_CHR_WIDTH; + iy = (id / font_width) * LZY_CHR_HEIGHT; + dsubimage(x, y, &LZY_GINT_FONT, ix, iy, LZY_CHR_WIDTH, LZY_CHR_HEIGHT, + DIMAGE_NONE); - return 0; + return 0; #endif } int LZY_DrawText(const char *text, int x, int y) { #ifndef LZY_GINT_FONT - LZY_UNUSED(text); - LZY_UNUSED(x); - LZY_UNUSED(y); - return -1; + LZY_UNUSED(text); + LZY_UNUSED(x); + LZY_UNUSED(y); + return -1; #else - int err = 0; + int err = 0; - if (text == NULL) - return -1; + if (text == NULL) + return -1; - for (; *text != '\0'; text++) { - const int rc = LZY_DrawChar(*text, x, y); - if (rc) - err = rc - 1; - x += LZY_CHR_WIDTH; - } + for (; *text != '\0'; text++) { + const int rc = LZY_DrawChar(*text, x, y); + if (rc) + err = rc - 1; + x += LZY_CHR_WIDTH; + } - return err; + return err; #endif } LZY_Music *LZY_MusicLoad(const char *path) { - LZY_UNUSED(path); - return NULL; + LZY_UNUSED(path); + return NULL; } int LZY_MusicDestroy(LZY_Music *music) { - LZY_UNUSED(music); - return -1; + LZY_UNUSED(music); + return -1; } int LZY_MusicPlay(LZY_Music *music, int loops) { - LZY_UNUSED(music); - LZY_UNUSED(loops); - return -1; + LZY_UNUSED(music); + LZY_UNUSED(loops); + return -1; } LZY_Sound *LZY_SoundLoad(const char *path) { - LZY_UNUSED(path); - return NULL; + LZY_UNUSED(path); + return NULL; } int LZY_SoundDestroy(LZY_Sound *sound) { - LZY_UNUSED(sound); - return -1; + LZY_UNUSED(sound); + return -1; } void LZY_SoundSetVolume(LZY_Sound *sound, float volume) { - LZY_UNUSED(sound); - LZY_UNUSED(volume); + LZY_UNUSED(sound); + LZY_UNUSED(volume); } int LZY_SoundPlay(LZY_Sound *sound, int loops) { - LZY_UNUSED(sound); - LZY_UNUSED(loops); - return -1; + LZY_UNUSED(sound); + LZY_UNUSED(loops); + return -1; } int LZY_PollEvent(LZY_Event *e) { - for (;;) { - const key_event_t gk_e = pollevent(); + for (;;) { + const key_event_t gk_e = pollevent(); - switch (gk_e.type) { - case KEYEV_NONE: - return 0; - case KEYEV_DOWN: - e->type = LZY_KEYDOWN; - e->u.key.scancode = gk_e.key; - if (gk_e.key == KEY_EXIT) - should_quit = 1; - return 1; - case KEYEV_UP: - e->type = LZY_KEYUP; - e->u.key.scancode = gk_e.key; - return 1; - default: - break; - } - } + switch (gk_e.type) { + case KEYEV_NONE: + return 0; + case KEYEV_DOWN: + e->type = LZY_KEYDOWN; + e->u.key.scancode = gk_e.key; + if (gk_e.key == KEY_EXIT) + should_quit = 1; + return 1; + case KEYEV_UP: + e->type = LZY_KEYUP; + e->u.key.scancode = gk_e.key; + return 1; + default: + break; + } + } } void LZY_CycleEvents(void) { - clearevents(); - should_quit = should_quit || keydown(KEY_EXIT); + clearevents(); + should_quit = should_quit || keydown(KEY_EXIT); } -int LZY_KeyDown(unsigned int key) { - return keydown(key); -} +int LZY_KeyDown(unsigned int key) { return keydown(key); } -int LZY_ShouldQuit(void) { - return should_quit; -} +int LZY_ShouldQuit(void) { return should_quit; } -void LZY_Log(const char *msg) { - LZY_UNUSED(msg); -} +void LZY_Log(const char *msg) { LZY_UNUSED(msg); } -void LZY_Sleep(int time) { - sleep_ms(time); -} +void LZY_Sleep(int time) { sleep_ms(time); } -const char *LZY_GetError(void) { - return NULL; -} +const char *LZY_GetError(void) { return NULL; } #else /* end FXCG50, begin SDL2 */ #include LZY_SDL_INCLUDE @@ -511,561 +497,546 @@ static uint64_t next_time; static char *datadir; static char *str_dup(const char *src) { - char *copy; + char *copy; - if (src == NULL) { - error = "src is NULL"; - return NULL; - } + if (src == NULL) { + error = "src is NULL"; + return NULL; + } - copy = calloc(strlen(src) + 1, sizeof(char)); - if (copy == NULL) { - error = "calloc failed"; - return NULL; - } + copy = calloc(strlen(src) + 1, sizeof(char)); + if (copy == NULL) { + error = "calloc failed"; + return NULL; + } - strcpy(copy, src); - return copy; + strcpy(copy, src); + return copy; } static char *path_relative(const char *path) { - char *buf; + char *buf; - if (datadir == NULL) { - error = "datadir is NULL"; - return NULL; - } + if (datadir == NULL) { + error = "datadir is NULL"; + return NULL; + } - if (path == NULL) { - error = "path is NULL"; - return NULL; - } + if (path == NULL) { + error = "path is NULL"; + return NULL; + } - buf = calloc(strlen(datadir) + strlen(path) + 2, sizeof(char)); - if (buf == NULL) { - error = "calloc failed"; - return NULL; - } + buf = calloc(strlen(datadir) + strlen(path) + 2, sizeof(char)); + if (buf == NULL) { + error = "calloc failed"; + return NULL; + } - strcpy(buf, datadir); - strcat(buf, "/"); - strcat(buf, path); + strcpy(buf, datadir); + strcat(buf, "/"); + strcat(buf, path); - return buf; + return buf; } int LZY_Init(int argc, const char **argv, const char *title, int target_fps, const char *tset_path, const char *font_path) { - const int img_flags = IMG_INIT_PNG; - char *buf; + const int img_flags = IMG_INIT_PNG; + char *buf; - datadir = str_dup((argc < 1) ? ("./rl") : (argv[0])); - if (datadir == NULL) - return -1; - dirname(datadir); + datadir = str_dup((argc < 1) ? ("./rl") : (argv[0])); + if (datadir == NULL) + return -1; + dirname(datadir); - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { - error = SDL_GetError(); - return -1; - } + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { + error = SDL_GetError(); + return -1; + } - if ((IMG_Init(img_flags) & img_flags) != img_flags) { - error = IMG_GetError(); - return -1; - } + if ((IMG_Init(img_flags) & img_flags) != img_flags) { + error = IMG_GetError(); + return -1; + } - if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) { - error = Mix_GetError(); - return -1; - } + if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) { + error = Mix_GetError(); + return -1; + } - SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0); + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0); - window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, LZY_DISPLAY_WIDTH, - LZY_DISPLAY_HEIGHT, SDL_WINDOW_RESIZABLE); - if (window == NULL) { - error = SDL_GetError(); - return -1; - } + window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, LZY_DISPLAY_WIDTH, + LZY_DISPLAY_HEIGHT, SDL_WINDOW_RESIZABLE); + if (window == NULL) { + error = SDL_GetError(); + return -1; + } - SDL_SetWindowMinimumSize(window, LZY_DISPLAY_WIDTH, LZY_DISPLAY_HEIGHT); + SDL_SetWindowMinimumSize(window, LZY_DISPLAY_WIDTH, LZY_DISPLAY_HEIGHT); - renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); - if (renderer == NULL) { - error = SDL_GetError(); - return -1; - } + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + if (renderer == NULL) { + error = SDL_GetError(); + return -1; + } - target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, - SDL_TEXTUREACCESS_TARGET, LZY_DISPLAY_WIDTH, - LZY_DISPLAY_HEIGHT); - if (target == NULL) { - error = SDL_GetError(); - return -1; - } + target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, + SDL_TEXTUREACCESS_TARGET, LZY_DISPLAY_WIDTH, + LZY_DISPLAY_HEIGHT); + if (target == NULL) { + error = SDL_GetError(); + return -1; + } - buf = path_relative(tset_path); - if (buf == NULL) - return -1; - tset = IMG_LoadTexture(renderer, buf); - if (tset == NULL) { - error = IMG_GetError(); - return -1; - } - free(buf); + buf = path_relative(tset_path); + if (buf == NULL) + return -1; + tset = IMG_LoadTexture(renderer, buf); + if (tset == NULL) { + error = IMG_GetError(); + return -1; + } + free(buf); - if (SDL_QueryTexture(tset, NULL, NULL, &tset_width, &tset_height) < 0) { - error = SDL_GetError(); - return -1; - } - tset_width /= LZY_TILE_SIZE; - tset_height /= LZY_TILE_SIZE; + if (SDL_QueryTexture(tset, NULL, NULL, &tset_width, &tset_height) < 0) { + error = SDL_GetError(); + return -1; + } + tset_width /= LZY_TILE_SIZE; + tset_height /= LZY_TILE_SIZE; - buf = path_relative(font_path); - if (buf == NULL) - return -1; - font = IMG_LoadTexture(renderer, buf); - if (font == NULL) { - error = IMG_GetError(); - return -1; - } - free(buf); + buf = path_relative(font_path); + if (buf == NULL) + return -1; + font = IMG_LoadTexture(renderer, buf); + if (font == NULL) { + error = IMG_GetError(); + return -1; + } + free(buf); - if (SDL_QueryTexture(font, NULL, NULL, &font_width, &font_height) < 0) { - error = SDL_GetError(); - return -1; - } - font_width /= LZY_CHR_WIDTH; - font_height /= LZY_CHR_HEIGHT; + if (SDL_QueryTexture(font, NULL, NULL, &font_width, &font_height) < 0) { + error = SDL_GetError(); + return -1; + } + font_width /= LZY_CHR_WIDTH; + font_height /= LZY_CHR_HEIGHT; - fps_limiter = target_fps > 0; - if (fps_limiter) { - min_dt = 1000 / target_fps; - next_time = SDL_GetTicks64(); - } + fps_limiter = target_fps > 0; + if (fps_limiter) { + min_dt = 1000 / target_fps; + next_time = SDL_GetTicks64(); + } - return 0; + return 0; } void LZY_Quit(void) { - if (datadir != NULL) { - free(datadir); - datadir = NULL; - } + if (datadir != NULL) { + free(datadir); + datadir = NULL; + } - if (tset != NULL) { - SDL_DestroyTexture(tset); - tset = NULL; - } + if (tset != NULL) { + SDL_DestroyTexture(tset); + tset = NULL; + } - if (target != NULL) { - SDL_DestroyTexture(target); - target = NULL; - } + if (target != NULL) { + SDL_DestroyTexture(target); + target = NULL; + } - if (renderer != NULL) { - SDL_DestroyRenderer(renderer); - renderer = NULL; - } + if (renderer != NULL) { + SDL_DestroyRenderer(renderer); + renderer = NULL; + } - if (window != NULL) { - SDL_DestroyWindow(window); - window = NULL; - } + if (window != NULL) { + SDL_DestroyWindow(window); + window = NULL; + } - Mix_Quit(); - IMG_Quit(); - SDL_Quit(); + Mix_Quit(); + IMG_Quit(); + SDL_Quit(); } int LZY_DrawBegin(void) { - if (fps_limiter) - next_time += min_dt; + if (fps_limiter) + next_time += min_dt; - if (SDL_SetRenderTarget(renderer, target) < 0) { - error = SDL_GetError(); - return -1; - } - return 0; + if (SDL_SetRenderTarget(renderer, target) < 0) { + error = SDL_GetError(); + return -1; + } + return 0; } int LZY_DrawEnd(void) { - int win_w, win_h, off_x, off_y; - float ratio_w, ratio_h, scale; - SDL_Rect src, dst; + int win_w, win_h, off_x, off_y; + float ratio_w, ratio_h, scale; + SDL_Rect src, dst; - if (SDL_SetRenderTarget(renderer, NULL)) { - error = SDL_GetError(); - return -1; - } + if (SDL_SetRenderTarget(renderer, NULL)) { + error = SDL_GetError(); + return -1; + } - LZY_DrawSetColor(0x00, 0x00, 0x00); - if (LZY_DrawClear()) - return -2; + LZY_DrawSetColor(0x00, 0x00, 0x00); + if (LZY_DrawClear()) + return -2; - SDL_GetWindowSize(window, &win_w, &win_h); - ratio_w = (float)win_w / LZY_DISPLAY_WIDTH; - ratio_h = (float)win_h / LZY_DISPLAY_HEIGHT; - scale = (ratio_w < ratio_h) ? (ratio_w) : (ratio_h); - off_x = (win_w - LZY_DISPLAY_WIDTH * scale) / 2; - off_y = (win_h - LZY_DISPLAY_HEIGHT * scale) / 2; + SDL_GetWindowSize(window, &win_w, &win_h); + ratio_w = (float)win_w / LZY_DISPLAY_WIDTH; + ratio_h = (float)win_h / LZY_DISPLAY_HEIGHT; + scale = (ratio_w < ratio_h) ? (ratio_w) : (ratio_h); + off_x = (win_w - LZY_DISPLAY_WIDTH * scale) / 2; + off_y = (win_h - LZY_DISPLAY_HEIGHT * scale) / 2; - src.x = 0; - src.y = 0; - src.w = LZY_DISPLAY_WIDTH; - src.h = LZY_DISPLAY_HEIGHT; + src.x = 0; + src.y = 0; + src.w = LZY_DISPLAY_WIDTH; + src.h = LZY_DISPLAY_HEIGHT; - dst.x = off_x; - dst.y = off_y; - dst.w = LZY_DISPLAY_WIDTH * scale; - dst.h = LZY_DISPLAY_HEIGHT * scale; + dst.x = off_x; + dst.y = off_y; + dst.w = LZY_DISPLAY_WIDTH * scale; + dst.h = LZY_DISPLAY_HEIGHT * scale; - if (SDL_RenderCopy(renderer, target, &src, &dst) < 0) { - error = SDL_GetError(); - return -3; - } + if (SDL_RenderCopy(renderer, target, &src, &dst) < 0) { + error = SDL_GetError(); + return -3; + } - SDL_RenderPresent(renderer); + SDL_RenderPresent(renderer); - if (fps_limiter) { - const uint64_t cur_time = SDL_GetTicks64(); - if (next_time <= cur_time) - next_time = cur_time; - else - SDL_Delay(next_time - cur_time); - } - return 0; + if (fps_limiter) { + const uint64_t cur_time = SDL_GetTicks64(); + if (next_time <= cur_time) + next_time = cur_time; + else + SDL_Delay(next_time - cur_time); + } + return 0; } void LZY_DrawSetColor(uint8_t r, uint8_t g, uint8_t b) { - SDL_SetRenderDrawColor(renderer, r, g, b, 0xff); + SDL_SetRenderDrawColor(renderer, r, g, b, 0xff); } void LZY_DrawSetColorNone(void) { - SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00); + SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00); } int LZY_DrawClear(void) { - if (SDL_RenderClear(renderer) < 0) { - error = SDL_GetError(); - return -1; - } - return 0; + if (SDL_RenderClear(renderer) < 0) { + error = SDL_GetError(); + return -1; + } + return 0; } int LZY_DrawPoint(int x, int y) { - if (SDL_RenderDrawPoint(renderer, x, y) < 0) { - error = SDL_GetError(); - return -1; - } - return 0; + if (SDL_RenderDrawPoint(renderer, x, y) < 0) { + error = SDL_GetError(); + return -1; + } + return 0; } int LZY_DrawRect(int x, int y, unsigned int w, unsigned int h) { - const SDL_Rect rect = {x, y, w, h}; + const SDL_Rect rect = {x, y, w, h}; - if (w == 0 || h == 0) { - error = "rectangle dimensions cannot be 0"; - return -1; - } + if (w == 0 || h == 0) { + error = "rectangle dimensions cannot be 0"; + return -1; + } - if (SDL_RenderDrawRect(renderer, &rect) < 0) { - error = SDL_GetError(); - return -2; - } - return 0; + if (SDL_RenderDrawRect(renderer, &rect) < 0) { + error = SDL_GetError(); + return -2; + } + return 0; } int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h) { - const SDL_Rect rect = {x, y, w, h}; + const SDL_Rect rect = {x, y, w, h}; - if (w == 0 || h == 0) { - error = "rectangle dimensions cannot be 0"; - return -1; - } + if (w == 0 || h == 0) { + error = "rectangle dimensions cannot be 0"; + return -1; + } - if (SDL_RenderFillRect(renderer, &rect) < 0) { - error = SDL_GetError(); - return -2; - } - return 0; + if (SDL_RenderFillRect(renderer, &rect) < 0) { + error = SDL_GetError(); + return -2; + } + return 0; } int LZY_DrawTile(unsigned int id, int x, int y) { - SDL_Rect src, dst; + SDL_Rect src, dst; - if (id >= (unsigned int)(tset_width * tset_height)) { - error = "id exceeds boundaries"; - return -1; - } + if (id >= (unsigned int)(tset_width * tset_height)) { + error = "id exceeds boundaries"; + return -1; + } - src.x = (id % tset_width) * LZY_TILE_SIZE; - src.y = (id / tset_width) * LZY_TILE_SIZE; - src.w = LZY_TILE_SIZE; - src.h = LZY_TILE_SIZE; + src.x = (id % tset_width) * LZY_TILE_SIZE; + src.y = (id / tset_width) * LZY_TILE_SIZE; + src.w = LZY_TILE_SIZE; + src.h = LZY_TILE_SIZE; - dst.x = x; - dst.y = y; - dst.w = LZY_TILE_SIZE; - dst.h = LZY_TILE_SIZE; + dst.x = x; + dst.y = y; + dst.w = LZY_TILE_SIZE; + dst.h = LZY_TILE_SIZE; - if (SDL_RenderCopy(renderer, tset, &src, &dst) < 0) { - error = SDL_GetError(); - return -2; - } + if (SDL_RenderCopy(renderer, tset, &src, &dst) < 0) { + error = SDL_GetError(); + return -2; + } - return 0; + return 0; } int LZY_DrawTileEx(unsigned int id, int x, int y, unsigned int w, unsigned int h) { - SDL_Rect src, dst; + SDL_Rect src, dst; - if (w * h == 0) { - error = "tile size can't be 0"; - return -1; - } + if (w * h == 0) { + error = "tile size can't be 0"; + return -1; + } - if (id >= (unsigned int)(tset_width * tset_height)) { - error = "id exceeds boundaries"; - return -1; - } + if (id >= (unsigned int)(tset_width * tset_height)) { + error = "id exceeds boundaries"; + return -1; + } - src.x = id % tset_width; - src.y = id / tset_width; + src.x = id % tset_width; + src.y = id / tset_width; - if (src.x >= tset_width || src.x + w - 1 >= (unsigned int)tset_width || - src.y >= tset_height || - src.y + h - 1 >= (unsigned int)tset_height) { - error = "tile is not in tileset"; - return -1; - } + if (src.x >= tset_width || src.x + w - 1 >= (unsigned int)tset_width || + src.y >= tset_height || src.y + h - 1 >= (unsigned int)tset_height) { + error = "tile is not in tileset"; + return -1; + } - src.x *= LZY_TILE_SIZE; - src.y *= LZY_TILE_SIZE; - src.w = w * LZY_TILE_SIZE; - src.h = h * LZY_TILE_SIZE; + src.x *= LZY_TILE_SIZE; + src.y *= LZY_TILE_SIZE; + src.w = w * LZY_TILE_SIZE; + src.h = h * LZY_TILE_SIZE; - dst.x = x; - dst.y = y; - dst.w = w * LZY_TILE_SIZE; - dst.h = h * LZY_TILE_SIZE; + dst.x = x; + dst.y = y; + dst.w = w * LZY_TILE_SIZE; + dst.h = h * LZY_TILE_SIZE; - if (SDL_RenderCopy(renderer, tset, &src, &dst) < 0) { - error = SDL_GetError(); - return -1; - } + if (SDL_RenderCopy(renderer, tset, &src, &dst) < 0) { + error = SDL_GetError(); + return -1; + } - return 0; + return 0; } int LZY_DrawChar(unsigned char chr, int x, int y) { - const unsigned int id = (unsigned int)chr - LZY_FIRST_CHR; - SDL_Rect src, dst; + const unsigned int id = (unsigned int)chr - LZY_FIRST_CHR; + SDL_Rect src, dst; - if (id >= (unsigned int)(font_width * font_height)) { - error = "chr exceeds boundaries"; - return -1; - } + if (id >= (unsigned int)(font_width * font_height)) { + error = "chr exceeds boundaries"; + return -1; + } - src.x = (id % font_width) * LZY_CHR_WIDTH; - src.y = (id / font_width) * LZY_CHR_HEIGHT; - src.w = LZY_CHR_WIDTH; - src.h = LZY_CHR_HEIGHT; + src.x = (id % font_width) * LZY_CHR_WIDTH; + src.y = (id / font_width) * LZY_CHR_HEIGHT; + src.w = LZY_CHR_WIDTH; + src.h = LZY_CHR_HEIGHT; - dst.x = x; - dst.y = y; - dst.w = LZY_CHR_WIDTH; - dst.h = LZY_CHR_HEIGHT; + dst.x = x; + dst.y = y; + dst.w = LZY_CHR_WIDTH; + dst.h = LZY_CHR_HEIGHT; - if (SDL_RenderCopy(renderer, font, &src, &dst) < 0) { - error = SDL_GetError(); - return -2; - } + if (SDL_RenderCopy(renderer, font, &src, &dst) < 0) { + error = SDL_GetError(); + return -2; + } - return 0; + return 0; } int LZY_DrawText(const char *text, int x, int y) { - int err = 0; + int err = 0; - if (text == NULL) { - error = "text is NULL"; - return -1; - } + if (text == NULL) { + error = "text is NULL"; + return -1; + } - for (; *text != '\0'; text++) { - const int rc = LZY_DrawChar(*text, x, y); - if (rc) - err = rc - 1; - x += LZY_CHR_WIDTH; - } + for (; *text != '\0'; text++) { + const int rc = LZY_DrawChar(*text, x, y); + if (rc) + err = rc - 1; + x += LZY_CHR_WIDTH; + } - return err; + return err; } LZY_Music *LZY_MusicLoad(const char *path) { - char *buf; - LZY_Music *music; + char *buf; + LZY_Music *music; - buf = path_relative(path); - if (buf == NULL) - return NULL; - music = Mix_LoadMUS(buf); - if (music == NULL) - error = Mix_GetError(); - free(buf); + buf = path_relative(path); + if (buf == NULL) + return NULL; + music = Mix_LoadMUS(buf); + if (music == NULL) + error = Mix_GetError(); + free(buf); - return music; + return music; } int LZY_MusicDestroy(LZY_Music *music) { - if (music == NULL) { - error = "music is NULL"; - return -1; - } + if (music == NULL) { + error = "music is NULL"; + return -1; + } - Mix_FreeMusic(music); - return 0; + Mix_FreeMusic(music); + return 0; } int LZY_MusicPlay(LZY_Music *music, int loops) { - if (music == NULL) { - error = "music is NULL"; - return -1; - } + if (music == NULL) { + error = "music is NULL"; + return -1; + } - if (Mix_PlayMusic(music, loops) < 0) { - error = Mix_GetError(); - return -2; - } - return 0; + if (Mix_PlayMusic(music, loops) < 0) { + error = Mix_GetError(); + return -2; + } + return 0; } LZY_Sound *LZY_SoundLoad(const char *path) { - char *buf; - LZY_Sound *sound; + char *buf; + LZY_Sound *sound; - buf = path_relative(path); - if (buf == NULL) - return NULL; - sound = Mix_LoadWAV(buf); - if (sound == NULL) - error = Mix_GetError(); - free(buf); + buf = path_relative(path); + if (buf == NULL) + return NULL; + sound = Mix_LoadWAV(buf); + if (sound == NULL) + error = Mix_GetError(); + free(buf); - return sound; + return sound; } int LZY_SoundDestroy(LZY_Sound *sound) { - if (sound == NULL) { - error = "sound is NULL"; - return -1; - } + if (sound == NULL) { + error = "sound is NULL"; + return -1; + } - Mix_FreeChunk(sound); - return 0; + Mix_FreeChunk(sound); + return 0; } void LZY_SoundSetVolume(LZY_Sound *sound, float volume) { - if (sound != NULL) - Mix_VolumeChunk(sound, MIX_MAX_VOLUME * volume); + if (sound != NULL) + Mix_VolumeChunk(sound, MIX_MAX_VOLUME * volume); } int LZY_SoundPlay(LZY_Sound *sound, int loops) { - if (sound == NULL) { - error = "sound is NULL"; - return -1; - } + if (sound == NULL) { + error = "sound is NULL"; + return -1; + } - if (Mix_PlayChannel(-1, sound, loops) < 0) { - error = Mix_GetError(); - return -2; - } + if (Mix_PlayChannel(-1, sound, loops) < 0) { + error = Mix_GetError(); + return -2; + } - return 0; + return 0; } int LZY_PollEvent(LZY_Event *e) { - SDL_Event sdl_e; + SDL_Event sdl_e; - while (SDL_PollEvent(&sdl_e) != 0) { - switch (sdl_e.type) { - case SDL_QUIT: - e->type = LZY_QUIT; - should_quit = 1; - return 1; - case SDL_KEYDOWN: { - if (!sdl_e.key.repeat) { - int i = LZYK_COUNT; - while (i-- > 0) { - if (sc[i] == - sdl_e.key.keysym.scancode) { - e->type = LZY_KEYDOWN; - e->u.key.scancode = i; - input[i] = 1; - return 1; - } - } - if (sdl_e.key.keysym.scancode == - fullscreen_sc) { - const unsigned int flag = - SDL_WINDOW_FULLSCREEN; - const unsigned int fullscreen = - SDL_GetWindowFlags(window) & flag; - SDL_SetWindowFullscreen(window, - !fullscreen); - } - } - } break; - case SDL_KEYUP: { - int i = LZYK_COUNT; - while (i-- > 0) { - if (sc[i] == sdl_e.key.keysym.scancode) { - e->type = LZY_KEYUP; - e->u.key.scancode = i; - input[i] = 0; - return 1; - } - } - } break; - default: - break; - } - } + while (SDL_PollEvent(&sdl_e) != 0) { + switch (sdl_e.type) { + case SDL_QUIT: + e->type = LZY_QUIT; + should_quit = 1; + return 1; + case SDL_KEYDOWN: { + if (!sdl_e.key.repeat) { + int i = LZYK_COUNT; + while (i-- > 0) { + if (sc[i] == sdl_e.key.keysym.scancode) { + e->type = LZY_KEYDOWN; + e->u.key.scancode = i; + input[i] = 1; + return 1; + } + } + if (sdl_e.key.keysym.scancode == fullscreen_sc) { + const unsigned int flag = SDL_WINDOW_FULLSCREEN; + const unsigned int fullscreen = SDL_GetWindowFlags(window) & flag; + SDL_SetWindowFullscreen(window, !fullscreen); + } + } + } break; + case SDL_KEYUP: { + int i = LZYK_COUNT; + while (i-- > 0) { + if (sc[i] == sdl_e.key.keysym.scancode) { + e->type = LZY_KEYUP; + e->u.key.scancode = i; + input[i] = 0; + return 1; + } + } + } break; + default: + break; + } + } - return 0; + return 0; } void LZY_CycleEvents(void) { - LZY_Event e; + LZY_Event e; - while (LZY_PollEvent(&e) != 0) - ; + while (LZY_PollEvent(&e) != 0) + ; } int LZY_KeyDown(unsigned int key) { - if (key >= LZYK_COUNT) - return 0; - return input[key]; + if (key >= LZYK_COUNT) + return 0; + return input[key]; } -int LZY_ShouldQuit(void) { - return should_quit; -} +int LZY_ShouldQuit(void) { return should_quit; } -void LZY_Log(const char *msg) { - SDL_Log("%s", msg); -} +void LZY_Log(const char *msg) { SDL_Log("%s", msg); } -const char *LZY_GetError(void) { - return error; -} +const char *LZY_GetError(void) { return error; } -void LZY_Sleep(int time) { - SDL_Delay(time); -} +void LZY_Sleep(int time) { SDL_Delay(time); } #endif /* SDL2 */ #endif /* LZY_IMPLEMENTATION */ - diff --git a/inc/main.h b/inc/main.h index 64580a6..28b4a3d 100644 --- a/inc/main.h +++ b/inc/main.h @@ -1,7 +1,7 @@ #define LZY_IMPLEMENTATION #define LZY_GINT_TILESET bimg_tset -#define LZY_GINT_FONT bimg_font -#define LZY_CHR_WIDTH 8 -#define LZY_CHR_HEIGHT 8 -#define LZY_FIRST_CHR ' ' -#define LZY_TILE_SIZE 24 +#define LZY_GINT_FONT bimg_font +#define LZY_CHR_WIDTH 8 +#define LZY_CHR_HEIGHT 8 +#define LZY_FIRST_CHR ' ' +#define LZY_TILE_SIZE 24 diff --git a/inc/overlay.h b/inc/overlay.h index 652072b..f408a61 100644 --- a/inc/overlay.h +++ b/inc/overlay.h @@ -1,4 +1,3 @@ #pragma once void overlay_draw(int x, int y, int id, int life); - diff --git a/inc/particle.h b/inc/particle.h index d160c93..e9420b2 100644 --- a/inc/particle.h +++ b/inc/particle.h @@ -2,14 +2,14 @@ #include "type.h" struct Particle { - struct Vec2 pos; - struct Vec2 dir; - int active, id, life; + struct Vec2 pos; + struct Vec2 dir; + int active, id, life; }; struct Particle_table { - int n; - struct Particle *particles; + int n; + struct Particle *particles; }; void particle_table_init(void); diff --git a/inc/player.h b/inc/player.h index 944dd88..8b4aabd 100644 --- a/inc/player.h +++ b/inc/player.h @@ -2,16 +2,16 @@ #include "type.h" struct Cooldown { - int up, down, side, neutral; + int up, down, side, neutral; }; struct Player { - struct Vec2 pos; - struct FVec2 spd; - struct FVec2 rem; - struct Vec2 dir; - struct Cooldown cooldown; - int attack, last_attack, id, active, life, djump; + struct Vec2 pos; + struct FVec2 spd; + struct FVec2 rem; + struct Vec2 dir; + struct Cooldown cooldown; + int attack, last_attack, id, active, life, djump; }; void player_init(int chara); diff --git a/inc/sandbag.h b/inc/sandbag.h index 6d37107..39cf0b7 100644 --- a/inc/sandbag.h +++ b/inc/sandbag.h @@ -2,11 +2,11 @@ #include "type.h" struct Sandbag { - struct Vec2 pos; - struct FVec2 spd; - struct FVec2 rem; - struct Vec2 dir; - int hitstun, life, iced, active; + struct Vec2 pos; + struct FVec2 spd; + struct FVec2 rem; + struct Vec2 dir; + int hitstun, life, iced, active; }; void sandbag_init(void); diff --git a/inc/type.h b/inc/type.h index 5731d89..a799bb1 100644 --- a/inc/type.h +++ b/inc/type.h @@ -4,9 +4,9 @@ typedef int tile_t; struct Vec2 { - int x, y; + int x, y; }; struct FVec2 { - float x, y; + float x, y; }; diff --git a/src/attack.c b/src/attack.c index ca4caff..fe0f372 100644 --- a/src/attack.c +++ b/src/attack.c @@ -1,300 +1,280 @@ #include "attack.h" #include "bullet.h" -#include "player.h" #include "conf.h" -#include "tools.h" #include "level.h" -#include "particle.h" #include "lzy.h" +#include "particle.h" +#include "player.h" +#include "tools.h" +#include #include -#include static struct Attack_table attack_table; static void attack_table_free(void); static void attack_update(struct Attack *attack); static void attack_draw(struct Attack *attack, int timer); -static int attack_collide(struct Attack *attack, int x, int y, int w, int h, int hitbox); +static int attack_collide(struct Attack *attack, int x, int y, int w, int h, + int hitbox); -void -attack_table_init(void) -{ - attack_table_free(); - attack_table.attacks = malloc(MAX_ATTACKS * sizeof(struct Attack)); - attack_table.n = 0; +void attack_table_init(void) { + attack_table_free(); + attack_table.attacks = malloc(MAX_ATTACKS * sizeof(struct Attack)); + attack_table.n = 0; - for (int i = 0; i < MAX_ATTACKS; ++i) { - attack_table.attacks[i].active = 0; - } + for (int i = 0; i < MAX_ATTACKS; ++i) { + attack_table.attacks[i].active = 0; + } } -void -attack_table_free(void) -{ - if (attack_table.attacks != NULL) { - attack_table.attacks = NULL; - } +void attack_table_free(void) { + if (attack_table.attacks != NULL) { + attack_table.attacks = NULL; + } }; int attack(int x, int y, int dir_x, int dir_y, int type) { - int cooldown = 5; - for (int i = 0; i < MAX_ATTACKS; ++i) { - if (!attack_table.attacks[i].active) { - struct Attack * att = &attack_table.attacks[i]; - *att = (struct Attack){ - .pos = (struct Vec2){.x = x, .y = y}, - .type = type, - .active = 1, - .ice = 0, - .dam = 0, - }; - switch(type){ - case 1: - att->w = 5; - att->h = 5; - att->life = 2; - att->dir = (struct FVec2){.x = dir_x * 2, .y = dir_y * 0.5}; - att->dam = 2; - cooldown = 10; - particle_new(att->pos.x, att->pos.y, dir_x > 0 ? 7 : 8, att->life); - break; - case 2: - att->w = 10; - att->h = 10; - att->life = 2; - att->dam = 4; - att->dir = (struct FVec2){.x = 0, .y = -2}; - cooldown = 15; - particle_new(att->pos.x, att->pos.y, 9, att->life); - break; - case 3: - att->w = 10; - att->h = 5; - att->life = 2; - att->dam = 3; - att->dir = (struct FVec2){.x = 0, .y = 1.5}; - cooldown = 10; - particle_new(att->pos.x, att->pos.y, 4, att->life); - break; - case 4: - att->w = 16; - att->h = 16; - att->life = 3; - att->dam = 1; - att->dir = (struct FVec2){.x = dir_x * 0.5, .y = 0.5}; - cooldown = 60; - att->ice = 1; - break; - case 5: - att->pos.x -= dir_x * 2; - att->pos.y += 3; - att->w = 5; - att->h = 8; - att->life = 2; - att->dam = 2; - att->dir = (struct FVec2){.x = dir_x * 1.5, .y = dir_y * 0.5}; - cooldown = 10; - particle_new(att->pos.x, att->pos.y + 4, dir_x > 0 ? 7 : 8, att->life); - break; - case 6: - att->w = 6; - att->pos.x -= att->w / 2; - att->h = 2; - att->life = 80; - att->dam = 5; - att->dir = (struct FVec2){.x = 0, .y = -0.5}; - cooldown = 20; - bullet_fire(att->pos.x, att->pos.y, 0, 1, 8); - particle_new(att->pos.x, att->pos.y, 9, 2); - break; - case 7: - att->w = 13; - att->h = 4; - att->life = 2; - att->dam = 4; - att->dir = (struct FVec2){.x = 0, .y = 1.5}; - cooldown = 10; - particle_new(att->pos.x, att->pos.y, 3, att->life); - break; - case 8: - att->w = 8; - att->h = 8; - att->life = 2; - att->dam = 2; - att->dir = (struct FVec2){.x = dir_x * 0.25, .y = 1}; - cooldown = 10; - break; - case 9: - att->pos.x += dir_x * 4; - att->w = 8; - att->h = 12; - att->life = 2; - att->dam = 3; - att->dir = (struct FVec2){.x = dir_x * 2.5, .y = dir_y * 1}; - cooldown = 10; - particle_new(att->pos.x - dir_x * 4, att->pos.y, dir_x > 0 ? 7 : 8, att->life); - break; - case 10: - att->pos.y += 2; - att->w = 12; - att->h = 6; - att->life = 2; - att->dam = 5; - att->dir = (struct FVec2){.x = 0, .y = -4}; - cooldown = 15; - particle_new(att->pos.x, att->pos.y, 9, att->life); - break; - case 11: - att->w = 10; - att->h = 5; - att->life = 2; - att->dam = 5; - att->dir = (struct FVec2){.x = 0, .y = 1.5}; - cooldown = 10; - particle_new(att->pos.x, att->pos.y, 4, att->life); - break; - case 12: - att->w = 18; - att->h = 18; - att->life = 1; - att->dam = 6; - att->dir = (struct FVec2){.x = dir_x * 2, .y = 1.5}; - particle_new(att->pos.x, att->pos.y, 0, 2); - particle_new(att->pos.x + dir_x * 8, att->pos.y - 8, 0, 4); - particle_new(att->pos.x + dir_x * 16, att->pos.y - 16, 0, 8); - cooldown = 30; - break; - case 13: - att->w = 5; - att->h = 5; - att->life = 2; - att->dam = 2; - att->dir = (struct FVec2){.x = dir_x * 2, .y = dir_y * 0.5}; - cooldown = 10; - particle_new(att->pos.x, att->pos.y, dir_x > 0 ? 7 : 8, att->life); - break; - case 14: - att->w = 10; - att->h = 10; - att->life = 2; - att->dir = (struct FVec2){.x = 0, .y = -2}; - cooldown = 15; - att->dam = 3; - particle_new(att->pos.x, att->pos.y, 9, att->life); - break; - case 15: - att->w = 10; - att->h = 5; - att->life = 2; - att->dir = (struct FVec2){.x = 0, .y = 1.5}; - cooldown = 10; - att->dam = 3; - particle_new(att->pos.x, att->pos.y, 4, att->life); - break; - case 16: - att->w = 16; - att->h = 16; - att->life = 60; - att->dir = (struct FVec2){.x = 0, .y = 0}; - cooldown = 120; - att->dam = 1; - particle_new(att->pos.x, att->pos.y, 6, att->life); - break; - default: - att->w = 0; - att->h = 0; - att->life = 0 ; - att->dir = (struct FVec2){.x = dir_x, .y = 0}; - cooldown = 0; - break; - } - attack_table.attacks[i].pos.x -= attack_table.attacks[i].w / 2; - attack_table.attacks[i].pos.y -= attack_table.attacks[i].h / 2; - break; - } - } - ++attack_table.n; - return cooldown; + int cooldown = 5; + for (int i = 0; i < MAX_ATTACKS; ++i) { + if (!attack_table.attacks[i].active) { + struct Attack *att = &attack_table.attacks[i]; + *att = (struct Attack){ + .pos = (struct Vec2){.x = x, .y = y}, + .type = type, + .active = 1, + .ice = 0, + .dam = 0, + }; + switch (type) { + case 1: + att->w = 5; + att->h = 5; + att->life = 2; + att->dir = (struct FVec2){.x = dir_x * 2, .y = dir_y * 0.5}; + att->dam = 2; + cooldown = 10; + particle_new(att->pos.x, att->pos.y, dir_x > 0 ? 7 : 8, att->life); + break; + case 2: + att->w = 10; + att->h = 10; + att->life = 2; + att->dam = 4; + att->dir = (struct FVec2){.x = 0, .y = -2}; + cooldown = 15; + particle_new(att->pos.x, att->pos.y, 9, att->life); + break; + case 3: + att->w = 10; + att->h = 5; + att->life = 2; + att->dam = 3; + att->dir = (struct FVec2){.x = 0, .y = 1.5}; + cooldown = 10; + particle_new(att->pos.x, att->pos.y, 4, att->life); + break; + case 4: + att->w = 16; + att->h = 16; + att->life = 3; + att->dam = 1; + att->dir = (struct FVec2){.x = dir_x * 0.5, .y = 0.5}; + cooldown = 60; + att->ice = 1; + break; + case 5: + att->pos.x -= dir_x * 2; + att->pos.y += 3; + att->w = 5; + att->h = 8; + att->life = 2; + att->dam = 2; + att->dir = (struct FVec2){.x = dir_x * 1.5, .y = dir_y * 0.5}; + cooldown = 10; + particle_new(att->pos.x, att->pos.y + 4, dir_x > 0 ? 7 : 8, att->life); + break; + case 6: + att->w = 6; + att->pos.x -= att->w / 2; + att->h = 2; + att->life = 80; + att->dam = 5; + att->dir = (struct FVec2){.x = 0, .y = -0.5}; + cooldown = 20; + bullet_fire(att->pos.x, att->pos.y, 0, 1, 8); + particle_new(att->pos.x, att->pos.y, 9, 2); + break; + case 7: + att->w = 13; + att->h = 4; + att->life = 2; + att->dam = 4; + att->dir = (struct FVec2){.x = 0, .y = 1.5}; + cooldown = 10; + particle_new(att->pos.x, att->pos.y, 3, att->life); + break; + case 8: + att->w = 8; + att->h = 8; + att->life = 2; + att->dam = 2; + att->dir = (struct FVec2){.x = dir_x * 0.25, .y = 1}; + cooldown = 10; + break; + case 9: + att->pos.x += dir_x * 4; + att->w = 8; + att->h = 12; + att->life = 2; + att->dam = 3; + att->dir = (struct FVec2){.x = dir_x * 2.5, .y = dir_y * 1}; + cooldown = 10; + particle_new(att->pos.x - dir_x * 4, att->pos.y, dir_x > 0 ? 7 : 8, + att->life); + break; + case 10: + att->pos.y += 2; + att->w = 12; + att->h = 6; + att->life = 2; + att->dam = 5; + att->dir = (struct FVec2){.x = 0, .y = -4}; + cooldown = 15; + particle_new(att->pos.x, att->pos.y, 9, att->life); + break; + case 11: + att->w = 10; + att->h = 5; + att->life = 2; + att->dam = 5; + att->dir = (struct FVec2){.x = 0, .y = 1.5}; + cooldown = 10; + particle_new(att->pos.x, att->pos.y, 4, att->life); + break; + case 12: + att->w = 18; + att->h = 18; + att->life = 1; + att->dam = 6; + att->dir = (struct FVec2){.x = dir_x * 2, .y = 1.5}; + particle_new(att->pos.x, att->pos.y, 0, 2); + particle_new(att->pos.x + dir_x * 8, att->pos.y - 8, 0, 4); + particle_new(att->pos.x + dir_x * 16, att->pos.y - 16, 0, 8); + cooldown = 30; + break; + case 13: + att->w = 5; + att->h = 5; + att->life = 2; + att->dam = 2; + att->dir = (struct FVec2){.x = dir_x * 2, .y = dir_y * 0.5}; + cooldown = 10; + particle_new(att->pos.x, att->pos.y, dir_x > 0 ? 7 : 8, att->life); + break; + case 14: + att->w = 10; + att->h = 10; + att->life = 2; + att->dir = (struct FVec2){.x = 0, .y = -2}; + cooldown = 15; + att->dam = 3; + particle_new(att->pos.x, att->pos.y, 9, att->life); + break; + case 15: + att->w = 10; + att->h = 5; + att->life = 2; + att->dir = (struct FVec2){.x = 0, .y = 1.5}; + cooldown = 10; + att->dam = 3; + particle_new(att->pos.x, att->pos.y, 4, att->life); + break; + case 16: + att->w = 16; + att->h = 16; + att->life = 60; + att->dir = (struct FVec2){.x = 0, .y = 0}; + cooldown = 120; + att->dam = 1; + particle_new(att->pos.x, att->pos.y, 6, att->life); + break; + default: + att->w = 0; + att->h = 0; + att->life = 0; + att->dir = (struct FVec2){.x = dir_x, .y = 0}; + cooldown = 0; + break; + } + attack_table.attacks[i].pos.x -= attack_table.attacks[i].w / 2; + attack_table.attacks[i].pos.y -= attack_table.attacks[i].h / 2; + break; + } + } + ++attack_table.n; + return cooldown; } -void -attack_table_update(void) -{ - for (int i = 0; i < MAX_ATTACKS; ++i) { - if (attack_table.attacks[i].active) { - attack_update(&attack_table.attacks[i]); - } - } +void attack_table_update(void) { + for (int i = 0; i < MAX_ATTACKS; ++i) { + if (attack_table.attacks[i].active) { + attack_update(&attack_table.attacks[i]); + } + } } -void -attack_update(struct Attack *attack) -{ - if (attack->type == 6) { - attack->pos.y += 8; - } - - --attack->life; - if (attack->life < 0) { - player_reset_attack(); - attack_destroy(attack); - } +void attack_update(struct Attack *attack) { + if (attack->type == 6) { + attack->pos.y += 8; + } + + --attack->life; + if (attack->life < 0) { + player_reset_attack(); + attack_destroy(attack); + } } -void -attack_table_draw(int timer) -{ - for (int i = 0; i < MAX_ATTACKS; ++i) { - if (attack_table.attacks[i].active) { - attack_draw(&attack_table.attacks[i], timer); - } - } +void attack_table_draw(int timer) { + for (int i = 0; i < MAX_ATTACKS; ++i) { + if (attack_table.attacks[i].active) { + attack_draw(&attack_table.attacks[i], timer); + } + } } -void -attack_draw(struct Attack *attack, int timer) -{ +void attack_draw(struct Attack *attack, int timer) {} + +void attack_destroy(struct Attack *attack) { + attack->active = 0; + --attack_table.n; } -void -attack_destroy(struct Attack *attack) -{ - attack->active = 0; - --attack_table.n; +int attack_nb(void) { return attack_table.n; } + +struct Attack *attack_table_collide(int x, int y, int w, int h, int hitbox) { + for (int i = 0; i < MAX_ATTACKS; ++i) { + if (attack_table.attacks[i].active) { + if (attack_collide(&attack_table.attacks[i], x, y, w, h, hitbox)) { + return &attack_table.attacks[i]; + } + } + } + return NULL; } -int -attack_nb(void) -{ - return attack_table.n; -} - -struct Attack * -attack_table_collide(int x, int y, int w, int h, int hitbox) -{ - for (int i = 0; i < MAX_ATTACKS; ++i) { - if (attack_table.attacks[i].active) { - if(attack_collide(&attack_table.attacks[i], x, y, w, h, hitbox)) { - return &attack_table.attacks[i]; - } - } - } - return NULL; -} - -int -attack_collide(struct Attack *attack, int x, int y, int w, int h, int hitbox) -{ - if ((attack->pos.x + hitbox < x + w && - attack->pos.x + attack->w - hitbox > x && - attack->pos.y + hitbox < y + h && - attack->pos.y + attack->h - hitbox > y) || - (attack->pos.x + hitbox > x && - attack->pos.x + attack->w - hitbox < x + w && - attack->pos.y + hitbox > y && - attack->pos.y + attack->h - hitbox < y + h)) { - return 1; - } - return 0; +int attack_collide(struct Attack *attack, int x, int y, int w, int h, + int hitbox) { + if ((attack->pos.x + hitbox < x + w && + attack->pos.x + attack->w - hitbox > x && + attack->pos.y + hitbox < y + h && + attack->pos.y + attack->h - hitbox > y) || + (attack->pos.x + hitbox > x && + attack->pos.x + attack->w - hitbox < x + w && + attack->pos.y + hitbox > y && + attack->pos.y + attack->h - hitbox < y + h)) { + return 1; + } + return 0; } diff --git a/src/bullet.c b/src/bullet.c index edd408a..a8c5836 100644 --- a/src/bullet.c +++ b/src/bullet.c @@ -1,11 +1,11 @@ #include "bullet.h" #include "conf.h" -#include "tools.h" #include "level.h" #include "lzy.h" +#include "tools.h" -#include #include +#include static struct Bullet_table bullet_table; @@ -13,89 +13,71 @@ static void bullet_table_free(void); static void bullet_update(struct Bullet *bullet); static void bullet_draw(struct Bullet *bullet, int timer); -void -bullet_table_init(void) -{ - bullet_table_free(); - bullet_table.bullets = malloc(MAX_BULLETS * sizeof(struct Bullet)); - bullet_table.n = 0; +void bullet_table_init(void) { + bullet_table_free(); + bullet_table.bullets = malloc(MAX_BULLETS * sizeof(struct Bullet)); + bullet_table.n = 0; - for (int i = 0; i < MAX_BULLETS; ++i) { - bullet_table.bullets[i].active = 0; - } + for (int i = 0; i < MAX_BULLETS; ++i) { + bullet_table.bullets[i].active = 0; + } } -void -bullet_table_free(void) -{ - if (bullet_table.bullets != NULL) { - bullet_table.bullets = NULL; - } +void bullet_table_free(void) { + if (bullet_table.bullets != NULL) { + bullet_table.bullets = NULL; + } }; void bullet_fire(int x, int y, int dir_x, int dir_y, int v) { - for (int i = 0; i < MAX_BULLETS; ++i) { - if (!bullet_table.bullets[i].active) { - bullet_table.bullets[i] = (struct Bullet){ - .pos = (struct Vec2){.x = x - 8, .y = y - 8}, - .dir = (struct Vec2){.x = dir_x, .y = dir_y}, - .v = v, - .active = 1}; - break; - } - } - ++bullet_table.n; + for (int i = 0; i < MAX_BULLETS; ++i) { + if (!bullet_table.bullets[i].active) { + bullet_table.bullets[i] = + (struct Bullet){.pos = (struct Vec2){.x = x - 8, .y = y - 8}, + .dir = (struct Vec2){.x = dir_x, .y = dir_y}, + .v = v, + .active = 1}; + break; + } + } + ++bullet_table.n; } -void -bullet_table_update(void) -{ - for (int i = 0; i < MAX_BULLETS; ++i) { - if (bullet_table.bullets[i].active) { - bullet_update(&bullet_table.bullets[i]); - } - } +void bullet_table_update(void) { + for (int i = 0; i < MAX_BULLETS; ++i) { + if (bullet_table.bullets[i].active) { + bullet_update(&bullet_table.bullets[i]); + } + } } -void -bullet_update(struct Bullet *bullet) -{ - bullet->pos.x += bullet->dir.x * bullet->v; - bullet->pos.y += bullet->dir.y * bullet->v; +void bullet_update(struct Bullet *bullet) { + bullet->pos.x += bullet->dir.x * bullet->v; + bullet->pos.y += bullet->dir.y * bullet->v; - /* boom */ - const struct Vec2 level_dim = level_get_dim(); - if (bullet->pos.x < -72 || bullet->pos.x > level_dim.x * TILE_S + 48 || - bullet->pos.y < -96 || bullet->pos.y > level_dim.y * TILE_S + 48) { - bullet_destroy(bullet); - } + /* boom */ + const struct Vec2 level_dim = level_get_dim(); + if (bullet->pos.x < -72 || bullet->pos.x > level_dim.x * TILE_S + 48 || + bullet->pos.y < -96 || bullet->pos.y > level_dim.y * TILE_S + 48) { + bullet_destroy(bullet); + } } -void -bullet_table_draw(int timer) -{ - for (int i = 0; i < MAX_BULLETS; ++i) { - if (bullet_table.bullets[i].active) { - bullet_draw(&bullet_table.bullets[i], timer); - } - } +void bullet_table_draw(int timer) { + for (int i = 0; i < MAX_BULLETS; ++i) { + if (bullet_table.bullets[i].active) { + bullet_draw(&bullet_table.bullets[i], timer); + } + } } -void -bullet_draw(struct Bullet *bullet, int timer) -{ - LZY_DrawTile(15, bullet->pos.x, bullet->pos.y); +void bullet_draw(struct Bullet *bullet, int timer) { + LZY_DrawTile(15, bullet->pos.x, bullet->pos.y); } -void -bullet_destroy(struct Bullet *bullet) -{ - bullet->active = 0; - --bullet_table.n; +void bullet_destroy(struct Bullet *bullet) { + bullet->active = 0; + --bullet_table.n; } -int -bullet_nb(void) -{ - return bullet_table.n; -} +int bullet_nb(void) { return bullet_table.n; } diff --git a/src/level.c b/src/level.c index 498dfa5..20ffb91 100644 --- a/src/level.c +++ b/src/level.c @@ -1,16 +1,16 @@ /* Ne pas lire ce code svp merci */ #include "level.h" +#include "attack.h" +#include "bullet.h" #include "conf.h" +#include "lzy.h" +#include "particle.h" #include "player.h" #include "sandbag.h" -#include "bullet.h" -#include "attack.h" -#include "particle.h" -#include "lzy.h" -#include #include +#include static int id; static struct Level level; @@ -19,217 +19,205 @@ static void level_free(void); static void level_load(void); static void level_draw_bg(void); -void -level_init(int lvl_id, int chara) -{ - level = (struct Level){ - .w = 17, - .h = 10, - }; - level.s = level.w * level.h; - id = lvl_id; - level_free(); - level_load(); +void level_init(int lvl_id, int chara) { + level = (struct Level){ + .w = 17, + .h = 10, + }; + level.s = level.w * level.h; + id = lvl_id; + level_free(); + level_load(); - player_init(chara); - sandbag_init(); - bullet_table_init(); - attack_table_init(); - particle_table_init(); + player_init(chara); + sandbag_init(); + bullet_table_init(); + attack_table_init(); + particle_table_init(); } -void -level_draw(void) -{ - level_draw_bg(); - for (int m = 0; m < level.h; ++m) { - for (int n = 0; n < level.w; ++n) { - tile_t tile = level_get_tile(n, m); - - LZY_DrawTile(tile, n * TILE_S - DRAW_SHIFT, m * TILE_S); - } - } +void level_draw(void) { + level_draw_bg(); + for (int m = 0; m < level.h; ++m) { + for (int n = 0; n < level.w; ++n) { + tile_t tile = level_get_tile(n, m); + + LZY_DrawTile(tile, n * TILE_S - DRAW_SHIFT, m * TILE_S); + } + } } -tile_t -level_get_px(int x, int y) -{ - x += DRAW_SHIFT; - const int pos = x / TILE_S + y / TILE_S * level.w; - if (pos > level.s || pos < 0 || x < 0 || x > TILE_S * level.w) return 0; - const int tile = level.data[pos]; - return tile - (tile != 0); +tile_t level_get_px(int x, int y) { + x += DRAW_SHIFT; + const int pos = x / TILE_S + y / TILE_S * level.w; + if (pos > level.s || pos < 0 || x < 0 || x > TILE_S * level.w) + return 0; + const int tile = level.data[pos]; + return tile - (tile != 0); } -tile_t -level_get_tile(int x, int y) -{ - if (x + y * level.w > level.s || x + y * level.w < 0) return 0; - const int tile = level.data[x + y * level.w]; - return tile - (tile != 0); +tile_t level_get_tile(int x, int y) { + if (x + y * level.w > level.s || x + y * level.w < 0) + return 0; + const int tile = level.data[x + y * level.w]; + return tile - (tile != 0); } -int -level_count(tile_t tile) -{ - int i = 0; - for (int t = 0; t < level.h * level.w - 1; ++t) { - if (level.data[t] == tile) { - ++i; - } - } - return i; +int level_count(tile_t tile) { + int i = 0; + for (int t = 0; t < level.h * level.w - 1; ++t) { + if (level.data[t] == tile) { + ++i; + } + } + return i; } -int -level_search_i(tile_t tile, int occ) -{ - for (int t = 0; t < level.h * level.w - 1; ++t) { - if (level.data[t] == tile) { - if (!(occ - 1)) { - return t; - } else { - --occ; - } - } - } - return 0; +int level_search_i(tile_t tile, int occ) { + for (int t = 0; t < level.h * level.w - 1; ++t) { + if (level.data[t] == tile) { + if (!(occ - 1)) { + return t; + } else { + --occ; + } + } + } + return 0; } -struct Vec2 -level_search_s(tile_t tile, int occ) -{ - for (int m = 0; m < level.h; ++m) { - for (int n = 0; n < level.w; ++n) { - if (level.data[n + m * level.w] == tile) { - if (!(occ - 1)) { - return (struct Vec2){n, m}; - } else { - --occ; - } - } - } - } - return (struct Vec2){0, 0}; +struct Vec2 level_search_s(tile_t tile, int occ) { + for (int m = 0; m < level.h; ++m) { + for (int n = 0; n < level.w; ++n) { + if (level.data[n + m * level.w] == tile) { + if (!(occ - 1)) { + return (struct Vec2){n, m}; + } else { + --occ; + } + } + } + } + return (struct Vec2){0, 0}; } -struct Vec2 -level_get_dim(void) -{ - return (struct Vec2){level.w, level.h}; +struct Vec2 level_get_dim(void) { + return (struct Vec2){level.w, level.h}; } -static void -level_free(void) -{ - level.data = (tile_t *)malloc(170 * sizeof(tile_t)); +static void level_free(void) { + level.data = (tile_t *)malloc(170 * sizeof(tile_t)); } -static void -level_load(void) -{ - /* TODO : LEVEL LOADING */ - tile_t data[3][170] = {{ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 18, 20, 0, 0, 0, 0, 0, 0, 0, 18, 20, 0, 0, 0, - 0, 0, 0, 41, 67, 19, 19, 19, 19, 19, 19, 19, 69, 43, 0, 0, 0, - 0, 0, 0, 64, 46, 44, 65, 65, 65, 65, 65, 46, 44, 66, 0, 0, 0, - 0, 0, 0, 0, 41, 43, 0, 0, 0, 0, 0, 41, 43, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 84, 85, 86, 87, 88, 89, 90, 91, 92, 84, 85, 0, 0, 0, - 0, 0, 0, 107, 108, 109, 110, 111, 112, 113, 114, 115, 107, 108, 0, 0, 0, - 0, 0, 0, 130, 131, 132, 133, 134, 135, 136, 137, 138, 130, 131, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 183, 182, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 183, 182, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 177, 156, 157, 159, 155, 155, 155, 159, 156, 157, 177, 0, 0, 0, - 0, 0, 0, 161, 179, 180, 161, 160, 160, 160, 161, 179, 180, 161, 0, 0, 0, - 0, 0, 0, 161, 161, 161, 161, 160, 160, 160, 161, 161, 161, 161, 0, 0, 0}}; +static void level_load(void) { + /* TODO : LEVEL LOADING */ + tile_t data[3][170] = { + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 20, 0, 0, 0, 0, 0, 0, 0, + 18, 20, 0, 0, 0, 0, 0, 0, 41, 67, 19, 19, 19, 19, 19, 19, 19, 69, 43, + 0, 0, 0, 0, 0, 0, 64, 46, 44, 65, 65, 65, 65, 65, 46, 44, 66, 0, 0, + 0, 0, 0, 0, 0, 41, 43, 0, 0, 0, 0, 0, 41, 43, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 84, 85, 86, 87, 88, 89, 90, 91, 92, 84, 85, 0, 0, 0, + 0, 0, 0, 107, 108, 109, 110, 111, 112, 113, 114, 115, 107, 108, 0, 0, 0, + 0, 0, 0, 130, 131, 132, 133, 134, 135, 136, 137, 138, 130, 131, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 183, 182, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 183, 182, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 177, 156, 157, 159, 155, 155, + 155, 159, 156, 157, 177, 0, 0, 0, 0, 0, 0, 161, 179, 180, 161, 160, + 160, 160, 161, 179, 180, 161, 0, 0, 0, 0, 0, 0, 161, 161, 161, 161, + 160, 160, 160, 161, 161, 161, 161, 0, 0, 0}}; - memcpy(level.data, data[id], sizeof(data[0])); + memcpy(level.data, data[id], sizeof(data[0])); - if(id==2) { - bg.data = (tile_t *)malloc(170 * sizeof(tile_t)); - - tile_t owo[170] = {240, 262, 262, 0, 288, 242, 241, 242, 242, 242, 240, 242, 288, 0, 262, 262, 241, - 0, 242, 242, 0, 288, 242, 242, 242, 242, 242, 242, 242, 288, 0, 242, 242, 0, - 0, 262, 262, 0, 0, 241, 263, 0, 0, 0, 264, 240, 0, 0, 262, 262, 0, - 0, 0, 0, 0, 263, 0, 242, 242, 242, 242, 242, 0, 264, 0, 0, 0, 0, - 286, 287, 0, 0, 240, 0, 241, 242, 242, 242, 240, 0, 242, 0, 0, 286, 287, - 0, 0, 0, 0, 0, 0, 0, 262, 262, 262, 0, 0, 240, 0, 288, 0, 0, - 263, 0, 242, 0, 239, 239, 285, 262, 262, 262, 285, 239, 239, 0, 241, 0, 264, - 242, 0, 0, 264, 0, 0, 0, 0, 0, 0, 0, 0, 0, 263, 0, 0, 242, - 242, 286, 287, 242, 0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 286, 287, 242, - 242, 263, 264, 242, 0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 242, 263, 242}; - memcpy(bg.data, owo, sizeof(owo)); - } + if (id == 2) { + bg.data = (tile_t *)malloc(170 * sizeof(tile_t)); + + tile_t owo[170] = { + 240, 262, 262, 0, 288, 242, 241, 242, 242, 242, 240, 242, 288, 0, + 262, 262, 241, 0, 242, 242, 0, 288, 242, 242, 242, 242, 242, 242, + 242, 288, 0, 242, 242, 0, 0, 262, 262, 0, 0, 241, 263, 0, + 0, 0, 264, 240, 0, 0, 262, 262, 0, 0, 0, 0, 0, 263, + 0, 242, 242, 242, 242, 242, 0, 264, 0, 0, 0, 0, 286, 287, + 0, 0, 240, 0, 241, 242, 242, 242, 240, 0, 242, 0, 0, 286, + 287, 0, 0, 0, 0, 0, 0, 0, 262, 262, 262, 0, 0, 240, + 0, 288, 0, 0, 263, 0, 242, 0, 239, 239, 285, 262, 262, 262, + 285, 239, 239, 0, 241, 0, 264, 242, 0, 0, 264, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 263, 0, 0, 242, 242, 286, 287, 242, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 286, 287, 242, 242, + 263, 264, 242, 0, 0, 0, 0, 0, 0, 0, 0, 0, 242, 242, + 263, 242}; + memcpy(bg.data, owo, sizeof(owo)); + } } -static void -level_draw_bg(void) { - switch(id){ - case 0: - default: - for (int i = 0; i < 3; ++i) { - LZY_DrawTileEx(115, i * TILE_S * 6, 0, 6, 10); - } - break; +static void level_draw_bg(void) { + switch (id) { + case 0: + default: + for (int i = 0; i < 3; ++i) { + LZY_DrawTileEx(115, i * TILE_S * 6, 0, 6, 10); + } + break; - case 1: - LZY_DrawSetColor(3, 2, 30); - LZY_DrawFillRect(0, 112, 396, 112); - for (int i = 0; i < level.w; ++i) { - LZY_DrawTileEx(121, i * TILE_S, 0, 1, 5); - } - LZY_DrawTileEx(122, 162, 94, 3, 2); - LZY_DrawTileEx(168, 66, 216, 2, 1); - LZY_DrawTileEx(168, 282, 216, 2, 1); - LZY_DrawTileEx(170, 66, 120, 2, 1); - LZY_DrawTileEx(170, 282, 120, 2, 1); + case 1: + LZY_DrawSetColor(3, 2, 30); + LZY_DrawFillRect(0, 112, 396, 112); + for (int i = 0; i < level.w; ++i) { + LZY_DrawTileEx(121, i * TILE_S, 0, 1, 5); + } + LZY_DrawTileEx(122, 162, 94, 3, 2); + LZY_DrawTileEx(168, 66, 216, 2, 1); + LZY_DrawTileEx(168, 282, 216, 2, 1); + LZY_DrawTileEx(170, 66, 120, 2, 1); + LZY_DrawTileEx(170, 282, 120, 2, 1); - LZY_DrawTile(283, -6, 144); - LZY_DrawTile(283, 42, 216); - LZY_DrawTile(283, 42, 120); - LZY_DrawTile(283, 378, 120); - LZY_DrawTileEx(282, 330, 144, 1, 3); - LZY_DrawTileEx(236, 18, 168, 1, 2); - LZY_DrawTileEx(236, 354, 144, 1, 2); - LZY_DrawTileEx(306, 378, 192, 1, 2); - LZY_DrawTileEx(237, -6, 192, 1, 2); - break; + LZY_DrawTile(283, -6, 144); + LZY_DrawTile(283, 42, 216); + LZY_DrawTile(283, 42, 120); + LZY_DrawTile(283, 378, 120); + LZY_DrawTileEx(282, 330, 144, 1, 3); + LZY_DrawTileEx(236, 18, 168, 1, 2); + LZY_DrawTileEx(236, 354, 144, 1, 2); + LZY_DrawTileEx(306, 378, 192, 1, 2); + LZY_DrawTileEx(237, -6, 192, 1, 2); + break; - case 2: - LZY_DrawSetColor(255, 255, 255); - for (int m = 0; m < level.h; ++m) { - for (int n = 0; n < level.w; ++n) { - int tile = bg.data[n + m * level.w]; - tile = tile - (tile != 0); - if (tile) LZY_DrawTile(tile, n * TILE_S - 6, m * TILE_S); - else LZY_DrawFillRect(n * TILE_S - 6, m * TILE_S, 24, 24); - } - } - } + case 2: + LZY_DrawSetColor(255, 255, 255); + for (int m = 0; m < level.h; ++m) { + for (int n = 0; n < level.w; ++n) { + int tile = bg.data[n + m * level.w]; + tile = tile - (tile != 0); + if (tile) + LZY_DrawTile(tile, n * TILE_S - 6, m * TILE_S); + else + LZY_DrawFillRect(n * TILE_S - 6, m * TILE_S, 24, 24); + } + } + } } void level_delete_tile(tile_t tile) { - for (int m = 0; m < level.h; ++m) { - for (int n = 0; n < level.w; ++n) { - if (level_get_tile(n, m) == tile) level.data[m + n * level.w] = 0; - } - } + for (int m = 0; m < level.h; ++m) { + for (int n = 0; n < level.w; ++n) { + if (level_get_tile(n, m) == tile) + level.data[m + n * level.w] = 0; + } + } } diff --git a/src/main.c b/src/main.c index 2663de2..ba73107 100644 --- a/src/main.c +++ b/src/main.c @@ -1,15 +1,15 @@ #include "main.h" -#include "lzy.h" -#include "player.h" -#include "bullet.h" #include "attack.h" +#include "bullet.h" #include "level.h" -#include "sandbag.h" +#include "lzy.h" #include "overlay.h" #include "particle.h" +#include "player.h" +#include "sandbag.h" -#include #include +#include static int frame, chara, level; static int log_init(int x); @@ -21,219 +21,218 @@ static void draw_bg(int frame); /* debug */ -int -init(int argc, const char **argv) -{ - srand(69); - if (LZY_Init(argc, argv, "lzy example", 30, "res/tset.png", - "res/font.png")) { - LZY_Log(LZY_GetError()); - LZY_Quit(); - return 1; - } +int init(int argc, const char **argv) { + srand(69); + if (LZY_Init(argc, argv, "lzy example", 30, "res/tset.png", "res/font.png")) { + LZY_Log(LZY_GetError()); + LZY_Quit(); + return 1; + } - menu(); - - level_init(level, chara); - frame = 0; - return 0; + menu(); + + level_init(level, chara); + frame = 0; + return 0; } -void -update(void) -{ - LZY_CycleEvents(); - player_update(); - sandbag_update(frame); - bullet_table_update(); - attack_table_update(); - particle_table_update(); +void update(void) { + LZY_CycleEvents(); + player_update(); + sandbag_update(frame); + bullet_table_update(); + attack_table_update(); + particle_table_update(); } -void -draw(int frame) -{ - /* draw */ - LZY_DrawBegin(); - { - level_draw(); - player_draw(frame); - sandbag_draw(frame); - bullet_table_draw(frame); - attack_table_draw(frame); - particle_table_draw(frame); - - overlay_draw(114, 176, chara, player_get_life()); - overlay_draw(210, 176, -1, sandbag_get_life()); - } - LZY_DrawEnd(); +void draw(int frame) { + /* draw */ + LZY_DrawBegin(); + { + level_draw(); + player_draw(frame); + sandbag_draw(frame); + bullet_table_draw(frame); + attack_table_draw(frame); + particle_table_draw(frame); + + overlay_draw(114, 176, chara, player_get_life()); + overlay_draw(210, 176, -1, sandbag_get_life()); + } + LZY_DrawEnd(); } -int -main(int argc, const char **argv) -{ - if (init(argc, argv)) { - return 1; - } /* error */ +int main(int argc, const char **argv) { + if (init(argc, argv)) { + return 1; + } /* error */ - while (!LZY_ShouldQuit()) { - update(); - draw(frame); + while (!LZY_ShouldQuit()) { + update(); + draw(frame); - frame += 1; - } + frame += 1; + } - LZY_Log("cya"); - LZY_Quit(); + LZY_Log("cya"); + LZY_Quit(); - return 0; + return 0; } int menu(void) { - /* main loop of the menu */ - int running = 1; - int frame = 0; - while(running) { - LZY_CycleEvents(); - - LZY_DrawBegin(); - { - draw_bg(frame); - LZY_DrawTileEx(247, LZY_DISPLAY_WIDTH / 2 - 72, 30, 6, 5); - if (frame % 16 < 8) { - LZY_DrawText("- PRESS SHIFT -", 134, 181); - LZY_DrawText("- PRESS SHIFT -", 134, 180); - } - } - LZY_DrawEnd(); - running = !LZY_KeyDown(LZYK_O); - frame += 1; - } + /* main loop of the menu */ + int running = 1; + int frame = 0; + while (running) { + LZY_CycleEvents(); - while(LZY_KeyDown(LZYK_O)) {LZY_CycleEvents();} - running = 1; - int selec = 1; - int wait = 0; + LZY_DrawBegin(); + { + draw_bg(frame); + LZY_DrawTileEx(247, LZY_DISPLAY_WIDTH / 2 - 72, 30, 6, 5); + if (frame % 16 < 8) { + LZY_DrawText("- PRESS SHIFT -", 134, 181); + LZY_DrawText("- PRESS SHIFT -", 134, 180); + } + } + LZY_DrawEnd(); + running = !LZY_KeyDown(LZYK_O); + frame += 1; + } - while(running) { - LZY_CycleEvents(); + while (LZY_KeyDown(LZYK_O)) { + LZY_CycleEvents(); + } + running = 1; + int selec = 1; + int wait = 0; - LZY_DrawBegin(); - { - draw_bg(frame); - LZY_DrawTile(61, 186 + (selec - 2.5) * 48, 64.0f + sin((float)frame / 5) * 5.0f); - for (int i=1; i<5; ++i) { - LZY_DrawTile(i * 23, 186 + (i - 2.5) * 48, 100); - } + while (running) { + LZY_CycleEvents(); - switch (selec) { - case 1: - LZY_DrawText("DELTA", 178, 151); - LZY_DrawText("DELTA", 178, 150); - LZY_DrawText("PAR MASSENA", 16, 200); - LZY_DrawText("DANS FROZEN FRENZY", 16, 208); - break; - case 2: - LZY_DrawText("GRAVITY DUCK", 150, 151); - LZY_DrawText("GRAVITY DUCK", 150, 150); - LZY_DrawText("PAR PIEROTLL", 16, 200); - LZY_DrawText("DANS GRAVITY DUCK", 16, 208); - break; - case 3: - LZY_DrawText("AVENTURIER POUDINGUE", 118, 151); - LZY_DrawText("AVENTURIER POUDINGUE", 118, 150); - LZY_DrawText("PAR DRAK", 16, 200); - LZY_DrawText("DANS AVENTURA, LE ROYAUME POUDINGUE", 16, 208); - break; - case 4: - LZY_DrawText("SAKIMI HAKYU", 150, 151); - LZY_DrawText("SAKIMI HAKYU", 150, 150); - LZY_DrawText("PAR REDEYES", 16, 200); - LZY_DrawText("DANS SWORD BURST ZERO", 16, 208); - break; - } - } - LZY_DrawEnd(); - running = !LZY_KeyDown(LZYK_O); - if(LZY_KeyDown(LZYK_LEFT) && selec > 1 && !wait) { - selec -= 1; - wait = 5; - }; - if(LZY_KeyDown(LZYK_RIGHT) && selec < 4 && !wait) { - selec += 1; - wait = 5; - }; - frame += 1; - if (wait > 0) --wait; - } - - while(LZY_KeyDown(LZYK_O)) {LZY_CycleEvents();} - running = 1; - int selec_level = 1; - wait = 0; + LZY_DrawBegin(); + { + draw_bg(frame); + LZY_DrawTile(61, 186 + (selec - 2.5) * 48, + 64.0f + sin((float)frame / 5) * 5.0f); + for (int i = 1; i < 5; ++i) { + LZY_DrawTile(i * 23, 186 + (i - 2.5) * 48, 100); + } - while(running) { - LZY_CycleEvents(); + switch (selec) { + case 1: + LZY_DrawText("DELTA", 178, 151); + LZY_DrawText("DELTA", 178, 150); + LZY_DrawText("PAR MASSENA", 16, 200); + LZY_DrawText("DANS FROZEN FRENZY", 16, 208); + break; + case 2: + LZY_DrawText("GRAVITY DUCK", 150, 151); + LZY_DrawText("GRAVITY DUCK", 150, 150); + LZY_DrawText("PAR PIEROTLL", 16, 200); + LZY_DrawText("DANS GRAVITY DUCK", 16, 208); + break; + case 3: + LZY_DrawText("AVENTURIER POUDINGUE", 118, 151); + LZY_DrawText("AVENTURIER POUDINGUE", 118, 150); + LZY_DrawText("PAR DRAK", 16, 200); + LZY_DrawText("DANS AVENTURA, LE ROYAUME POUDINGUE", 16, 208); + break; + case 4: + LZY_DrawText("SAKIMI HAKYU", 150, 151); + LZY_DrawText("SAKIMI HAKYU", 150, 150); + LZY_DrawText("PAR REDEYES", 16, 200); + LZY_DrawText("DANS SWORD BURST ZERO", 16, 208); + break; + } + } + LZY_DrawEnd(); + running = !LZY_KeyDown(LZYK_O); + if (LZY_KeyDown(LZYK_LEFT) && selec > 1 && !wait) { + selec -= 1; + wait = 5; + }; + if (LZY_KeyDown(LZYK_RIGHT) && selec < 4 && !wait) { + selec += 1; + wait = 5; + }; + frame += 1; + if (wait > 0) + --wait; + } - LZY_DrawBegin(); - { - draw_bg(frame); - LZY_DrawTile(61, 90 + (selec_level - 1) * 96, 40.0f + sin((float)frame / 5) * 5.0f); - for (int i=1; i<4; ++i) { - LZY_DrawTileEx(307 + (i - 1) * 3, 66 + (i - 1) * 96, 76, 3, 2); - } + while (LZY_KeyDown(LZYK_O)) { + LZY_CycleEvents(); + } + running = 1; + int selec_level = 1; + wait = 0; - switch (selec_level) { - case 1: - LZY_DrawText("GRAVITY FOREST DX", 130, 151); - LZY_DrawText("GRAVITY FOREST DX", 130, 150); - break; - case 2: - LZY_DrawText("SANCTUAIRE DE RECUPERATION", 90, 151); - LZY_DrawText("SANCTUAIRE DE RECUPERATION", 90, 150); - break; - case 3: - LZY_DrawText("PLANETE PLATFORMER", 126, 151); - LZY_DrawText("PLANETE PLATFORMER", 126, 150); - break; - } - } - LZY_DrawEnd(); - running = !LZY_KeyDown(LZYK_O); - if(LZY_KeyDown(LZYK_LEFT) && selec_level > 1 && !wait) { - selec_level -= 1; - wait = 5; - }; - if(LZY_KeyDown(LZYK_RIGHT) && selec_level < 3 && !wait) { - selec_level += 1; - wait = 5; - }; - frame += 1; - if (wait > 0) --wait; - } + while (running) { + LZY_CycleEvents(); - chara = selec - 1; - level = selec_level - 1; - return 1; + LZY_DrawBegin(); + { + draw_bg(frame); + LZY_DrawTile(61, 90 + (selec_level - 1) * 96, + 40.0f + sin((float)frame / 5) * 5.0f); + for (int i = 1; i < 4; ++i) { + LZY_DrawTileEx(307 + (i - 1) * 3, 66 + (i - 1) * 96, 76, 3, 2); + } + + switch (selec_level) { + case 1: + LZY_DrawText("GRAVITY FOREST DX", 130, 151); + LZY_DrawText("GRAVITY FOREST DX", 130, 150); + break; + case 2: + LZY_DrawText("SANCTUAIRE DE RECUPERATION", 90, 151); + LZY_DrawText("SANCTUAIRE DE RECUPERATION", 90, 150); + break; + case 3: + LZY_DrawText("PLANETE PLATFORMER", 126, 151); + LZY_DrawText("PLANETE PLATFORMER", 126, 150); + break; + } + } + LZY_DrawEnd(); + running = !LZY_KeyDown(LZYK_O); + if (LZY_KeyDown(LZYK_LEFT) && selec_level > 1 && !wait) { + selec_level -= 1; + wait = 5; + }; + if (LZY_KeyDown(LZYK_RIGHT) && selec_level < 3 && !wait) { + selec_level += 1; + wait = 5; + }; + frame += 1; + if (wait > 0) + --wait; + } + + chara = selec - 1; + level = selec_level - 1; + return 1; } void draw_bg(int frame) { - LZY_DrawSetColor(240, 181, 65); - LZY_DrawClear(); - LZY_DrawSetColor(255, 137, 51); + LZY_DrawSetColor(240, 181, 65); + LZY_DrawClear(); + LZY_DrawSetColor(255, 137, 51); - for (int x = frame%32-32; x < LZY_DISPLAY_WIDTH+32 ; x += 32){ - for (int y = frame%32-32; y < LZY_DISPLAY_HEIGHT+32 ; y += 32){ - LZY_DrawRect(x-1, y-1, 32, 32); - LZY_DrawRect(x, y, 32, 32); - LZY_DrawRect(x+1, y+1, 32, 32); - } - } + for (int x = frame % 32 - 32; x < LZY_DISPLAY_WIDTH + 32; x += 32) { + for (int y = frame % 32 - 32; y < LZY_DISPLAY_HEIGHT + 32; y += 32) { + LZY_DrawRect(x - 1, y - 1, 32, 32); + LZY_DrawRect(x, y, 32, 32); + LZY_DrawRect(x + 1, y + 1, 32, 32); + } + } - LZY_DrawSetColor(207, 117, 43); - for (int x = (frame*2)%32-32; x < LZY_DISPLAY_WIDTH+32 ; x += 32){ - for (int y = (frame*2)%32-32; y < LZY_DISPLAY_HEIGHT+32 ; y += 32){ - LZY_DrawRect(x, y, 32, 32); - } - } + LZY_DrawSetColor(207, 117, 43); + for (int x = (frame * 2) % 32 - 32; x < LZY_DISPLAY_WIDTH + 32; x += 32) { + for (int y = (frame * 2) % 32 - 32; y < LZY_DISPLAY_HEIGHT + 32; y += 32) { + LZY_DrawRect(x, y, 32, 32); + } + } } diff --git a/src/overlay.c b/src/overlay.c index 63f49cc..507d6cf 100644 --- a/src/overlay.c +++ b/src/overlay.c @@ -2,13 +2,15 @@ #include "lzy.h" static void draw_num(int i, int x, int y) { - LZY_DrawTile(35 + i % 2 + (i / 2) * TILESET_W, x, y); + LZY_DrawTile(35 + i % 2 + (i / 2) * TILESET_W, x, y); } -void overlay_draw(int x, int y, int id, int life) { - LZY_DrawTileEx(id == -1 ? 243 : 191 + id * 4, x, y, 4, 2); - if (life > 100) draw_num(life / 100 , x + 12, y + 12); - if (life > 10) draw_num((life % 100 / 10), x + 30, y + 12); - draw_num(life % 10, x + 48, y + 12); - LZY_DrawTile(60, x + 62 , y + 12); +void overlay_draw(int x, int y, int id, int life) { + LZY_DrawTileEx(id == -1 ? 243 : 191 + id * 4, x, y, 4, 2); + if (life > 100) + draw_num(life / 100, x + 12, y + 12); + if (life > 10) + draw_num((life % 100 / 10), x + 30, y + 12); + draw_num(life % 10, x + 48, y + 12); + LZY_DrawTile(60, x + 62, y + 12); } diff --git a/src/particle.c b/src/particle.c index 4e7c47b..de9b07b 100644 --- a/src/particle.c +++ b/src/particle.c @@ -1,11 +1,11 @@ #include "particle.h" #include "conf.h" -#include "tools.h" #include "level.h" #include "lzy.h" +#include "tools.h" -#include #include +#include static struct Particle_table particle_table; @@ -13,86 +13,68 @@ static void particle_table_free(void); static void particle_update(struct Particle *particle); static void particle_draw(struct Particle *particle, int timer); -void -particle_table_init(void) -{ - particle_table_free(); - particle_table.particles = malloc(MAX_PARTICLES * sizeof(struct Particle)); - particle_table.n = 0; +void particle_table_init(void) { + particle_table_free(); + particle_table.particles = malloc(MAX_PARTICLES * sizeof(struct Particle)); + particle_table.n = 0; - for (int i = 0; i < MAX_PARTICLES; ++i) { - particle_table.particles[i].active = 0; - } + for (int i = 0; i < MAX_PARTICLES; ++i) { + particle_table.particles[i].active = 0; + } } -void -particle_table_free(void) -{ - if (particle_table.particles != NULL) { - particle_table.particles = NULL; - } +void particle_table_free(void) { + if (particle_table.particles != NULL) { + particle_table.particles = NULL; + } }; void particle_new(int x, int y, int id, int life) { - for (int i = 0; i < MAX_PARTICLES; ++i) { - if (!particle_table.particles[i].active) { - particle_table.particles[i] = (struct Particle){ - .pos = (struct Vec2){.x = x - TILE_S / 2, .y = y - TILE_S / 2}, - .id = id, - .life = life, - .active = 1}; - break; - } - } - ++particle_table.n; + for (int i = 0; i < MAX_PARTICLES; ++i) { + if (!particle_table.particles[i].active) { + particle_table.particles[i] = (struct Particle){ + .pos = (struct Vec2){.x = x - TILE_S / 2, .y = y - TILE_S / 2}, + .id = id, + .life = life, + .active = 1}; + break; + } + } + ++particle_table.n; } -void -particle_table_update(void) -{ - for (int i = 0; i < MAX_PARTICLES; ++i) { - if (particle_table.particles[i].active) { - particle_update(&particle_table.particles[i]); - } - } +void particle_table_update(void) { + for (int i = 0; i < MAX_PARTICLES; ++i) { + if (particle_table.particles[i].active) { + particle_update(&particle_table.particles[i]); + } + } } -void -particle_update(struct Particle *particle) -{ - particle->life -= 1; - /* boom */ - const struct Vec2 level_dim = level_get_dim(); - if (particle->life < 0) { - particle_destroy(particle); - } +void particle_update(struct Particle *particle) { + particle->life -= 1; + /* boom */ + const struct Vec2 level_dim = level_get_dim(); + if (particle->life < 0) { + particle_destroy(particle); + } } -void -particle_table_draw(int timer) -{ - for (int i = 0; i < MAX_PARTICLES; ++i) { - if (particle_table.particles[i].active) { - particle_draw(&particle_table.particles[i], timer); - } - } +void particle_table_draw(int timer) { + for (int i = 0; i < MAX_PARTICLES; ++i) { + if (particle_table.particles[i].active) { + particle_draw(&particle_table.particles[i], timer); + } + } } -void -particle_draw(struct Particle *particle, int timer) -{ - LZY_DrawTile(particle->id + 1, particle->pos.x, particle->pos.y); +void particle_draw(struct Particle *particle, int timer) { + LZY_DrawTile(particle->id + 1, particle->pos.x, particle->pos.y); } -void -particle_destroy(struct Particle *particle) -{ - particle->active = 0; - --particle_table.n; +void particle_destroy(struct Particle *particle) { + particle->active = 0; + --particle_table.n; } -int -particle_nb(void) -{ - return particle_table.n; -} +int particle_nb(void) { return particle_table.n; } diff --git a/src/player.c b/src/player.c index f23a4ed..571c91e 100644 --- a/src/player.c +++ b/src/player.c @@ -1,10 +1,10 @@ #include "player.h" #include "attack.h" -#include "level.h" #include "conf.h" -#include "tools.h" +#include "level.h" #include "lzy.h" #include "particle.h" +#include "tools.h" static struct Player player; static void player_move(struct Vec2 spd); @@ -15,276 +15,265 @@ static void player_attack(int x, int y, int type); static void player_cooldown_update(); static int jump_pressed; -void -player_init(int chara) -{ - player.pos = (struct Vec2){84, 96}; - player.spd = (struct FVec2){0.0f, 0.0f}; - player.rem = (struct FVec2){0.0f, 0.0f}; - player.dir = (struct Vec2){1, 0}; - player.cooldown = (struct Cooldown){0, 0, 0, 0}; - player.attack = 0; - player.last_attack = 0; - player.life = 0; - player.id = chara; - player.djump = 0; +void player_init(int chara) { + player.pos = (struct Vec2){84, 96}; + player.spd = (struct FVec2){0.0f, 0.0f}; + player.rem = (struct FVec2){0.0f, 0.0f}; + player.dir = (struct Vec2){1, 0}; + player.cooldown = (struct Cooldown){0, 0, 0, 0}; + player.attack = 0; + player.last_attack = 0; + player.life = 0; + player.id = chara; + player.djump = 0; } -void -player_update(void) -{ - const int on_ground = player_collide(player.pos.x, player.pos.y + 5, 1, 4); - if(on_ground) player.djump = 1; - - const struct Vec2 level_dim = level_get_dim(); +void player_update(void) { + const int on_ground = player_collide(player.pos.x, player.pos.y + 5, 1, 4); + if (on_ground) + player.djump = 1; - const struct Vec2 dir = {LZY_KeyDown(LZYK_RIGHT) - LZY_KeyDown(LZYK_LEFT), - LZY_KeyDown(LZYK_DOWN) - LZY_KeyDown(LZYK_UP)}; + const struct Vec2 level_dim = level_get_dim(); - player.spd.x *= (1 - PLAYER_FRIC); - player.spd.x += dir.x * PLAYER_ACC; - player.spd.y += GRAVITY * (player.last_attack == 4 && player.id == 1 ? -1 : 1); + const struct Vec2 dir = {LZY_KeyDown(LZYK_RIGHT) - LZY_KeyDown(LZYK_LEFT), + LZY_KeyDown(LZYK_DOWN) - LZY_KeyDown(LZYK_UP)}; - /* direction */ - if (dir.y) { - player.dir.y = dir.y; - } - if (dir.x) { - player.dir.x = dir.x; - } - - if (LZY_KeyDown(LZYK_O)) { - if(on_ground) { - player.spd.y = -JUMP_H; - jump_pressed = 1; - } - else if(player.djump && !jump_pressed) { - player.spd.y = -JUMP_H; - player.djump = 0; - particle_new(player.pos.x + PLAYER_W / 2, player.pos.y + PLAYER_H, 10, 5); - } - } + player.spd.x *= (1 - PLAYER_FRIC); + player.spd.x += dir.x * PLAYER_ACC; + player.spd.y += + GRAVITY * (player.last_attack == 4 && player.id == 1 ? -1 : 1); - if (jump_pressed && !LZY_KeyDown(LZYK_O)) { - jump_pressed = 0; - } + /* direction */ + if (dir.y) { + player.dir.y = dir.y; + } + if (dir.x) { + player.dir.x = dir.x; + } - if (player_collide(player.pos.x, player.pos.y + 5, 176, 4)) { - player.spd.y = -16; - } - - if (player_collide(player.pos.x, player.pos.y + 5, 153, 4)) { - player.spd.y = -3 * 5 * (player.life > 50 ? (float)player.life / 100 + 0.5 : 1); - player.spd.x = -player.dir.x * 5 * (player.life > 50 ? (float)player.life / 100 + 0.5 : 1); - player.life += 10; - } - - player_move(player_update_rem()); + if (LZY_KeyDown(LZYK_O)) { + if (on_ground) { + player.spd.y = -JUMP_H; + jump_pressed = 1; + } else if (player.djump && !jump_pressed) { + player.spd.y = -JUMP_H; + player.djump = 0; + particle_new(player.pos.x + PLAYER_W / 2, player.pos.y + PLAYER_H, 10, 5); + } + } - /* defining the direction of the shoot */ - const struct Vec2 shoot = { - (dir.x || dir.y) ? dir.x : player.dir.x, - (dir.x || dir.y) ? dir.y : player.dir.y - }; + if (jump_pressed && !LZY_KeyDown(LZYK_O)) { + jump_pressed = 0; + } - if (LZY_KeyDown(LZYK_X) && !player.last_attack) { - player_attack(player.pos.x, player.pos.y, 1); - } + if (player_collide(player.pos.x, player.pos.y + 5, 176, 4)) { + player.spd.y = -16; + } - if (!LZY_KeyDown(LZYK_X)) { - player.last_attack = 0; - } + if (player_collide(player.pos.x, player.pos.y + 5, 153, 4)) { + player.spd.y = + -3 * 5 * (player.life > 50 ? (float)player.life / 100 + 0.5 : 1); + player.spd.x = -player.dir.x * 5 * + (player.life > 50 ? (float)player.life / 100 + 0.5 : 1); + player.life += 10; + } - player_cooldown_update(); - - if(player.pos.x > level_dim.x * TILE_S + 48 || player.pos.x < -72 || - player.pos.y > level_dim.y * TILE_S + 48 || player.pos.y < -96) { - player_init(player.id); - } + player_move(player_update_rem()); + + /* defining the direction of the shoot */ + const struct Vec2 shoot = {(dir.x || dir.y) ? dir.x : player.dir.x, + (dir.x || dir.y) ? dir.y : player.dir.y}; + + if (LZY_KeyDown(LZYK_X) && !player.last_attack) { + player_attack(player.pos.x, player.pos.y, 1); + } + + if (!LZY_KeyDown(LZYK_X)) { + player.last_attack = 0; + } + + player_cooldown_update(); + + if (player.pos.x > level_dim.x * TILE_S + 48 || player.pos.x < -72 || + player.pos.y > level_dim.y * TILE_S + 48 || player.pos.y < -96) { + player_init(player.id); + } } static void player_attack(int x, int y, int type) { - const int up = LZY_KeyDown(LZYK_UP); - const int down = LZY_KeyDown(LZYK_DOWN); - const int left = LZY_KeyDown(LZYK_LEFT); - const int right = LZY_KeyDown(LZYK_RIGHT); - if(down && !player.cooldown.down) { - player.cooldown.down = attack(x + PLAYER_W / 2, - y + PLAYER_H / 2 + 12, player.dir.x, 1, 2 + player.id * 4); - player.attack = 2; - player.last_attack = 2; - return; - } - if(up && !player.cooldown.up) { - player.cooldown.up = attack(x + PLAYER_W / 2, - y + PLAYER_H / 2 - 12, player.dir.x, 1, 3 + player.id * 4); - player.attack = 3; - player.last_attack = 3; - return; - } - if((left || right) && !player.cooldown.side) { - player.cooldown.side = attack(x + PLAYER_W / 2 + player.dir.x * 12, - y + PLAYER_H / 2, player.dir.x, 1, 1 + player.id * 4); - player.attack = 1; - player.last_attack = 1; - return; - } - if(!(up || down || left || right) && !player.cooldown.neutral) { - player.cooldown.neutral = attack(x + PLAYER_W / 2, - y + PLAYER_H / 2, player.dir.x, 1, 4 + player.id * 4); - player.attack = 4; - player.last_attack = 4; - } + const int up = LZY_KeyDown(LZYK_UP); + const int down = LZY_KeyDown(LZYK_DOWN); + const int left = LZY_KeyDown(LZYK_LEFT); + const int right = LZY_KeyDown(LZYK_RIGHT); + if (down && !player.cooldown.down) { + player.cooldown.down = attack(x + PLAYER_W / 2, y + PLAYER_H / 2 + 12, + player.dir.x, 1, 2 + player.id * 4); + player.attack = 2; + player.last_attack = 2; + return; + } + if (up && !player.cooldown.up) { + player.cooldown.up = attack(x + PLAYER_W / 2, y + PLAYER_H / 2 - 12, + player.dir.x, 1, 3 + player.id * 4); + player.attack = 3; + player.last_attack = 3; + return; + } + if ((left || right) && !player.cooldown.side) { + player.cooldown.side = + attack(x + PLAYER_W / 2 + player.dir.x * 12, y + PLAYER_H / 2, + player.dir.x, 1, 1 + player.id * 4); + player.attack = 1; + player.last_attack = 1; + return; + } + if (!(up || down || left || right) && !player.cooldown.neutral) { + player.cooldown.neutral = attack(x + PLAYER_W / 2, y + PLAYER_H / 2, + player.dir.x, 1, 4 + player.id * 4); + player.attack = 4; + player.last_attack = 4; + } } -static struct Vec2 -player_update_rem(void) -{ - struct FVec2 spdrem = (struct FVec2){player.spd.x + player.rem.x, - player.spd.y + player.rem.y}; - struct Vec2 ispd = (struct Vec2){spdrem.x, spdrem.y}; +static struct Vec2 player_update_rem(void) { + struct FVec2 spdrem = + (struct FVec2){player.spd.x + player.rem.x, player.spd.y + player.rem.y}; + struct Vec2 ispd = (struct Vec2){spdrem.x, spdrem.y}; - player.rem.x = spdrem.x - (float)ispd.x; - player.rem.y = spdrem.y - (float)ispd.y; - return ispd; + player.rem.x = spdrem.x - (float)ispd.x; + player.rem.y = spdrem.y - (float)ispd.y; + return ispd; } -static void -player_move(struct Vec2 spd) -{ - if (player_collide(player.pos.x, player.pos.y, 1, 4)) { - return; - } - - int coll_shift = 0; +static void player_move(struct Vec2 spd) { + if (player_collide(player.pos.x, player.pos.y, 1, 4)) { + return; + } - const int sign_x = sign(spd.x); - if (sign_x) {player.dir.x = sign_x;} - - if (spd.x) { - player.pos.x += spd.x; + int coll_shift = 0; - if (player_collide(player.pos.x, player.pos.y, 1, 1)) { - for(float i = 0.1f; i < 4; i+= 0.1) { - if(!player_collide(player.pos.x, player.pos.y + i, 1, 1)) { - player.pos.y += i; - coll_shift = 1; - break; - } - if(!player_collide(player.pos.x, player.pos.y - i, 1, 1)) { - player.pos.y -= i; - coll_shift = 1; - break; - } - } - } + const int sign_x = sign(spd.x); + if (sign_x) { + player.dir.x = sign_x; + } - if(player_collide(player.pos.x, player.pos.y, 1, 1)) { - player.spd.x = 0.0f; - player.rem.x = 0.0f; - while (player_collide(player.pos.x, player.pos.y, 1, 1)) { - player.pos.x -= sign_x; - } - } - } - - const int sign_y = sign(spd.y); - if (sign_y) {player.dir.y = sign_y;} + if (spd.x) { + player.pos.x += spd.x; - if (spd.y && !coll_shift) { - player.pos.y += spd.y; + if (player_collide(player.pos.x, player.pos.y, 1, 1)) { + for (float i = 0.1f; i < 4; i += 0.1) { + if (!player_collide(player.pos.x, player.pos.y + i, 1, 1)) { + player.pos.y += i; + coll_shift = 1; + break; + } + if (!player_collide(player.pos.x, player.pos.y - i, 1, 1)) { + player.pos.y -= i; + coll_shift = 1; + break; + } + } + } - if (player_collide(player.pos.x, player.pos.y, 1, 1)) { - for(float i = 0.1f; i < 4; i += 0.1) { - if(!player_collide(player.pos.x + i, player.pos.y, 1, 1)) { - player.pos.x += i; - break; - } - if(!player_collide(player.pos.x - i, player.pos.y, 1, 1)) { - player.pos.x -= i; - break; - } - } - } + if (player_collide(player.pos.x, player.pos.y, 1, 1)) { + player.spd.x = 0.0f; + player.rem.x = 0.0f; + while (player_collide(player.pos.x, player.pos.y, 1, 1)) { + player.pos.x -= sign_x; + } + } + } - if(player_collide(player.pos.x, player.pos.y, 1, 1)) { - player.spd.y = 0.0f; - player.rem.y = 0.0f; - while (player_collide(player.pos.x, player.pos.y, 1, 1)) { - player.pos.y -= sign_y; - } - } - } + const int sign_y = sign(spd.y); + if (sign_y) { + player.dir.y = sign_y; + } + + if (spd.y && !coll_shift) { + player.pos.y += spd.y; + + if (player_collide(player.pos.x, player.pos.y, 1, 1)) { + for (float i = 0.1f; i < 4; i += 0.1) { + if (!player_collide(player.pos.x + i, player.pos.y, 1, 1)) { + player.pos.x += i; + break; + } + if (!player_collide(player.pos.x - i, player.pos.y, 1, 1)) { + player.pos.x -= i; + break; + } + } + } + + if (player_collide(player.pos.x, player.pos.y, 1, 1)) { + player.spd.y = 0.0f; + player.rem.y = 0.0f; + while (player_collide(player.pos.x, player.pos.y, 1, 1)) { + player.pos.y -= sign_y; + } + } + } } -void -player_draw(int frame) -{ - if(player.attack) { - LZY_DrawTile(24 + TILESET_W * player.id + player.attack + (player.dir.x > 0 ? 0 : 1) * 6, - player.pos.x, player.pos.y); - } else { - if(LZY_KeyDown(LZYK_X)) { - LZY_DrawTile(24 + player.last_attack + TILESET_W * player.id - + (player.dir.x > 0 ? 0 : 1) * 6, - player.pos.x, player.pos.y); - } else { - LZY_DrawTile((frame % 30 > 15 ? 23 : 24) + TILESET_W * player.id - + (player.dir.x > 0 ? 0 : 1) * 6, - player.pos.x, player.pos.y); - } - } +void player_draw(int frame) { + if (player.attack) { + LZY_DrawTile(24 + TILESET_W * player.id + player.attack + + (player.dir.x > 0 ? 0 : 1) * 6, + player.pos.x, player.pos.y); + } else { + if (LZY_KeyDown(LZYK_X)) { + LZY_DrawTile(24 + player.last_attack + TILESET_W * player.id + + (player.dir.x > 0 ? 0 : 1) * 6, + player.pos.x, player.pos.y); + } else { + LZY_DrawTile((frame % 30 > 15 ? 23 : 24) + TILESET_W * player.id + + (player.dir.x > 0 ? 0 : 1) * 6, + player.pos.x, player.pos.y); + } + } } -static int -player_collide_pixel(int x, int y, tile_t id) -{ - const tile_t tile = level_get_px(x, y); +static int player_collide_pixel(int x, int y, tile_t id) { + const tile_t tile = level_get_px(x, y); - if (!tile) { - return 0; - } + if (!tile) { + return 0; + } - if (id == 1) { - return (tile) % TILESET_W; - } + if (id == 1) { + return (tile) % TILESET_W; + } - return tile == id; + return tile == id; } -static int -player_collide(int x, int y, tile_t id, int h) -{ +static int player_collide(int x, int y, tile_t id, int h) { - const struct Vec2 pos_tl = (struct Vec2){x + h, y + h}; - const struct Vec2 pos_br = - (struct Vec2){x + PLAYER_S - h - 1, y + PLAYER_S - h - 1}; - const struct Vec2 middle = - (struct Vec2){x + PLAYER_S / 2, y + PLAYER_S / 2}; + const struct Vec2 pos_tl = (struct Vec2){x + h, y + h}; + const struct Vec2 pos_br = + (struct Vec2){x + PLAYER_S - h - 1, y + PLAYER_S - h - 1}; + const struct Vec2 middle = (struct Vec2){x + PLAYER_S / 2, y + PLAYER_S / 2}; - if (player_collide_pixel(pos_tl.x, pos_tl.y, id) || - player_collide_pixel(pos_br.x, pos_br.y, id) || - player_collide_pixel(pos_tl.x, pos_br.y, id) || - player_collide_pixel(pos_br.x, pos_tl.y, id)) { - return 1; - } - return 0; + if (player_collide_pixel(pos_tl.x, pos_tl.y, id) || + player_collide_pixel(pos_br.x, pos_br.y, id) || + player_collide_pixel(pos_tl.x, pos_br.y, id) || + player_collide_pixel(pos_br.x, pos_tl.y, id)) { + return 1; + } + return 0; } -void -player_reset_attack() { - player.attack = 0; +void player_reset_attack() { player.attack = 0; } + +void player_cooldown_update() { + if (player.cooldown.up > 0) + player.cooldown.up -= 1; + if (player.cooldown.down > 0) + player.cooldown.down -= 1; + if (player.cooldown.side > 0) + player.cooldown.side -= 1; + if (player.cooldown.neutral > 0) + player.cooldown.neutral -= 1; } -void -player_cooldown_update() { - if (player.cooldown.up > 0) player.cooldown.up -= 1; - if (player.cooldown.down > 0) player.cooldown.down -= 1; - if (player.cooldown.side > 0) player.cooldown.side -= 1; - if (player.cooldown.neutral > 0) player.cooldown.neutral -= 1; -} - -int -player_get_life(void) { - return player.life; -} +int player_get_life(void) { return player.life; } diff --git a/src/sandbag.c b/src/sandbag.c index a286f1a..0609ced 100644 --- a/src/sandbag.c +++ b/src/sandbag.c @@ -1,10 +1,10 @@ #include "sandbag.h" #include "attack.h" -#include "level.h" #include "conf.h" -#include "tools.h" +#include "level.h" #include "lzy.h" #include "particle.h" +#include "tools.h" static struct Sandbag sandbag; static void sandbag_move(struct Vec2 spd); @@ -12,202 +12,200 @@ static struct Vec2 sandbag_update_rem(); static int sandbag_collide_pixel(int x, int y, tile_t id); static int sandbag_collide(int x, int y, tile_t id, int h); -void -sandbag_init(void) -{ - sandbag.pos = (struct Vec2){300, 96}; - sandbag.spd = (struct FVec2){0.0f, 0.0f}; - sandbag.rem = (struct FVec2){0.0f, 0.0f}; - sandbag.dir = (struct Vec2){1, 0}; - sandbag.hitstun = 0; - sandbag.iced = 0; - sandbag.life = 0; - sandbag.active = 1; +void sandbag_init(void) { + sandbag.pos = (struct Vec2){300, 96}; + sandbag.spd = (struct FVec2){0.0f, 0.0f}; + sandbag.rem = (struct FVec2){0.0f, 0.0f}; + sandbag.dir = (struct Vec2){1, 0}; + sandbag.hitstun = 0; + sandbag.iced = 0; + sandbag.life = 0; + sandbag.active = 1; } -void -sandbag_update(int frame) -{ - const struct Vec2 level_dim = level_get_dim(); - - if(!sandbag.active) return; - const struct Attack *attack = attack_table_collide(sandbag.pos.x, sandbag.pos.y, 24, 24, 0); - if(attack != NULL) { - sandbag.spd.x = attack->dir.x * 5 * (sandbag.life > 50 ? (float)sandbag.life / 50 : 1); - sandbag.spd.y = -attack->dir.y * 5 * (sandbag.life > 50 ? (float)sandbag.life / 100 + 0.5 : 1); - if(!sandbag.hitstun) { - sandbag.life += attack->dam; - LZY_Sleep(10); - }; - sandbag.hitstun = 5; - if(attack->ice) {sandbag.iced = 30;} - } +void sandbag_update(int frame) { + const struct Vec2 level_dim = level_get_dim(); - sandbag.spd.x *= (1 - AIR_FRIC) / (sandbag.iced ? 2 : 1); - sandbag.spd.y += GRAVITY; + if (!sandbag.active) + return; + const struct Attack *attack = + attack_table_collide(sandbag.pos.x, sandbag.pos.y, 24, 24, 0); + if (attack != NULL) { + sandbag.spd.x = + attack->dir.x * 5 * (sandbag.life > 50 ? (float)sandbag.life / 50 : 1); + sandbag.spd.y = -attack->dir.y * 5 * + (sandbag.life > 50 ? (float)sandbag.life / 100 + 0.5 : 1); + if (!sandbag.hitstun) { + sandbag.life += attack->dam; + LZY_Sleep(10); + }; + sandbag.hitstun = 5; + if (attack->ice) { + sandbag.iced = 30; + } + } - if (sandbag.spd.x) { - sandbag.dir.x = sign(sandbag.spd.x); - } - - sandbag_move(sandbag_update_rem()); + sandbag.spd.x *= (1 - AIR_FRIC) / (sandbag.iced ? 2 : 1); + sandbag.spd.y += GRAVITY; - if(sandbag.hitstun > 0) {sandbag.hitstun -= 1;} - if(sandbag.iced > 0) {sandbag.iced -= 1;} - if(sandbag.hitstun && frame % 2 && sandbag.life > 80) { - particle_new(sandbag.pos.x + 12, sandbag.pos.y + 12, 10 + rand() % 2, 5); - } - if(frame % 4 && sandbag.life > 120) { - particle_new(sandbag.pos.x + 12, sandbag.pos.y + 12, 12 + (frame % 8) / 2 % 2, 2); - } + if (sandbag.spd.x) { + sandbag.dir.x = sign(sandbag.spd.x); + } - if(sandbag.pos.x > level_dim.x * TILE_S + 48 || sandbag.pos.x < -72 || - sandbag.pos.y > level_dim.y * TILE_S + 48 || sandbag.pos.y < -96) { - sandbag_init(); - } + sandbag_move(sandbag_update_rem()); + + if (sandbag.hitstun > 0) { + sandbag.hitstun -= 1; + } + if (sandbag.iced > 0) { + sandbag.iced -= 1; + } + if (sandbag.hitstun && frame % 2 && sandbag.life > 80) { + particle_new(sandbag.pos.x + 12, sandbag.pos.y + 12, 10 + rand() % 2, 5); + } + if (frame % 4 && sandbag.life > 120) { + particle_new(sandbag.pos.x + 12, sandbag.pos.y + 12, + 12 + (frame % 8) / 2 % 2, 2); + } + + if (sandbag.pos.x > level_dim.x * TILE_S + 48 || sandbag.pos.x < -72 || + sandbag.pos.y > level_dim.y * TILE_S + 48 || sandbag.pos.y < -96) { + sandbag_init(); + } } -static struct Vec2 -sandbag_update_rem(void) -{ - struct FVec2 spdrem = (struct FVec2){sandbag.spd.x + sandbag.rem.x, - sandbag.spd.y + sandbag.rem.y}; - struct Vec2 ispd = (struct Vec2){spdrem.x, spdrem.y}; +static struct Vec2 sandbag_update_rem(void) { + struct FVec2 spdrem = (struct FVec2){sandbag.spd.x + sandbag.rem.x, + sandbag.spd.y + sandbag.rem.y}; + struct Vec2 ispd = (struct Vec2){spdrem.x, spdrem.y}; - sandbag.rem.x = spdrem.x - (float)ispd.x; - sandbag.rem.y = spdrem.y - (float)ispd.y; - return ispd; + sandbag.rem.x = spdrem.x - (float)ispd.x; + sandbag.rem.y = spdrem.y - (float)ispd.y; + return ispd; } -static void -sandbag_move(struct Vec2 spd) -{ - if (sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 4)) { - return; - } +static void sandbag_move(struct Vec2 spd) { + if (sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 4)) { + return; + } - /* OOB */ - const struct Vec2 level_dim = level_get_dim(); - - int coll_shift = 0; + /* OOB */ + const struct Vec2 level_dim = level_get_dim(); - const int sign_x = sign(spd.x); - if (sign_x) {sandbag.dir.x = sign_x;} - - if (spd.x) { - sandbag.pos.x += spd.x; + int coll_shift = 0; - if (sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 1)) { - for(float i = 0.1f; i < 4; i+= 0.1) { - if(!sandbag_collide(sandbag.pos.x, sandbag.pos.y + i, 1, 1)) { - sandbag.pos.y += i; - coll_shift = 1; - break; - } - if(!sandbag_collide(sandbag.pos.x, sandbag.pos.y - i, 1, 1)) { - sandbag.pos.y -= i; - coll_shift = 1; - break; - } - } - } + const int sign_x = sign(spd.x); + if (sign_x) { + sandbag.dir.x = sign_x; + } - if(sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 1)) { - if(abs(sandbag.spd.x) > 16) { - sandbag.spd.x = - sandbag.spd.x / 4; - } else { - sandbag.spd.x = 0.0f; - } - sandbag.rem.x = 0.0f; - while (sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 1)) { - sandbag.pos.x -= sign_x; - } - } - } - - const int sign_y = sign(spd.y); - if (sign_y) {sandbag.dir.y = sign_y;} + if (spd.x) { + sandbag.pos.x += spd.x; - if (spd.y && !coll_shift) { - sandbag.pos.y += spd.y; + if (sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 1)) { + for (float i = 0.1f; i < 4; i += 0.1) { + if (!sandbag_collide(sandbag.pos.x, sandbag.pos.y + i, 1, 1)) { + sandbag.pos.y += i; + coll_shift = 1; + break; + } + if (!sandbag_collide(sandbag.pos.x, sandbag.pos.y - i, 1, 1)) { + sandbag.pos.y -= i; + coll_shift = 1; + break; + } + } + } - if (sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 1)) { - for(float i = 0.1f; i < 4; i += 0.1) { - if(!sandbag_collide(sandbag.pos.x + i, sandbag.pos.y, 1, 1)) { - sandbag.pos.x += i; - break; - } - if(!sandbag_collide(sandbag.pos.x - i, sandbag.pos.y, 1, 1)) { - sandbag.pos.x -= i; - break; - } - } - } + if (sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 1)) { + if (abs(sandbag.spd.x) > 16) { + sandbag.spd.x = -sandbag.spd.x / 4; + } else { + sandbag.spd.x = 0.0f; + } + sandbag.rem.x = 0.0f; + while (sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 1)) { + sandbag.pos.x -= sign_x; + } + } + } - if(sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 1)) { - if(abs(sandbag.spd.y) > 16) { - sandbag.spd.y = - sandbag.spd.y / 4; - } else { - sandbag.spd.y = 0.0f; - } - sandbag.rem.y = 0.0f; - while (sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 1)) { - sandbag.pos.y -= sign_y; - } - } - } + const int sign_y = sign(spd.y); + if (sign_y) { + sandbag.dir.y = sign_y; + } + + if (spd.y && !coll_shift) { + sandbag.pos.y += spd.y; + + if (sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 1)) { + for (float i = 0.1f; i < 4; i += 0.1) { + if (!sandbag_collide(sandbag.pos.x + i, sandbag.pos.y, 1, 1)) { + sandbag.pos.x += i; + break; + } + if (!sandbag_collide(sandbag.pos.x - i, sandbag.pos.y, 1, 1)) { + sandbag.pos.x -= i; + break; + } + } + } + + if (sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 1)) { + if (abs(sandbag.spd.y) > 16) { + sandbag.spd.y = -sandbag.spd.y / 4; + } else { + sandbag.spd.y = 0.0f; + } + sandbag.rem.y = 0.0f; + while (sandbag_collide(sandbag.pos.x, sandbag.pos.y, 1, 1)) { + sandbag.pos.y -= sign_y; + } + } + } } -void -sandbag_draw(int frame) -{ - if(!sandbag.active) return; - if(sandbag.hitstun) { - LZY_DrawTile(39, sandbag.pos.x, sandbag.pos.y); - } else { - LZY_DrawTile(38, sandbag.pos.x, sandbag.pos.y); - } - if(sandbag.iced > 0) { - LZY_DrawTile(16, sandbag.pos.x, sandbag.pos.y); - } +void sandbag_draw(int frame) { + if (!sandbag.active) + return; + if (sandbag.hitstun) { + LZY_DrawTile(39, sandbag.pos.x, sandbag.pos.y); + } else { + LZY_DrawTile(38, sandbag.pos.x, sandbag.pos.y); + } + if (sandbag.iced > 0) { + LZY_DrawTile(16, sandbag.pos.x, sandbag.pos.y); + } } -static int -sandbag_collide_pixel(int x, int y, tile_t id) -{ - const tile_t tile = level_get_px(x, y); +static int sandbag_collide_pixel(int x, int y, tile_t id) { + const tile_t tile = level_get_px(x, y); - if (!tile) { - return 0; - } + if (!tile) { + return 0; + } - if (id == 1) { - return (tile) % TILESET_W; - } + if (id == 1) { + return (tile) % TILESET_W; + } - return tile == id; + return tile == id; } -static int -sandbag_collide(int x, int y, tile_t id, int h) -{ +static int sandbag_collide(int x, int y, tile_t id, int h) { - const struct Vec2 pos_tl = (struct Vec2){x + h, y + h}; - const struct Vec2 pos_br = - (struct Vec2){x + PLAYER_S - h - 1, y + PLAYER_S - h - 1}; - const struct Vec2 middle = - (struct Vec2){x + PLAYER_S / 2, y + PLAYER_S / 2}; + const struct Vec2 pos_tl = (struct Vec2){x + h, y + h}; + const struct Vec2 pos_br = + (struct Vec2){x + PLAYER_S - h - 1, y + PLAYER_S - h - 1}; + const struct Vec2 middle = (struct Vec2){x + PLAYER_S / 2, y + PLAYER_S / 2}; - if (sandbag_collide_pixel(pos_tl.x, pos_tl.y, id) || - sandbag_collide_pixel(pos_br.x, pos_br.y, id) || - sandbag_collide_pixel(pos_tl.x, pos_br.y, id) || - sandbag_collide_pixel(pos_br.x, pos_tl.y, id)) { - return 1; - } - return 0; + if (sandbag_collide_pixel(pos_tl.x, pos_tl.y, id) || + sandbag_collide_pixel(pos_br.x, pos_br.y, id) || + sandbag_collide_pixel(pos_tl.x, pos_br.y, id) || + sandbag_collide_pixel(pos_br.x, pos_tl.y, id)) { + return 1; + } + return 0; } -int -sandbag_get_life(void) { - return sandbag.life; -} +int sandbag_get_life(void) { return sandbag.life; } diff --git a/src/tools.c b/src/tools.c index 25da409..f1a0167 100644 --- a/src/tools.c +++ b/src/tools.c @@ -3,26 +3,17 @@ #include #include -void -log_int(int x) -{ - char buf[64]; - sprintf(buf, "%d", x); - LZY_Log(buf); +void log_int(int x) { + char buf[64]; + sprintf(buf, "%d", x); + LZY_Log(buf); } -int -sign(int x) -{ - return (x > 0) - (x < 0); /* from jtmm2 */ -} +int sign(int x) { return (x > 0) - (x < 0); /* from jtmm2 */ } - -int -length(struct Vec2 *a, struct Vec2 *b) -{ - const int dist_x = abs(a->x - b->x); - const int dist_y = abs(a->y - b->y); - const float dist = sqrt(dist_x * dist_x + dist_y * dist_y); - return dist; +int length(struct Vec2 *a, struct Vec2 *b) { + const int dist_x = abs(a->x - b->x); + const int dist_y = abs(a->y - b->y); + const float dist = sqrt(dist_x * dist_x + dist_y * dist_y); + return dist; }