Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1025,7 +1029,7 @@ bool takeWeapon(Snake* snake, Item* weaponItem) {
}
sprite->setHp(sprite->hp() +
static_cast<int>(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,
Expand All @@ -1052,8 +1056,8 @@ void dropItemNearSprite(const Sprite* sprite, ItemType itemType) {
} else {
generateItem(x, y, itemType);
}
return;
}
return;
}
}
}
Expand Down Expand Up @@ -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<int>(snake->sprites().size());
}
}
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -1389,12 +1396,12 @@ void dealDamage(const std::shared_ptr<Snake>& 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;
}
Expand All @@ -1406,7 +1413,7 @@ void dealDamage(const std::shared_ptr<Snake>& src,
src->score()->addKilled(1);
}
}
dest->score()->addStand(damage);
dest->score()->addStand(static_cast<int>(calcDamage));
}

bool makeSnakeCross(const std::shared_ptr<Snake>& snake) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 4 additions & 9 deletions src/helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>(a);
return a && b ? a->y() < b->y() : static_cast<bool>(a);
});
for (const auto& animation : buffer) {
renderAnimation(animation);
Expand Down
5 changes: 5 additions & 0 deletions src/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Score**>(realloc(scores, sizeof(Score*) * (++*n)));
scores[*n - 1] = cloneScore(score);
}
return scores;
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void Score::calcScore(int gameLevel) {
rank_ = 0.0;
return;
}
rank_ = static_cast<double>(damage_) / got_ +
rank_ = static_cast<double>(damage_) / got_ -
static_cast<double>(stand_) / got_ + got_ * 50 + killed_ * 100;
rank_ *= gameLevel + 1;
}
Expand Down
6 changes: 3 additions & 3 deletions src/weapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "types.h"

extern Texture textures[TEXTURES_SIZE];
extern BulletList bullets;

namespace {
std::shared_ptr<Animation> createWeaponAnimation(int textureId, LoopType loop,
Expand Down Expand Up @@ -66,7 +67,7 @@ void applyWeaponDamage(const std::shared_ptr<Snake>& src,
src->score()->addKilled(1);
}
}
dest->score()->addStand(weapon.damage());
dest->score()->addStand(static_cast<int>(calcDamage));
ctx.buffManager.invokeWeaponBuff(src, weapon, dest, weapon.damage());
}

Expand Down Expand Up @@ -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};
}
Expand Down
Loading