add basic shoot on enemy

This commit is contained in:
h4570
2022-08-14 20:48:10 +02:00
parent 37257b253a
commit 26596fa2c3
6 changed files with 39 additions and 13 deletions
+22 -1
View File
@@ -39,6 +39,7 @@ Enemy::Enemy(Engine* engine, const EnemyInfo& t_info) {
walkSequence = {0, 1, 2};
fightSequence = {3, 4, 5};
spawnPoint = info.spawnPoint;
mesh->setPosition(info.spawnPoint);
mesh->animation.setSequence(walkSequence);
@@ -61,9 +62,12 @@ Enemy::~Enemy() {
delete pair;
}
void Enemy::update(const Heightmap& heightmap, const Vec4& playerPosition) {
void Enemy::update(const Heightmap& heightmap, const Vec4& playerPosition,
const PlayerShootAction& shootAction) {
auto* enemyPosition = mesh->getPosition();
handlePlayerShoot(shootAction);
auto diff = *enemyPosition - playerPosition;
auto ang = Math::atan2(diff.x, diff.z);
@@ -81,6 +85,23 @@ void Enemy::update(const Heightmap& heightmap, const Vec4& playerPosition) {
mesh->update();
}
void Enemy::handlePlayerShoot(const PlayerShootAction& shootAction) {
if (!shootAction.isShooting) {
return;
}
const auto& ray = shootAction.ray.value();
auto bbox =
mesh->getCurrentBoundingBox().getTransformed(mesh->getModelMatrix());
auto isOnEnemy = ray.intersectBox(bbox.min(), bbox.max());
if (isOnEnemy) {
mesh->setPosition(spawnPoint);
TYRA_LOG("Enemy hit!");
}
}
void Enemy::walk(const Heightmap& heightmap, const Vec4& positionDiff) {
if (isFighting) {
mesh->animation.setSequence(walkSequence);
+4 -3
View File
@@ -43,7 +43,7 @@ EnemyManager::EnemyManager(Engine* engine, const Heightmap& heightmap) {
auto* sample = engine->audio.adpcm.load(
FileUtils::fromCwd("game/models/zombie/punch.adpcm"));
const int enemyCount = 10;
const int enemyCount = 2;
for (int i = 0; i < enemyCount; i++) {
EnemyInfo info;
info.adpcmChannel = 9 + i;
@@ -80,9 +80,10 @@ std::vector<RendererDynamicPair*> EnemyManager::getPairs() const {
}
void EnemyManager::update(const Heightmap& heightmap,
const Vec4& playerPosition) {
const Vec4& playerPosition,
const PlayerShootAction& shootAction) {
for (auto* enemy : enemies) {
enemy->update(heightmap, playerPosition);
enemy->update(heightmap, playerPosition, shootAction);
}
}
+1 -5
View File
@@ -67,12 +67,8 @@ void GameState::update() {
skybox->update(player->getPosition());
player->update(terrain->heightmap);
enemyManager->update(terrain->heightmap, player->getPosition());
auto shootAction = player->getShootAction();
if (shootAction.isShooting) {
shootAction.ray.value().print();
}
enemyManager->update(terrain->heightmap, player->getPosition(), shootAction);
auto cameraInfo = player->getCameraInfo();
dbgObj->setPosition(*cameraInfo.looksAt);