SFML и C++ Уроки \ Разработка игр › Форумы › SFML System, Window и другие вопросы › Стрельба из автомата › Ответ в теме: Стрельба из автомата
You’re God damn right. ))
Вот упрощенная версия кода:
main.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
while (window.isOpen()){ timer_Bullet += time; while (window.pollEvent(event)){ ..... } for (it = Bullet.begin(); it != Bullet.end(); it++){ if (!((*it)->isMove)){ (*it)->mouseCoord.x = pos.x; (*it)->mouseCoord.y = pos.y; (*it)->rotBull = atan2f(pos.x - hero.x, hero.y - pos.y) * 180.0 / M_PI; } (*it)->isMove = true; } for (it = Bullet.begin(); it != Bullet.end(); it++){ if ((*it)->isMove){ distanceBull = sqrt(pow(((*it)->mouseCoord.x - (*it)->x), 2) + pow(((*it)->mouseCoord.y - (*it)->y), 2)); if (distanceBull > 100){ (*it)->x += (*it)->speed*time*((*it)->mouseCoord.x - (*it)->x) / distanceBull; (*it)->y += (*it)->speed*time*((*it)->mouseCoord.y - (*it)->y) / distanceBull; } else{ (*it)->isMove = false; (*it)->life = false; } } } for (it = Bullet.begin(); it != Bullet.end();){ (*it)->update(time); if ((*it)->life == false){ it = Bullet.erase(it); } else it++; } if (event.type == Event::MouseButtonPressed) if (event.key.code == Mouse::Left){ if (timer_Bullet > 150.f){ Bullet.push_back(new TBullet(weaponsSprite, "MachineBullet", hero.x, hero.y, hero.anim.anim.sprite.getTextureRect().width, hero.anim.anim.sprite.getTextureRect().height, hero.STATE)); --(static_cast<TWeapons*>(Inventory.inventory[2])->ammo); timer_Bullet = 0.f; } } } else{ hero.shoot = false; if (hero.onHandM) static_cast<TWeapons*>(Inventory.inventory[2])->reloadWeap(hero); } for (it = Bullet.begin(); it != Bullet.end(); it++) if ((*it)->life) window.draw((*it)->sprite); } |