Skip to content
Snippets Groups Projects
Unverified Commit 8afdfefc authored by Moritz Freund's avatar Moritz Freund Committed by Leon Lux
Browse files

Health and Damage gets read out from Level File and Bullets don't collide with each other

parent 8053892e
No related branches found
No related tags found
1 merge request!55Abgabe des Projektes
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
BulletObject::BulletObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, BulletObject::BulletObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale,
float friction, const std::vector<int> &tileIDs) float friction, const std::vector<int> &tileIDs)
: DynamicObject(world, position, size, density, scale, friction, tileIDs) : DynamicObject(world, position, size, density, scale, friction, tileIDs, true)
{ {
m_body->SetLinearDamping(0.0f); m_body->SetLinearDamping(0.0f);
} }
......
#include "DynamicObject.hpp" #include "DynamicObject.hpp"
DynamicObject::DynamicObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, DynamicObject::DynamicObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale,
float friction, const std::vector<int> &tileIDs) float friction, const std::vector<int> &tileIDs, bool isBullet)
: GameObject(world, position, size, density, scale, friction, tileIDs) : GameObject(world, position, size, density, scale, friction, tileIDs, isBullet, true)
{ {
m_body->SetLinearDamping(3.0f); m_body->SetLinearDamping(3.0f);
} }
\ No newline at end of file
...@@ -28,5 +28,5 @@ class DynamicObject : public GameObject ...@@ -28,5 +28,5 @@ class DynamicObject : public GameObject
* @param tileIDs The tile IDs representing the object's texture in the tilesheet. * @param tileIDs The tile IDs representing the object's texture in the tilesheet.
*/ */
DynamicObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, DynamicObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale,
float friction, const std::vector<int> &tileIDs); float friction, const std::vector<int> &tileIDs, bool isBullet);
}; };
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
#include <box2d/b2_math.h> #include <box2d/b2_math.h>
Enemy::Enemy(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction, Enemy::Enemy(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction,
BulletManager &bulletmanager, const std::vector<int> &tileIDs) BulletManager &bulletmanager, const std::vector<int> &tileIDs, int health, int damage)
: DynamicObject(world, position, size, density, scale, friction, tileIDs), m_lastFire(1000), m_fireRate(2), : DynamicObject(world, position, size, density, scale, friction, tileIDs, false), m_lastFire(1000), m_fireRate(2),
m_fireDelay(static_cast<Uint32>(std::round(1000.0 / m_fireRate))) m_fireDelay(static_cast<Uint32>(std::round(1000.0 / m_fireRate))), m_health(health), m_damage(damage)
{ {
m_body->GetUserData().pointer = 4; // 4 = Enemy (idenfier for contact listener) m_body->GetUserData().pointer = 4; // 4 = Enemy (idenfier for contact listener)
} }
...@@ -30,6 +30,11 @@ void Enemy::set_fireRate(int rate) ...@@ -30,6 +30,11 @@ void Enemy::set_fireRate(int rate)
m_fireDelay = static_cast<Uint32>(std::round(1000.0 / m_fireRate)); m_fireDelay = static_cast<Uint32>(std::round(1000.0 / m_fireRate));
} }
void Enemy::take_damage(int damage)
{
m_health-=damage;
}
b2Body *Enemy::getBody() b2Body *Enemy::getBody()
{ {
return m_body; return m_body;
......
...@@ -30,7 +30,7 @@ class Enemy : public DynamicObject ...@@ -30,7 +30,7 @@ class Enemy : public DynamicObject
* @param tileIDs The tile IDs representing the enemy's texture in the tilesheet. * @param tileIDs The tile IDs representing the enemy's texture in the tilesheet.
*/ */
Enemy(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction, Enemy(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction,
BulletManager &bulletmanager, const std::vector<int> &tileIDs); BulletManager &bulletmanager, const std::vector<int> &tileIDs, int health, int damage);
/** /**
* @brief Retrieves the Enemy's physics body. * @brief Retrieves the Enemy's physics body.
...@@ -50,6 +50,8 @@ class Enemy : public DynamicObject ...@@ -50,6 +50,8 @@ class Enemy : public DynamicObject
*/ */
bool shoot(const b2Vec2 &playerPosition, BulletManager &bulletManager); bool shoot(const b2Vec2 &playerPosition, BulletManager &bulletManager);
void take_damage(int damage);
/** /**
* @brief Sets the enemy's fire rate. * @brief Sets the enemy's fire rate.
* *
...@@ -60,8 +62,8 @@ class Enemy : public DynamicObject ...@@ -60,8 +62,8 @@ class Enemy : public DynamicObject
void set_fireRate(int rate); void set_fireRate(int rate);
private: private:
int health; /**< The health of the enemy, representing its hit points. */ int m_health; /**< The health of the enemy, representing its hit points. */
int damage; /**< The amount of damage the enemy can inflict on other objects. */ int m_damage; /**< The amount of damage the enemy can inflict on other objects. */
int m_fireRate; /**< The rate at which the enemy can fire projectiles. */ int m_fireRate; /**< The rate at which the enemy can fire projectiles. */
Uint32 m_fireDelay; /**< The delay between shots in milliseconds. */ Uint32 m_fireDelay; /**< The delay between shots in milliseconds. */
Uint32 m_lastFire; /**< The timestamp of the last shot fired. */ Uint32 m_lastFire; /**< The timestamp of the last shot fired. */
......
...@@ -15,10 +15,10 @@ EnemyManager::~EnemyManager() ...@@ -15,10 +15,10 @@ EnemyManager::~EnemyManager()
m_enemies.clear(); m_enemies.clear();
} }
void EnemyManager::addEnemy(const b2Vec2 &position, const std::vector<int32_t> &spriteIDs) void EnemyManager::addEnemy(const b2Vec2 &position, const std::vector<int32_t> &spriteIDs, int health, int damage)
{ {
Enemy *newEnemy = new Enemy(m_world, position, b2Vec2(16.0f, 16.0f), 1.0f, m_tilesheetManager.getGameScale(), 1.0f, Enemy *newEnemy = new Enemy(m_world, position, b2Vec2(16.0f, 16.0f), 1.0f, m_tilesheetManager.getGameScale(), 1.0f,
m_bulletManager, spriteIDs); m_bulletManager, spriteIDs, health, damage);
m_enemies.emplace_back(newEnemy); m_enemies.emplace_back(newEnemy);
} }
......
...@@ -49,7 +49,7 @@ class EnemyManager ...@@ -49,7 +49,7 @@ class EnemyManager
* *
* @param position The position where the new enemy will be placed in the world. * @param position The position where the new enemy will be placed in the world.
*/ */
void addEnemy(const b2Vec2 &position, const std::vector<int32_t> &spriteIDs); void addEnemy(const b2Vec2 &position, const std::vector<int32_t> &spriteIDs, int health, int damage);
/** /**
* @brief Handles the random shooting of enemy projectiles. * @brief Handles the random shooting of enemy projectiles.
......
...@@ -31,7 +31,7 @@ void GameContext::loadRoom(const lvl::Room &next_room) ...@@ -31,7 +31,7 @@ void GameContext::loadRoom(const lvl::Room &next_room)
{ {
b2Vec2 enemy_pos = {static_cast<float>(enemy_it->position.x * m_tileSize), b2Vec2 enemy_pos = {static_cast<float>(enemy_it->position.x * m_tileSize),
static_cast<float>(enemy_it->position.y * m_tileSize)}; static_cast<float>(enemy_it->position.y * m_tileSize)};
m_enemyManager.addEnemy(enemy_pos, m_level.getEnemies().at(enemy_it->id).getSprites()); m_enemyManager.addEnemy(enemy_pos, m_level.getEnemies().at(enemy_it->id).getSprites(), m_level.getEnemies()[0].getHealth(), 5);
} }
const std::unordered_set<int> valid_ids = {102, 103, 104, 134, 135, 136, 166, 167, 168, 198, 199}; // door ids const std::unordered_set<int> valid_ids = {102, 103, 104, 134, 135, 136, 166, 167, 168, 198, 199}; // door ids
......
#include "GameObject.hpp" #include "GameObject.hpp"
#include "TilesheetManager.hpp" #include "TilesheetManager.hpp"
#include <box2d/b2_body.h>
GameObject::GameObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, GameObject::GameObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale,
float friction, const std::vector<int> &tileIDs) float friction, const std::vector<int> &tileIDs, bool isBullet, bool isDynamic)
: m_world(world), m_size(scale * size), m_frameDuration(0.0f), m_lastFrameSwitch(0.0f), m_currentFrame(tileIDs[0]), : m_world(world), m_size(scale * size), m_frameDuration(0.0f), m_lastFrameSwitch(0.0f), m_currentFrame(tileIDs[0]),
m_firstFrame(tileIDs[0]), m_lastFrame(m_firstFrame + tileIDs.size() - 1), m_tileIDs(tileIDs) m_firstFrame(tileIDs[0]), m_lastFrame(m_firstFrame + tileIDs.size() - 1), m_tileIDs(tileIDs)
{ {
...@@ -10,7 +11,14 @@ GameObject::GameObject(b2World *world, const b2Vec2 &position, const b2Vec2 &siz ...@@ -10,7 +11,14 @@ GameObject::GameObject(b2World *world, const b2Vec2 &position, const b2Vec2 &siz
// Box2D // Box2D
m_pixelsPerMeterScaled = pixelsPerMeter * scale; m_pixelsPerMeterScaled = pixelsPerMeter * scale;
b2BodyDef bodyDef; b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody; if(isDynamic)
{
bodyDef.type = b2_dynamicBody;
}
else
{
bodyDef.type = b2_staticBody;
}
b2Vec2 scaledPosition = {position.x / m_pixelsPerMeterScaled, position.y / m_pixelsPerMeterScaled}; b2Vec2 scaledPosition = {position.x / m_pixelsPerMeterScaled, position.y / m_pixelsPerMeterScaled};
bodyDef.position = scaledPosition; bodyDef.position = scaledPosition;
m_body = m_world->CreateBody(&bodyDef); m_body = m_world->CreateBody(&bodyDef);
...@@ -24,6 +32,10 @@ GameObject::GameObject(b2World *world, const b2Vec2 &position, const b2Vec2 &siz ...@@ -24,6 +32,10 @@ GameObject::GameObject(b2World *world, const b2Vec2 &position, const b2Vec2 &siz
fixtureDef.shape = &boxShape; fixtureDef.shape = &boxShape;
fixtureDef.density = density; fixtureDef.density = density;
fixtureDef.friction = friction; fixtureDef.friction = friction;
if(isBullet)
{
fixtureDef.filter.groupIndex = -1;
}
m_body->CreateFixture(&fixtureDef); m_body->CreateFixture(&fixtureDef);
......
...@@ -34,7 +34,7 @@ class GameObject ...@@ -34,7 +34,7 @@ class GameObject
* @param tileIDs The list of tile IDs representing the animation frames for the object. * @param tileIDs The list of tile IDs representing the animation frames for the object.
*/ */
GameObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction, GameObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction,
const std::vector<int> &tileIDs); const std::vector<int> &tileIDs, bool isBullet, bool isDynamic);
/** /**
* @brief Destructor for the GameObject class. * @brief Destructor for the GameObject class.
......
...@@ -9,7 +9,7 @@ GameWindow::GameWindow(const int32_t width, const int32_t height, const float_t ...@@ -9,7 +9,7 @@ GameWindow::GameWindow(const int32_t width, const int32_t height, const float_t
m_objectManager(m_level, &m_world, m_tilesheetManager), m_objectManager(m_level, &m_world, m_tilesheetManager),
m_player(&m_world, b2Vec2({static_cast<float>(width) / 2, static_cast<float>(height) / 2}), m_player(&m_world, b2Vec2({static_cast<float>(width) / 2, static_cast<float>(height) / 2}),
b2Vec2({16.0f, 16.0f}), 1.0f, m_Scale, 1.0f, m_bulletManager, m_tilesheetManager, b2Vec2({16.0f, 16.0f}), 1.0f, m_Scale, 1.0f, m_bulletManager, m_tilesheetManager,
m_level.getPlayer().getSprites()), m_level.getPlayer().getSprites(), m_level.getPlayer().getHealth(), 5),
m_gameContext(m_level, m_enemyManager, m_bulletManager, m_tilesheetManager, m_objectManager), m_gameContext(m_level, m_enemyManager, m_bulletManager, m_tilesheetManager, m_objectManager),
m_Camera(width, height, m_level.getRooms().at(2).getWidth() * m_level.getSpritesheet().getTileSize() * m_Scale, m_Camera(width, height, m_level.getRooms().at(2).getWidth() * m_level.getSpritesheet().getTileSize() * m_Scale,
m_level.getRooms().at(2).getHeight() * m_level.getSpritesheet().getTileSize() * m_Scale) m_level.getRooms().at(2).getHeight() * m_level.getSpritesheet().getTileSize() * m_Scale)
......
#include "Player.hpp" #include "Player.hpp"
Player::Player(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction, Player::Player(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction,
BulletManager &bulletmanager, TilesheetManager &tilesheetManager, const std::vector<int> &tileIDs) BulletManager &bulletmanager, TilesheetManager &tilesheetManager, const std::vector<int> &tileIDs, int health, int damage)
: DynamicObject(world, position, size, density, scale, friction, tileIDs), m_alive(true), : DynamicObject(world, position, size, density, scale, friction, tileIDs, false), m_alive(true),
m_bulletManager(bulletmanager), m_lastFire(1000), m_fireRate(5), m_bulletManager(bulletmanager), m_lastFire(1000), m_fireRate(5),
m_fireDelay(static_cast<Uint32>(std::round(1000.0 / m_fireRate))) m_fireDelay(static_cast<Uint32>(std::round(1000.0 / m_fireRate))),
m_health(health),
m_damage(damage)
{ {
m_body->GetUserData().pointer = 2; // 2 = Player (idenfier for contact listener) m_body->GetUserData().pointer = 2; // 2 = Player (idenfier for contact listener)
} }
...@@ -97,3 +99,8 @@ void Player::set_fireRate(int rate) // if items are ever implemented, this will ...@@ -97,3 +99,8 @@ void Player::set_fireRate(int rate) // if items are ever implemented, this will
m_fireRate = rate; m_fireRate = rate;
m_fireDelay = static_cast<Uint32>(std::round(1000.0 / m_fireRate)); m_fireDelay = static_cast<Uint32>(std::round(1000.0 / m_fireRate));
} }
void Player::take_damage(int damage)
{
m_health-=damage;
}
...@@ -29,7 +29,7 @@ class Player : public DynamicObject ...@@ -29,7 +29,7 @@ class Player : public DynamicObject
* @param tileIDs The tile IDs representing the player's texture in the tilesheet. * @param tileIDs The tile IDs representing the player's texture in the tilesheet.
*/ */
Player(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction, Player(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float density, float scale, float friction,
BulletManager &bulletmanager, TilesheetManager &tilesheetManager, const std::vector<int> &tileIDs); BulletManager &bulletmanager, TilesheetManager &tilesheetManager, const std::vector<int> &tileIDs, int health, int damage);
/** /**
* @brief Destructor for the Player class. * @brief Destructor for the Player class.
...@@ -68,9 +68,12 @@ class Player : public DynamicObject ...@@ -68,9 +68,12 @@ class Player : public DynamicObject
*/ */
void set_fireRate(int rate); void set_fireRate(int rate);
void take_damage(int damage);
private: private:
bool m_alive; /**< A flag indicating if the player is alive. */ bool m_alive; /**< A flag indicating if the player is alive. */
int m_health; /**< The player's health value, determines if the player is alive. */ int m_health; /**< The player's health value, determines if the player is alive. */
int m_damage;
int m_fireRate; /**< The player's fire rate (bullets per second). */ int m_fireRate; /**< The player's fire rate (bullets per second). */
Uint32 m_fireDelay; /**< The delay between shots in milliseconds. */ Uint32 m_fireDelay; /**< The delay between shots in milliseconds. */
Uint32 m_lastFire; /**< The timestamp of the last shot fired. */ Uint32 m_lastFire; /**< The timestamp of the last shot fired. */
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
StaticObject::StaticObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float scale, float friction, StaticObject::StaticObject(b2World *world, const b2Vec2 &position, const b2Vec2 &size, float scale, float friction,
const std::vector<int> &tileIDs) const std::vector<int> &tileIDs)
: GameObject(world, position, size, 0.0f, scale, friction, tileIDs) : GameObject(world, position, size, 0.0f, scale, friction, tileIDs, false, false)
{ {
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment