From 9e65d04d22b9b979cf9d98ce24d8bc673fc2a338 Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Sat, 26 Oct 2024 12:10:04 +0200 Subject: [PATCH] CPC : premier ennemi --- include/C3D/config.h | 2 +- include/C3D/fixed.h | 2 +- src/game.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ src/game.h | 4 ++++ src/gfx.c | 5 ++++ src/gfx.h | 4 ++++ src/main.c | 8 ++++--- 7 files changed, 77 insertions(+), 5 deletions(-) diff --git a/include/C3D/config.h b/include/C3D/config.h index 13ad996..3b7bdca 100644 --- a/include/C3D/config.h +++ b/include/C3D/config.h @@ -7,7 +7,7 @@ #define screen_w 128 #define screen_h 64 -#define viewport_w 128 +#define viewport_w 100 #define viewport_h 64 #define max_dist fix(32) //Max sprites to attempt to render diff --git a/include/C3D/fixed.h b/include/C3D/fixed.h index 3d46aae..ed9ed39 100644 --- a/include/C3D/fixed.h +++ b/include/C3D/fixed.h @@ -72,7 +72,7 @@ static inline double f2double(fixed_t f) return (double)f / 65536; } -static inline fixed_t feasein(fixed_t x) +static inline fixed_t fsq(fixed_t x) { return fmul(x, x); } diff --git a/src/game.c b/src/game.c index 13681a3..48378ea 100644 --- a/src/game.c +++ b/src/game.c @@ -3,6 +3,8 @@ #include "C3D/sprites.h" #include "C3D/fixed.h" +#include "C3D/game.h" +#include "game.h" int sreserve_c = 0; Sprite sprite_reserve[SINDEX_S]; @@ -37,3 +39,58 @@ void cc_rmsprite(int pos){ } sreserve_c--; } + +void cc_hit(CCGame *game, int hitstr){ + if(game->hit > 0) + return; + game->hit = 40; + game->hp -= hitstr; +} + +int cc_ai_mov(RcGame *game, Sprite *spr, fixed_t spd, int hitstr, int coll){ + fixed_t dx = spr->pos.x - game->player.pos.x; + fixed_t dy = spr->pos.y - game->player.pos.y; + fixed_t player_dist = fsq(dx) + fsq(dy); + int dxs = (dx >= 0) * 2 - 1; + int dys = (dy >= 0) * 2 - 1; + + if(player_dist > fsq(max_dist)) + return 0; + if(fixabs(ffloor(dx)) <= 1 && fixabs(ffloor(dy)) <= 1) + cc_hit((CCGame*)game, hitstr); + if(fixabs(dx) >= fixabs(dy)) + spr->pos.x -= dxs * spd; + else + spr->pos.y -= dys * spd; + return 0; +} + +int cc_ai(RcGame *game){ + for(int i = 0; i < sreserve_c; i++){ + Sprite *spr = &sprite_reserve[i]; + switch(spr->tex){ + default: + case 0: + case 1: + case 2: + case 3: + break; + case 4:{ + cc_ai_mov(game, spr, fix(.025), 5, 0); + break; + } + } + } + return 0; +} + +int cc_logic(RcGame *_game){ + CCGame *game = (void*)_game; + if(game->hit) game->hit --; + return 0; +} + +void init_logic(){ + add_logic_hook((RcLogicFunc*)&cc_ai); + add_logic_hook((RcLogicFunc*)&cc_logic); +} diff --git a/src/game.h b/src/game.h index a368d5f..acde659 100644 --- a/src/game.h +++ b/src/game.h @@ -11,8 +11,12 @@ enum { typedef struct { RcGame _bgame; int curr_map; + int16_t hit; + int16_t hp; } CCGame; //CpC game Game struct Sprite *cc_mksprite(V2d pos, int type); void cc_rmsprite(int pos); + +void init_logic(); diff --git a/src/gfx.c b/src/gfx.c index 13794fe..fd64337 100644 --- a/src/gfx.c +++ b/src/gfx.c @@ -42,3 +42,8 @@ int anim_tex(GUNUSED RcGame *game){ void init_gfx(){ add_logic_hook((RcLogicFunc*)&anim_tex); } + +void draw_gui(CCGame *game){ + dprint_opt(127,20,3,C_NONE,DTEXT_RIGHT,DTEXT_TOP,"%d",game->hp); + dtext_opt(127,30,3,C_NONE,DTEXT_RIGHT,DTEXT_TOP,"HP",-1); +} diff --git a/src/gfx.h b/src/gfx.h index 6682fa7..5ba9844 100644 --- a/src/gfx.h +++ b/src/gfx.h @@ -11,4 +11,8 @@ #include "C3D/map.h" #include "C3D/config.h" +#include "game.h" + void init_gfx(); + +void draw_gui(CCGame *game); diff --git a/src/main.c b/src/main.c index 6676d90..a9fdeb3 100644 --- a/src/main.c +++ b/src/main.c @@ -134,9 +134,9 @@ int main(){ add_logic_hook((RcLogicFunc*)&test_logic); init_gfx(); - init_map(&game); - + init_logic(); + while (!rcgame->flags.exit) { prof_t frame = prof_make(); prof_enter(frame); @@ -166,7 +166,9 @@ int main(){ timers.raycast_time = prof_make(); timers.draw_time = prof_make(); #endif - + if(game.hit > 20){ + drect(0,0,127,63,C_INVERT); + } dupdate(); prof_leave(frame); frame_time = prof_time(frame);