12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- /*
- * 2D Game Engine
- * Collision.h:
- * Based on pikuma.com 2D game engine in C++ and Lua course
- * Copyright (c) 2021 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 15/02/2021.
- */
- #ifndef GAMEENGINE_COLLISION_H
- #define GAMEENGINE_COLLISION_H
- #include <ECS.h>
- #include <Logger.h>
- #include <Components/Transform.h>
- #include <Components/BoxCollider.h>
- class CollisionSystem: public System
- {
- class BoxRect
- {
- double x0, y0, x1, y1;
- public:
- BoxRect(TransformComponent tc, BoxColliderComponent bc)
- {
- this->x0 = tc.position.x + bc.offset.x;
- this->x1 = this->x0 + bc.width;
- this->y0 = tc.position.y + bc.offset.y;
- this->y1 = this->y0 + bc.height;
- }
- bool collideWith(BoxRect &b) const
- {
- return ((this->x0 < b.x1) && (this->x1 > b.x0) && (this->y0 < b.y1) && (this->y1 > b.y0));
- }
- };
- public:
- CollisionSystem()
- {
- this->requireComponent<TransformComponent>();
- this->requireComponent<BoxColliderComponent>();
- }
- void update()
- {
- auto entities = this->getSystemEntities();
- for(auto i = entities.begin(); i != entities.end(); i++)
- {
- for(auto j = (i + 1); j != entities.end(); j++)
- {
- BoxRect boxA = BoxRect(i->getComponent<TransformComponent>(),
- i->getComponent<BoxColliderComponent>());
- BoxRect boxB = BoxRect(j->getComponent<TransformComponent>(),
- j->getComponent<BoxColliderComponent>());
- if (boxA.collideWith(boxB))
- {
- Logger::Debug("Entity #%d collided with #%d!", i->getId(), j->getId());
- }
- }
- }
- }
- };
- #endif /* GAMEENGINE_COLLISION_H */
|