SFML и C++ Уроки \ Разработка игр › Форумы › SFML готовые решения (сниппеты) и советы › Оптимизация списка
В этой теме 1 ответ, 2 участника, последнее обновление LastikPro 7 года/лет, 10 мес. назад.
Просмотр 2 сообщений - с 1 по 2 (из 2 всего)
-
АвторСообщения
-
Ребята, у меня одна сплошная проверка элементов списка
Могу ли я как то оптимизировать это?
Что если я разобью на проверки столкновения
1. Пуля и враг
2. Игрок и бонусы различные
3. Игрок и враг
4. (Пуля врага && стрела) с игроком.Код станет оптимизированее или еще хуже? Помогите пожалуйста
C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370for (it = entities.begin(); it != entities.end(); it++){//1. монеты, патроны и аптечкаif ((*it)->Name == "Shield"){Entity *shield = *it;if (shield->health > 0)if (p.getRect().intersects(shield->getRect())){p.isShield = true;p.shieldHealth = 100;shield->life = false;soundTakeItem.play();}}if ((*it)->Name == "CoinY"){Entity *coinY = *it;if (coinY->health > 0)if (p.getRect().intersects(coinY->getRect())){p.playerScore += 2;coinY->life = false;soundCoin.play();}}if ((*it)->Name == "CoinG"){Entity *coinG = *it;if (coinG->health > 0)if (p.getRect().intersects(coinG->getRect())){p.playerScore++;coinG->life = false;soundCoin.play();}}if ((*it)->Name == "Bullets"){Entity *bullets = *it;if (bullets->health > 0)if (p.getRect().intersects(bullets->getRect())){p.bulletScore += 10;bullets->life = false;soundTakeItem.play();}}if ((*it)->Name == "HealthPoint"){Entity *hp = *it;if (hp->health > 0)if (p.getRect().intersects(hp->getRect())){p.medicScore++;hp->life = false;soundTakeItem.play();}}//2. врагиif ((*it)->Name == "EnemyEagleGray"){Entity *enemy = *it;if ((*it)->health <= 0) continue;if (p.health > 0){if (p.getRect().intersects(enemy->getRect())){if ((p.dy > 0) && (p.onGround == false)){enemy->dx = 0; p.dy = -0.3; enemy->health = 0;p.enemyScore--;}if (enemy->dx > 0) // Если враг идет вправо{enemy->dir = 1;enemy->dx = 0;enemy->x = p.x + p.w;p.hit = true;soundPlayerHit.play();p.health -= 10;}if (enemy->dx < 0) // Если игрок идет влево{enemy->dir = 0;enemy->dx = 0;enemy->x = p.x - enemy->w;p.hit = true;soundPlayerHit.play();p.health -= 10;}if (p.dx < 0 && p.onGround) // Если игрок идет влево{if (!p.hit){p.health -= 10;}if (enemy->dx == 0){p.x = enemy->x + enemy->w;p.hit = true;soundPlayerHit.play();}}if (p.dx > 0 && p.onGround) // Если игрок идет вправо{if (!p.hit){p.health -= 10;}if (enemy->dx == 0){p.x = enemy->x - p.w;p.hit = true;soundPlayerHit.play();}}}if (p.getRect().intersects(enemy->getEnemyview())){if (enemy->dx > 0){enemy->dx = 0;enemy->shoot = true;enemy->detection = true;}if (enemy->dx < 0){enemy->dx = 0;enemy->shoot = true;enemy->detection = true;}}if (!(p.getRect().intersects(enemy->getEnemyview()))){if (enemy->dx == 0 && enemy->dir == 1){enemy->dx = -0.05;enemy->shoot = false;}if (enemy->dx == 0 && enemy->dir == 0){enemy->dx = 0.05;enemy->shoot = false;}}if (enemy->isShoot == true){entities.push_back(new Bullet("EBullet", anim_Enemy_Bullets, lvl, enemy->x, enemy->y + 6, enemy->dir));soundEnemyShoot.play();enemy->isShoot = false;}//3. пулиfor (list<Entity*>::iterator it2 = entities.begin(); it2 != entities.end(); it2++){Entity *bullet = *it2;if (bullet->Name == "Bullet"){if (bullet->health > 0){if (enemy->getRect().intersects(bullet->getRect())){if (bullet->dx > 0){soundEnemyHit.play();if (enemy->dx > 0) { enemy->dir = 1; enemy->dx = -0.05; }bullet->x = enemy->x - bullet->w;bullet->dx = 0;bullet->health = 0;enemy->health -= 20;if (enemy->health <= 0){p.enemyScore--;}}if (bullet->dx < 0){soundEnemyHit.play();if (enemy->dx < 0) { enemy->dir = 0; enemy->dx = 0.05; }bullet->x = enemy->x + enemy->w;bullet->dx = 0;bullet->health = 0;enemy->health -= 20;if (enemy->health <= 0){p.enemyScore--;}}}}}Entity *enemybullet = *it2;if (enemybullet->Name == "EBullet"){if (enemybullet->health > 0){if (enemybullet->getRect().intersects(p.getRect()) && !p.block){if (enemybullet->dx > 0){enemybullet->x = p.x - enemybullet->w + 10;enemybullet->dx = 0;enemybullet->health = 0;p.health -= 20;p.hit = true;soundPlayerHit.play();if (p.health <= 0 || !p.life){if (enemy->dir == 0) { enemy->dx = 0.05; }if (enemy->dir == 1) { enemy->dx = -0.05; }enemy->isShoot = false;enemy->shoot = false;enemy->detection = false;p.dx = 0;soundPlayerDead.play();}}if (enemybullet->dx < 0){enemybullet->x = p.x + p.w;enemybullet->dx = 0;enemybullet->health = 0;p.health -= 20;p.hit = true;soundPlayerHit.play();if (p.health <= 0 || !p.life){if (enemy->dir == 0) { enemy->dx = 0.05; }if (enemy->dir == 1) { enemy->dx = -0.05; }enemy->isShoot = false;enemy->shoot = false;enemy->detection = false;p.dx = 0;soundPlayerDead.play();}}}}}}}}if ((*it)->Name == "Button"){if (p.getRect().intersects((*it)->getRect())){(*it)->onButton = true;(*it)->ArrowShoot = false;}if (!(p.getRect().intersects((*it)->getRect()))){(*it)->onButton = false;}}if ((*it)->ArrowShoot){for (list<Entity*>::iterator it3 = entities.begin(); it3 != entities.end(); it3++){Entity *base = *it3;if (base->Name == "ArrowBase"){entities.push_back(new Bullet("Arrow", anim_ArrowsButtons, lvl, base->x, base->y, base->dir));soundArrowShoot.play();/*base->ArrowShoot = false;*/}}}if ((*it)->Name == "Arrow"){Entity *arrow = *it;if (p.life == true || p.health > 0){if (arrow->getRect().intersects(p.getRect()) && p.block){if (arrow->dx > 0 && p.block){arrow->x = p.x - arrow->w;p.shieldHealth -= 50;arrow->dx = 0;arrow->health = 0;}}if (arrow->getRect().intersects(p.getRect()) && !p.block){if (arrow->dx > 0 && !p.block){soundPlayerArrowHit.play();arrow->x = p.x - arrow->w;arrow->dx = 0;arrow->health = 0;p.health -= 30;if (p.health <= 0 || !p.life){p.dx = 0;soundPlayerDead.play();}}}}}// 4. Ящик с пулейif ((*it)->Name == "Box"){Entity *box = *it;if (box->health > 0){if (p.getRect().intersects(box->getRect())){if (p.dx > 0 && p.onGround){p.x = box->x - p.w;}if (p.dx < 0 && p.onGround){p.x = box->x + box->w;}if (p.dy > 0 && !(p.onGround)){p.y = box->y - 44;p.dy = 0;if (p.dx == 0){p.state = p.stay;}if (p.dx > 0 || p.dx < 0){p.state = p.walk;}}}}for (list<Entity*>::iterator it2 = entities.begin(); it2 != entities.end(); it2++){Entity *bullet = *it2;if (bullet->Name == "Bullet"){if (bullet->health > 0 && box->health > 0){if (bullet->getRect().intersects(box->getRect())){if (bullet->dx > 0){bullet->x = box->x - bullet->w;bullet->dx = 0;box->health = 0;bullet->health = 0;if (box->health <= 0){soundBoxExplode.play();}}if (bullet->dx < 0){bullet->x = box->x + box->w;bullet->dx = 0;box->health = 0;bullet->health = 0;if (box->health <= 0){soundBoxExplode.play();}}}}}}}}ей или наоборот еще хуже? Помогите пожалуйста
Чем меньше цыклов прохода по всем елементам(for (it = entities.begin(); it != entities.end(); it++)) тем лучше.
-
АвторСообщения
Просмотр 2 сообщений - с 1 по 2 (из 2 всего)
Для ответа в этой теме необходимо авторизоваться.