From dcbb8cc47450735fc4c255d1e230bc4f3d1f26c6 Mon Sep 17 00:00:00 2001 From: Sam-Si <13261099+Sam-Si@users.noreply.github.com> Date: Thu, 19 Mar 2026 22:10:35 +0530 Subject: [PATCH] fixes to the game --- src/game.cpp | 28 +++++++++++++++++++++++----- src/game.h | 2 +- src/helper.cpp | 13 ++++--------- src/helper.h | 4 ++-- src/render.cpp | 2 +- src/storage.cpp | 5 +++++ src/types.cpp | 2 +- src/weapon.cpp | 6 +++--- 8 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 49233f1..7241eb8 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -919,6 +919,10 @@ void initPlayer(int playerType) { appendSpriteToSnake(snake, SPRITE_KNIGHT, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + playersCount * 2 * UNIT, Direction::Right); + // Also register in EntityManager so attack/bullet/render systems can find it + GameContext& ctx = getGameContext(); + ctx.entityManager.addSnake(snake); + ctx.entityManager.setPlayerCount(playersCount + 1); playersCount++; } void generateHeroItem(int x, int y) { @@ -1025,7 +1029,7 @@ bool takeWeapon(Snake* snake, Item* weaponItem) { } sprite->setHp(sprite->hp() + static_cast(GAME_HP_MEDICINE_EXTRA_DELTA / 100.0 * - sprite->totalHp() * 5)); + sprite->totalHp())); auto effect = createAndPushAnimation( animationsList[RENDER_LIST_EFFECT_ID], &textures[RES_HP_MED], nullptr, LoopType::Once, SPRITE_ANIMATION_DURATION, 0, 0, SDL_FLIP_NONE, 0, @@ -1052,8 +1056,8 @@ void dropItemNearSprite(const Sprite* sprite, ItemType itemType) { } else { generateItem(x, y, itemType); } + return; } - return; } } } @@ -1187,11 +1191,13 @@ void initEnemies(int enemiesCount) { return; } + GameContext& ctx = getGameContext(); for (int i = 0; i < enemiesCount;) { const Point pos = getAvaliablePos(); auto snake = g_entitySpawner->spawnMonster(pos.x, pos.y); if (snake) { spriteSnake[spritesCount++] = snake; + ctx.entityManager.addSnake(snake); i += static_cast(snake->sprites().size()); } } @@ -1203,6 +1209,7 @@ void initEnemies(int enemiesCount) { auto boss = g_entitySpawner->spawnBoss(pos.x, pos.y); if (boss) { spriteSnake[spritesCount++] = boss; + ctx.entityManager.addSnake(boss); } } } @@ -1389,12 +1396,12 @@ void dealDamage(const std::shared_ptr& src, if (dest->buffs()[BUFF_FROZEN]) { calcDamage *= GAME_FROZEN_DAMAGE_K; } - if (src && src != spriteSnake[GAME_MONSTERS_TEAM]) { + if (src && src->team() != GAME_MONSTERS_TEAM) { if (src->buffs()[BUFF_ATTACK]) { calcDamage *= GAME_BUFF_ATTACK_K; } } - if (dest != spriteSnake[GAME_MONSTERS_TEAM]) { + if (dest->team() != GAME_MONSTERS_TEAM) { if (dest->buffs()[BUFF_DEFFENCE]) { calcDamage /= GAME_BUFF_DEFENSE_K; } @@ -1406,7 +1413,7 @@ void dealDamage(const std::shared_ptr& src, src->score()->addKilled(1); } } - dest->score()->addStand(damage); + dest->score()->addStand(static_cast(calcDamage)); } bool makeSnakeCross(const std::shared_ptr& snake) { @@ -1983,6 +1990,17 @@ int gameLoop() { spriteSnake[spritesCount--] = nullptr; } } + // Keep EntityManager in sync with legacy spriteSnake array + { + GameContext& ctx = getGameContext(); + ctx.entityManager.clear(); + ctx.entityManager.setPlayerCount(playersCount); + for (int i = 0; i < spritesCount; i++) { + if (spriteSnake[i]) { + ctx.entityManager.addSnake(spriteSnake[i]); + } + } + } if (willTerm) { termCount--; if (!termCount) { diff --git a/src/game.h b/src/game.h index 67c22f7..3149e5b 100644 --- a/src/game.h +++ b/src/game.h @@ -38,7 +38,7 @@ #define GAME_MAP_RELOAD_PERIOD 120 #define GAME_BUFF_ATTACK_K 2.5 #define GAME_BUFF_DEFENSE_K 2 -#define GAME_FROZEN_DAMAGE_K 0.1 +#define GAME_FROZEN_DAMAGE_K 1.5 // Drop Rate // Win diff --git a/src/helper.cpp b/src/helper.cpp index 33e9911..6a53f9b 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -32,23 +32,18 @@ bool RectRectCross(const SDL_Rect* a, const SDL_Rect* b) { } bool RectCirCross(const SDL_Rect* a, int x, int y, int r) { if (inr(x, a->x, a->x + a->w) && inr(y, a->y, a->y + a->h)) { - puts("failed1"); return true; } - if (abs(x - a->x) <= r) { - puts("failed2"); + if (abs(x - a->x) <= r && inr(y, a->y, a->y + a->h)) { return true; } - if (abs(x - a->x - a->w) <= r) { - puts("failed3"); + if (abs(x - a->x - a->w) <= r && inr(y, a->y, a->y + a->h)) { return true; } - if (abs(y - a->y) <= r) { - puts("failed4"); + if (abs(y - a->y) <= r && inr(x, a->x, a->x + a->w)) { return true; } - if (abs(y - a->y - a->h) <= r) { - puts("failed5"); + if (abs(y - a->y - a->h) <= r && inr(x, a->x, a->x + a->w)) { return true; } return false; diff --git a/src/helper.h b/src/helper.h index 011c3a3..d0cd718 100644 --- a/src/helper.h +++ b/src/helper.h @@ -8,8 +8,8 @@ #include "sprite.h" #include "types.h" -#define MAX(x, y) (x > y ? x : y) -#define MIN(x, y) (x > y ? y : x) +#define MAX(x, y) ((x) > (y) ? (x) : (y)) +#define MIN(x, y) ((x) > (y) ? (y) : (x)) #define PI 3.1415926535 #define HELPER_RECT_CROSS_LIMIT 8 int randInt(int l, int r); diff --git a/src/render.cpp b/src/render.cpp index 711b0e9..9393158 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -362,7 +362,7 @@ void renderAnimationLinkListWithSort(const AnimationList& list) { buffer.push_back(animation); } std::sort(buffer.begin(), buffer.end(), [](const auto& a, const auto& b) { - return a && b ? b->y() < a->y() : static_cast(a); + return a && b ? a->y() < b->y() : static_cast(a); }); for (const auto& animation : buffer) { renderAnimation(animation); diff --git a/src/storage.cpp b/src/storage.cpp index a160fe3..fe152ae 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -65,6 +65,11 @@ Score** insertScoreToRanklist(Score* score, int* n, Score** scores) { return scores; } } + // Score is lower than all existing entries but list isn't full - append it + if (*n < STORAGE_RANKLIST_NUM) { + scores = static_cast(realloc(scores, sizeof(Score*) * (++*n))); + scores[*n - 1] = cloneScore(score); + } return scores; } diff --git a/src/types.cpp b/src/types.cpp index b747855..9becc7e 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -252,7 +252,7 @@ void Score::calcScore(int gameLevel) { rank_ = 0.0; return; } - rank_ = static_cast(damage_) / got_ + + rank_ = static_cast(damage_) / got_ - static_cast(stand_) / got_ + got_ * 50 + killed_ * 100; rank_ *= gameLevel + 1; } diff --git a/src/weapon.cpp b/src/weapon.cpp index 22ef45e..88af258 100644 --- a/src/weapon.cpp +++ b/src/weapon.cpp @@ -12,6 +12,7 @@ #include "types.h" extern Texture textures[TEXTURES_SIZE]; +extern BulletList bullets; namespace { std::shared_ptr createWeaponAnimation(int textureId, LoopType loop, @@ -66,7 +67,7 @@ void applyWeaponDamage(const std::shared_ptr& src, src->score()->addKilled(1); } } - dest->score()->addStand(weapon.damage()); + dest->score()->addStand(static_cast(calcDamage)); ctx.buffManager.invokeWeaponBuff(src, weapon, dest, weapon.damage()); } @@ -149,8 +150,7 @@ class RangedBehavior final : public WeaponBehavior { context.attackerSprite->y(), rad, context.attacker->team(), weapon.flyAnimation()); - GameContext& ctx = getGameContext(); - ctx.entityManager.addBullet(bullet); + ::bullets.push_back(bullet); pushAnimationToRender(RENDER_LIST_EFFECT_ID, bullet->animation()); return {.attacked = true}; }