diff --git a/YuppleMayham/CMakeLists.txt b/YuppleMayham/CMakeLists.txt index f2590cf..7d5615a 100644 --- a/YuppleMayham/CMakeLists.txt +++ b/YuppleMayham/CMakeLists.txt @@ -6,13 +6,13 @@ find_package(SDL2 REQUIRED) find_package(SDL2_IMAGE REQUIRED) include_directories(SYSTEM "c:/sdks/glad/include") -include_directories(SYSTEM "c:/sdks/tinyxml2-10.0.0/include") +include_directories(SYSTEM "c:/sdks/tinyxml2-10.0.0/debug/include") include_directories(SYSTEM "c:/sdks/glm") include_directories(SYSTEM "C:/sdks/sol2-3.3.0/single/single/include") include_directories(SYSTEM "c:/sdks/lua-5.4.6/include") include_directories(SYSTEM "C:/sdks/freetype-2.13.2/include") link_directories(SYSTEM "C:/sdks/freetype-2.13.2/objs") -link_directories(SYSTEM "c:/sdks/tinyxml2-10.0.0/lib") +link_directories(SYSTEM "c:/sdks/tinyxml2-10.0.0/debug/lib") link_directories(SYSTEM "c:/sdks/lua-5.4.6/lib") include_directories(${PROJECT_SOURCE_DIR}/YupplyMayham/gameplay) diff --git a/YuppleMayham/gameplay/physics.h b/YuppleMayham/gameplay/physics.h index 5095101..c9d2af0 100644 --- a/YuppleMayham/gameplay/physics.h +++ b/YuppleMayham/gameplay/physics.h @@ -9,6 +9,8 @@ class EventManager; struct PhysicsComponent { bool isBullet = false; + // This ID holds the ID of the gameactor that owns this object, that being themselves or a bullet. + unsigned int ID = 0; // If the ID stays 0 then that object has no owner and will be uncollidable struct RigidBody { glm::vec3 position; glm::vec3 velocity; @@ -29,8 +31,9 @@ struct PhysicsComponent { class PhysicsComponentFactory { public: - static PhysicsComponent makeBullet(const glm::vec3& pos, float mass, float radius) { + static PhysicsComponent makeBullet(const unsigned int ID, const glm::vec3& pos, float mass, float radius) { PhysicsComponent p; + p.ID = ID; p.rigidBody.position = pos; p.rigidBody.mass = mass; p.collider = { PhysicsComponent::Collider::Shape::Circle, glm::vec3(radius, radius, 0.f), glm::vec3(radius) }; @@ -43,9 +46,11 @@ class PhysicsEngine { public: PhysicsEngine() {} + using CollisionPair = std::pair; + void hookEventManager(const std::shared_ptr& eventManager); - std::shared_ptr createObject(const glm::vec3& pos, float mass, PhysicsComponent::Collider::Shape shape, glm::vec3 dimensions, glm::vec3 offset = glm::vec3(0.f)); + std::shared_ptr createObject(const unsigned int ID, const glm::vec3& pos, float mass, PhysicsComponent::Collider::Shape shape, glm::vec3 dimensions, glm::vec3 offset = glm::vec3(0.f)); void loadCollisionMap(const std::vector>&, float tileSize); void addObject(const std::shared_ptr&); void removeObject(const std::shared_ptr&); @@ -56,7 +61,7 @@ private: void getPossibleCollisions(); int getTileCollider(const glm::vec3& position); std::vector> objects; - std::vector> objCollisions; + std::vector objCollisions; std::vector> collisionMap; float tileSize = 32.f; std::shared_ptr eventManager; diff --git a/YuppleMayham/gameplay/weapons/bullet.h b/YuppleMayham/gameplay/weapons/bullet.h index 24bcc62..922221a 100644 --- a/YuppleMayham/gameplay/weapons/bullet.h +++ b/YuppleMayham/gameplay/weapons/bullet.h @@ -10,15 +10,26 @@ class Camera; class Bullet : public Entity { public: - Bullet(std::shared_ptr shader, glm::vec3 fireFrom, glm::vec2 direction, float bulletSpeed, float bulletDrop, glm::vec2 bulletSize) : + Bullet + ( + const unsigned int owner, + std::shared_ptr shader, + glm::vec3 fireFrom, + glm::vec2 direction, + float bulletSpeed, + float bulletDrop, + glm::vec2 bulletSize + ) : Entity(shader), origin(fireFrom), direction(direction), bulletSpeed(bulletSpeed), bulletDrop(bulletDrop), - bulletSize(bulletSize) { + bulletSize(bulletSize) + { this->setPosition(origin); this->setScale(glm::vec3(bulletSize, 1.0f)); + ownerid = owner; }; void addComponent(std::shared_ptr component); @@ -32,6 +43,7 @@ public: ~Bullet(); private: + unsigned int ownerid; glm::vec3 origin; glm::vec2 direction; float bulletSpeed; diff --git a/YuppleMayham/gameplay/weapons/bulletmanager.h b/YuppleMayham/gameplay/weapons/bulletmanager.h index 7869724..01b9d7b 100644 --- a/YuppleMayham/gameplay/weapons/bulletmanager.h +++ b/YuppleMayham/gameplay/weapons/bulletmanager.h @@ -16,7 +16,8 @@ class BulletManager { public: void addBullet(const std::shared_ptr& bullet); - void addBullet(const glm::vec3& fireFrom, + void addBullet(const unsigned int owner, + const glm::vec3& fireFrom, const glm::vec2& direction, float bulletSpeed, float bulletDrop, diff --git a/YuppleMayham/gameplay/weapons/weapon.h b/YuppleMayham/gameplay/weapons/weapon.h index 5f75094..bf5843b 100644 --- a/YuppleMayham/gameplay/weapons/weapon.h +++ b/YuppleMayham/gameplay/weapons/weapon.h @@ -18,6 +18,7 @@ class Camera; class BulletManager; class EventManager; class ResourceManager; +class GameActor; struct WeaponData; class Weapon : public Entity @@ -25,7 +26,7 @@ class Weapon : public Entity public: Weapon(const WeaponData* data, const std::shared_ptr& weaponShader, const std::shared_ptr& bulletShader, ResourceManager* resourceManager); - void setWielder(Entity* wielder) { this->wielder = wielder; } + void setWielder(const std::shared_ptr& wielder) { this->wielder = wielder; } void addComponent(const std::shared_ptr& component); void hookEventManager(const std::shared_ptr& eventManager); @@ -49,7 +50,8 @@ private: std::shared_ptr bulletSpread; std::shared_ptr bulletModifer; - Entity* wielder = nullptr; + + std::shared_ptr wielder; int lastFireTime = 0; Direction lastDir; diff --git a/YuppleMayham/src/gameplay/gameactor.cpp b/YuppleMayham/src/gameplay/gameactor.cpp index 8b22395..3b57800 100644 --- a/YuppleMayham/src/gameplay/gameactor.cpp +++ b/YuppleMayham/src/gameplay/gameactor.cpp @@ -23,7 +23,7 @@ void GameActor::equipWeapon(std::shared_ptr weapon) { currentWeapon = std::move(weapon); if (currentWeapon) - currentWeapon->setWielder(this); + currentWeapon->setWielder(std::shared_ptr(this)); } void GameActor::attachEventManager(std::shared_ptr eventManager) diff --git a/YuppleMayham/src/gameplay/physics.cpp b/YuppleMayham/src/gameplay/physics.cpp index aca2445..603da0b 100644 --- a/YuppleMayham/src/gameplay/physics.cpp +++ b/YuppleMayham/src/gameplay/physics.cpp @@ -17,9 +17,15 @@ void PhysicsEngine::hookEventManager(const std::shared_ptr& eventM }); } -std::shared_ptr PhysicsEngine::createObject(const glm::vec3& pos, float mass, PhysicsComponent::Collider::Shape shape, glm::vec3 dimensions, glm::vec3 offset) +std::shared_ptr PhysicsEngine::createObject(const unsigned int ID, + const glm::vec3& pos, + float mass, + PhysicsComponent::Collider::Shape shape, + glm::vec3 dimensions, + glm::vec3 offset) { auto component = std::make_shared (); + component->ID = ID; component->rigidBody.position = pos; component->rigidBody.mass = mass; component->collider.shape = shape; @@ -69,7 +75,7 @@ void PhysicsEngine::getPossibleCollisions() for (size_t j = i + 1; j < objects.size(); ++j) { auto& colliderObj = objects[j]; - if (obj.get() == colliderObj.get()) continue; + if (obj.get() == colliderObj.get() || obj->ID == colliderObj->ID) continue; float colliderRight = colliderObj->rigidBody.position.x + colliderObj->collider.dimensions.x; float colliderBottom = colliderObj->rigidBody.position.y + colliderObj->collider.dimensions.y; float objectRight = obj->rigidBody.position.x + obj->collider.dimensions.x; @@ -78,7 +84,7 @@ void PhysicsEngine::getPossibleCollisions() objectRight >= colliderObj->rigidBody.position.x) || (obj->rigidBody.position.y <= colliderBottom && objectBottom >= colliderObj->rigidBody.position.y)) - objCollisions.push_back(std::make_pair(obj.get(), colliderObj.get())); + objCollisions.push_back(CollisionPair(obj.get(), colliderObj.get())); } } } @@ -95,6 +101,15 @@ void PhysicsEngine::resolvePossibleCollisions() if (glm::length(distance) < sumOfRadius) { // We got impact! + // That impact is a bullet hitting a gameactor! + if (objs.first->isBullet || objs.second->isBullet && !(objs.first->isBullet && objs.second->isBullet)) + { + eventManager->notify(std::make_shared( + (objs.first->isBullet) ? objs.first->ID : objs.second->ID, + (!objs.first->isBullet) ? objs.first->ID : objs.second->ID + )); + } + // Apply impulse force glm::vec3 normal = distance / glm::length(distance); float penetrationDepth = sumOfRadius - glm::length(distance); glm::vec3 correctionVector = normal * (penetrationDepth / ((1 / objs.first->rigidBody.mass) + (1 / objs.second->rigidBody.mass))); @@ -103,8 +118,6 @@ void PhysicsEngine::resolvePossibleCollisions() float e = std::min(objs.first->rigidBody.elasticity, objs.second->rigidBody.elasticity); float impulseMag = (-(1 + e) * glm::dot(vrel, normal)) / ((1 / objs.first->rigidBody.mass) + (1 / objs.second->rigidBody.mass)); - //objs.first->rigidBody.applyForce(normal, impulseMag); - //objs.second->rigidBody.applyForce(normal, -impulseMag); objs.first->rigidBody.position += (correctionVector / objs.first->rigidBody.mass); objs.second->rigidBody.position -= (correctionVector / objs.second->rigidBody.mass); objs.first->rigidBody.velocity += impulseMag * normal / objs.first->rigidBody.mass; diff --git a/YuppleMayham/src/gameplay/scene.cpp b/YuppleMayham/src/gameplay/scene.cpp index f64e738..6ff48ef 100644 --- a/YuppleMayham/src/gameplay/scene.cpp +++ b/YuppleMayham/src/gameplay/scene.cpp @@ -76,7 +76,9 @@ void Scene::loadDebugShooterScene() entity->setScale(glm::vec3(mapData.tileSize, mapData.tileSize, 1.f)); entity->addPhysicsComponent( - physicsEngine->createObject(entity->getPosition(), 49.0, + physicsEngine->createObject(entity->getActorID(), + entity->getPosition(), + 49.0, PhysicsComponent::Collider::Shape::Circle, glm::vec3(mapData.tileSize / 2), glm::abs(entity->getCenter() - entity->getPosition())) diff --git a/YuppleMayham/src/gameplay/weapons/bulletmanager.cpp b/YuppleMayham/src/gameplay/weapons/bulletmanager.cpp index e7650d6..f77f269 100644 --- a/YuppleMayham/src/gameplay/weapons/bulletmanager.cpp +++ b/YuppleMayham/src/gameplay/weapons/bulletmanager.cpp @@ -24,7 +24,7 @@ void BulletManager::addBullet(const std::shared_ptr& bullet) bullets.emplace_back(bullet); } -void BulletManager::addBullet(const glm::vec3& fireFrom, +void BulletManager::addBullet(const unsigned int owner, const glm::vec3& fireFrom, const glm::vec2& direction, float bulletSpeed, float bulletDrop, @@ -32,7 +32,7 @@ void BulletManager::addBullet(const glm::vec3& fireFrom, const std::shared_ptr& shader, const std::shared_ptr& sprite) { - auto bullet = std::make_shared(shader, fireFrom, direction, bulletSpeed, bulletDrop, bulletSize); + auto bullet = std::make_shared(owner, shader, fireFrom, direction, bulletSpeed, bulletDrop, bulletSize); bullet->addComponent(sprite); //bullet->addPhysicsComponent(phy) bullets.emplace_back(bullet); diff --git a/YuppleMayham/src/gameplay/weapons/weapon.cpp b/YuppleMayham/src/gameplay/weapons/weapon.cpp index 6f9c286..07ffc2c 100644 --- a/YuppleMayham/src/gameplay/weapons/weapon.cpp +++ b/YuppleMayham/src/gameplay/weapons/weapon.cpp @@ -2,7 +2,7 @@ #include "../../../utility/resourcemanager.h" #include "../../../gameplay/weapons/bulletmanager.h" #include "../../../gameplay/weapons/bullet.h" -#include "../../../gameplay/entity.h" +#include "../../../gameplay/gameactor.h" #include "../../../utility/component.h" #include "../../../utility/events.h" #include "../../../gameplay/physics.h" @@ -76,9 +76,9 @@ void Weapon::shoot() origin.x -= ((bulletSize.x) * sizeMod) * 0.5f; origin.y -= ((bulletSize.y) * sizeMod) * 0.5f; - auto bullet = std::make_shared(bulletShader, origin, facing, bulletSpeed, bulletDrop, bulletSize); + auto bullet = std::make_shared(wielder->getActorID(), bulletShader, origin, facing, bulletSpeed, bulletDrop, bulletSize); bullet->addComponent(bulletSprite); - bullet->addPhysicsComponent(std::make_shared(PhysicsComponentFactory::makeBullet(origin, mass, bulletSize.x / 2))); + bullet->addPhysicsComponent(std::make_shared(PhysicsComponentFactory::makeBullet(wielder->getActorID(), origin, mass, bulletSize.x / 2))); bullet->getPhysicsComponent()->rigidBody.velocity += bulletSpeed * glm::vec3(facing.x, facing.y, 0.f) / mass; if (eventManager) eventManager->notify(std::make_shared(bullet)); @@ -136,6 +136,5 @@ void Weapon::render(const std::shared_ptr& camera) component->bind(); component->render(); } - DebugDrawer::getInstance().addCircle(wielder->getCenter(), wielder->getScale().x + (weaponSize.x * 0.5) + (weaponOffset.x), glm::vec4(1.f, 0.5f, 0.25f, 0.4f)); bulletManager->render(camera); } \ No newline at end of file diff --git a/YuppleMayham/utility/events.h b/YuppleMayham/utility/events.h index 5753f2b..e79627e 100644 --- a/YuppleMayham/utility/events.h +++ b/YuppleMayham/utility/events.h @@ -31,6 +31,14 @@ public: std::shared_ptr physObj; }; +class BulletCollideEvent : public Event { +public: + BulletCollideEvent(const unsigned int ownerID, const unsigned int victimID) : ownerID(ownerID), victimID(victimID) {} + std::string getType() const override { return "OnBulletCollide"; } + unsigned int ownerID; + unsigned int victimID; +}; + class DirectionChangeEvent : public Event { public: DirectionChangeEvent(const int actorid, Direction direction) : actorid(actorid), direction(direction) {}