[Updated] Enemies to make the game a bit more beginner-friendly

This commit is contained in:
Ninjaws
2024-12-20 17:32:59 +01:00
parent 98f2fe1683
commit 367f1aa137
4 changed files with 12 additions and 8 deletions
+5
View File
@@ -42,6 +42,7 @@
font-size: larger; font-size: larger;
font-family: 'Press Start 2P', cursive; font-family: 'Press Start 2P', cursive;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.6); text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.6);
user-select: none;
} }
.links { .links {
@@ -107,6 +108,10 @@
return canvas; return canvas;
})() })()
}; };
window.onload = function() {
document.getElementById('canvas').focus();
};
</script> </script>
<script src=game.js async></script> <script src=game.js async></script>
</body> </body>
+1 -1
View File
@@ -65,7 +65,7 @@ private:
uint8_t health; uint8_t health;
uint8_t maxHealth = 5; uint8_t maxHealth = 5;
Vector2 velocity = {0, 0}; Vector2 velocity = {0, 0};
float spottingRange = 300; // From how far away enemies can spot the player float spottingRange = 280.0f; // From how far away enemies can spot the player
float gravity = 0.6f; float gravity = 0.6f;
float moveSpeed = 1.5f; float moveSpeed = 1.5f;
float animationDelay = 0.2f; // In seconds float animationDelay = 0.2f; // In seconds
+5 -6
View File
@@ -152,8 +152,8 @@ void Enemy::detectPlayer()
bool spotted = false; bool spotted = false;
Vector2 playerCenter = {player.getTexture().getPosition().x + player.getTexture().getSize().x / 2.0f, player.getTexture().getPosition().y + player.getTexture().getSize().y / 2.0f}; Vector2 playerCenter = {player.getTexture().getPosition().x + player.getTexture().getSize().x / 2.0f, player.getTexture().getPosition().y + player.getTexture().getSize().y / 2.0f};
Vector2 enemyCenter = {texture.getPosition().x + texture.getSize().x / 2.0f, texture.getPosition().y + texture.getSize().y / 2.0f}; Vector2 enemyCenter = {texture.getPosition().x + texture.getSize().x / 2.0f, texture.getPosition().y + texture.getSize().y / 2.0f};
if (this->spriteAnimation.y == Left && this->texture.getPosition().x - player.getTexture().getPosition().x <= spottingRange && if (this->spriteAnimation.y == Left && this->texture.getPosition().x - player.getTexture().getPosition().x <= this->spottingRange &&
this->texture.getPosition().x - player.getTexture().getPosition().x > 55 && this->texture.getPosition().y + this->texture.getSize().y >= player.getTexture().getPosition().y) this->texture.getPosition().x - player.getTexture().getPosition().x > 55 && this->texture.getPosition().y + this->texture.getSize().y + 5 >= player.getTexture().getPosition().y)
{ {
spotted = true; spotted = true;
float gunRotationLeft = atan2(playerCenter.y - enemyCenter.y, float gunRotationLeft = atan2(playerCenter.y - enemyCenter.y,
@@ -161,8 +161,8 @@ void Enemy::detectPlayer()
float target = -gunRotationLeft * 180 / M_PI; float target = -gunRotationLeft * 180 / M_PI;
this->gunLeft.setRotation(std::min(this->gunLeft.getRotation() + aimSpeed, target)); this->gunLeft.setRotation(std::min(this->gunLeft.getRotation() + aimSpeed, target));
} }
else if (spriteAnimation.y == Right && player.getTexture().getPosition().x - this->texture.getPosition().x <= spottingRange && else if (spriteAnimation.y == Right && player.getTexture().getPosition().x - this->texture.getPosition().x <= this->spottingRange &&
player.getTexture().getPosition().x - this->texture.getPosition().x > 55 && this->texture.getPosition().y + this->texture.getSize().y >= player.getTexture().getPosition().y) player.getTexture().getPosition().x - this->texture.getPosition().x > 55 && this->texture.getPosition().y + this->texture.getSize().y + 5 >= player.getTexture().getPosition().y)
{ {
spotted = true; spotted = true;
float gunRotationRight = atan2(playerCenter.y - enemyCenter.y, float gunRotationRight = atan2(playerCenter.y - enemyCenter.y,
@@ -188,8 +188,7 @@ void Enemy::detectPlayer()
} }
} }
void Enemy::shoot() void Enemy::shoot() {
{
Player &player = PlayerService::getInstance().getPlayer(); Player &player = PlayerService::getInstance().getPlayer();
/** Stop shooting if the player is dead (and falling out of the map) */ /** Stop shooting if the player is dead (and falling out of the map) */
if (player.getHealth() > 0 && playerSpotted && GetTime() - this->lastShot >= (1 / this->shotsPerSecond)) if (player.getHealth() > 0 && playerSpotted && GetTime() - this->lastShot >= (1 / this->shotsPerSecond))
+1 -1
View File
@@ -14,7 +14,7 @@ void EnemyService::initEnemies()
this->enemies[1].getObject().setPosition({51.0f * tileSize, 10.0f * tileSize}); this->enemies[1].getObject().setPosition({51.0f * tileSize, 10.0f * tileSize});
this->enemies[2].getObject().setPosition({56.0f * tileSize, 5.0f * tileSize}); this->enemies[2].getObject().setPosition({56.0f * tileSize, 5.0f * tileSize});
this->enemies[3].getObject().setPosition({78.0f * tileSize, 10.0f * tileSize}); this->enemies[3].getObject().setPosition({78.0f * tileSize, 10.0f * tileSize});
this->enemies[4].getObject().setPosition({82.0f * tileSize, 15.0f * tileSize}); this->enemies[4].getObject().setPosition({81.0f * tileSize, 15.0f * tileSize});
this->enemies[5].getObject().setPosition({88.0f * tileSize, 4.0f * tileSize}); this->enemies[5].getObject().setPosition({88.0f * tileSize, 4.0f * tileSize});
this->enemies[6].getObject().setPosition({98.0f * tileSize, 11.0f * tileSize}); this->enemies[6].getObject().setPosition({98.0f * tileSize, 11.0f * tileSize});
this->enemies[7].getObject().setPosition({103.0f * tileSize, 15.0f * tileSize}); this->enemies[7].getObject().setPosition({103.0f * tileSize, 15.0f * tileSize});