[Added] Enemy player detection and shooting, [Moved] Lots of entity code to cpp files to avoid circular dependencies

This commit is contained in:
Ninjaws
2024-12-17 20:08:35 +01:00
parent e8c78770b5
commit 2400b0500a
21 changed files with 820 additions and 701 deletions
+19
View File
@@ -0,0 +1,19 @@
#include "entities/Bullet.h"
#include "services/MapService.h"
void Bullet::envCollision()
{
if (position.x < 0 || position.y < 0 || position.y > GetScreenHeight())
{
hasCollided = true;
return;
}
const int tileSize = MapService::getInstance().getMap().getTileSize();
Vector2i tilePos = Vector2i((int)this->position.x / tileSize, (int)this->position.y / tileSize);
if (MapService::getInstance().getMap().getColMap()[tilePos.y][tilePos.x] == 1)
{
hasCollided = true;
return;
}
}
+18
View File
@@ -0,0 +1,18 @@
#include "entities/EBullet.h"
#include "services/PlayerService.h"
void EBullet::objCollision()
{
Player& player = PlayerService::getInstance().getPlayer();
/** If the player is already falling of the map, dont allow bullets to hit it anymore */
if(player.getHealth() == 0)
return;
if(this->position.x + this->size > player.getTexture().getPosition().x && this->position.x-this->size < player.getTexture().getPosition().x + player.getTexture().getSize().x &&
this->position.y + this->size > player.getTexture().getPosition().y && this->position.y-this->size < player.getTexture().getPosition().y + player.getTexture().getSize().y) {
player.takeDamage(this->damage);
AssetService::getInstance().playSound(ESound::PLAYER_DAMAGE);
this->hasCollided = true;
}
}
+213
View File
@@ -0,0 +1,213 @@
#include "entities/Enemy.h"
#include "services/PlayerService.h"
#include "services/MapService.h"
#include "services/BulletService.h"
void Enemy::logic()
{
if (health == 0)
{
fall();
}
else
{
detectPlayer();
if (playerSpotted)
{
shoot();
}
else
{
walk();
checkForCollision();
animate();
}
}
texture.move(velocity);
}
void Enemy::walk()
{
if (spriteAnimation.y == Left)
{
velocity.x = -moveSpeed;
}
else
{
velocity.x = moveSpeed;
}
}
void Enemy::checkForCollision()
{
std::vector corners = getCorners();
std::vector<std::vector<int>> colMap = MapService::getInstance().getMap().getColMap();
hasCollided = false;
if (colMap[corners[2].y][corners[2].x] == 0 || colMap[corners[3].y][corners[3].x] == 0) // No ground below enemy
{
hasCollided = true;
}
else if (colMap[corners[0].y][corners[0].x] == 1 || colMap[corners[1].y][corners[1].x] == 1)
{
hasCollided = true;
}
if (hasCollided)
{
if (spriteAnimation.y == Left)
spriteAnimation.y = Right;
else
spriteAnimation.y = Left;
velocity.x *= -1;
}
}
std::vector<Vector2i> Enemy::getCorners()
{
float bottom = texture.getPosition().y + texture.getSize().y + velocity.y;
float left = texture.getPosition().x + velocity.x;
float right = texture.getPosition().x + texture.getSize().x + velocity.x;
float top = texture.getPosition().y + velocity.y;
int tileSize = MapService::getInstance().getMap().getTileSize();
// Vector2i leftTop(Vector2i((int)left / tileSize, (int)(top + 5) / tileSize));
// Vector2i leftBottom(Vector2i((int)left / tileSize, (int)(bottom - 5) / tileSize));
Vector2i leftCenter = Vector2i((int)left / tileSize, (int)(bottom - texture.getSize().y / 2.0) / tileSize);
// Vector2i rightTop(Vector2i((int)right / tileSize, (int)(top + 5) / tileSize));
// Vector2i rightBottom(Vector2i((int)right / tileSize, (int)(bottom - 5) / tileSize));
Vector2i rightCenter = Vector2i((int)right / tileSize, (int)(bottom - texture.getSize().y / 2.0) / tileSize);
Vector2i bottomLeft(Vector2i((int)(left + 5) / tileSize, (int)bottom / tileSize)); // +5 to separate bottom from side
Vector2i bottomRight(Vector2i((int)(right - 5) / tileSize, (int)bottom / tileSize)); // -5 to separate bottom from side
std::vector<Vector2i> corners;
corners.push_back(leftCenter);
corners.push_back(rightCenter);
// corners.push_back(leftBottom);
// corners.push_back(rightTop);
// corners.push_back(rightBottom);
corners.push_back(bottomLeft);
corners.push_back(bottomRight);
return corners;
}
void Enemy::animate()
{
if (GetTime() - prevFrame > animationDelay || hasCollided)
{
spriteAnimation.x = std::fmod(spriteAnimation.x + 1, 3);
Rectangle sourceRect = this->texture.getSourceRect();
sourceRect.x = spriteAnimation.x * this->texture.getSize().x;
sourceRect.y = spriteAnimation.y * this->texture.getSize().y;
// sourceRect.x = std::fmod(sourceRect.x, 3 * this->texture.getSize().x);
this->texture.setSourceRect(sourceRect);
prevFrame = GetTime();
}
}
void Enemy::drawHealthBar()
{
float barLength = (this->texture.getSize().x / maxHealth) * health;
DrawRectangle(this->texture.getPosition().x, this->texture.getPosition().y - 12.0f, barLength, 3, RED);
}
void Enemy::drawGun()
{
if (this->spriteAnimation.y == Right)
{
DrawTexturePro(
this->gunRight.getTexture(),
{0, 0, this->gunRight.getSize().x, this->gunRight.getSize().y},
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f,
this->gunRight.getSize().x,
this->gunRight.getSize().y},
{this->gunRight.getSize().x / 3.0f, this->gunRight.getSize().y / 2.0f},
this->gunRight.getRotation(),
WHITE);
}
else if (this->spriteAnimation.y == Left)
{
DrawTexturePro(
this->gunLeft.getTexture(),
{0, 0, this->gunLeft.getSize().x, this->gunLeft.getSize().y},
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f,
this->gunLeft.getSize().x,
this->gunLeft.getSize().y},
{this->gunLeft.getSize().x - this->gunLeft.getSize().x / 3.0f, this->gunLeft.getSize().y / 2.0f},
this->gunLeft.getRotation(),
WHITE);
}
}
void Enemy::fall()
{
velocity.y += gravity;
}
void Enemy::detectPlayer()
{
// playerSpotted = false;
Player &player = PlayerService::getInstance().getPlayer();
if (this->health > 0)
{
bool spotted = false;
if (this->spriteAnimation.y == Left && this->texture.getPosition().x - player.getTexture().getPosition().x <= spottingRange &&
this->texture.getPosition().x - player.getTexture().getPosition().x > 55 && this->texture.getPosition().y + this->texture.getSize().y >= player.getTexture().getPosition().y)
{
spotted = true;
}
else if (spriteAnimation.y == Right && player.getTexture().getPosition().x - this->texture.getPosition().x <= spottingRange &&
player.getTexture().getPosition().x - this->texture.getPosition().x > 55 && this->texture.getPosition().y + this->texture.getSize().y >= player.getTexture().getPosition().y)
{
spotted = true;
}
/** Don't set it again if the detection continues */
if (spotted && !this->playerSpotted)
{
AssetService::getInstance().playSound(ESound::PLAYER_SPOTTED);
this->playerSpotted = true;
velocity.x = 0.0f;
}
else if (!spotted)
{
this->playerSpotted = false;
}
}
}
void Enemy::shoot()
{
Player &player = PlayerService::getInstance().getPlayer();
/** Stop shooting if the player is dead (and falling out of the map) */
if (player.getHealth() > 0 && playerSpotted && GetTime() - this->lastShot >= (1 / this->shotsPerSecond))
{
this->lastShot = GetTime();
// Play shooting sound
AssetService::getInstance().playSound(ESound::SHOOT);
if ((player.getTexture().getPosition().x > this->texture.getPosition().x + this->gunLeft.getSize().x && spriteAnimation.y == Right) ||
(player.getTexture().getPosition().x + (this->gunLeft.getSize().x / 2) < this->texture.getPosition().x && spriteAnimation.y == Left))
{
float angleShot = atan2(player.getTexture().getPosition().y - (this->texture.getPosition().y + this->texture.getSize().y / 2.0),
player.getTexture().getPosition().x - (this->texture.getPosition().x + this->texture.getSize().x / 2.0));
/** Making the bullet originate from the gun's mouth */
float offsetDistance = this->gunLeft.getSize().x - this->gunLeft.getSize().x / 3;
float offsetX = cos(angleShot) * offsetDistance;
float offsetY = sin(angleShot) * offsetDistance;
Vector2 bulletStartLocation = {this->texture.getPosition().x + this->texture.getSize().x / 2.0f + offsetX,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f + offsetY};
BulletService::getInstance().addEnemyBullet(bulletStartLocation, angleShot);
}
}
}
+33
View File
@@ -0,0 +1,33 @@
#include "entities/PBullet.h"
#include "services/CollectableService.h"
#include "services/EnemyService.h"
void PBullet::objCollision()
{
/** Check for collisions with creates */
for (auto &crate : CollectableService::getInstance().getCrates())
{
if(this->position.x + this->size > crate.getObject().getPosition().x && this->position.x-this->size < crate.getObject().getPosition().x + crate.getObject().getSize().x &&
this->position.y + this->size > crate.getObject().getPosition().y && this->position.y-this->size < crate.getObject().getPosition().y + crate.getObject().getSize().y) {
crate.setHit(true);
AssetService::getInstance().playSound(ESound::CRATE_BREAK);
this->hasCollided = true;
}
}
for (auto &enemy : EnemyService::getInstance().getEnemies())
{
/** If the enemy is already falling of the map, dont allow bullets to hit it anymore */
if(enemy.getHealth() == 0)
continue;
if(this->position.x + this->size > enemy.getObject().getPosition().x && this->position.x-this->size < enemy.getObject().getPosition().x + enemy.getObject().getSize().x &&
this->position.y + this->size > enemy.getObject().getPosition().y && this->position.y-this->size < enemy.getObject().getPosition().y + enemy.getObject().getSize().y) {
enemy.takeDamage();
if(enemy.getHealth() == 0) {
AssetService::getInstance().playSound(ESound::ENEMY_DEFEATED);
}
this->hasCollided = true;
}
}
}
+305
View File
@@ -0,0 +1,305 @@
#include "entities/Player.h"
#include "services/BulletService.h"
#include "services/GameService.h"
#include "services/CollectableService.h"
#include "services/InputService.h"
void Player::applyGravity()
{
if (!groundCollision)
{
velocity.y += gravity;
}
else
{
// velocity.y = 0.0f;
}
}
void Player::movement()
{
if (InputService::getInstance().isKeyDown(KEY_A))
{
spriteAnimation.y = Left;
velocity.x = -moveSpeed;
animate();
}
else if (InputService::getInstance().isKeyDown(KEY_D))
{
spriteAnimation.y = Right;
velocity.x = moveSpeed;
animate();
}
else
{
velocity.x = 0.0f;
}
positionUpdates();
handleCollision();
if (!groundCollision)
{
velocity.y += gravity;
}
else
{
velocity.y = 0.0f;
}
if (InputService::getInstance().isKeyPressed(KEY_W) && groundCollision)
{
velocity.y -= jumpPower;
}
move(velocity);
}
void Player::move(Vector2 velocity)
{
this->texture.move(velocity);
}
void Player::animate()
{
if (GetTime() - prevFrame > animationDelay)
{
spriteAnimation.x = std::fmod(spriteAnimation.x + 1, 3);
Rectangle sourceRect = this->texture.getSourceRect();
sourceRect.x = spriteAnimation.x * this->texture.getSize().x;
sourceRect.y = spriteAnimation.y * this->texture.getSize().y;
// sourceRect.x = std::fmod(sourceRect.x, 3 * this->texture.getSize().x);
this->texture.setSourceRect(sourceRect);
prevFrame = GetTime();
}
}
void Player::positionUpdates()
{
const Vector2 pos = this->texture.getPosition();
const Vector2 size = this->texture.getSize();
const int tileSize = MapService::getInstance().getMap().getTileSize();
const float bottom = pos.y + size.y + velocity.y;
const float left = pos.x + velocity.x;
const float right = pos.x + size.x + velocity.x;
const float top = pos.y + velocity.y;
Vector2i topLeft(Vector2i((int)(left + 5) / tileSize, (int)top / tileSize)); // +5 to separate top from side
Vector2i topRight(Vector2i((int)(right - 5) / tileSize, (int)top / tileSize)); // -5 to separate top from side
Vector2i leftTop(Vector2i((int)left / tileSize, (int)(top + 5) / tileSize));
Vector2i leftBottom(Vector2i((int)left / tileSize, (int)(bottom - 5) / tileSize));
Vector2i rightTop(Vector2i((int)right / tileSize, (int)(top + 5) / tileSize));
Vector2i rightBottom(Vector2i((int)right / tileSize, (int)(bottom - 5) / tileSize));
Vector2i bottomLeft(Vector2i((int)(left + 5) / tileSize, (int)bottom / tileSize)); // +5 to separate bottom from side
Vector2i bottomRight(Vector2i((int)(right - 5) / tileSize, (int)bottom / tileSize)); // -5 to separate bottom from side
tiles.clear();
tiles.push_back(topLeft);
tiles.push_back(topRight);
tiles.push_back(leftTop);
tiles.push_back(leftBottom);
tiles.push_back(rightTop);
tiles.push_back(rightBottom);
tiles.push_back(bottomLeft);
tiles.push_back(bottomRight);
}
void Player::handleCollision()
{
timesCollision.clear();
for (int i = 0; i < tiles.size(); i++)
{
if (MapService::getInstance().getMap().getColMap()[tiles[i].y][tiles[i].x] == 1)
{
timesCollision.push_back(i);
}
}
groundCollision = false;
if (timesCollision.size() > 0)
{
for (int i = 0; i < timesCollision.size(); i++)
{
if (timesCollision[i] == 0 || timesCollision[i] == 1) // Topleft and Topright
{
velocity.y = 0;
}
else if (timesCollision[i] == 2 || timesCollision[i] == 3 || // Lefttop and Leftbottom
timesCollision[i] == 4 || timesCollision[i] == 5) // Righttop and Lefttop
{
velocity.x = 0;
}
else if (timesCollision[i] == 6 || timesCollision[i] == 7) // Bottomleft and Bottomright
{
groundCollision = true;
}
}
}
}
void Player::consumableCollision()
{
/** Check for collisions with ammo packs */
for (auto &ammoPack : CollectableService::getInstance().getAmmoPacks())
{
if (this->texture.getPosition().x + this->texture.getSize().x > ammoPack.getObject().getPosition().x &&
this->texture.getPosition().x<ammoPack.getObject().getPosition().x + ammoPack.getObject().getSize().x &&this->texture.getPosition().y + this->texture.getSize().y> ammoPack.getObject().getPosition().y &&
this->texture.getPosition().y < ammoPack.getObject().getPosition().y + ammoPack.getObject().getSize().y)
{
ammoPack.setPickedUp(true);
AssetService::getInstance().playSound(ESound::AMMO_PICKUP);
ammo += ammoPack.getAmmoAmount();
}
}
/** Check for collisions with health packs */
for (auto &healthPack : CollectableService::getInstance().getHealthPacks())
{
if (this->texture.getPosition().x + this->texture.getSize().x > healthPack.getObject().getPosition().x &&
this->texture.getPosition().x<healthPack.getObject().getPosition().x + healthPack.getObject().getSize().x &&this->texture.getPosition().y + this->texture.getSize().y> healthPack.getObject().getPosition().y &&
this->texture.getPosition().y < healthPack.getObject().getPosition().y + healthPack.getObject().getSize().y)
{
if (health < max_health)
{
healthPack.setPickedUp(true);
AssetService::getInstance().playSound(ESound::HEAL_PICKUP);
health = std::max(health + healthPack.getHealAmount(), (int)max_health);
}
}
}
/** Checks for collisions with powerups */
for (auto &powerup : CollectableService::getInstance().getPowerups())
{
if (this->texture.getPosition().x + this->texture.getSize().x > powerup.getObject().getPosition().x &&
this->texture.getPosition().x<powerup.getObject().getPosition().x + powerup.getObject().getSize().x &&this->texture.getPosition().y + this->texture.getSize().y> powerup.getObject().getPosition().y &&
this->texture.getPosition().y < powerup.getObject().getPosition().y + powerup.getObject().getSize().y)
{
// if(health < max_health) {
powerup.setPickedUp(true);
AssetService::getInstance().playSound(ESound::POWERUP);
jumpPower += powerup.getExtraJumpPower();
gainedPowerup = true;
// health = std::max(health+powerup.getHealAmount(),(int)max_health);
// }
}
}
/** Checks for collisions with the finish flag */
auto &flag = CollectableService::getInstance().getFlag();
if (this->texture.getPosition().x + this->texture.getSize().x > flag.getObject().getPosition().x &&
this->texture.getPosition().x<flag.getObject().getPosition().x + flag.getObject().getSize().x &&this->texture.getPosition().y + this->texture.getSize().y> flag.getObject().getPosition().y &&
this->texture.getPosition().y < flag.getObject().getPosition().y + flag.getObject().getSize().y)
{
flag.setTouched(true);
}
}
void Player::shoot()
{
if (InputService::getInstance().isMouseButtonDown(MOUSE_BUTTON_LEFT) && GetTime() - prevShot > firingRate && groundCollision)
{
prevShot = GetTime();
if (this->ammo == 0 || (GameService::getInstance().getMouseWorldPos().x > (this->texture.getPosition().x - this->gunLeft.getSize().x / 2.0f) && spriteAnimation.y == Left) ||
(GameService::getInstance().getMouseWorldPos().x < (this->texture.getPosition().x + this->texture.getSize().x + gunLeft.getSize().x / 2.0f) && spriteAnimation.y == Right))
{
// Play empty gun sound
AssetService::getInstance().playSound(ESound::NO_AMMO);
return;
}
// Play shooting sound
AssetService::getInstance().playSound(ESound::SHOOT);
ammo--;
Vector2 mouseWorldPos = GameService::getInstance().getMouseWorldPos();
if (mouseWorldPos.x > this->texture.getPosition().x + (this->gunLeft.getSize().x) && spriteAnimation.y == Right ||
mouseWorldPos.x + (this->gunLeft.getSize().x / 2) < this->texture.getPosition().x && spriteAnimation.y == Left)
{
float angleShot = atan2(mouseWorldPos.y - (this->texture.getPosition().y + this->texture.getSize().y / 2.0),
mouseWorldPos.x - (this->texture.getPosition().x + this->texture.getSize().x / 2.0));
/** Making the bullet originate from the gun's mouth */
float offsetDistance = this->gunLeft.getSize().x / 2;
float offsetX = cos(angleShot) * offsetDistance;
float offsetY = sin(angleShot) * offsetDistance;
Vector2 bulletStartLocation = {this->texture.getPosition().x + this->texture.getSize().x / 2.0f + offsetX,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f + offsetY};
BulletService::getInstance().addPlayerBullet(bulletStartLocation, angleShot);
}
}
}
void Player::outofboundsCheck()
{
if (this->texture.getPosition().y > StateService::getInstance().getScreenSize().y)
{
this->health = 0;
GameService::getInstance().setGameOver(true);
}
}
void Player::rotateGun()
{
if (GameService::getInstance().getMouseWorldPos().x > this->texture.getPosition().x + (this->gunLeft.getSize().x) && spriteAnimation.y == Right)
{
float gunRotationRight = atan2(GameService::getInstance().getMouseWorldPos().y - (this->texture.getPosition().y + this->texture.getSize().y / 2.0),
GameService::getInstance().getMouseWorldPos().x - (this->texture.getPosition().x + this->texture.getSize().x / 2.0));
this->gunRight.setRotation(gunRotationRight * 180 / M_PI);
}
else if (GameService::getInstance().getMouseWorldPos().x + (this->gunLeft.getSize().x / 2) < this->texture.getPosition().x && spriteAnimation.y == Left)
{
float gunRotationLeft = atan2(GameService::getInstance().getMouseWorldPos().y - (this->texture.getPosition().y + this->texture.getSize().y / 2.0),
(this->texture.getPosition().x + this->texture.getSize().x / 2.0) - GameService::getInstance().getMouseWorldPos().x);
this->gunLeft.setRotation(-gunRotationLeft * 180 / M_PI);
}
}
void Player::drawGun()
{
if (GameService::getInstance().getMouseWorldPos().x > this->texture.getPosition().x + this->gunLeft.getSize().x && spriteAnimation.y == Right)
{
// + this->texture.getSize().x / 2.0f
// + this->texture.getSize().y / 2.0f
DrawTexturePro(
this->gunRight.getTexture(),
{0, 0, this->gunRight.getSize().x, this->gunRight.getSize().y},
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f,
this->gunRight.getSize().x,
this->gunRight.getSize().y},
{this->gunRight.getSize().x / 2.0f, this->gunRight.getSize().y / 2.0f},
this->gunRight.getRotation(),
WHITE);
}
else if (GameService::getInstance().getMouseWorldPos().x + (this->gunLeft.getSize().x / 2) < this->texture.getPosition().x && spriteAnimation.y == Left)
{
DrawTexturePro(
this->gunLeft.getTexture(),
{0, 0, this->gunLeft.getSize().x, this->gunLeft.getSize().y},
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f,
this->gunLeft.getSize().x,
this->gunLeft.getSize().y},
{this->gunLeft.getSize().x / 2.0f, this->gunLeft.getSize().y / 2.0f},
this->gunLeft.getRotation(),
WHITE);
}
}
void Player::fall()
{
outofboundsCheck();
velocity.y += gravity;
move(velocity);
}
+1
View File
@@ -31,6 +31,7 @@ void AssetService::setAssets()
sounds[ESound::VICTORY] = loadSound("Audio/Sounds/victorySound.wav");
sounds[ESound::PLAYER_DAMAGE] = loadSound("Audio/Sounds/WaterBalloon.wav");
sounds[ESound::ENEMY_DEFEATED] = loadSound("Audio/Sounds/pain.wav");
sounds[ESound::PLAYER_SPOTTED] = loadSound("Audio/Sounds/playerSpotted.wav");
backgrounds[EBackground::TUTORIAL] = loadTexture("Textures/TutorialBackground2.png");
backgrounds[EBackground::GAME1] = loadTexture("Textures/GameBackground_1.png");