向有射程的敌人发射子弹
Shooting bullets to enemy that it has range
每增加一点时间,每一座塔都会产生一颗子弹:
for (int i = 0; i < towerVec.size(); i++)
{
Tower_shot tshot(&bulletTexture, sf::Vector2u(1, 3), towerVec[i].row);
tshot.tower_shot.setPosition(towerVec[i].getPosX(), towerVec[i].getPosY());
tshot.startingX = towerVec[i].getPosX();
tshot.startingY = towerVec[i].getPosY();
tshotVec.push_back(tshot);
}
然后每一颗子弹都被射出:
for (int j = 0; j < enemyVec.size(); j++)
{
for (int i = 0; i < tshotVec.size(); i++)
{
if (tshotVec[i].ifFire(enemyVec[j].body.getPosition()))
tshotVec[i].fire(enemyVec[j].body.getPosition(), window);
}
}
射击功能正在检查是否有射程到达敌人,如果没有,则尝试另一个。
bool Tower_shot::ifFire(sf::Vector2f EnemyPos)
{
this->aimDir = sf::Vector2f(EnemyPos.x + 37 - this->tower_shot.getPosition().x, EnemyPos.y + 37 - this->tower_shot.getPosition().y);
if (sqrt(pow(this->aimDir.x, 2) + pow(this->aimDir.y, 2)) < 250) return true;
return false;
}
void Tower_shot::fire(sf::Vector2f EnemyPos, sf::RenderWindow &window)
{
this->aimDir = sf::Vector2f(EnemyPos.x + 37 - this->tower_shot.getPosition().x, EnemyPos.y + 37 - this->tower_shot.getPosition().y);
this->aimDirNorm = sf::Vector2f(this->aimDir.x / sqrt(pow(this->aimDir.x, 2) + pow(this->aimDir.y, 2)), this->aimDir.y / sqrt(pow(this->aimDir.x, 2) + pow(this->aimDir.y, 2)));
this->currVelocity = this->aimDirNorm * this->maxSpeed;
animation.Update(row);
tower_shot.setTextureRect(animation.uvRect);
window.draw(tower_shot);
this->tower_shot.move(this->currVelocity);
}
问题是:子弹的行为很奇怪,就像子弹无法决定哪个敌人正在移动并试图移动到射程中的每个敌人。有什么线索如何处理吗?我被卡住了
我是这样做的:
https://github.com/dragneel30/Unnamed-TD-game/blob/master/Tower.h#L104
塔是应该决定发射哪个敌人的人,之后,它会创建一个子弹并将检测到的敌人传递给子弹,然后子弹将存储指向该敌人的指针,因此,您的子弹不会再表现得很奇怪了。 因为它已经知道要跟随哪个敌人(存储的敌人(
相关文章:
- 如果目标对象死亡,Qt::BlockingQueuedConnection发射会发生什么
- 我正在使用快速 JSON 为C++回合制 RPG 解析对象敌人,并且遇到了一些奇怪的错误
- Yaml-cpp:在发射器中的每个新映射之前禁用自动开始文档 (---)
- 使用433Mhz发射器实时发送PWM值
- 向有射程的敌人发射子弹
- 编译器发射std :: move()运行时调用
- QT-悬停Qlabel时发射信号
- QThread信号发射不连接
- QTimer超时信号在每个超时事件中多次发射
- 当我尝试发射对象代码时,为什么llvm segfault在segfault
- QT:使用Qeventloop等待信号,如果信号发射得太早该怎么办
- FireMonkey自定义控制点击事件在设计师中发射
- 在GLUT中每秒产生2个敌人
- 无法从Qt C++捕获发射到QML的信号
- Qt时隙方法(这是另一类)在发射信号时不触发
- C++ 按生命对敌人进行分类
- 团结敌人 AI 脚本 C#
- QT条件信号发射效率
- QT下降事件不发射
- QT 5:从非QT线程发射信号