├── .gitignore ├── .vscode ├── launch.json └── settings.json ├── AIGame ├── CMakeLists.txt ├── SceneData │ └── EntryScene.scene └── Source │ ├── AIGameEntryPoint.cpp │ ├── GameObjects │ ├── Brick.cpp │ ├── Brick.h │ ├── Enemy.cpp │ ├── Enemy.h │ ├── PlayerShip.cpp │ └── PlayerShip.h │ └── Scenes │ ├── EntryScene.cpp │ └── EntryScene.h ├── CMakeLists.txt ├── ConsoleCraftEngine.sln ├── ConsoleCraftEngine.vcxproj ├── ConsoleCraftEngine.vcxproj.filters ├── CraftBreaker ├── CMakeLists.txt └── Source │ ├── BreakerGameEntry.cpp │ ├── GameObjects │ ├── Ball.cpp │ ├── Ball.h │ ├── Breaker.cpp │ ├── Breaker.h │ ├── Brick.cpp │ └── Brick.h │ └── Scenes │ ├── BreakerScene.cpp │ └── BreakerScene.h ├── CraftCity ├── CMakeLists.txt ├── CraftCity.vcxproj ├── CraftCity.vcxproj.filters └── Source │ ├── BuildingSystem │ ├── BuildHandler.cpp │ ├── BuildHandler.h │ ├── BuildingMenu.cpp │ ├── BuildingMenu.h │ ├── DemolishHandler.cpp │ └── DemolishHandler.h │ ├── CityStatics │ ├── CityStatics.cpp │ ├── CityStatics.h │ ├── PlayerVault.cpp │ └── PlayerVault.h │ ├── CraftCityGameEntry.cpp │ ├── GameObjects │ ├── Buildings │ │ ├── Buildable.cpp │ │ ├── Buildable.h │ │ ├── BuildingTrigger.cpp │ │ ├── BuildingTrigger.h │ │ ├── House.cpp │ │ ├── House.h │ │ ├── IndustrialBuilding.cpp │ │ ├── IndustrialBuilding.h │ │ ├── Road.cpp │ │ └── Road.h │ ├── SelectCursor.cpp │ └── SelectCursor.h │ ├── Scenes │ ├── CraftCityMainMenuScene.cpp │ ├── CraftCityMainMenuScene.h │ ├── CraftCityScene.cpp │ └── CraftCityScene.h │ └── UI │ ├── PlayerStatsUI.cpp │ └── PlayerStatsUI.h ├── CraftMatch ├── CMakeLists.txt ├── CraftMatch.vcxproj ├── CraftMatch.vcxproj.filters └── Source │ ├── CraftMatchGameEntry.cpp │ ├── GameObjects │ ├── ClubItem.cpp │ ├── ClubItem.h │ ├── DiamondItem.cpp │ ├── DiamondItem.h │ ├── GridUnit.cpp │ ├── GridUnit.h │ ├── HeartItem.cpp │ ├── HeartItem.h │ ├── Item.cpp │ ├── Item.h │ ├── SpadesItem.cpp │ └── SpadesItem.h │ ├── Grid.cpp │ ├── Grid.h │ ├── Scenes │ ├── MatchScene.cpp │ └── MatchScene.h │ ├── UnitSelector.cpp │ └── UnitSelector.h ├── CraftPhysicsTest ├── CMakeLists.txt └── Source │ ├── BridgeBuilderGameEntry.cpp │ ├── Polygons │ ├── Car.cpp │ └── Car.h │ ├── RoadCreator │ ├── RoadCreator.cpp │ └── RoadCreator.h │ └── Scenes │ ├── PhysicsTestScene.cpp │ └── PhysicsTestScene.h ├── CraftRogue ├── CMakeLists.txt ├── CraftRogue.vcxproj ├── CraftRogue.vcxproj.filters └── Source │ ├── Components │ ├── Health.cpp │ ├── Health.h │ ├── PlayerUpgradeComponent.cpp │ └── PlayerUpgradeComponent.h │ ├── CraftRogueGameEntry.cpp │ ├── GameObjects │ ├── Enemies │ │ ├── EnemyAmeboid.cpp │ │ ├── EnemyAmeboid.h │ │ ├── EnemyRogue.cpp │ │ └── EnemyRogue.h │ ├── Player.cpp │ ├── Player.h │ └── Weapons │ │ ├── BlastGun.cpp │ │ ├── BlastGun.h │ │ ├── PlasmaBullet.cpp │ │ ├── PlasmaBullet.h │ │ ├── PlasmaGun.cpp │ │ ├── PlasmaGun.h │ │ ├── WaveGun.cpp │ │ ├── WaveGun.h │ │ ├── Weapon.cpp │ │ └── Weapon.h │ └── Scenes │ ├── RogueScene.cpp │ └── RogueScene.h ├── CraftShooterGame ├── CMakeLists 32bits.txt ├── CMakeLists.txt ├── CraftShooterGame.sln ├── CraftShooterGame.vcxproj ├── CraftShooterGame.vcxproj.filters └── Source │ ├── Components │ ├── PlayerController.cpp │ └── PlayerController.h │ ├── GameObjects │ ├── Pong │ │ ├── Ball.cpp │ │ ├── Ball.h │ │ ├── Paddle.cpp │ │ └── Paddle.h │ └── Shooter │ │ ├── Bullet.cpp │ │ ├── Bullet.h │ │ ├── Enemy.cpp │ │ ├── Enemy.h │ │ ├── PlayerShip.cpp │ │ └── PlayerShip.h │ ├── Scenes │ ├── Pong │ │ ├── PongScene.cpp │ │ └── PongScene.h │ └── Shooter │ │ ├── ShooterScene.cpp │ │ └── ShooterScene.h │ └── ShooterGameEntry.cpp ├── LICENSE ├── README.md ├── Source ├── Core.h ├── Core │ ├── Camera.cpp │ ├── Camera.h │ ├── Collision.cpp │ ├── Collision.h │ ├── Component │ │ ├── AI │ │ │ ├── AIMovement.cpp │ │ │ ├── AIMovement.h │ │ │ ├── Pathfinder.cpp │ │ │ └── Pathfinder.h │ │ ├── Component.cpp │ │ ├── Component.h │ │ ├── DestructionEffectComponent.cpp │ │ ├── DestructionEffectComponent.h │ │ ├── PlayerController.cpp │ │ ├── PlayerController.h │ │ ├── RigidbodyComponent.cpp │ │ ├── RigidbodyComponent.h │ │ ├── SpringComponent.cpp │ │ └── SpringComponent.h │ ├── EntryPoint.h │ ├── Event.cpp │ ├── Event.h │ ├── EventDispatcher.cpp │ ├── EventDispatcher.h │ ├── GameObject.cpp │ ├── GameObject.h │ ├── Input.cpp │ ├── Input.h │ ├── LineDrawer.cpp │ ├── LineDrawer.h │ ├── ParticleSystem │ │ ├── BlastParticle.cpp │ │ ├── BlastParticle.h │ │ ├── ParticleObject.cpp │ │ ├── ParticleObject.h │ │ ├── ParticleSource.cpp │ │ ├── ParticleSource.h │ │ ├── StaticBlastParticle.cpp │ │ ├── StaticBlastParticle.h │ │ ├── WaveParticle.cpp │ │ └── WaveParticle.h │ ├── Physics │ │ ├── CollisionResolver.cpp │ │ ├── CollisionResolver.h │ │ ├── JointRenderer.cpp │ │ ├── JointRenderer.h │ │ ├── Polygon │ │ │ ├── JointCreator.cpp │ │ │ ├── JointCreator.h │ │ │ ├── LineParticle.h │ │ │ ├── Polygon.cpp │ │ │ ├── Polygon.h │ │ │ ├── PolygonCollision.cpp │ │ │ ├── PolygonCollision.h │ │ │ ├── PolygonCreator │ │ │ │ ├── PolygonCreator.cpp │ │ │ │ ├── PolygonCreator.h │ │ │ │ ├── PolygonCursor.cpp │ │ │ │ └── PolygonCursor.h │ │ │ ├── PolygonHelper.h │ │ │ ├── PolygonParticle.cpp │ │ │ └── PolygonParticle.h │ │ ├── Raycaster.cpp │ │ ├── Raycaster.h │ │ ├── Rope.h │ │ ├── RopeParticle.h │ │ └── SoftBody.h │ ├── Renderer.cpp │ ├── Renderer.h │ ├── Scene.cpp │ ├── Scene.h │ ├── SceneManager.cpp │ ├── SceneManager.h │ ├── UIHandler.cpp │ └── UIHandler.h ├── CoreStructs │ ├── Transform.h │ └── Vector.h ├── box2d │ ├── include │ │ └── box2d │ │ │ ├── b2_api.h │ │ │ ├── b2_block_allocator.h │ │ │ ├── b2_body.h │ │ │ ├── b2_broad_phase.h │ │ │ ├── b2_chain_shape.h │ │ │ ├── b2_circle_shape.h │ │ │ ├── b2_collision.h │ │ │ ├── b2_common.h │ │ │ ├── b2_contact.h │ │ │ ├── b2_contact_manager.h │ │ │ ├── b2_distance.h │ │ │ ├── b2_distance_joint.h │ │ │ ├── b2_draw.h │ │ │ ├── b2_dynamic_tree.h │ │ │ ├── b2_edge_shape.h │ │ │ ├── b2_fixture.h │ │ │ ├── b2_friction_joint.h │ │ │ ├── b2_gear_joint.h │ │ │ ├── b2_growable_stack.h │ │ │ ├── b2_joint.h │ │ │ ├── b2_math.h │ │ │ ├── b2_motor_joint.h │ │ │ ├── b2_mouse_joint.h │ │ │ ├── b2_polygon_shape.h │ │ │ ├── b2_prismatic_joint.h │ │ │ ├── b2_pulley_joint.h │ │ │ ├── b2_revolute_joint.h │ │ │ ├── b2_rope.h │ │ │ ├── b2_settings.h │ │ │ ├── b2_shape.h │ │ │ ├── b2_stack_allocator.h │ │ │ ├── b2_time_of_impact.h │ │ │ ├── b2_time_step.h │ │ │ ├── b2_timer.h │ │ │ ├── b2_types.h │ │ │ ├── b2_weld_joint.h │ │ │ ├── b2_wheel_joint.h │ │ │ ├── b2_world.h │ │ │ ├── b2_world_callbacks.h │ │ │ └── box2d.h │ └── src │ │ ├── CMakeLists.txt │ │ ├── collision │ │ ├── b2_broad_phase.cpp │ │ ├── b2_chain_shape.cpp │ │ ├── b2_circle_shape.cpp │ │ ├── b2_collide_circle.cpp │ │ ├── b2_collide_edge.cpp │ │ ├── b2_collide_polygon.cpp │ │ ├── b2_collision.cpp │ │ ├── b2_distance.cpp │ │ ├── b2_dynamic_tree.cpp │ │ ├── b2_edge_shape.cpp │ │ ├── b2_polygon_shape.cpp │ │ └── b2_time_of_impact.cpp │ │ ├── common │ │ ├── b2_block_allocator.cpp │ │ ├── b2_draw.cpp │ │ ├── b2_math.cpp │ │ ├── b2_settings.cpp │ │ ├── b2_stack_allocator.cpp │ │ └── b2_timer.cpp │ │ ├── dynamics │ │ ├── b2_body.cpp │ │ ├── b2_chain_circle_contact.cpp │ │ ├── b2_chain_circle_contact.h │ │ ├── b2_chain_polygon_contact.cpp │ │ ├── b2_chain_polygon_contact.h │ │ ├── b2_circle_contact.cpp │ │ ├── b2_circle_contact.h │ │ ├── b2_contact.cpp │ │ ├── b2_contact_manager.cpp │ │ ├── b2_contact_solver.cpp │ │ ├── b2_contact_solver.h │ │ ├── b2_distance_joint.cpp │ │ ├── b2_edge_circle_contact.cpp │ │ ├── b2_edge_circle_contact.h │ │ ├── b2_edge_polygon_contact.cpp │ │ ├── b2_edge_polygon_contact.h │ │ ├── b2_fixture.cpp │ │ ├── b2_friction_joint.cpp │ │ ├── b2_gear_joint.cpp │ │ ├── b2_island.cpp │ │ ├── b2_island.h │ │ ├── b2_joint.cpp │ │ ├── b2_motor_joint.cpp │ │ ├── b2_mouse_joint.cpp │ │ ├── b2_polygon_circle_contact.cpp │ │ ├── b2_polygon_circle_contact.h │ │ ├── b2_polygon_contact.cpp │ │ ├── b2_polygon_contact.h │ │ ├── b2_prismatic_joint.cpp │ │ ├── b2_pulley_joint.cpp │ │ ├── b2_revolute_joint.cpp │ │ ├── b2_weld_joint.cpp │ │ ├── b2_wheel_joint.cpp │ │ ├── b2_world.cpp │ │ └── b2_world_callbacks.cpp │ │ └── rope │ │ └── b2_rope.cpp └── conio.h ├── create_new_preoject.sh └── docs └── quickstart.md /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "(gdb) Launch", 9 | "type": "cppdbg", 10 | "request": "launch", 11 | "program": "${workspaceFolder}/AIGame/build/AIGame", 12 | "args": [], 13 | "stopAtEntry": false, 14 | "cwd": "${fileDirname}", 15 | "environment": [], 16 | "externalConsole": false, 17 | "MIMode": "gdb", 18 | "setupCommands": [ 19 | { 20 | "description": "Enable pretty-printing for gdb", 21 | "text": "-enable-pretty-printing", 22 | "ignoreFailures": true 23 | }, 24 | { 25 | "description": "Set Disassembly Flavor to Intel", 26 | "text": "-gdb-set disassembly-flavor intel", 27 | "ignoreFailures": true 28 | } 29 | ] 30 | } 31 | 32 | ] 33 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "cmake.configureOnOpen": true, 3 | "files.associations": { 4 | "iostream": "cpp", 5 | "array": "cpp", 6 | "atomic": "cpp", 7 | "bit": "cpp", 8 | "*.tcc": "cpp", 9 | "cctype": "cpp", 10 | "clocale": "cpp", 11 | "cmath": "cpp", 12 | "compare": "cpp", 13 | "concepts": "cpp", 14 | "cstdarg": "cpp", 15 | "cstddef": "cpp", 16 | "cstdint": "cpp", 17 | "cstdio": "cpp", 18 | "cstdlib": "cpp", 19 | "cwchar": "cpp", 20 | "cwctype": "cpp", 21 | "deque": "cpp", 22 | "map": "cpp", 23 | "string": "cpp", 24 | "unordered_map": "cpp", 25 | "vector": "cpp", 26 | "exception": "cpp", 27 | "algorithm": "cpp", 28 | "functional": "cpp", 29 | "iterator": "cpp", 30 | "memory": "cpp", 31 | "memory_resource": "cpp", 32 | "numeric": "cpp", 33 | "random": "cpp", 34 | "string_view": "cpp", 35 | "system_error": "cpp", 36 | "tuple": "cpp", 37 | "type_traits": "cpp", 38 | "utility": "cpp", 39 | "initializer_list": "cpp", 40 | "iosfwd": "cpp", 41 | "istream": "cpp", 42 | "limits": "cpp", 43 | "new": "cpp", 44 | "numbers": "cpp", 45 | "ostream": "cpp", 46 | "stdexcept": "cpp", 47 | "streambuf": "cpp", 48 | "typeinfo": "cpp", 49 | "chrono": "cpp", 50 | "ctime": "cpp", 51 | "ratio": "cpp", 52 | "semaphore": "cpp", 53 | "stop_token": "cpp", 54 | "thread": "cpp", 55 | "condition_variable": "cpp", 56 | "cstring": "cpp", 57 | "set": "cpp", 58 | "future": "cpp", 59 | "mutex": "cpp", 60 | "cinttypes": "cpp" 61 | }, 62 | "C_Cpp.default.compilerPath": "/usr/bin/g++" 63 | } -------------------------------------------------------------------------------- /AIGame/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(AIGame) 3 | set(CMAKE_BUILD_TYPE Debug) 4 | 5 | file(GLOB_RECURSE ENGINE_SOURCES ../Source/*.cpp) 6 | file(GLOB_RECURSE ENGINE_HEADERS ../Source/*.h) 7 | 8 | 9 | add_library(ConsoleCraftEngine SHARED ${ENGINE_SOURCES} ) 10 | file(GLOB_RECURSE GAME_SOURCES Source/*.cpp) 11 | file(GLOB_RECURSE GAME_HEADERS Source/*.h) 12 | 13 | 14 | link_directories(${CMAKE_CURRENT_SOURCE_DIR}) 15 | add_executable(AIGame ${GAME_SOURCES} ${GAME_HEADERS}) 16 | include_directories(Engine/Source/) 17 | target_include_directories(AIGame PUBLIC 18 | ${CMAKE_CURRENT_SOURCE_DIR}/Source 19 | ${CMAKE_CURRENT_SOURCE_DIR}/../Source) 20 | 21 | 22 | target_link_libraries(AIGame PUBLIC ConsoleCraftEngine ncurses) 23 | -------------------------------------------------------------------------------- /AIGame/SceneData/EntryScene.scene: -------------------------------------------------------------------------------- 1 | [] -------------------------------------------------------------------------------- /AIGame/Source/AIGameEntryPoint.cpp: -------------------------------------------------------------------------------- 1 | #include "Scenes/EntryScene.h" 2 | #include "Core/EntryPoint.h" 3 | #include 4 | class AIGame 5 | { 6 | public: 7 | ~AIGame() 8 | { 9 | }; 10 | Engine engine; 11 | void StartGame() 12 | { 13 | engine.scenes.push_back(new EntryScene()); 14 | engine.StartGame(); 15 | } 16 | void Clean() 17 | { 18 | engine.Clean(); 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | AIGame *game = new AIGame(); 25 | game->StartGame(); 26 | game->Clean(); 27 | delete game; 28 | system("pause"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /AIGame/Source/GameObjects/Brick.cpp: -------------------------------------------------------------------------------- 1 | #include "Brick.h" 2 | void Brick::Init() 3 | { 4 | sprite = {{2,2,2,2}, 5 | {2,2,2,2}, 6 | {2,2,2,2}, 7 | {2,2,2,2}, 8 | {2,2,2,2}}; 9 | symbol = "#"; 10 | transform.Size = Vector2(4, 6); 11 | } 12 | -------------------------------------------------------------------------------- /AIGame/Source/GameObjects/Brick.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | 4 | class Brick : public GameObject 5 | { 6 | public: 7 | Brick(class Scene& scene) : GameObject("Brick", scene) 8 | { 9 | 10 | } 11 | void Init() override; 12 | }; -------------------------------------------------------------------------------- /AIGame/Source/GameObjects/Enemy.cpp: -------------------------------------------------------------------------------- 1 | #include "Enemy.h" 2 | #include "Core/ParticleSystem/ParticleSource.h" 3 | #include "Core/Component/AI/AIMovement.h" 4 | #include "Core/Scene.h" 5 | void Enemy::Init() 6 | { 7 | { 8 | 9 | sprite = { 10 | {4, 4, 4, 4}, 11 | {4, 4, 4, 4}, 12 | {4, 0, 0, 4}}; 13 | } 14 | particleSource = new ParticleSource(*this); 15 | auto aiMovement = new AIMovement(*this); 16 | AddComponent(particleSource); 17 | AddComponent(aiMovement); 18 | aiMovement->SetTargetTransform(&GetCurrentScene().FindGameObject("PlayerShip")->transform); 19 | isNavIgnore = true; 20 | transform.Size = Vector2(4, 4); 21 | } 22 | 23 | void Enemy::Update(float deltaTime) 24 | { 25 | // transform.MovePosition(-1 * deltaTime, 0); 26 | } 27 | 28 | void Enemy::OnCollided(GameObject &other) 29 | { 30 | 31 | if (other.name == "Bullet") 32 | { 33 | // auto enemyKilledEvent = Event(EventType::OnEnemyKilled); 34 | // EventDispatcher::CallEvent(enemyKilledEvent); 35 | // particleSource->EmitParticle(6, ENEMYTYPEPARTICLE); 36 | // Destroy(); 37 | } 38 | } 39 | 40 | void Enemy::OnCollidedBorder(int border) 41 | { 42 | // Destroy(); 43 | } 44 | -------------------------------------------------------------------------------- /AIGame/Source/GameObjects/Enemy.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | #include "Core/EventDispatcher.h" 4 | #include "Core/Event.h" 5 | class Enemy : public GameObject 6 | { 7 | public: 8 | Enemy(class Scene& scene) : GameObject("Enemy", scene) 9 | { 10 | symbol = '\xDB'; 11 | 12 | } 13 | void Init() override; 14 | void Update(float deltaTime) override; 15 | 16 | virtual void OnCollided(GameObject& other) override; 17 | virtual void OnCollidedBorder(int border) override; 18 | private: 19 | class ParticleSource* particleSource; 20 | }; 21 | 22 | -------------------------------------------------------------------------------- /AIGame/Source/GameObjects/PlayerShip.cpp: -------------------------------------------------------------------------------- 1 | #include "PlayerShip.h" 2 | #include "Core/Component/PlayerController.h" 3 | #include "Core/Input.h" 4 | #include "Core/Scene.h" 5 | #include "Core/EventDispatcher.h" 6 | #include "Core/ParticleSystem/ParticleSource.h" 7 | #include "Core/Renderer.h" 8 | #include 9 | 10 | PlayerShip::~PlayerShip() 11 | { 12 | EventDispatcher::RemoveListener(OnEventFunction); 13 | } 14 | 15 | void PlayerShip::Init() 16 | { 17 | 18 | //scoreUIData.position = Vector2(0, 29); 19 | //scoreUIData.text = "Score: 0"; 20 | //scoreUIDataPtr = std::make_shared(scoreUIData); 21 | //scene.uiHandler->AddString(scoreUIDataPtr); 22 | 23 | AddComponent(new PlayerController(*this, 0)); 24 | sprite = 25 | { 26 | {2,2,0,2}, 27 | {1,1,1,1}, 28 | {1,1,1,1}, 29 | {2,2,0,2} 30 | }; 31 | isNavIgnore = true; 32 | transform.Size = Vector2(4, 4); 33 | 34 | auto inputEvent = std::bind(&PlayerShip::Fire, this, std::placeholders::_1); 35 | Input::AddListener(inputEvent); 36 | 37 | OnEventFunction = std::bind(&PlayerShip::OnEvent, this, std::placeholders::_1); 38 | EventDispatcher::AddListener(OnEventFunction); 39 | 40 | particleSource = new ParticleSource(*this); 41 | AddComponent(particleSource); 42 | } 43 | 44 | void PlayerShip::Update(float deltaTime) 45 | { 46 | 47 | } 48 | 49 | void PlayerShip::OnCollided(GameObject& other) 50 | { 51 | if (other.name == "Enemy") 52 | { 53 | // std::cout << "GAME OVER!" << '\n'; 54 | // GetCurrentScene().hasGameOver = true; 55 | } 56 | } 57 | 58 | void PlayerShip::Fire(int keyDown) 59 | { 60 | if (keyDown == SPACEBAR) 61 | { 62 | //sprite = Renderer::RotateSprite(sprite); 63 | //GetCurrentScene().AddGameObject(new Bullet(GetCurrentScene()), transform.Position); 64 | 65 | } 66 | } 67 | 68 | void PlayerShip::OnEvent(Event& event) 69 | { 70 | switch (event.GetEventType()) 71 | { 72 | case EventType::OnEnemyKilled: 73 | score++; 74 | GetCurrentScene().camera->StartShake(0.25f); 75 | //scoreUIDataPtr->text = "Score: " + std::to_string(score); 76 | //UIHandler::uiText = "Score: " + std::to_string(score); 77 | break; 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /AIGame/Source/GameObjects/PlayerShip.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | #include "Core/Event.h" 4 | #include 5 | #include "Core/UIHandler.h" 6 | class PlayerShip : public GameObject 7 | { 8 | public: 9 | PlayerShip(class Scene &scene) : GameObject("PlayerShip", scene) 10 | { 11 | symbol = '\xDB'; 12 | } 13 | ~PlayerShip(); 14 | void Init() override; 15 | void Update(float deltaTime) override; 16 | void OnCollided(GameObject &other) override; 17 | 18 | private: 19 | UIData scoreUIData; 20 | std::shared_ptr scoreUIDataPtr; 21 | void Fire(int keyDown); 22 | void OnEvent(Event &event); 23 | std::function OnEventFunction; 24 | class ParticleSource *particleSource; 25 | int score = 0; 26 | }; 27 | -------------------------------------------------------------------------------- /AIGame/Source/Scenes/EntryScene.cpp: -------------------------------------------------------------------------------- 1 | #include "EntryScene.h" 2 | #include "../GameObjects/PlayerShip.h" 3 | #include "../GameObjects/Enemy.h" 4 | #include "../GameObjects/Brick.h" 5 | 6 | #include "Core/Input.h" 7 | EntryScene:: 8 | ~EntryScene() 9 | { 10 | } 11 | void EntryScene::Init() 12 | { 13 | Scene::Init(); 14 | 15 | auto keyEvent = BIND_EVENT_FN(EntryScene::OnInput); 16 | Input::AddListener(keyEvent); 17 | } 18 | void EntryScene::OnInput(int input) 19 | { 20 | if (std::tolower(input) == 'd') 21 | { 22 | // camera->MoveCamera(Vector2(1, 0)); 23 | } 24 | } 25 | void EntryScene::Start() 26 | { 27 | Scene::Start(); 28 | playerShip = new PlayerShip(*this); 29 | enemy = new Enemy(*this); 30 | AddGameObject(playerShip, Vector2(15, 5)); 31 | AddGameObject(enemy, Vector2(5, 12)); 32 | AddGameObject(new Brick(*this), Vector2(5, 5)); 33 | AddGameObject(new Brick(*this), Vector2(12, 12)); 34 | AddGameObject(new Brick(*this), Vector2(17, 5)); 35 | AddGameObject(new Brick(*this), Vector2(23, 12)); 36 | AddGameObject(new Brick(*this), Vector2(28, 5)); 37 | 38 | m_Linedrawer = new LineDrawer(*this); 39 | 40 | m_Linedrawer->CreateLineParticles(100, 1); 41 | } 42 | 43 | void EntryScene::Update(float deltaTime) 44 | { 45 | Scene::Update(deltaTime); 46 | m_Linedrawer->ResetDrawingParticleIndex(); 47 | m_LastTimeSinceCameraMove += deltaTime; 48 | if (m_LastTimeSinceCameraMove > 0.5f) 49 | { 50 | // camera->MoveCameraDown(); 51 | m_LastTimeSinceCameraMove = 0; 52 | } 53 | PathNode start(enemy->transform.GetCenterPosition().X, enemy->transform.GetCenterPosition().Y); 54 | PathNode goal(playerShip->transform.GetCenterPosition().X, playerShip->transform.GetCenterPosition().Y); 55 | std::vector path = pathfinder->FindPath(start, goal); 56 | if (!path.empty()) 57 | { 58 | 59 | for (int i = 0; i < path.size() - 1; i++) 60 | { 61 | Vector2 posStart = Vector2(path[i].x, path[i].y); 62 | Vector2 posEnd = Vector2(path[i + 1].x, path[i + 1].y); 63 | 64 | m_Linedrawer->DrawLine(posStart, posEnd); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /AIGame/Source/Scenes/EntryScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/Scene.h" 3 | 4 | #include 5 | #include "Core/LineDrawer.h" 6 | #include 7 | #include "Core/Component/AI/Pathfinder.h" 8 | 9 | class EntryScene : public Scene 10 | { 11 | public: 12 | ~EntryScene(); 13 | 14 | void Init() override; 15 | void Start() override; 16 | void Update(float deltaTime) override; 17 | 18 | 19 | private: 20 | void OnInput(int input); 21 | float m_LastTimeSinceCameraMove = 0; 22 | LineDrawer* m_Linedrawer; 23 | GameObject* playerShip; 24 | GameObject* enemy; 25 | }; 26 | 27 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.12) 4 | project(ConsoleCraftEngine) 5 | 6 | # Source files 7 | file(GLOB_RECURSE SOURCES 8 | Source/*.cpp 9 | ) 10 | 11 | # Header files 12 | file(GLOB_RECURSE HEADERS 13 | Source/*.h 14 | ) 15 | 16 | # Library 17 | add_library(ConsoleCraftEngine SHARED ${SOURCES} ${HEADERS}) 18 | 19 | # Include directory 20 | target_include_directories(ConsoleCraftEngine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Source) 21 | 22 | -------------------------------------------------------------------------------- /CraftBreaker/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ConsoleCraftEngine/CraftBreaker/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.12) 4 | project(CraftBreaker) 5 | 6 | 7 | 8 | # Source files 9 | file(GLOB_RECURSE ENGINE_SOURCES 10 | ../Source/*.cpp 11 | ) 12 | 13 | # Header files 14 | file(GLOB_RECURSE ENGINE_HEADERS 15 | ../Source/*.h 16 | ) 17 | add_library(ConsoleCraftEngine SHARED ${ENGINE_SOURCES} ) 18 | 19 | 20 | file(GLOB_RECURSE GAME_SOURCES Source/*.cpp) 21 | file(GLOB_RECURSE GAME_HEADERS Source/*.h) 22 | # Executable 23 | link_directories(${CMAKE_CURRENT_SOURCE_DIR}) 24 | add_executable(CraftBreaker ${GAME_SOURCES} ) 25 | 26 | # Include directory 27 | target_include_directories(CraftBreaker PUBLIC 28 | #${CMAKE_CURRENT_SOURCE_DIR}/Source #TODO this is not necessary 29 | ${CMAKE_CURRENT_SOURCE_DIR}/../../ConsoleCraftEngine/Source ) 30 | 31 | # Link with ConsoleCraftEngine library 32 | target_link_libraries(CraftBreaker PUBLIC ConsoleCraftEngine ncurses) 33 | 34 | # Include directory 35 | #target_include_directories(CraftBreaker PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Source) 36 | 37 | -------------------------------------------------------------------------------- /CraftBreaker/Source/BreakerGameEntry.cpp: -------------------------------------------------------------------------------- 1 | #include "Scenes/BreakerScene.h" 2 | #include "Core/EntryPoint.h" 3 | #include 4 | class BreakerGame 5 | { 6 | public: 7 | ~BreakerGame() 8 | { 9 | } 10 | Engine engine; 11 | void StartGame() 12 | { 13 | engine.scenes.push_back(new BreakerScene()); 14 | engine.StartGame(); 15 | } 16 | void Clean() 17 | { 18 | engine.Clean(); 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | BreakerGame *game = new BreakerGame(); 25 | game->StartGame(); 26 | game->Clean(); 27 | delete game; 28 | system("pause"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /CraftBreaker/Source/GameObjects/Ball.cpp: -------------------------------------------------------------------------------- 1 | #include "Ball.h" 2 | 3 | void Ball::Update(float deltaTime) 4 | { 5 | collideTimer += deltaTime; 6 | transform.MovePosition(velocity.X * deltaTime, velocity.Y * deltaTime); 7 | if (transform.Position.X >= (SCREENWIDTH / 2.5f) - 1 || transform.Position.X <= 1) 8 | { 9 | velocity.X *= -1; 10 | } 11 | if (transform.Position.Y >= SCREENHEIGHT -1|| transform.Position.Y <= 1) 12 | { 13 | velocity.Y *= -1; 14 | } 15 | 16 | } 17 | 18 | void Ball::OnCollided(GameObject& other) 19 | { 20 | if(other.name == "Particle") return; 21 | if(collideTimer < 0.5f) 22 | { 23 | return; 24 | } 25 | collideTimer = 0; 26 | if(other.name == "Breaker") 27 | { 28 | if(other.transform.Position.Y - transform.Position.Y > 0) 29 | { 30 | if(other.transform.Position.X + other.GetWidth() / 2 - transform.Position.X > 0) //ball is on the left 31 | { 32 | 33 | velocity.X -= 5; 34 | velocity.Y *= -1; 35 | return; 36 | } 37 | else 38 | { 39 | velocity.X += 5; 40 | velocity.Y *= -1; 41 | return; 42 | } 43 | } 44 | } 45 | else 46 | //Destroy(); 47 | { 48 | velocity.X *= -1; 49 | velocity.Y *= -1; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /CraftBreaker/Source/GameObjects/Ball.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | 4 | class Ball : public GameObject 5 | { 6 | public: 7 | Ball(class Scene& scene) : GameObject("Ball", scene) 8 | { 9 | 10 | } 11 | 12 | 13 | void Init() override 14 | { 15 | symbol = '0'; 16 | sprite = { {5}}; 17 | } 18 | void Update(float deltaTime) override; 19 | virtual void OnCollided(GameObject& other) override; 20 | 21 | friend std::ostream& operator<<(std::ostream& os, const Ball& ball) 22 | { 23 | os << "Ball position: " << ball.transform << "\n"; 24 | return os; 25 | } 26 | private: 27 | float collideTimer = 0; 28 | Vector2 velocity = Vector2(8.f, 15.f); 29 | 30 | }; -------------------------------------------------------------------------------- /CraftBreaker/Source/GameObjects/Breaker.cpp: -------------------------------------------------------------------------------- 1 | #include "Breaker.h" 2 | #include "Core/Component/PlayerController.h" 3 | void Breaker::Init() 4 | { 5 | AddComponent(new PlayerController(*this, 0)); 6 | sprite = { 7 | {3,3,3,3,3,3} 8 | }; 9 | symbol = '#'; 10 | } -------------------------------------------------------------------------------- /CraftBreaker/Source/GameObjects/Breaker.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | 4 | class Breaker : public GameObject 5 | { 6 | public: 7 | Breaker(class Scene& scene) : GameObject("Breaker", scene) 8 | { 9 | 10 | } 11 | void Init() override; 12 | 13 | }; -------------------------------------------------------------------------------- /CraftBreaker/Source/GameObjects/Brick.cpp: -------------------------------------------------------------------------------- 1 | #include "Brick.h" 2 | #include "Core/Component/DestructionEffectComponent.h" 3 | void Brick::Init() 4 | { 5 | destructionEffectComponent = new DestructionEffectComponent(*this); 6 | AddComponent(destructionEffectComponent); 7 | sprite = {{2,2,2,2}, 8 | {2,2,2,2}, 9 | {2,2,2,2}}; 10 | symbol = "#"; 11 | } 12 | 13 | void Brick::OnCollided(const GameObject &other) 14 | { 15 | if(other.name == "Ball") 16 | { 17 | destructionEffectComponent->StartEffect(); 18 | Destroy(); 19 | } 20 | } -------------------------------------------------------------------------------- /CraftBreaker/Source/GameObjects/Brick.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | 4 | class Brick : public GameObject 5 | { 6 | public: 7 | Brick(class Scene& scene) : GameObject("Brick", scene) 8 | { 9 | 10 | } 11 | void Init() override; 12 | void OnCollided(GameObject& other); 13 | private: 14 | class DestructionEffectComponent* destructionEffectComponent; 15 | }; -------------------------------------------------------------------------------- /CraftBreaker/Source/Scenes/BreakerScene.cpp: -------------------------------------------------------------------------------- 1 | #include "BreakerScene.h" 2 | #include "../GameObjects/Breaker.h" 3 | #include "../GameObjects/Ball.h" 4 | #include "../GameObjects/Brick.h" 5 | #include 6 | #include "Core/Input.h" 7 | #include "Core/UIHandler.h" 8 | #include "CoreStructs/Vector.h" 9 | void BreakerScene::Init() 10 | { 11 | // scoreUIData.position = Vector2(10, 10); 12 | // scoreUIData.text = "Score: "; 13 | // uiHandler->AddString(scoreUIData); 14 | 15 | int offsetX = 5; 16 | int offsetY = 2; 17 | int brickHeight = 5; 18 | int brickWidth = 5; 19 | AddGameObject(new Breaker(*this), Vector2(10, SCREENHEIGHT - 2)); 20 | AddGameObject(new Ball(*this), Vector2(10, 12)); 21 | for (int j = 0; j < 2; j++) 22 | for (int i = 0; i < 6; i++) 23 | { 24 | AddGameObject(new Brick(*this), Vector2(offsetX + i * brickWidth, offsetY + j * brickWidth)); 25 | } 26 | // camera->isMoving = true; 27 | camera->MoveCamera(Vector2(4, 0)); 28 | 29 | 30 | auto event = std::bind(&BreakerScene::MoveCamera, this, std::placeholders::_1); 31 | Input::AddListener(event); 32 | } 33 | 34 | void BreakerScene::Update(float deltaTime) 35 | { 36 | Scene::Update(deltaTime); 37 | } 38 | 39 | void BreakerScene::MoveCamera(int input) 40 | { 41 | if (tolower(Input::GetKeyDown()) == 'k') 42 | { 43 | camera->MoveCamera(Vector2(50, 0)); 44 | } 45 | if (tolower(Input::GetKeyDown()) == 'h') 46 | { 47 | camera->MoveCamera(Vector2(-50, 0)); 48 | } 49 | if (tolower(Input::GetKeyDown()) == 'j') 50 | { 51 | camera->MoveCamera(Vector2(0, 50)); 52 | } 53 | if (tolower(Input::GetKeyDown()) == 'u') 54 | { 55 | camera->MoveCamera(Vector2(0, -50)); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /CraftBreaker/Source/Scenes/BreakerScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/Scene.h" 3 | #include "Core/UIHandler.h" 4 | class BreakerScene : public Scene 5 | { 6 | public: 7 | void Init() override; 8 | void Update(float deltaTime) override; 9 | private: 10 | UIData scoreUIData; 11 | void MoveCamera(int input); 12 | }; -------------------------------------------------------------------------------- /CraftCity/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ConsoleCraftEngine/CraftCity/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.12) 4 | project(CraftCity) 5 | set(CMAKE_BUILD_TYPE Debug) 6 | 7 | 8 | # Source files 9 | file(GLOB_RECURSE ENGINE_SOURCES 10 | ../Source/*.cpp 11 | ) 12 | 13 | # Header files 14 | file(GLOB_RECURSE ENGINE_HEADERS 15 | ../Source/*.h 16 | ) 17 | add_library(ConsoleCraftEngine SHARED ${ENGINE_SOURCES} ) 18 | 19 | 20 | file(GLOB_RECURSE GAME_SOURCES Source/*.cpp) 21 | file(GLOB_RECURSE GAME_HEADERS Source/*.h) 22 | # Executable 23 | link_directories(${CMAKE_CURRENT_SOURCE_DIR}) 24 | add_executable(CraftCity ${GAME_SOURCES} ${GAME_HEADERS}) 25 | 26 | # Include directory 27 | target_include_directories(CraftCity PUBLIC 28 | ${CMAKE_CURRENT_SOURCE_DIR}/Source #TODO this is not necessary 29 | ../Source ) 30 | 31 | # Link with ConsoleCraftEngine library 32 | target_link_libraries(CraftCity PUBLIC ConsoleCraftEngine ncurses) 33 | 34 | # Include directory 35 | #target_include_directories(CraftCity PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Source) 36 | 37 | -------------------------------------------------------------------------------- /CraftCity/Source/BuildingSystem/BuildHandler.cpp: -------------------------------------------------------------------------------- 1 | #include "BuildHandler.h" 2 | #include "BuildingMenu.h" 3 | #include "Core/Scene.h" 4 | #include "CoreStructs/Vector.h" 5 | #include "../GameObjects/Buildings/House.h" 6 | #include "../GameObjects/Buildings/Road.h" 7 | #include "../GameObjects/Buildings/IndustrialBuilding.h" 8 | #include "../CityStatics/PlayerVault.h" 9 | #include 10 | BuildHandler::BuildHandler(const BuildingMenu &buildingMenu, Scene &scene, PlayerVault *playerVault) : buildingMenu(buildingMenu), scene(scene), playerVault(playerVault) 11 | { 12 | } 13 | 14 | void BuildHandler::Build(Vector2 position) 15 | { 16 | if (buildingMenu.IsActive) 17 | { 18 | Buildable *buildable = nullptr; 19 | switch (buildingMenu.GetCurrentBuildingType()) 20 | { 21 | case BuildingType::House: 22 | buildable = new House(scene); 23 | break; 24 | case BuildingType::Road: 25 | buildable = new Road(scene); 26 | break; 27 | case BuildingType::Industrial: 28 | buildable = new IndustrialBuilding(scene); 29 | break; 30 | } 31 | if (buildable != nullptr) 32 | { 33 | scene.AddGameObject(buildable, position); 34 | playerVault->SpendMoney(buildable->Price); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CraftCity/Source/BuildingSystem/BuildHandler.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | class BuildHandler 3 | { 4 | public: 5 | BuildHandler(const class BuildingMenu& buldingMenu, class Scene& scene, class PlayerVault* playerVault); 6 | ~BuildHandler() 7 | { 8 | 9 | } 10 | void Build(struct Vector2 position); 11 | private: 12 | class PlayerVault* playerVault; 13 | const BuildingMenu& buildingMenu; 14 | Scene& scene; 15 | }; -------------------------------------------------------------------------------- /CraftCity/Source/BuildingSystem/BuildingMenu.cpp: -------------------------------------------------------------------------------- 1 | #include "BuildingMenu.h" 2 | #include "Core/Input.h" 3 | #include "../GameObjects/SelectCursor.h" 4 | #include 5 | BuildingMenu::BuildingMenu(UIHandler *uiHandler, SelectCursor *selectCursor) : uiHandler(uiHandler), selectCursor(selectCursor) 6 | { 7 | auto keyEvent = BIND_EVENT_FN(BuildingMenu::OnInput); 8 | Input::AddListener(keyEvent); 9 | InitPanels(); 10 | ShowBuildingPanel(false); 11 | } 12 | 13 | void BuildingMenu::ShowBuildingPanel(bool isActive) 14 | { 15 | IsActive = isActive; 16 | buildingPanel->SetActive(isActive); 17 | if (!isActive) 18 | { 19 | infoPanel->SetActive(true); 20 | infoPanel->ChangeText(0, "b to open building menu"); 21 | } 22 | 23 | } 24 | 25 | BuildingType BuildingMenu::GetCurrentBuildingType() const 26 | { 27 | return buildingType; 28 | } 29 | 30 | void BuildingMenu::OnInput(int input) 31 | { 32 | switch (input) 33 | { 34 | case 'b': 35 | ShowBuildingPanel(!buildingPanel->isActive); 36 | infoPanel->ChangeText(0, "Select building type"); 37 | break; 38 | 39 | case '1': 40 | if (buildingPanel->isActive) 41 | { 42 | buildingType = BuildingType::Road; 43 | selectCursor->SetSize(Vector2(2, 2), '='); 44 | infoPanel->ChangeText(0, ToString(buildingType)); 45 | break; 46 | } 47 | case '2': 48 | 49 | if (buildingPanel->isActive) 50 | { 51 | buildingType = BuildingType::House; 52 | selectCursor->SetSize(Vector2(4, 5), '#'); 53 | infoPanel->ChangeText(0, ToString(buildingType)); 54 | break; 55 | } 56 | case '3': 57 | 58 | if (buildingPanel->isActive) 59 | { 60 | buildingType = BuildingType::Industrial; 61 | selectCursor->SetSize(Vector2(4, 4), 'X'); 62 | infoPanel->ChangeText(0, ToString(buildingType)); 63 | break; 64 | } 65 | } 66 | } 67 | 68 | void BuildingMenu::InitPanels() 69 | { 70 | // info panel 71 | infoPanel = std::make_shared(); 72 | infoPanel->AddString(Vector2(SCREENWIDTH / 2, SCREENHEIGHT - 5), ""); 73 | // building panel 74 | buildingPanel = std::make_shared(); 75 | buildingPanel->isActive = false; 76 | buildingPanel->AddString(Vector2(15, SCREENHEIGHT - 5), "1. = Road"); 77 | buildingPanel->AddString(Vector2(15, SCREENHEIGHT - 4), "2. # House"); 78 | buildingPanel->AddString(Vector2(15, SCREENHEIGHT - 3), "3. @ Electricity"); 79 | if (uiHandler != nullptr) 80 | { 81 | uiHandler->AddPanel(buildingPanel); 82 | uiHandler->AddPanel(infoPanel); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /CraftCity/Source/BuildingSystem/BuildingMenu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/UIHandler.h" 3 | #include 4 | enum class BuildingType 5 | { 6 | Road = 0, 7 | House = 1, 8 | Industrial = 2 9 | 10 | }; 11 | inline const char *ToString(BuildingType bt) 12 | { 13 | switch (bt) 14 | { 15 | case BuildingType::Road: 16 | return "Road is selected"; 17 | case BuildingType::House: 18 | return "House is selected"; 19 | case BuildingType::Industrial: 20 | return "Industrial is selected"; 21 | default: 22 | return "[Unknown building type]"; 23 | } 24 | } 25 | class BuildingMenu 26 | { 27 | public: 28 | BuildingMenu(class UIHandler *uiHandler, class SelectCursor* selectCursor); 29 | 30 | void ShowBuildingPanel(bool isActive); 31 | 32 | BuildingType GetCurrentBuildingType() const; 33 | bool IsActive = false; 34 | 35 | private: 36 | UIHandler *uiHandler; 37 | std::shared_ptr buildingPanel; 38 | std::shared_ptr infoPanel; 39 | 40 | BuildingType buildingType; 41 | SelectCursor* selectCursor; 42 | 43 | void OnInput(int input); 44 | void InitPanels(); 45 | }; 46 | -------------------------------------------------------------------------------- /CraftCity/Source/BuildingSystem/DemolishHandler.cpp: -------------------------------------------------------------------------------- 1 | #include "DemolishHandler.h" 2 | #include "../GameObjects/Buildings/Buildable.h" 3 | #include "../CityStatics/CityStatics.h" 4 | 5 | void DemolishHandler::Demolish(GameObject &buildable) 6 | { 7 | if (auto building = static_cast(&buildable)) 8 | { 9 | 10 | building->IsDemolishFlag = true; 11 | } 12 | } -------------------------------------------------------------------------------- /CraftCity/Source/BuildingSystem/DemolishHandler.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class DemolishHandler 4 | { 5 | public: 6 | DemolishHandler(class CityStatics& CityStatics) : cityStatics(cityStatics) 7 | { 8 | 9 | } 10 | void Demolish(class GameObject& gameObject); 11 | 12 | private: 13 | CityStatics& cityStatics; 14 | }; -------------------------------------------------------------------------------- /CraftCity/Source/CityStatics/CityStatics.cpp: -------------------------------------------------------------------------------- 1 | #include "CityStatics.h" 2 | #include "PlayerVault.h" 3 | #include "../GameObjects/Buildings/Buildable.h" 4 | #include 5 | CityStatics::CityStatics(PlayerVault *playerVault) : playerVault(playerVault) 6 | { 7 | } 8 | 9 | void CityStatics::Update(float deltaTime) 10 | { 11 | timeElapsed += deltaTime; 12 | periodicTimePassed += deltaTime; 13 | if (periodicTimePassed >= periodicTime) 14 | { 15 | periodicTimePassed = 0; 16 | OnPeriodicTimePass(); 17 | } 18 | for (auto &building : buildings) 19 | { 20 | if (building->IsDemolishFlag) 21 | { 22 | RemoveBuildable(building); 23 | 24 | } 25 | } 26 | } 27 | 28 | void CityStatics::AddBuildable(Buildable *building) 29 | { 30 | buildings.push_back(building); 31 | } 32 | 33 | void CityStatics::RemoveBuildable(Buildable *buildable) 34 | { 35 | auto it = std::find(buildings.begin(), buildings.end(), buildable); 36 | if (it != buildings.end()) 37 | { 38 | buildings.erase(it); 39 | } 40 | buildable->Destroy(); 41 | } 42 | 43 | void CityStatics::OnPeriodicTimePass() 44 | { 45 | tickPassed += 1; 46 | 47 | bool hasAddedPerson = false; 48 | bool hasRemovedPerson = false; 49 | for (int i = 0; i < buildings.size(); i++) 50 | { 51 | buildings[i]->OnPeriodicTick(tickPassed); 52 | playerVault->EarnMoney(buildings[i]->Income); 53 | if (buildings[i]->AddPerson() && !hasAddedPerson) 54 | { 55 | // TODO: if total people is less than people in city 56 | TotalPeople++; 57 | hasAddedPerson = true; 58 | } 59 | // if(buildings[i]->RemovePerson() && !hasAddedPerson) 60 | //{ 61 | // //TODO: if total people is more than people in city 62 | // TotalPeople--; 63 | // hasRemovedPerson = true; 64 | // } 65 | } 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /CraftCity/Source/CityStatics/CityStatics.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | class CityStatics 4 | { 5 | public: 6 | CityStatics(class PlayerVault* playerVault); 7 | int AverageRating = 0; 8 | int TotalIncome = 0; 9 | int TotalPeople = 0; 10 | 11 | void Update(float deltaTime); 12 | void AddBuildable(class Buildable* buildable); 13 | void RemoveBuildable(Buildable* buildable); 14 | private: 15 | std::vector buildings; 16 | PlayerVault* playerVault; 17 | 18 | void OnPeriodicTimePass(); 19 | int tickPassed = 0; 20 | double timeElapsed = 0; 21 | float periodicTime = 1; 22 | float periodicTimePassed = 0; 23 | 24 | int houseCount = 0; 25 | int industrialCount = 0; 26 | }; 27 | 28 | -------------------------------------------------------------------------------- /CraftCity/Source/CityStatics/PlayerVault.cpp: -------------------------------------------------------------------------------- 1 | #include "PlayerVault.h" 2 | 3 | int PlayerVault::GetMoneyAmount() const 4 | { 5 | return money; 6 | } 7 | 8 | void PlayerVault::SpendMoney(int moneyAmount) 9 | { 10 | money -= moneyAmount; 11 | } 12 | 13 | void PlayerVault::EarnMoney(int moneyAmount) 14 | { 15 | money += moneyAmount; 16 | } 17 | -------------------------------------------------------------------------------- /CraftCity/Source/CityStatics/PlayerVault.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | class PlayerVault 3 | { 4 | public: 5 | int GetMoneyAmount() const; 6 | void SpendMoney(int moneyAmount); 7 | void EarnMoney(int moneyAmount); 8 | private: 9 | int money = 100; 10 | }; 11 | 12 | -------------------------------------------------------------------------------- /CraftCity/Source/CraftCityGameEntry.cpp: -------------------------------------------------------------------------------- 1 | #include "Scenes/CraftCityScene.h" 2 | #include "Scenes/CraftCityMainMenuScene.h" 3 | #include "Core/EntryPoint.h" 4 | #include 5 | class CraftCity 6 | { 7 | public: 8 | ~CraftCity() 9 | { 10 | }; 11 | Engine engine; 12 | void StartGame() 13 | { 14 | engine.scenes.push_back(new CraftCityScene()); 15 | engine.scenes.push_back(new CraftCityMainMenuScene()); 16 | engine.StartGame(); 17 | } 18 | void Clean() 19 | { 20 | engine.Clean(); 21 | } 22 | }; 23 | 24 | int main() 25 | { 26 | CraftCity* game = new CraftCity(); 27 | game->StartGame(); 28 | game->Clean(); 29 | delete game; 30 | system("pause"); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /CraftCity/Source/GameObjects/Buildings/Buildable.cpp: -------------------------------------------------------------------------------- 1 | #include "Buildable.h" 2 | #include "BuildingTrigger.h" 3 | #include "../../Scenes/CraftCityScene.h" 4 | #include "../../CityStatics/CityStatics.h" 5 | Buildable::Buildable(Scene& scene) : GameObject("Building", scene) 6 | { 7 | 8 | if (auto cityScene = static_cast(&scene)) 9 | { 10 | cityScene->cityStatics->AddBuildable(this); 11 | } 12 | 13 | } 14 | 15 | void Buildable::Init() 16 | { 17 | defaultSprite = sprite; 18 | for (int it = electricity; it != road; it++) 19 | { 20 | OnInfrastructureDisconnected(static_cast(it)); 21 | } 22 | } 23 | 24 | void Buildable::Update(float deltaTime) 25 | { 26 | 27 | } 28 | 29 | void Buildable::OnPeriodicTick(int tickPassed) 30 | { 31 | if (buildingTrigger == nullptr) 32 | return; 33 | 34 | bool hasRoadNow = buildingTrigger->HasRoad; 35 | bool hasElectricityNow = buildingTrigger->HasElectricity; 36 | bool hasWaterNow = buildingTrigger->HasWater; 37 | 38 | if (!HasRoad && hasRoadNow) //if has just connected road 39 | OnInfrastructureConnected(InfrastructureType::road); 40 | 41 | 42 | if (HasRoad && !hasRoadNow) 43 | OnInfrastructureDisconnected(InfrastructureType::road); 44 | 45 | if (!HasElectricity && hasElectricityNow) 46 | OnInfrastructureConnected(InfrastructureType::electricity); 47 | 48 | if (HasElectricity && !hasElectricityNow) 49 | OnInfrastructureDisconnected(InfrastructureType::electricity); 50 | 51 | HasRoad = hasRoadNow; 52 | } 53 | -------------------------------------------------------------------------------- /CraftCity/Source/GameObjects/Buildings/Buildable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | 4 | enum InfrastructureType 5 | { 6 | electricity, 7 | water, 8 | road 9 | }; 10 | class Buildable : public GameObject 11 | { 12 | public: 13 | Buildable(class Scene& scene); 14 | void Init() override; 15 | void Update(float deltaTime) override; 16 | virtual bool AddPerson() {return false;}; 17 | virtual bool RemovePerson() {return false;}; 18 | virtual void OnPeriodicTick(int tickPassed); 19 | 20 | int Rating = 0; 21 | int Income = 0; 22 | int MaxPeople = 0; 23 | int CurrentPeople = 0; 24 | int Price = 10; 25 | int PeriodicCost = 1; 26 | 27 | bool HasElectricity = false; 28 | bool HasRoad = false; 29 | bool HasWater = false; 30 | bool HasWastePipe = false; 31 | 32 | bool IsDemolishFlag = false; 33 | private: 34 | float elapsedTime = 0; 35 | 36 | protected: 37 | virtual void OnInfrastructureConnected(InfrastructureType infrastructureType) {} 38 | virtual void OnInfrastructureDisconnected(InfrastructureType infrastructureType) {} 39 | 40 | std::vector> defaultSprite; 41 | int progress = 0; 42 | class BuildingTrigger *buildingTrigger = nullptr; 43 | }; -------------------------------------------------------------------------------- /CraftCity/Source/GameObjects/Buildings/BuildingTrigger.cpp: -------------------------------------------------------------------------------- 1 | #include "BuildingTrigger.h" 2 | #include "Road.h" 3 | void BuildingTrigger::OnCollided(GameObject &other) 4 | { 5 | if (other.name == "Road" && other != owner) 6 | { 7 | if (auto otherRoad = dynamic_cast(&other)) 8 | { 9 | if (otherRoad->HasConnectedToMain) 10 | { 11 | 12 | if (auto ownerRoad = static_cast(&owner)) 13 | { 14 | mainConnectionRoad = otherRoad; 15 | ownerRoad->HasConnectedToMain = true; 16 | } 17 | HasRoad = true; 18 | } 19 | } 20 | } 21 | 22 | // TODO: add for electricity and water 23 | } 24 | 25 | void BuildingTrigger::OnCollisionExit(GameObject &other) 26 | { 27 | if (other.name == "Road" && other != owner) 28 | { 29 | std::cout << other.name << '\n'; 30 | if (auto otherRoad = static_cast(&other)) 31 | { 32 | if (mainConnectionRoad == otherRoad) 33 | { 34 | 35 | if (auto ownerRoad = static_cast(&owner)) 36 | { 37 | ownerRoad->HasConnectedToMain = false; 38 | } 39 | HasRoad = false; 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /CraftCity/Source/GameObjects/Buildings/BuildingTrigger.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | class BuildingTrigger : public GameObject 4 | { 5 | public: 6 | BuildingTrigger(class Scene& scene, Vector2 size, GameObject& owner) : GameObject("Trigger", scene), size(size), owner(owner) { 7 | sprite = { {0} }; 8 | } 9 | void OnCollided(GameObject& other) override; 10 | void OnCollisionExit(GameObject& other) override; 11 | 12 | class Road* mainConnectionRoad; 13 | bool HasRoad = false; 14 | bool HasElectricity = false; 15 | bool HasWater = false; 16 | 17 | int GetWidth() const override 18 | { 19 | return size.X; 20 | } 21 | int GetHeight() const override 22 | { 23 | return size.Y; 24 | } 25 | 26 | 27 | private: 28 | Vector2 size; 29 | GameObject& owner; 30 | }; 31 | 32 | -------------------------------------------------------------------------------- /CraftCity/Source/GameObjects/Buildings/House.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Buildable.h" 3 | class House : public Buildable 4 | { 5 | public: 6 | House(class Scene& scene) : Buildable(scene) 7 | { 8 | name = "House"; 9 | Price = 20; 10 | MaxPeople = 4; 11 | CurrentPeople = 0; 12 | Income = 10; 13 | 14 | sprite = { 15 | {GREEN, GREEN, GREEN, GREEN}, 16 | {GREEN, 0, 0, GREEN}, 17 | {GREEN, 0, 0, GREEN}, 18 | {GREEN, 0, 0, GREEN}, 19 | {GREEN, GREEN, GREEN, GREEN} }; 20 | } 21 | 22 | void Init() override; 23 | void OnPeriodicTick(int tickPassed) override; 24 | 25 | bool AddPerson() override; 26 | bool RemovePerson() override; 27 | protected: 28 | void OnInfrastructureConnected(InfrastructureType) override; 29 | void OnInfrastructureDisconnected(InfrastructureType) override; 30 | 31 | private: 32 | void UpdateVisualForPeople(); 33 | void CalculatePotentionMaxPeople(); 34 | int potentialMaxPeople = 0; 35 | 36 | 37 | 38 | 39 | }; -------------------------------------------------------------------------------- /CraftCity/Source/GameObjects/Buildings/IndustrialBuilding.cpp: -------------------------------------------------------------------------------- 1 | #include "IndustrialBuilding.h" 2 | #include "BuildingTrigger.h" 3 | #include "Core/Scene.h" 4 | void IndustrialBuilding::Init() 5 | { 6 | Buildable::Init(); 7 | buildingTrigger = new BuildingTrigger(scene, Vector2(GetWidth() * 2, GetHeight() * 2), *this); 8 | scene.AddGameObject(buildingTrigger, transform.Position - Vector2(GetWidth() / 2, GetHeight() / 2)); 9 | } 10 | 11 | void IndustrialBuilding::OnInfrastructureConnected(InfrastructureType) 12 | { 13 | overrideColor = GREEN; 14 | } 15 | 16 | void IndustrialBuilding::OnInfrastructureDisconnected(InfrastructureType) 17 | { 18 | overrideColor = RED; 19 | } 20 | 21 | void IndustrialBuilding::OnPeriodicTick(int tickPassed) 22 | { 23 | Buildable::OnPeriodicTick(tickPassed); 24 | } 25 | 26 | void IndustrialBuilding::CalculateIncome() 27 | { 28 | Income = 0; 29 | if (HasRoad) 30 | { 31 | Income += 5; 32 | } 33 | if (HasElectricity) 34 | { 35 | Income += 5; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CraftCity/Source/GameObjects/Buildings/IndustrialBuilding.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Buildable.h" 3 | class IndustrialBuilding : public Buildable 4 | { 5 | public: 6 | IndustrialBuilding(class Scene& scene) : Buildable(scene) 7 | { 8 | name = "Industrial"; 9 | sprite = { 10 | {GREEN, GREEN, GREEN, GREEN}, 11 | {GREEN, 0, 0, GREEN}, 12 | {GREEN, 0, 0, GREEN}, 13 | {GREEN, GREEN, GREEN, GREEN} }; 14 | symbol = 'X'; 15 | 16 | Price = 30; 17 | Income = 0; 18 | } 19 | void Init() override; 20 | void OnInfrastructureConnected(InfrastructureType) override; 21 | void OnInfrastructureDisconnected(InfrastructureType) override; 22 | void OnPeriodicTick(int tickPassed) override; 23 | private: 24 | void CalculateIncome(); 25 | 26 | }; 27 | 28 | -------------------------------------------------------------------------------- /CraftCity/Source/GameObjects/Buildings/Road.cpp: -------------------------------------------------------------------------------- 1 | #include "Road.h" 2 | #include "Core/Scene.h" 3 | #include "BuildingTrigger.h" 4 | void Road::Init() 5 | { 6 | Buildable::Init(); 7 | symbol = '='; 8 | buildingTrigger = new BuildingTrigger(scene, Vector2(GetWidth() * 2, GetHeight() * 2), *this); 9 | scene.AddGameObject(buildingTrigger, transform.Position - Vector2(GetWidth() / 2, GetHeight() / 2)); 10 | } 11 | 12 | void Road::OnInfrastructureConnected(InfrastructureType infrastructureType) 13 | { 14 | overrideColor = GREEN; 15 | } 16 | 17 | void Road::OnInfrastructureDisconnected(InfrastructureType infrastructureType) 18 | { 19 | overrideColor = RED; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /CraftCity/Source/GameObjects/Buildings/Road.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Buildable.h" 3 | class Road : public Buildable 4 | { 5 | public: 6 | Road(class Scene& scene) : Buildable(scene) 7 | { 8 | name = "Road"; 9 | sprite = { {1,1}, 10 | {1,1} }; 11 | 12 | } 13 | bool IsMainRoad = false; 14 | bool HasConnectedToMain = false; 15 | void Init() override; 16 | void OnInfrastructureConnected(InfrastructureType infrastructureType) override; 17 | void OnInfrastructureDisconnected(InfrastructureType infrastructureType) override; 18 | 19 | private: 20 | }; 21 | 22 | -------------------------------------------------------------------------------- /CraftCity/Source/GameObjects/SelectCursor.cpp: -------------------------------------------------------------------------------- 1 | #include "SelectCursor.h" 2 | 3 | void SelectCursor::SetSize(Vector2 size, char &&symbol) 4 | { 5 | this->symbol = symbol; 6 | this->size = size; 7 | transform.HasClearedFlag = false; 8 | transform.HasMovedThisFrame = true; 9 | // transform.MovePosition(-1,0); 10 | // transform.MovePosition(1,0); 11 | for (size_t i = 0; i < sprite.size(); i++) 12 | { 13 | for (size_t j = 0; j < sprite[i].size(); j++) 14 | { 15 | sprite[i][j] = 0; // Set all elements to 0 16 | } 17 | } 18 | for (size_t i = 0; i < size.Y; i++) 19 | { 20 | for (size_t j = 0; j < size.X; j++) 21 | { 22 | if (i == 0 || j == size.X - 1 || j == 0 || i == size.Y - 1) 23 | sprite[i][j] = 1; 24 | } 25 | } 26 | 27 | overrideColor = 2; 28 | } 29 | 30 | void SelectCursor::OnCollided(GameObject &other) 31 | { 32 | if (other.name == "Trigger") 33 | return; 34 | collidedObject = &other; 35 | /// collidedObject->overrideColor = 1; 36 | overrideColor = RED; 37 | CanBuild = false; 38 | } 39 | 40 | void SelectCursor::OnCollisionExit(GameObject &other) 41 | { 42 | 43 | if (collidedObject) 44 | { 45 | if (collidedObject == &other || collidedObject->isDestroyedFlag) 46 | { 47 | overrideColor = GREEN; 48 | CanBuild = true; 49 | collidedObject = nullptr; 50 | } 51 | } 52 | } 53 | 54 | void SelectCursor::Update(float deltaTime) 55 | { 56 | } 57 | -------------------------------------------------------------------------------- /CraftCity/Source/GameObjects/SelectCursor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | #include "Core/Component/PlayerController.h" 4 | class SelectCursor : public GameObject 5 | { 6 | public: 7 | SelectCursor(class Scene &scene) : GameObject("SelectCursor", scene) 8 | { 9 | } 10 | ~SelectCursor() 11 | { 12 | } 13 | void Init() override 14 | { 15 | //AddComponent(new PlayerController(*this, 0)); 16 | sprite = { 17 | {0, 0, 0, 0}, 18 | {0, 0, 0, 0}, 19 | {0, 0, 0, 0}, 20 | {0, 0, 0, 0}, 21 | {0, 0, 0, 0}, 22 | }; 23 | } 24 | void SetSize(Vector2 size, char&& symbol); 25 | int GetWidth() const override 26 | { 27 | return size.X; 28 | } 29 | int GetHeight() const override 30 | { 31 | return size.Y; 32 | } 33 | 34 | GameObject* GetCollidedObject() 35 | { 36 | return collidedObject; 37 | } 38 | void ResetCollidedObject() 39 | { 40 | collidedObject = nullptr; 41 | } 42 | void OnCollided(GameObject &other) override; 43 | void OnCollisionExit(GameObject &other) override; 44 | bool CanBuild = true; 45 | private: 46 | GameObject *collidedObject = nullptr; 47 | Vector2 size; 48 | void Update(float deltaTime) override; 49 | float initTime = 1; 50 | }; 51 | -------------------------------------------------------------------------------- /CraftCity/Source/Scenes/CraftCityMainMenuScene.cpp: -------------------------------------------------------------------------------- 1 | #include "CraftCityMainMenuScene.h" 2 | 3 | 4 | void CraftCityMainMenuScene::Init() 5 | { 6 | Scene::Init(); 7 | 8 | } 9 | void CraftCityMainMenuScene::Update(float deltaTime) 10 | { 11 | Scene::Update(deltaTime); 12 | std::cout << "main menu started"; 13 | 14 | } -------------------------------------------------------------------------------- /CraftCity/Source/Scenes/CraftCityMainMenuScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Core/Scene.h" 4 | 5 | 6 | class CraftCityMainMenuScene : public Scene 7 | { 8 | public: 9 | ~CraftCityMainMenuScene() 10 | { 11 | 12 | } 13 | void Init() override; 14 | void Update(float deltaTime) override; 15 | 16 | 17 | }; 18 | 19 | -------------------------------------------------------------------------------- /CraftCity/Source/Scenes/CraftCityScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/Scene.h" 3 | class CraftCityScene : public Scene 4 | { 5 | public: 6 | ~CraftCityScene(); 7 | 8 | void Init() override; 9 | void Update(float deltaTime) override; 10 | 11 | class CityStatics* cityStatics; 12 | private: 13 | 14 | void OnInput(int input); 15 | Vector2 cameraOffset; 16 | class PlayerVault* playerVault; 17 | class BuildingMenu* buildMenu; 18 | class BuildHandler* buildHandler; 19 | class SelectCursor* selectCursor; 20 | class PlayerStatsUI* playerStatsUI; 21 | class DemolishHandler* demolishHandler; 22 | 23 | }; 24 | 25 | -------------------------------------------------------------------------------- /CraftCity/Source/UI/PlayerStatsUI.cpp: -------------------------------------------------------------------------------- 1 | #include "PlayerStatsUI.h" 2 | 3 | #include "CoreStructs/Vector.h" 4 | #include "../CityStatics/PlayerVault.h" 5 | #include "../CityStatics/CityStatics.h" 6 | PlayerStatsUI::PlayerStatsUI(UIHandler *uiHandler, PlayerVault *playerVault, CityStatics* cityStatics) : uiHandler(uiHandler), playerVault(playerVault), cityStatics(cityStatics) 7 | { 8 | playerStatsPanel = std::make_shared(); 9 | playerStatsPanel->AddString(Vector2(SCREENWIDTH - 3, SCREENHEIGHT - 3), "$100"); 10 | playerStatsPanel->AddString(Vector2(SCREENWIDTH - 6, SCREENHEIGHT - 2), "People: "); 11 | uiHandler->AddPanel(playerStatsPanel); 12 | } 13 | 14 | 15 | void PlayerStatsUI::Update(float deltaTime) 16 | { 17 | playerStatsPanel->ChangeText(0, "$" + std::to_string((playerVault->GetMoneyAmount()))); 18 | playerStatsPanel->ChangeText(1, "People: " + std::to_string((cityStatics->TotalPeople))); 19 | } 20 | -------------------------------------------------------------------------------- /CraftCity/Source/UI/PlayerStatsUI.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "Core/UIHandler.h" 4 | class PlayerStatsUI 5 | { 6 | public: 7 | PlayerStatsUI( UIHandler *uiHandler, class PlayerVault *playerVault, class CityStatics* cityStatics); 8 | void Update(float deltaTime); 9 | 10 | private: 11 | UIHandler *uiHandler; 12 | PlayerVault *playerVault; 13 | CityStatics* cityStatics; 14 | 15 | std::shared_ptr playerStatsPanel; 16 | 17 | }; -------------------------------------------------------------------------------- /CraftMatch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ConsoleCraftEngine/CraftShooterGame/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.12) 4 | project(CraftShooterGame) 5 | 6 | set(CMAKE_CXX_STANDARD 17) 7 | 8 | 9 | # Source files 10 | file(GLOB_RECURSE ENGINE_SOURCES 11 | ../Source/*.cpp 12 | ) 13 | 14 | # Header files 15 | file(GLOB_RECURSE ENGINE_HEADERS 16 | ../Source/*.h 17 | ) 18 | add_library(ConsoleCraftEngine SHARED ${ENGINE_SOURCES} ) 19 | 20 | 21 | 22 | 23 | file(GLOB_RECURSE GAME_SOURCES Source/*.cpp) 24 | file(GLOB_RECURSE GAME_HEADERS Source/*.h) 25 | # Executable 26 | link_directories(${CMAKE_CURRENT_SOURCE_DIR}) 27 | add_executable(CraftMatch ${GAME_SOURCES} ${GAME_HEADERS}) 28 | 29 | # Include directory 30 | target_include_directories(CraftMatch PUBLIC 31 | ${CMAKE_CURRENT_SOURCE_DIR}/Source #TODO this is not necessary 32 | ${CMAKE_CURRENT_SOURCE_DIR}/../../ConsoleCraftEngine/Source ) 33 | 34 | # Link with ConsoleCraftEngine library 35 | target_link_libraries(CraftMatch PUBLIC ConsoleCraftEngine ncurses) 36 | 37 | # Include directory 38 | #target_include_directories(CraftMatch PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Source) 39 | 40 | -------------------------------------------------------------------------------- /CraftMatch/Source/CraftMatchGameEntry.cpp: -------------------------------------------------------------------------------- 1 | #include "Scenes/MatchScene.h" 2 | #include "Core/EntryPoint.h" 3 | #include 4 | class CraftMatchGame 5 | { 6 | public: 7 | ~CraftMatchGame() 8 | { 9 | } 10 | Engine engine; 11 | void StartGame() 12 | { 13 | engine.scenes.push_back(new MatchScene()); 14 | engine.StartGame(); 15 | } 16 | void Clean() 17 | { 18 | engine.Clean(); 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | CraftMatchGame *game = new CraftMatchGame(); 25 | game->StartGame(); 26 | game->Clean(); 27 | delete game; 28 | system("pause"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /CraftMatch/Source/GameObjects/ClubItem.cpp: -------------------------------------------------------------------------------- 1 | #include "ClubItem.h" 2 | 3 | void ClubItem::Init() 4 | { 5 | Item::Init(); 6 | //symbol = '\x05'; 7 | overrideColor = 6; 8 | } 9 | -------------------------------------------------------------------------------- /CraftMatch/Source/GameObjects/ClubItem.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Item.h" 3 | class ClubItem : public Item 4 | { 5 | public: 6 | ClubItem(Scene& scene) : Item(scene) { 7 | itemType = CLUB; 8 | } 9 | 10 | void Init() override; 11 | 12 | }; 13 | 14 | -------------------------------------------------------------------------------- /CraftMatch/Source/GameObjects/DiamondItem.cpp: -------------------------------------------------------------------------------- 1 | #include "DiamondItem.h" 2 | 3 | void DiamondItem::Init() 4 | { 5 | Item::Init(); 6 | //symbol = '\x04'; 7 | overrideColor = 5; 8 | } 9 | -------------------------------------------------------------------------------- /CraftMatch/Source/GameObjects/DiamondItem.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Item.h" 3 | 4 | class DiamondItem : public Item 5 | { 6 | public: 7 | DiamondItem(Scene& scene) : Item(scene) { 8 | itemType = DIAMOND; 9 | } 10 | 11 | void Init() override; 12 | }; 13 | 14 | -------------------------------------------------------------------------------- /CraftMatch/Source/GameObjects/GridUnit.cpp: -------------------------------------------------------------------------------- 1 | #include "GridUnit.h" 2 | #include "Core/Scene.h" 3 | void GridUnit::Init() 4 | { 5 | sprite = { {1,1,1,1,1}, 6 | {1,0,0,0,1}, 7 | {1,0,0,0,1}, 8 | {1,1,1,1,1} 9 | }; 10 | overrideColor = 0; 11 | symbol = '\xDB'; 12 | particleSource = new ParticleSource(*this); 13 | AddComponent(particleSource); 14 | 15 | } 16 | 17 | void GridUnit::OnCreatingLine() 18 | { 19 | overrideColor = 4; 20 | } 21 | 22 | void GridUnit::OnSelected() 23 | { 24 | overrideColor = 3; 25 | } 26 | 27 | void GridUnit::OnUnselected() 28 | { 29 | overrideColor = 0; 30 | } 31 | 32 | void GridUnit::OnSelectionBlown() 33 | { 34 | particleSource->EmitParticle(6, ENEMYTYPEPARTICLE); 35 | //GetCurrentScene().camera->StartShake(0.3f); 36 | UnitItem->Destroy(); 37 | UnitItem = nullptr; 38 | } 39 | -------------------------------------------------------------------------------- /CraftMatch/Source/GameObjects/GridUnit.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | #include "Item.h" 4 | #include "Core/ParticleSystem/ParticleSource.h" 5 | class GridUnit : public GameObject 6 | { 7 | public: 8 | GridUnit(Scene& scene) :GameObject("GridObject", scene) 9 | { 10 | 11 | } 12 | void Init() override; 13 | 14 | void OnCreatingLine(); 15 | void OnSelected(); 16 | void OnUnselected(); 17 | void OnSelectionBlown(); 18 | Vector2 GetItemSlot() 19 | { 20 | return Vector2(transform.Position.X + 1, transform.Position.Y + 1); 21 | } 22 | 23 | Item* UnitItem = nullptr; 24 | private: 25 | ParticleSource* particleSource; 26 | }; 27 | -------------------------------------------------------------------------------- /CraftMatch/Source/GameObjects/HeartItem.cpp: -------------------------------------------------------------------------------- 1 | #include "HeartItem.h" 2 | 3 | void HeartItem::Init() 4 | { 5 | Item::Init(); 6 | //symbol = '\x03'; 7 | overrideColor = 4; 8 | 9 | } 10 | -------------------------------------------------------------------------------- /CraftMatch/Source/GameObjects/HeartItem.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Item.h" 3 | 4 | 5 | class HeartItem : public Item 6 | { 7 | public: 8 | HeartItem(Scene& scene) : Item(scene) { 9 | itemType = HEART; 10 | } 11 | 12 | void Init() override; 13 | }; 14 | 15 | 16 | -------------------------------------------------------------------------------- /CraftMatch/Source/GameObjects/Item.cpp: -------------------------------------------------------------------------------- 1 | #include "Item.h" 2 | 3 | void Item::SetTargetPosition(Vector2 targetPosition) 4 | { 5 | this->targetPosition = targetPosition; 6 | } 7 | 8 | void Item::Init() 9 | { 10 | targetPosition = transform.Position; 11 | } 12 | 13 | void Item::Update(float deltaTime) 14 | { 15 | if (targetPosition.ToInt() != transform.Position.ToInt()) 16 | { 17 | //transform.Position = targetPosition; 18 | //transform.MovePosition(0, 15 * deltaTime); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /CraftMatch/Source/GameObjects/Item.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | 4 | const int HEART = 0; 5 | const int CLUB = 1; 6 | const int DIAMOND = 2; 7 | const int SPADES = 3; 8 | class Item : public GameObject 9 | { 10 | public: 11 | Item(Scene& scene) : GameObject("Item", scene) 12 | { 13 | 14 | } 15 | int itemType; 16 | void SetTargetPosition(Vector2 targetPosition); 17 | void Init() override; 18 | void Update(float deltaTime) override; 19 | private: 20 | Vector2 targetPosition; 21 | 22 | }; 23 | 24 | -------------------------------------------------------------------------------- /CraftMatch/Source/GameObjects/SpadesItem.cpp: -------------------------------------------------------------------------------- 1 | #include "SpadesItem.h" 2 | 3 | void SpadesItem::Init() 4 | { 5 | Item::Init(); 6 | //symbol = '\x06'; 7 | overrideColor = 7; 8 | } 9 | -------------------------------------------------------------------------------- /CraftMatch/Source/GameObjects/SpadesItem.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Item.h" 3 | 4 | class SpadesItem : public Item 5 | { 6 | public: 7 | SpadesItem(Scene& scene) : Item(scene) { 8 | itemType = SPADES; 9 | } 10 | 11 | void Init() override; 12 | }; 13 | 14 | -------------------------------------------------------------------------------- /CraftMatch/Source/Grid.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/Scene.h" 3 | const int WIDTH = 10; 4 | const int HEIGHT = 5; 5 | class Grid 6 | { 7 | public: 8 | Grid(Scene &scene) : scene(&scene) {} 9 | void SetGridObject(class GridUnit &gridUnit, int x, int y); 10 | GridUnit *GetGridUnit(int x, int y) const 11 | { 12 | return grid[x][y]; 13 | } 14 | void FillBlanks(); 15 | void RefreshItemRenders(); 16 | void CreateItemsAtStart(); 17 | void CreateRandomItem(int i, int j); 18 | Scene &GetScene() const 19 | { 20 | return *scene; 21 | } 22 | void Update(float deltaTime); 23 | 24 | private: 25 | Scene *scene; 26 | class GridUnit *grid[WIDTH][HEIGHT]; 27 | float timePassed = 0; 28 | bool hasRefreshedAfterBlown = false; 29 | }; 30 | -------------------------------------------------------------------------------- /CraftMatch/Source/Scenes/MatchScene.cpp: -------------------------------------------------------------------------------- 1 | #include "MatchScene.h" 2 | #include "../GameObjects/GridUnit.h" 3 | #include "../Grid.h" 4 | #include "../UnitSelector.h" 5 | #include "../GameObjects/SpadesItem.h" 6 | #include "../GameObjects/HeartItem.h" 7 | #include "../GameObjects/DiamondItem.h" 8 | #include "../GameObjects/ClubItem.h" 9 | MatchScene::~MatchScene() 10 | { 11 | delete grid; 12 | 13 | } 14 | void MatchScene::Init() 15 | { 16 | CreateGrid(WIDTH, HEIGHT); 17 | grid->CreateItemsAtStart(); 18 | } 19 | 20 | void MatchScene::Update(float deltaTime) 21 | { 22 | Scene::Update(deltaTime); 23 | grid->Update(deltaTime); 24 | unitSelector->Update(deltaTime); 25 | } 26 | 27 | void MatchScene::CreateGrid(int width, int height) 28 | { 29 | int gridOffsetX = 15; 30 | int gridOffsetY = 5; 31 | grid = new Grid(*this); 32 | unitSelector = new UnitSelector(*grid); 33 | auto gridUnit = new GridUnit(*this); 34 | 35 | for (int i = 0; i < width; i++) 36 | for (int j = 0; j < height; j++) 37 | { 38 | auto gridUnit = new GridUnit(*this); 39 | AddGameObject(gridUnit, Vector2(5 * i + gridOffsetX, 4 * j + gridOffsetY)); 40 | grid->SetGridObject(*gridUnit, i, j); 41 | } 42 | } 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /CraftMatch/Source/Scenes/MatchScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/Scene.h" 3 | class MatchScene : public Scene 4 | { 5 | public: 6 | ~MatchScene() override; 7 | void Init() override; 8 | void Update(float deltaTime) override; 9 | class Grid* grid; 10 | 11 | private: 12 | void CreateGrid(int width, int height); 13 | class UnitSelector* unitSelector = nullptr; 14 | 15 | }; 16 | 17 | -------------------------------------------------------------------------------- /CraftMatch/Source/UnitSelector.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Grid.h" 3 | #include 4 | #include "Core/UIHandler.h" 5 | #include 6 | class UnitSelector 7 | { 8 | public: 9 | UnitSelector(Grid& grid); 10 | void Update(float deltaTime); 11 | 12 | 13 | private: 14 | int selectedX = 0; 15 | int selectedY = HEIGHT - 1; 16 | int score = 0; 17 | 18 | Grid* grid; 19 | std::deque selectedUnits; 20 | 21 | int firstSelectedItemType = -1; 22 | 23 | bool isSelecting = false; 24 | void OnInput(int input); 25 | void SelectUnit(int x, int y); 26 | void UnselectUnit(int x, int y); 27 | void ClearSelectedUnits(); 28 | 29 | int GetItemType(int x, int y); 30 | 31 | float blowTimer = 0.3f; 32 | bool isBlowing = false; 33 | 34 | UIData scoreUIData; 35 | std::shared_ptr scoreUIDataPtr; 36 | }; 37 | 38 | -------------------------------------------------------------------------------- /CraftPhysicsTest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ConsoleCraftEngine/CraftPhysicsTest/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.12) 4 | project(PhysicsTest) 5 | 6 | set(CMAKE_BUILD_TYPE Debug) 7 | 8 | 9 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 10 | # Source files 11 | file(GLOB_RECURSE ENGINE_SOURCES 12 | ../Source/*.cpp 13 | ) 14 | 15 | # Header files 16 | file(GLOB_RECURSE ENGINE_HEADERS 17 | ../Source/*.h 18 | ) 19 | add_library(ConsoleCraftEngine SHARED ${ENGINE_SOURCES} ) 20 | 21 | 22 | file(GLOB_RECURSE GAME_SOURCES Source/*.cpp) 23 | file(GLOB_RECURSE GAME_HEADERS Source/*.h) 24 | # Executable 25 | link_directories(${CMAKE_CURRENT_SOURCE_DIR}) 26 | add_executable(PhysicsTest ${GAME_SOURCES} ${GAME_HEADERS}) 27 | 28 | # Include directory 29 | target_include_directories(PhysicsTest PUBLIC 30 | ${CMAKE_CURRENT_SOURCE_DIR}/Source #TODO this is not necessary 31 | ${CMAKE_CURRENT_SOURCE_DIR}/../../ConsoleCraftEngine/Source ) 32 | 33 | # Link with ConsoleCraftEngine library 34 | target_link_libraries(PhysicsTest PUBLIC ConsoleCraftEngine ncurses) 35 | # Include directory 36 | #target_include_directories(PhysicsTest PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Source) 37 | 38 | -------------------------------------------------------------------------------- /CraftPhysicsTest/Source/BridgeBuilderGameEntry.cpp: -------------------------------------------------------------------------------- 1 | #include "Scenes/PhysicsTestScene.h" 2 | #include "Core/EntryPoint.h" 3 | #include 4 | class BridgeBuilder 5 | { 6 | public: 7 | ~BridgeBuilder() 8 | { 9 | } 10 | Engine engine; 11 | void StartGame() 12 | { 13 | engine.scenes.push_back(new PhysicsTestScene()); 14 | engine.StartGame(); 15 | } 16 | void Clean() 17 | { 18 | engine.Clean(); 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | BridgeBuilder *game = new BridgeBuilder(); 25 | game->StartGame(); 26 | game->Clean(); 27 | delete game; 28 | system("pause"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /CraftPhysicsTest/Source/Polygons/Car.cpp: -------------------------------------------------------------------------------- 1 | #include "Car.h" 2 | #include "CoreStructs/Vector.h" 3 | #include "Core/Physics/Polygon/PolygonCreator/PolygonCreator.h" 4 | #include "Core/Scene.h" 5 | Car::Car(Scene& scene, PolygonCreator &polygonCreator) : polygonCreator(polygonCreator), scene(scene) 6 | { 7 | } 8 | 9 | void Car::Init(Vector2 position) 10 | { 11 | //Create car body 12 | polygonCreator.StartCreating(position, 3); 13 | polygonCreator.SetPosition(position + Vector2(3, 0)); 14 | polygonCreator.SetPosition(position + Vector2(7, -3)); 15 | polygonCreator.SetPosition(position + Vector2(10, -3)); 16 | polygonCreator.SetPosition(position + Vector2(12, 0)); 17 | polygonCreator.SetPosition(position + Vector2(18, 0)); 18 | polygonCreator.SetPosition(position + Vector2(18, 2)); 19 | polygonCreator.SetPosition(position + Vector2(0, 2)); 20 | auto carBodyPolygon = polygonCreator.ApplyAndGetPolygon(); 21 | 22 | int tyreSize = 3; 23 | int tyreSegment = 6; 24 | //Create left tyre 25 | auto tyreLeft = polygonCreator.CreateCircle(Vector2(position), tyreSize, tyreSegment, 2); 26 | b2RevoluteJointDef leftRevololuteJointDef; 27 | leftRevololuteJointDef.bodyA = carBodyPolygon->GetBody(); 28 | leftRevololuteJointDef.bodyB = tyreLeft->GetBody(); 29 | leftRevololuteJointDef.collideConnected = false; 30 | leftRevololuteJointDef.localAnchorA.Set(5, 2); 31 | //Set motor 32 | leftRevololuteJointDef.enableMotor = true; 33 | leftRevololuteJointDef.maxMotorTorque = 1000; 34 | leftRevololuteJointDef.motorSpeed = 3.14 * 4; 35 | 36 | scene.world->CreateJoint(&leftRevololuteJointDef); 37 | 38 | 39 | //Create right tyre 40 | auto tyreRight = polygonCreator.CreateCircle(Vector2(position), tyreSize, tyreSegment, 2); 41 | b2RevoluteJointDef rightRevololuteJointDef; 42 | rightRevololuteJointDef.bodyA = carBodyPolygon->GetBody(); 43 | rightRevololuteJointDef.bodyB = tyreRight->GetBody(); 44 | rightRevololuteJointDef.collideConnected = false; 45 | rightRevololuteJointDef.localAnchorA.Set(-5, 2); 46 | 47 | //Set motor 48 | rightRevololuteJointDef.enableMotor = true; 49 | rightRevololuteJointDef.maxMotorTorque = 1000; 50 | rightRevololuteJointDef.motorSpeed = 3.14 * 4; 51 | 52 | 53 | scene.world->CreateJoint(&rightRevololuteJointDef); 54 | 55 | } 56 | 57 | Car::~Car() 58 | { 59 | } 60 | -------------------------------------------------------------------------------- /CraftPhysicsTest/Source/Polygons/Car.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | class Car 3 | { 4 | public: 5 | Car(class Scene& scene, class PolygonCreator& polygonCreator); 6 | ~Car(); 7 | void Init(class Vector2 position); 8 | private: 9 | PolygonCreator& polygonCreator; 10 | Scene& scene; 11 | }; -------------------------------------------------------------------------------- /CraftPhysicsTest/Source/RoadCreator/RoadCreator.cpp: -------------------------------------------------------------------------------- 1 | #include "RoadCreator.h" 2 | #include "Core/Physics/Polygon/PolygonCreator/PolygonCreator.h" 3 | #include "Core/Physics/Polygon/JointCreator.h" 4 | #include "Core/Physics/Polygon/Polygon.h" 5 | #include "Core/Scene.h" 6 | RoadCreator::RoadCreator(Scene &scene, PolygonCreator &polygonCreator) : scene(scene), polygonCreator(polygonCreator) 7 | { 8 | } 9 | void RoadCreator::CreateRoad(int amount, Vector2 startPosition, Vector2 endPosition) 10 | { 11 | float jointOffset = 2; 12 | 13 | Vector2 stepSize = (endPosition - startPosition) / amount; 14 | b2Body *previousBody = nullptr; 15 | b2Body *firstBody = nullptr; 16 | 17 | for (int i = 0; i < amount; ++i) 18 | { 19 | 20 | Vector2 currentPosition = startPosition + (stepSize * i); 21 | Polygon *currentPolygon = polygonCreator.CreateRectanglePolygon({currentPosition, Vector2(6, 2), 0}); 22 | 23 | b2Body *currentBody = currentPolygon->GetBody(); 24 | 25 | if (i == 0 || i == amount - 1) 26 | { 27 | firstBody = currentBody; 28 | currentBody->SetType(b2_staticBody); 29 | } 30 | 31 | if (previousBody != nullptr) 32 | { 33 | b2DistanceJointDef jointDef; 34 | jointDef.bodyA = previousBody; 35 | jointDef.bodyB = currentBody; 36 | jointDef.collideConnected = false; 37 | jointDef.localAnchorA = previousBody->GetLocalCenter() + b2Vec2(jointOffset, 0); 38 | jointDef.localAnchorB = currentBody->GetLocalCenter() - b2Vec2(jointOffset, 0); 39 | jointDef.maxLength = 15; 40 | jointDef.stiffness = 2325; 41 | jointDef.length = 2; 42 | 43 | b2Joint *joint = scene.world->CreateJoint(&jointDef); 44 | } 45 | 46 | previousBody = currentBody; 47 | } 48 | 49 | if (firstBody != nullptr) 50 | { 51 | firstBody->SetType(b2_staticBody); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /CraftPhysicsTest/Source/RoadCreator/RoadCreator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class RoadCreator 4 | { 5 | public: 6 | RoadCreator(class Scene& scene, class PolygonCreator& polygonCreator); 7 | void CreateRoad(int amount, class Vector2 startPosition, Vector2 endPosition); 8 | 9 | private: 10 | Scene& scene; 11 | class PolygonCreator& polygonCreator; 12 | }; -------------------------------------------------------------------------------- /CraftPhysicsTest/Source/Scenes/PhysicsTestScene.cpp: -------------------------------------------------------------------------------- 1 | #include "PhysicsTestScene.h" 2 | #include "../RoadCreator/RoadCreator.h" 3 | #include "Core/Input.h" 4 | #include "Core/Component/RigidbodyComponent.h" 5 | #include "Core/Physics/Polygon/PolygonCreator/PolygonCreator.h" 6 | #include "Core/Physics/Polygon/JointCreator.h" 7 | #include "Core/Physics/JointRenderer.h" 8 | #include "Core/Physics/Polygon/Polygon.h" 9 | #include "../Polygons/Car.h" 10 | PhysicsTestScene::~PhysicsTestScene() 11 | { 12 | delete polygonCreator; 13 | polygonCreator = nullptr; 14 | delete jointCreator; 15 | jointCreator = nullptr; 16 | delete jointRenderer; 17 | jointRenderer = nullptr; 18 | } 19 | void PhysicsTestScene::Init() 20 | { 21 | Scene::Init(); 22 | auto inputEvent = BIND_EVENT_FN(PhysicsTestScene::OnInput); 23 | Input::AddListener(inputEvent); 24 | polygonCreator = new PolygonCreator(*this); 25 | jointCreator = new JointCreator(*this); 26 | jointRenderer = new JointRenderer(*this); 27 | 28 | RoadCreator *roadCreator = new RoadCreator(*this, *polygonCreator); 29 | roadCreator->CreateRoad(6, {5, 15}, {54, 15}); 30 | 31 | Car *car = new Car(*this, *polygonCreator); 32 | car->Init(Vector2(10, 9)); 33 | // polygonCreator->CreateRectanglePolygon({Vector2(5, 9), Vector2(3, 9), 0}); 34 | // polygonCreator->CreateSquarePolygon({40, 5}, 5, 0); 35 | // polygonCreator->CreateCircle({12, 5}, 3, 6); 36 | // polygonCreator->CreateCircle({25, 5}, 3, 6); 37 | } 38 | 39 | void PhysicsTestScene::Start() 40 | { 41 | std::cout << " Press c to create vertex of a polygon \n"; 42 | std::cout << " Press space to apply polygon \n"; 43 | } 44 | 45 | void PhysicsTestScene::Update(float deltaTime) 46 | { 47 | Scene::Update(deltaTime); 48 | jointRenderer->Update(deltaTime); 49 | } 50 | 51 | void PhysicsTestScene::OnInput(int input) 52 | { 53 | } 54 | -------------------------------------------------------------------------------- /CraftPhysicsTest/Source/Scenes/PhysicsTestScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/Scene.h" 3 | 4 | #include 5 | class PhysicsTestScene : public Scene 6 | { 7 | public: 8 | ~PhysicsTestScene(); 9 | 10 | void Init() override; 11 | void Start() override; 12 | void Update(float deltaTime) override; 13 | 14 | private: 15 | class PolygonCreator *polygonCreator; 16 | class JointCreator *jointCreator; 17 | class JointRenderer *jointRenderer; 18 | void OnInput(int input); 19 | 20 | 21 | }; 22 | -------------------------------------------------------------------------------- /CraftRogue/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ConsoleCraftEngine/CraftRogueGame/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.12) 4 | project(RogueGame) 5 | 6 | 7 | 8 | # Source files 9 | file(GLOB_RECURSE ENGINE_SOURCES 10 | ../Source/*.cpp 11 | ) 12 | 13 | # Header files 14 | file(GLOB_RECURSE ENGINE_HEADERS 15 | ../Source/*.h 16 | ) 17 | add_library(ConsoleCraftEngine SHARED ${ENGINE_SOURCES} ) 18 | 19 | 20 | file(GLOB_RECURSE GAME_SOURCES Source/*.cpp) 21 | file(GLOB_RECURSE GAME_HEADERS Source/*.h) 22 | # Executable 23 | link_directories(${CMAKE_CURRENT_SOURCE_DIR}) 24 | add_executable(CraftRogue ${GAME_SOURCES} ${GAME_HEADERS}) 25 | 26 | # Include directory 27 | target_include_directories(CraftRogue PUBLIC 28 | ${CMAKE_CURRENT_SOURCE_DIR}/Source 29 | ${CMAKE_CURRENT_SOURCE_DIR}/../../ConsoleCraftEngine/Source 30 | ${CMAKE_CURRENT_SOURCE_DIR}/../../ConsoleCraftEngine/Source/Core 31 | ) 32 | 33 | # Link with ConsoleCraftEngine library 34 | target_link_libraries(CraftRogue PUBLIC ConsoleCraftEngine ncurses) 35 | 36 | # Include directory 37 | #target_include_directories(CraftRogue PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Source) 38 | 39 | -------------------------------------------------------------------------------- /CraftRogue/Source/Components/Health.cpp: -------------------------------------------------------------------------------- 1 | #include "Health.h" 2 | #include "Core/GameObject.h" 3 | void Health::GiveDamage(float healthAmount) 4 | { 5 | this->healthAmount -= healthAmount; 6 | 7 | if(this->healthAmount <= 0) 8 | OnHealthDeplated(); 9 | } 10 | 11 | void Health::AddHealth(float healthAmount) 12 | { 13 | this->healthAmount += healthAmount; 14 | } 15 | 16 | void Health::OnHealthDeplated() 17 | { 18 | HasHealthDeplated = true; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /CraftRogue/Source/Components/Health.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/Component/Component.h" 3 | class Health : public Component 4 | { 5 | public: 6 | Health(GameObject& owner) : Component(owner){} 7 | void GiveDamage(float healthAmount); 8 | void AddHealth(float healthAmount); 9 | bool HasHealthDeplated = false; 10 | private: 11 | float healthAmount = 1; 12 | virtual void OnHealthDeplated(); 13 | 14 | }; 15 | 16 | -------------------------------------------------------------------------------- /CraftRogue/Source/Components/PlayerUpgradeComponent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/Component/Component.h" 3 | #include "Core/UIHandler.h" 4 | #include 5 | class PlayerUpgradeComponent : public Component 6 | { 7 | public: 8 | PlayerUpgradeComponent(class GameObject& gameObject); 9 | 10 | void Init() override; 11 | void AddExperience(int experienceToAdd); 12 | bool isInUpgrade = false; 13 | private: 14 | int experience = 0; 15 | int experienceCap = 4; 16 | class UIData UpgradeUiData; 17 | std::shared_ptr UpgradeUiDataPtr; 18 | 19 | class UIData experienceUiData; 20 | std::shared_ptr experienceUiDataPtr; 21 | 22 | std::function inputEvent; 23 | void CreateUpgradeSquare(); 24 | void OnKeyPress(int input); 25 | }; 26 | 27 | -------------------------------------------------------------------------------- /CraftRogue/Source/CraftRogueGameEntry.cpp: -------------------------------------------------------------------------------- 1 | #include "Scenes/RogueScene.h" 2 | #include "Core/EntryPoint.h" 3 | #include 4 | class CraftRogue 5 | { 6 | public: 7 | ~CraftRogue() 8 | { 9 | }; 10 | Engine engine; 11 | void StartGame() 12 | { 13 | // engine.SceneManager_.ChangeScene(new RogueScene()); 14 | engine.scenes.push_back(new RogueScene()); 15 | engine.StartGame(); 16 | } 17 | void Clean() 18 | { 19 | engine.Clean(); 20 | } 21 | }; 22 | 23 | int main() 24 | { 25 | CraftRogue *game = new CraftRogue(); 26 | game->StartGame(); 27 | game->Clean(); 28 | delete game; 29 | system("pause"); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Enemies/EnemyAmeboid.cpp: -------------------------------------------------------------------------------- 1 | #include "EnemyAmeboid.h" 2 | #include "../../Components/Health.h" 3 | void EnemyAmeboid::Init() 4 | { 5 | EnemyRogue::Init(); 6 | GetComponent()->AddHealth(1); //Start with 2 healths 7 | symbol = '-'; 8 | std::string symbol00 = "_"; 9 | std::string symbol01 = "|"; 10 | symbols.push_back(symbol00); 11 | symbols.push_back(symbol01); 12 | std::vector> sprite00 = { 13 | {1, 1, 1, 1, 1}, 14 | {1, 2, 1, 2, 1}, 15 | {1, 1, 1, 1, 1}, 16 | {1, 5, 1, 5, 1}, 17 | }; 18 | std::vector> sprite01 = { 19 | {1, 1, 1, 1, 1}, 20 | {1, 2, 1, 2, 1}, 21 | {1, 1, 1, 1, 1}, 22 | {1, 1, 1, 1, 1}, 23 | }; 24 | sprites.push_back(sprite00); 25 | sprites.push_back(sprite01); 26 | } 27 | 28 | void EnemyAmeboid::Update(float deltaTime) 29 | { 30 | EnemyRogue::Update(deltaTime); 31 | } 32 | 33 | void EnemyAmeboid::OnMove() 34 | { 35 | animationIndex++; 36 | animationIndex = animationIndex % sprites.size(); 37 | 38 | symbol = symbols[animationIndex]; 39 | sprite = sprites[animationIndex]; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Enemies/EnemyAmeboid.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "EnemyRogue.h" 3 | 4 | class EnemyAmeboid : public EnemyRogue 5 | { 6 | public: 7 | EnemyAmeboid(class Scene& scene) : EnemyRogue(scene) 8 | { 9 | 10 | } 11 | void Init() override; 12 | void Update(float deltaTime) override; 13 | void OnMove() override; 14 | private: 15 | std::vector>> sprites; 16 | std::vector symbols; 17 | int animationIndex = 0; 18 | }; -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Enemies/EnemyRogue.cpp: -------------------------------------------------------------------------------- 1 | #include "EnemyRogue.h" 2 | 3 | #include "Core/Scene.h" 4 | #include "CoreStructs/Vector.h" 5 | #include "Core/ParticleSystem/ParticleSource.h" 6 | #include "Core/Event.h" 7 | #include "Core/EventDispatcher.h" 8 | 9 | #include "../../Components/Health.h" 10 | EnemyRogue::~EnemyRogue() 11 | { 12 | 13 | } 14 | void EnemyRogue::Init() 15 | { 16 | 17 | sprite = { 18 | {4, 4, 4, 4}, 19 | {4, 1, 1, 4}, 20 | {4, 0, 0, 4}}; 21 | particleSource = new ParticleSource(*this); 22 | AddComponent(particleSource); 23 | AddComponent(new Health(*this)); 24 | } 25 | void EnemyRogue::Update(float deltaTime) 26 | { 27 | if (GetComponent()->HasHealthDeplated && !isDead) 28 | { 29 | OnDie(); 30 | return; 31 | } 32 | MoveToPlayer(deltaTime); 33 | elapsedTime += deltaTime; 34 | if (elapsedTime >= 0.2f) 35 | { 36 | OnMove(); 37 | elapsedTime = 0; 38 | } 39 | } 40 | void EnemyRogue::OnDie() 41 | { 42 | isDead = true; 43 | particleSource->EmitParticle(4, ENEMYTYPEPARTICLE); 44 | Event enemyKilledEvent = Event(EventType::OnEnemyKilled); 45 | EventDispatcher::CallEvent(enemyKilledEvent); 46 | Destroy(); 47 | } 48 | void EnemyRogue::MoveToPlayer(float deltaTime) 49 | { 50 | auto player = GetCurrentScene().FindGameObject("Player"); 51 | if (player != nullptr) 52 | { 53 | Vector2 moveDirection = player->transform.Position - transform.Position; 54 | moveDirection.Normalize(); 55 | moveDirection = moveDirection * moveSpeed * deltaTime; 56 | transform.MovePosition(moveDirection.X, moveDirection.Y); 57 | } 58 | } 59 | void EnemyRogue::OnMove() 60 | { 61 | 62 | }; 63 | -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Enemies/EnemyRogue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | class EnemyRogue : public GameObject 4 | { 5 | public: 6 | EnemyRogue(class Scene &scene) : GameObject("Enemy", scene) 7 | { 8 | symbol = '#'; 9 | } 10 | ~EnemyRogue(); 11 | void Init() override; 12 | void Update(float deltaTime) override; 13 | 14 | 15 | void MoveToPlayer(float deltaTime); 16 | 17 | 18 | protected: 19 | float moveSpeed = 5; 20 | int health = 2; 21 | 22 | bool isDead = false; 23 | 24 | virtual void OnDie(); 25 | virtual void OnMove(); 26 | 27 | private: 28 | float elapsedTime = 0; 29 | class ParticleSource *particleSource; 30 | }; 31 | -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Player.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | #include "Core/UIHandler.h" 4 | #include "Core/Event.h" 5 | #include 6 | class Player : public GameObject 7 | { 8 | public: 9 | Player(class Scene &scene) : GameObject("Player", scene) {} 10 | ~Player(); 11 | 12 | void Init() override; 13 | 14 | void OnKeyPressed(int input); 15 | void UnlockWeapon(int index); 16 | 17 | private: 18 | UIData scoreUIData; 19 | std::shared_ptr scoreUIDataPtr; 20 | std::function OnRecievedEvent; 21 | void InitializeWeapon(Vector2 &startPosition); 22 | void RecievedEvent(Event &e); 23 | void Update(float deltaTime) override; 24 | void OnCollided(GameObject &other) override; 25 | class PlayerUpgradeComponent *playerUpgradeComponent; 26 | 27 | std::function inputEvent; 28 | 29 | int weaponIndex = 0; 30 | 31 | std::vector usableWeaponsIndex; 32 | std::vector weapons; 33 | 34 | bool canFire = true; 35 | float fireDuration = 0; 36 | float fireRate = 0.5f; 37 | 38 | int score = 0; 39 | }; -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Weapons/BlastGun.cpp: -------------------------------------------------------------------------------- 1 | #include "BlastGun.h" 2 | #include "PlasmaBullet.h" 3 | #include "Core/Scene.h" 4 | void BlastGun::Init() 5 | { 6 | isRenderable = false; 7 | } 8 | 9 | void BlastGun::Fire(GameObject &targetGameObject) 10 | { 11 | 12 | 13 | for (int i = 0; i < numberOfBullets; i++) 14 | { 15 | double randomAngle = static_cast(rand()) / RAND_MAX * 2 * 3.14f; 16 | 17 | double speed = 25.0; 18 | double randomDirectionX = speed * std::cos(randomAngle); 19 | double randomDirectionY = speed * std::sin(randomAngle); 20 | 21 | Vector2 randomDirection(randomDirectionX, randomDirectionY); 22 | randomDirection.Normalize(); 23 | auto plasmaBullet = new PlasmaBullet(GetCurrentScene(), randomDirection, damage); 24 | plasmaBullet->sprite = { 25 | {3,1}, 26 | {1,3} 27 | }; 28 | GetCurrentScene().AddGameObject(plasmaBullet, transform.Position); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Weapons/BlastGun.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Weapon.h" 3 | class BlastGun : public Weapon 4 | { 5 | public: 6 | BlastGun(class Scene &scene) : Weapon(scene) 7 | { 8 | } 9 | void Init() override; 10 | void Fire(GameObject &targetGameObject) override; 11 | void Upgrade() override 12 | { 13 | numberOfBullets++; 14 | numberOfBullets = numberOfBullets > 10 ? 10 : numberOfBullets; 15 | } 16 | 17 | private: 18 | int numberOfBullets = 4; 19 | }; -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Weapons/PlasmaBullet.cpp: -------------------------------------------------------------------------------- 1 | #include "PlasmaBullet.h" 2 | 3 | #include "Core/Scene.h" 4 | #include "Core/ParticleSystem/ParticleSource.h" 5 | #include "Core/GameObject.h" 6 | #include "../../Components/Health.h" 7 | void PlasmaBullet::Init() 8 | { 9 | 10 | 11 | particleSource = new ParticleSource(*this); 12 | AddComponent(particleSource); 13 | } 14 | 15 | void PlasmaBullet::Update(float deltaTime) 16 | { 17 | Vector2 movePosition = fireDirection * deltaTime * bulletSpeed; 18 | transform.MovePosition(movePosition.X, movePosition.Y); 19 | bulletSpeed += bulletAcceleration * deltaTime; 20 | timePassedSinceParticleSpawn += deltaTime; 21 | if (timePassedSinceParticleSpawn >= particleSpawnFreq) 22 | { 23 | timePassedSinceParticleSpawn = 0; 24 | particleSource->EmitParticle(4, FIRETYPEPARTICLE); 25 | } 26 | } 27 | 28 | void PlasmaBullet::OnCollidedBorder(int border) 29 | { 30 | Destroy(); 31 | } 32 | 33 | void PlasmaBullet::OnCollided(GameObject &other) 34 | { 35 | 36 | if (other.name == "Enemy") 37 | { 38 | other.GetComponent()->GiveDamage(damage); 39 | Destroy(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Weapons/PlasmaBullet.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | #include "CoreStructs/Vector.h" 4 | class PlasmaBullet : public GameObject 5 | { 6 | public: 7 | PlasmaBullet(class Scene &scene, Vector2 fireDirection, float damage) : GameObject("Bullet", scene) 8 | { 9 | sprite = { 10 | {3, 3}, 11 | {3, 3} 12 | }; 13 | symbol = '#'; 14 | this->fireDirection = fireDirection; 15 | this->damage = damage; 16 | } 17 | void Init() override; 18 | void Update(float deltaTime) override; 19 | void OnCollidedBorder(int border) override; 20 | virtual void OnCollided(GameObject &other) override; 21 | 22 | private: 23 | float bulletSpeed = 1.f; 24 | float bulletAcceleration = 100.f; 25 | float particleSpawnFreq = 0.2f; 26 | float timePassedSinceParticleSpawn = 0; 27 | Vector2 fireDirection = Vector2(0, 0); 28 | class ParticleSource *particleSource; 29 | float damage; 30 | }; 31 | -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Weapons/PlasmaGun.cpp: -------------------------------------------------------------------------------- 1 | #include "PlasmaGun.h" 2 | #include "PlasmaBullet.h" 3 | #include "Core/Scene.h" 4 | 5 | void PlasmaGun::Init() 6 | { 7 | 8 | isRenderable = false; 9 | } 10 | 11 | void PlasmaGun::Fire(GameObject &targetGameObject) 12 | { 13 | Vector2 fireDirection = targetGameObject.transform.Position - transform.Position; 14 | fireDirection.Normalize(); 15 | GetCurrentScene().AddGameObject(new PlasmaBullet(GetCurrentScene(), fireDirection, damage), transform.Position); 16 | } -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Weapons/PlasmaGun.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Weapon.h" 3 | #include "CoreStructs/Vector.h" 4 | 5 | class PlasmaGun : public Weapon 6 | { 7 | public: 8 | PlasmaGun(class Scene &scene) : Weapon(scene) {} 9 | ~PlasmaGun() {} 10 | void Upgrade() override 11 | { 12 | damage++; 13 | } 14 | void Init() override; 15 | 16 | void Fire(GameObject& targetGameObject) override; 17 | 18 | private: 19 | 20 | }; -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Weapons/WaveGun.cpp: -------------------------------------------------------------------------------- 1 | #include "WaveGun.h" 2 | #include "../../Components/Health.h" 3 | #include "Core/ParticleSystem/ParticleSource.h" 4 | void WaveGun::Init() 5 | { 6 | isRenderable = false; 7 | AddComponent(new ParticleSource(*this)); 8 | } 9 | 10 | void WaveGun::Update(float deltaTime) 11 | { 12 | if (targetGameObject != nullptr) 13 | { 14 | targetGameObject->canFindable = false; 15 | if (targetGameObject->transform.HasOwnerDestroyed) 16 | { 17 | targetGameObject = nullptr; 18 | StopWeapon(); 19 | } 20 | else 21 | { 22 | fireDuration += deltaTime; 23 | if (fireDuration >= damageTime) 24 | { 25 | targetGameObject->GetComponent()->GiveDamage(damage); 26 | fireDuration = 0; 27 | } 28 | } 29 | } 30 | } 31 | 32 | void WaveGun::Fire(GameObject &targetGameObject) 33 | { 34 | if (!isFiring) 35 | { 36 | this->targetGameObject = &targetGameObject; 37 | GetComponent()->EmitWaveParticle(targetGameObject.transform, Vector2(0, 0)); 38 | isFiring = true; 39 | } 40 | } 41 | 42 | void WaveGun::StopWeapon() 43 | { 44 | isFiring = false; 45 | GetComponent()->ClearWaveParticles(); 46 | } 47 | -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Weapons/WaveGun.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Weapon.h" 3 | 4 | class WaveGun : public Weapon 5 | { 6 | public: 7 | WaveGun(class Scene &scene) : Weapon(scene) 8 | { 9 | targetGameObject = nullptr; 10 | } 11 | ~WaveGun() 12 | { 13 | } 14 | void Init() override; 15 | void Update(float deltaTime) override; 16 | void Fire(GameObject &targetGameObject) override; 17 | void Upgrade() override 18 | { 19 | damageTime -= 0.1f; 20 | damageTime = damageTime < 0.5f ? 0.5 : damageTime; 21 | } 22 | void StopWeapon() override; 23 | 24 | private: 25 | bool isFiring = false; 26 | float damage = 1; 27 | float damageTime = 1; 28 | float fireDuration = 0; 29 | GameObject *targetGameObject; 30 | }; -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Weapons/Weapon.cpp: -------------------------------------------------------------------------------- 1 | #include "Weapon.h" 2 | 3 | -------------------------------------------------------------------------------- /CraftRogue/Source/GameObjects/Weapons/Weapon.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | #include "CoreStructs/Vector.h" 4 | class Weapon : public GameObject 5 | { 6 | public: 7 | Weapon(class Scene& scene) : GameObject("Weapon", scene) 8 | { 9 | 10 | } 11 | virtual void Upgrade(){}; 12 | virtual void StopWeapon(){} 13 | virtual void Fire(GameObject& targetGameObject){} 14 | protected: 15 | float damage = 1; 16 | 17 | }; -------------------------------------------------------------------------------- /CraftRogue/Source/Scenes/RogueScene.cpp: -------------------------------------------------------------------------------- 1 | #include "RogueScene.h" 2 | #include "../GameObjects/Player.h" 3 | #include "../GameObjects/Enemies/EnemyRogue.h" 4 | #include "../GameObjects/Enemies/EnemyAmeboid.h" 5 | #include "CoreStructs/Vector.h" 6 | #include "Core/SceneManager.h" 7 | 8 | const int STANDARDENEMYTYPE = 0; 9 | const int AMEBOIDENEMYTYPE = 1; 10 | void RogueScene::Init() 11 | { 12 | AddGameObject(new Player(*this), Vector2(30, 10)); 13 | 14 | srand(static_cast(time(nullptr))); 15 | SpawnEnemy(rand() % 2); 16 | } 17 | 18 | void RogueScene::Update(float deltaTime) 19 | { 20 | Scene::Update(deltaTime); 21 | 22 | if (!isPaused) 23 | spawnDurationPassed += deltaTime; 24 | 25 | if (spawnDurationPassed >= spawnTime) 26 | { 27 | spawnDurationPassed = 0; 28 | // SceneManager::ChangeScene(new RogueScene()); 29 | SpawnEnemy(rand() % 3); 30 | } 31 | } 32 | 33 | void RogueScene::SpawnEnemy(int enemyType) 34 | { 35 | numberOfEnemyToSpawn--; 36 | 37 | int edge = rand() % 4; 38 | 39 | int xPos, yPos; 40 | 41 | switch (edge) 42 | { 43 | case 3: // Top edge 44 | xPos = rand() % SCREENWIDTH; 45 | yPos = 0; 46 | break; 47 | case 1: // Right edge 48 | xPos = SCREENWIDTH; 49 | yPos = rand() % SCREENHEIGHT; 50 | break; 51 | case 2: // Bottom edge 52 | xPos = rand() % SCREENWIDTH; 53 | yPos = SCREENHEIGHT; 54 | break; 55 | case 0: // Left edge 56 | xPos = 0; 57 | yPos = rand() % SCREENHEIGHT; 58 | break; 59 | } 60 | switch (enemyType) 61 | { 62 | case 0: 63 | AddGameObject(new EnemyRogue(*this), Vector2(xPos, yPos)); 64 | break; 65 | case 1: 66 | AddGameObject(new EnemyRogue(*this), Vector2(xPos, yPos)); 67 | 68 | break; 69 | case 2: 70 | AddGameObject(new EnemyAmeboid(*this), Vector2(xPos, yPos)); 71 | break; 72 | default: 73 | break; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /CraftRogue/Source/Scenes/RogueScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/Scene.h" 3 | class RogueScene : public Scene 4 | { 5 | public: 6 | void Init() override; 7 | void Update(float deltaTime) override; 8 | 9 | private: 10 | void SpawnEnemy(int enemyType); 11 | 12 | int numberOfEnemyToSpawn = 5; 13 | float spawnTime = 2; 14 | float spawnDurationPassed = 0; 15 | }; 16 | -------------------------------------------------------------------------------- /CraftShooterGame/CMakeLists 32bits.txt: -------------------------------------------------------------------------------- 1 | # ConsoleCraftEngine/CraftShooterGame/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.12) 4 | project(CraftShooterGame) 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32") 6 | 7 | 8 | 9 | # Source files 10 | file(GLOB_RECURSE ENGINE_SOURCES 11 | ../Source/*.cpp 12 | ) 13 | 14 | # Header files 15 | file(GLOB_RECURSE ENGINE_HEADERS 16 | ../Source/*.h 17 | ) 18 | add_library(ConsoleCraftEngine SHARED ${ENGINE_SOURCES} ) 19 | 20 | 21 | file(GLOB_RECURSE GAME_SOURCES Source/*.cpp) 22 | file(GLOB_RECURSE GAME_HEADERS Source/*.h) 23 | # Executable 24 | link_directories(${CMAKE_CURRENT_SOURCE_DIR}) 25 | add_executable(CraftShooter ${GAME_SOURCES} ${GAME_HEADERS}) 26 | 27 | # Include directory 28 | target_include_directories(CraftShooter PUBLIC 29 | ${CMAKE_CURRENT_SOURCE_DIR}/Source #TODO this is not necessary 30 | ${CMAKE_CURRENT_SOURCE_DIR}/../../ConsoleCraftEngine/Source ) 31 | 32 | # Link with ConsoleCraftEngine library 33 | target_link_libraries(CraftShooter PUBLIC ConsoleCraftEngine /lib/i386-linux-gnu/libncurses.so.5 /lib/i386-linux-gnu/libtinfo.so.5) 34 | 35 | 36 | # Include directory 37 | #target_include_directories(CraftShooter PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Source) 38 | 39 | -------------------------------------------------------------------------------- /CraftShooterGame/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ConsoleCraftEngine/CraftShooterGame/CMakeLists.txt 2 | 3 | cmake_minimum_required(VERSION 3.12) 4 | project(CraftShooterGame) 5 | 6 | 7 | 8 | # Source files 9 | file(GLOB_RECURSE ENGINE_SOURCES 10 | ../Source/*.cpp 11 | ) 12 | 13 | # Header files 14 | file(GLOB_RECURSE ENGINE_HEADERS 15 | ../Source/*.h 16 | ) 17 | add_library(ConsoleCraftEngine SHARED ${ENGINE_SOURCES} ) 18 | 19 | 20 | file(GLOB_RECURSE GAME_SOURCES Source/*.cpp) 21 | file(GLOB_RECURSE GAME_HEADERS Source/*.h) 22 | # Executable 23 | link_directories(${CMAKE_CURRENT_SOURCE_DIR}) 24 | add_executable(CraftShooter ${GAME_SOURCES} ${GAME_HEADERS}) 25 | 26 | # Include directory 27 | target_include_directories(CraftShooter PUBLIC 28 | ${CMAKE_CURRENT_SOURCE_DIR}/Source #TODO this is not necessary 29 | ${CMAKE_CURRENT_SOURCE_DIR}/../../ConsoleCraftEngine/Source ) 30 | 31 | # Link with ConsoleCraftEngine library 32 | target_link_libraries(CraftShooter PUBLIC ConsoleCraftEngine ncurses) 33 | 34 | # Include directory 35 | #target_include_directories(CraftShooter PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Source) 36 | 37 | -------------------------------------------------------------------------------- /CraftShooterGame/CraftShooterGame.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.34114.132 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CraftShooterGame", "CraftShooterGame.vcxproj", "{E8763AD2-0672-4E7D-8DE1-9A2C4D0828A6}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ConsoleCraftEngine", "..\ConsoleCraftEngine.vcxproj", "{B34B49AB-2BE7-424F-B089-26AB03AE97E4}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|x64 = Debug|x64 13 | Debug|x86 = Debug|x86 14 | Release|x64 = Release|x64 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {E8763AD2-0672-4E7D-8DE1-9A2C4D0828A6}.Debug|x64.ActiveCfg = Debug|x64 19 | {E8763AD2-0672-4E7D-8DE1-9A2C4D0828A6}.Debug|x64.Build.0 = Debug|x64 20 | {E8763AD2-0672-4E7D-8DE1-9A2C4D0828A6}.Debug|x86.ActiveCfg = Debug|Win32 21 | {E8763AD2-0672-4E7D-8DE1-9A2C4D0828A6}.Debug|x86.Build.0 = Debug|Win32 22 | {E8763AD2-0672-4E7D-8DE1-9A2C4D0828A6}.Release|x64.ActiveCfg = Release|x64 23 | {E8763AD2-0672-4E7D-8DE1-9A2C4D0828A6}.Release|x64.Build.0 = Release|x64 24 | {E8763AD2-0672-4E7D-8DE1-9A2C4D0828A6}.Release|x86.ActiveCfg = Release|Win32 25 | {E8763AD2-0672-4E7D-8DE1-9A2C4D0828A6}.Release|x86.Build.0 = Release|Win32 26 | {B34B49AB-2BE7-424F-B089-26AB03AE97E4}.Debug|x64.ActiveCfg = Debug|x64 27 | {B34B49AB-2BE7-424F-B089-26AB03AE97E4}.Debug|x64.Build.0 = Debug|x64 28 | {B34B49AB-2BE7-424F-B089-26AB03AE97E4}.Debug|x86.ActiveCfg = Debug|Win32 29 | {B34B49AB-2BE7-424F-B089-26AB03AE97E4}.Debug|x86.Build.0 = Debug|Win32 30 | {B34B49AB-2BE7-424F-B089-26AB03AE97E4}.Release|x64.ActiveCfg = Release|x64 31 | {B34B49AB-2BE7-424F-B089-26AB03AE97E4}.Release|x64.Build.0 = Release|x64 32 | {B34B49AB-2BE7-424F-B089-26AB03AE97E4}.Release|x86.ActiveCfg = Release|Win32 33 | {B34B49AB-2BE7-424F-B089-26AB03AE97E4}.Release|x86.Build.0 = Release|Win32 34 | EndGlobalSection 35 | GlobalSection(SolutionProperties) = preSolution 36 | HideSolutionNode = FALSE 37 | EndGlobalSection 38 | GlobalSection(ExtensibilityGlobals) = postSolution 39 | SolutionGuid = {BE5B2517-AF74-416A-9CBD-A8F018C3ABEF} 40 | EndGlobalSection 41 | EndGlobal 42 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/Components/PlayerController.cpp: -------------------------------------------------------------------------------- 1 | #include "PlayerController.h" 2 | #include "Core/GameObject.h" 3 | #include 4 | #include "Core/Input.h" 5 | #include "Core/Scene.h" 6 | PlayerController::PlayerController(GameObject &go, int playerNo) : Component(go) 7 | { 8 | this->playerNo = playerNo; 9 | } 10 | 11 | void PlayerController::Init() 12 | { 13 | auto event = std::bind(&PlayerController::OnKeyDown, this, std::placeholders::_1); 14 | Input::AddListener(event); 15 | } 16 | 17 | void PlayerController::Update(float deltaTime) 18 | { 19 | } 20 | 21 | void PlayerController::OnKeyDown(int input) 22 | { 23 | if (!owner->GetCurrentScene().isPaused) 24 | { 25 | if (playerNo == 0) 26 | { 27 | if (tolower(Input::GetKeyDown()) == 'd') 28 | { 29 | owner->transform.MovePosition(1, 0); 30 | } 31 | if (tolower(Input::GetKeyDown()) == 'a') 32 | { 33 | owner->transform.MovePosition(-1, 0); 34 | } 35 | if (tolower(Input::GetKeyDown()) == 's') 36 | { 37 | owner->transform.MovePosition(0, 1); 38 | } 39 | if (tolower(Input::GetKeyDown()) == 'w') 40 | { 41 | owner->transform.MovePosition(0, -1); 42 | } 43 | } 44 | if (playerNo == 1) 45 | { 46 | if (tolower(Input::GetKeyDown()) == 'k') 47 | { 48 | owner->transform.MovePosition(1, 0); 49 | } 50 | if (tolower(Input::GetKeyDown()) == 'h') 51 | { 52 | owner->transform.MovePosition(-1, 0); 53 | } 54 | if (tolower(Input::GetKeyDown()) == 'j') 55 | { 56 | owner->transform.MovePosition(0, 1); 57 | } 58 | if (tolower(Input::GetKeyDown()) == 'u') 59 | { 60 | owner->transform.MovePosition(0, -1); 61 | } 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/Components/PlayerController.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/Component/Component.h" 3 | class PlayerController : public Component 4 | { 5 | public: 6 | PlayerController(class GameObject& go, int playerNo) ; 7 | void Init() override; 8 | void Update(float deltaTime) override; 9 | void OnKeyDown(int input); 10 | private: 11 | int playerNo = 0; 12 | }; 13 | 14 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/GameObjects/Pong/Ball.cpp: -------------------------------------------------------------------------------- 1 | #include "Ball.h" 2 | 3 | void Ball::Update(float deltaTime) 4 | { 5 | { 6 | transform.MovePosition(velocity.X * deltaTime, velocity.Y * deltaTime); 7 | if (transform.Position.X >= SCREENWIDTH - 1 || transform.Position.X <= 1) 8 | { 9 | velocity.X *= -1; 10 | } 11 | if (transform.Position.Y >= SCREENHEIGHT - 3 || transform.Position.Y <= 1) 12 | { 13 | velocity.Y *= -1; 14 | } 15 | 16 | } 17 | } 18 | 19 | void Ball::OnCollided(GameObject& other) 20 | { 21 | Destroy(); 22 | velocity.X *= -1; 23 | velocity.Y *= -1; 24 | } 25 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/GameObjects/Pong/Ball.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | #include 4 | class Ball : public GameObject 5 | { 6 | public: 7 | Ball() : GameObject("Ball", scene) 8 | { 9 | symbol = '#'; 10 | } 11 | 12 | 13 | void Init() override 14 | { 15 | sprite = { {1,1}, {1,1} }; 16 | } 17 | void Update(float deltaTime) override; 18 | virtual void OnCollided(GameObject& other) override; 19 | 20 | friend std::ostream& operator<<(std::ostream& os, const Ball& ball) 21 | { 22 | os << "Ball position: " << ball.transform << "\n"; 23 | return os; 24 | } 25 | private: 26 | 27 | Vector2 velocity = Vector2(50.f,10.f); 28 | 29 | }; -------------------------------------------------------------------------------- /CraftShooterGame/Source/GameObjects/Pong/Paddle.cpp: -------------------------------------------------------------------------------- 1 | #include "Paddle.h" 2 | #include "Core/Component/PlayerController.h" 3 | 4 | 5 | void Paddle::Init() 6 | { 7 | AddComponent(new PlayerController(*this, playerNo)); 8 | sprite = { {1,1}, {1,1} }; 9 | 10 | } 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/GameObjects/Pong/Paddle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "Core/GameObject.h" 4 | class Paddle : public GameObject 5 | { 6 | public: 7 | Paddle(int playerNo, class Scene& scene) : GameObject("Paddle", scene) 8 | { 9 | this->playerNo = playerNo; 10 | symbol = '\xDB'; 11 | } 12 | 13 | 14 | void Init() override; 15 | 16 | friend std::ostream& operator<<(std::ostream& os, const Paddle& paddle) 17 | { 18 | os << "Paddle position: " << paddle.transform << "\n"; 19 | return os; 20 | } 21 | private: 22 | int playerNo; 23 | 24 | }; -------------------------------------------------------------------------------- /CraftShooterGame/Source/GameObjects/Shooter/Bullet.cpp: -------------------------------------------------------------------------------- 1 | #include "Bullet.h" 2 | 3 | #include "Core/Scene.h" 4 | #include "Core/ParticleSystem/ParticleSource.h" 5 | 6 | void Bullet::Init() 7 | { 8 | 9 | sprite = { 10 | {1,0,1,0}, 11 | {1,1,1,1}, 12 | {1,0,1,0} }; 13 | particleSource = new ParticleSource(*this); 14 | AddComponent(particleSource); 15 | } 16 | 17 | void Bullet::Update(float deltaTime) 18 | { 19 | transform.MovePosition(bulletSpeed * deltaTime, 0); 20 | bulletSpeed += bulletAcceleration * deltaTime; 21 | timePassedSinceParticleSpawn += deltaTime; 22 | if (timePassedSinceParticleSpawn >= particleSpawnFreq) 23 | { 24 | timePassedSinceParticleSpawn = 0; 25 | particleSource->EmitParticle(4, FIRETYPEPARTICLE); 26 | } 27 | 28 | 29 | } 30 | 31 | void Bullet::OnCollidedBorder(int border) 32 | { 33 | Destroy(); 34 | } 35 | 36 | void Bullet::OnCollided(GameObject& other) 37 | { 38 | 39 | if (other.name == "Enemy") 40 | Destroy(); 41 | } 42 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/GameObjects/Shooter/Bullet.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | class Bullet : public GameObject 4 | { 5 | public: 6 | 7 | Bullet(class Scene& scene) : GameObject("Bullet", scene) 8 | { 9 | symbol = '#'; 10 | } 11 | void Init() override; 12 | void Update(float deltaTime) override; 13 | void OnCollidedBorder(int border) override; 14 | virtual void OnCollided(GameObject& other) override; 15 | private: 16 | float bulletSpeed = 1.f; 17 | float bulletAcceleration = 100.f; 18 | float particleSpawnFreq = 0.2f; 19 | float timePassedSinceParticleSpawn = 0; 20 | class ParticleSource* particleSource; 21 | }; 22 | 23 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/GameObjects/Shooter/Enemy.cpp: -------------------------------------------------------------------------------- 1 | #include "Enemy.h" 2 | #include "Core/ParticleSystem/ParticleSource.h" 3 | #include "Core/Scene.h" 4 | void Enemy::Init() 5 | { 6 | { 7 | 8 | sprite = { 9 | {4,4,4,4}, 10 | {4,4,4,4}, 11 | {4,0,0,4} 12 | }; 13 | 14 | } 15 | particleSource = new ParticleSource(*this); 16 | AddComponent(particleSource); 17 | } 18 | 19 | void Enemy::Update(float deltaTime) 20 | { 21 | transform.MovePosition(-15 * deltaTime, 0); 22 | } 23 | 24 | void Enemy::OnCollided(GameObject& other) 25 | { 26 | 27 | if (other.name == "Bullet") 28 | { 29 | auto enemyKilledEvent = Event(EventType::OnEnemyKilled); 30 | EventDispatcher::CallEvent(enemyKilledEvent); 31 | particleSource->EmitParticle(6, ENEMYTYPEPARTICLE); 32 | Destroy(); 33 | } 34 | } 35 | 36 | void Enemy::OnCollidedBorder(int border) 37 | { 38 | Destroy(); 39 | } 40 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/GameObjects/Shooter/Enemy.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | #include "Core/EventDispatcher.h" 4 | #include "Core/Event.h" 5 | class Enemy : public GameObject 6 | { 7 | public: 8 | Enemy(class Scene& scene) : GameObject("Enemy", scene) 9 | { 10 | symbol = '\xDB'; 11 | 12 | } 13 | void Init() override; 14 | void Update(float deltaTime) override; 15 | 16 | virtual void OnCollided(GameObject& other) override; 17 | virtual void OnCollidedBorder(int border) override; 18 | private: 19 | class ParticleSource* particleSource; 20 | }; 21 | 22 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/GameObjects/Shooter/PlayerShip.cpp: -------------------------------------------------------------------------------- 1 | #include "PlayerShip.h" 2 | #include "Core/Component/PlayerController.h" 3 | #include "Bullet.h" 4 | #include "Core/Input.h" 5 | #include "Core/Scene.h" 6 | #include "Core/EventDispatcher.h" 7 | #include "Core/ParticleSystem/ParticleSource.h" 8 | #include "Core/Renderer.h" 9 | #include 10 | 11 | PlayerShip::~PlayerShip() 12 | { 13 | EventDispatcher::RemoveListener(OnEventFunction); 14 | } 15 | 16 | void PlayerShip::Init() 17 | { 18 | 19 | scoreUIData.position = Vector2(0, 29); 20 | scoreUIData.text = "Score: 0"; 21 | scoreUIDataPtr = std::make_shared(scoreUIData); 22 | scene.uiHandler->AddString(scoreUIDataPtr); 23 | 24 | AddComponent(new PlayerController(*this, 0)); 25 | sprite = 26 | { 27 | {2,2,0,2}, 28 | {1,1,1,1}, 29 | {1,1,1,1}, 30 | {2,2,0,2} 31 | }; 32 | 33 | auto inputEvent = std::bind(&PlayerShip::Fire, this, std::placeholders::_1); 34 | Input::AddListener(inputEvent); 35 | 36 | OnEventFunction = std::bind(&PlayerShip::OnEvent, this, std::placeholders::_1); 37 | EventDispatcher::AddListener(OnEventFunction); 38 | 39 | particleSource = new ParticleSource(*this); 40 | AddComponent(particleSource); 41 | } 42 | 43 | void PlayerShip::Update(float deltaTime) 44 | { 45 | 46 | } 47 | 48 | void PlayerShip::OnCollided(GameObject& other) 49 | { 50 | if (other.name == "Enemy") 51 | { 52 | std::cout << "GAME OVER!" << '\n'; 53 | GetCurrentScene().hasGameOver = true; 54 | } 55 | } 56 | 57 | void PlayerShip::Fire(int keyDown) 58 | { 59 | if (keyDown == SPACEBAR) 60 | { 61 | //sprite = Renderer::RotateSprite(sprite); 62 | GetCurrentScene().AddGameObject(new Bullet(GetCurrentScene()), transform.Position); 63 | 64 | } 65 | } 66 | 67 | void PlayerShip::OnEvent(Event& event) 68 | { 69 | switch (event.GetEventType()) 70 | { 71 | case EventType::OnEnemyKilled: 72 | score++; 73 | GetCurrentScene().camera->StartShake(0.25f); 74 | scoreUIDataPtr->text = "Score: " + std::to_string(score); 75 | //UIHandler::uiText = "Score: " + std::to_string(score); 76 | break; 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/GameObjects/Shooter/PlayerShip.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/GameObject.h" 3 | #include "Core/Event.h" 4 | #include 5 | #include "Core/UIHandler.h" 6 | class PlayerShip : public GameObject 7 | { 8 | public: 9 | PlayerShip(class Scene &scene) : GameObject("PlayerShip", scene) 10 | { 11 | symbol = '\xDB'; 12 | } 13 | ~PlayerShip(); 14 | void Init() override; 15 | void Update(float deltaTime) override; 16 | void OnCollided(GameObject &other) override; 17 | 18 | private: 19 | UIData scoreUIData; 20 | std::shared_ptr scoreUIDataPtr; 21 | void Fire(int keyDown); 22 | void OnEvent(Event &event); 23 | std::function OnEventFunction; 24 | class ParticleSource *particleSource; 25 | int score = 0; 26 | }; 27 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/Scenes/Pong/PongScene.cpp: -------------------------------------------------------------------------------- 1 | #include "PongScene.h" 2 | #include "../../GameObjects/Pong/Ball.h" 3 | #include "../../GameObjects/Pong/Paddle.h" 4 | PongScene::PongScene() 5 | { 6 | 7 | 8 | } 9 | 10 | void PongScene::Init() 11 | { 12 | AddGameObject(new Paddle(1, *this), Vector2(SCREENWIDTH - 2, 10)); 13 | AddGameObject(new Paddle(0, *this), Vector2(1, 1)); 14 | AddGameObject(new Ball(), Vector2(10, 10)); 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/Scenes/Pong/PongScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/Scene.h" 3 | 4 | 5 | class PongScene : public Scene 6 | { 7 | 8 | public: 9 | PongScene(); 10 | 11 | ~PongScene() override 12 | { 13 | std::cout << "Destructed main scene" << '\n'; 14 | } 15 | 16 | void Init() override; 17 | }; 18 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/Scenes/Shooter/ShooterScene.cpp: -------------------------------------------------------------------------------- 1 | #include "ShooterScene.h" 2 | #include "../../GameObjects/Shooter/PlayerShip.h" 3 | #include "../../GameObjects/Shooter/Enemy.h" 4 | void ShooterScene::Init() 5 | { 6 | AddGameObject(new PlayerShip(*this), Vector2(20,15)); 7 | srand(static_cast(time(nullptr))); 8 | SpawnEnemy(); 9 | 10 | } 11 | 12 | void ShooterScene::Update(float deltaTime) 13 | { 14 | Scene::Update(deltaTime); 15 | //if (numberOfEnemyToSpawn > 0) 16 | spawnDurationPassed += deltaTime; 17 | 18 | 19 | if (spawnDurationPassed >= spawnTime) 20 | { 21 | spawnDurationPassed = 0; 22 | SpawnEnemy(); 23 | } 24 | 25 | } 26 | 27 | void ShooterScene::SpawnEnemy() 28 | { 29 | numberOfEnemyToSpawn--; 30 | int random_number = rand() % 15; 31 | 32 | AddGameObject(new Enemy(*this), Vector2(70, random_number + 4)); 33 | } 34 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/Scenes/Shooter/ShooterScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Core/Scene.h" 3 | class ShooterScene : public Scene 4 | { 5 | public: 6 | ~ShooterScene() override 7 | { 8 | std::cout << "Destructed main scene" << '\n'; 9 | } 10 | void Init() override; 11 | void Update(float deltaTime) override; 12 | 13 | private: 14 | void SpawnEnemy(); 15 | 16 | int numberOfEnemyToSpawn = 10; 17 | float spawnTime = 1.f; 18 | float spawnDurationPassed = 0; 19 | }; 20 | 21 | -------------------------------------------------------------------------------- /CraftShooterGame/Source/ShooterGameEntry.cpp: -------------------------------------------------------------------------------- 1 | #include "Scenes/Pong/PongScene.h" 2 | 3 | #include "Scenes/Shooter/ShooterScene.h" 4 | #include "Core/EntryPoint.h" 5 | #include 6 | class ShooterGame 7 | { 8 | public: 9 | ~ShooterGame() 10 | { 11 | } 12 | Engine engine; 13 | void StartGame() 14 | { 15 | engine.scenes.push_back(new ShooterScene()); 16 | engine.StartGame(); 17 | } 18 | void Clean() 19 | { 20 | engine.Clean(); 21 | } 22 | }; 23 | 24 | int main() 25 | { 26 | ShooterGame *game = new ShooterGame(); 27 | game->StartGame(); 28 | game->Clean(); 29 | delete game; 30 | system("pause"); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 ural89 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Console Craft Engine 2 | #### Console Craft Engine is a **terminal-based 2D game engine** written in C++. 3 | #### Written in C++ 4 | #### Will work in Linux and Windows 5 | #### Box2D physics engine implemented 6 | #### Now editor available: https://github.com/ural89/ConsoleCraftEditor 7 | ## Install 8 | #### yay -S craftrogue 9 | ## Build: 10 | #### cd ../../CraftShooterGame 11 | #### mkdir build 12 | #### cd build 13 | #### cmake .. 14 | #### make 15 | ## Documentation 16 | - [Quick Start Guide](docs/quickstart.md) 17 | 18 | #### Cherno review: https://youtu.be/B6pM9KcIFE4?si=oq9N51VUhzaLH95U 19 | ![](https://i.imgur.com/BLq1ezI.gif) 20 | ![](https://i.imgur.com/9d8UEKM.gif) 21 | ![](https://i.imgur.com/CzWwIyO.gif) 22 | ![](https://i.imgur.com/IBZJttl.gif) 23 | 24 | -------------------------------------------------------------------------------- /Source/Core.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef __GNUC__ 4 | #ifdef GE_BUILD_DLL 5 | #define GE_API __attribute__((visibility("default"))) 6 | #else 7 | #define GE_API 8 | #endif 9 | #else 10 | #ifdef GE_BUILD_DLL 11 | #define GE_API __declspec(dllexport) 12 | #else 13 | #define GE_API __declspec(dllimport) 14 | #endif 15 | #endif 16 | 17 | 18 | const int SCREENHEIGHT = 23; 19 | const int SCREENWIDTH = 80; 20 | 21 | -------------------------------------------------------------------------------- /Source/Core/Camera.cpp: -------------------------------------------------------------------------------- 1 | #include "Camera.h" 2 | #include 3 | #include "../CoreStructs/Vector.h" 4 | void Camera::StartShake(float duration) 5 | { 6 | if (isMoving) 7 | return; 8 | shakeDuration = duration; 9 | xAcceleration = xAccelerationStart; 10 | isMoving = true; 11 | } 12 | 13 | void Camera::UpdateCamera(float deltaTime) 14 | { 15 | if (shakeDuration > 0) 16 | ShakeCamera(deltaTime); 17 | } 18 | 19 | void Camera::MoveCameraRight() 20 | { 21 | MoveCamera(Vector2(1, 0)); 22 | } 23 | 24 | void Camera::MoveCameraLeft() 25 | { 26 | MoveCamera(Vector2(-1, 0)); 27 | } 28 | 29 | void Camera::MoveCameraUp() 30 | { 31 | MoveCamera(Vector2(0, -1)); 32 | } 33 | 34 | void Camera::MoveCameraDown() 35 | { 36 | MoveCamera(Vector2(0, 1)); 37 | } 38 | 39 | void Camera::MoveCamera(Vector2 moveAmount) 40 | { 41 | PreviousOffset = Vector2(offsetX + moveAmount.X, offsetY + moveAmount.Y).ToInt(); 42 | offsetX += moveAmount.X; 43 | offsetY += moveAmount.Y; 44 | int moveXDirection = (int) offsetX - (int)(offsetX + moveAmount.X); 45 | int moveYDirection = (int) offsetY - (int)(offsetY + moveAmount.Y); 46 | 47 | if(moveXDirection > 0) HasMovedDirection = RIGHTDIRECTION * moveAmount.Length(); 48 | else if(moveXDirection < 0) HasMovedDirection = LEFTDIRECTION * moveAmount.Length(); 49 | else if(moveYDirection > 0) HasMovedDirection = UPDIRECTION * moveAmount.Length(); 50 | else if(moveYDirection < 0) HasMovedDirection = DOWNDIRECTION * moveAmount.Length(); 51 | } 52 | 53 | void Camera::ShakeCamera(float deltaTime) 54 | { 55 | 56 | if (isGoingLeft) 57 | { 58 | offsetX = deltaTime * xAcceleration; 59 | offsetY = -deltaTime * xAcceleration; 60 | if (offsetX >= 0.01f) 61 | { 62 | isGoingLeft = false; 63 | } 64 | else 65 | { 66 | // xAcceleration += 0.5f * deltaTime; 67 | } 68 | } 69 | else 70 | { 71 | offsetX = -deltaTime * xAcceleration; 72 | offsetY = deltaTime * xAcceleration; 73 | if (offsetX <= -0.01f) 74 | { 75 | isGoingLeft = true; 76 | } 77 | else 78 | { 79 | // xAcceleration += 0.5f * deltaTime; 80 | } 81 | } 82 | shakeDuration -= deltaTime; 83 | if (shakeDuration <= 0) 84 | { 85 | isMoving = false; 86 | offsetX = 0; 87 | offsetY = 0; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /Source/Core/Camera.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Core.h" 3 | #include "../CoreStructs/Vector.h" 4 | class GE_API Camera 5 | { 6 | 7 | public: 8 | void StartShake(float duration); 9 | void UpdateCamera(float deltaTime); 10 | void MoveCameraRight(); 11 | void MoveCameraLeft(); 12 | void MoveCameraUp(); 13 | void MoveCameraDown();// for some reason moving camera with float values doesnt work 14 | 15 | bool isMoving = false; 16 | int HasMovedDirection = 0; 17 | Vector2 PreviousOffset; 18 | int offsetX = 0; 19 | int offsetY = 0; 20 | 21 | private: 22 | void MoveCamera(class Vector2 moveAmount); 23 | void ShakeCamera(float deltaTime); 24 | float xAcceleration; 25 | float xAccelerationStart = 10; 26 | bool isGoingLeft = true; 27 | float shakeDuration = 0.f; 28 | }; 29 | -------------------------------------------------------------------------------- /Source/Core/Collision.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Core.h" 3 | #include "GameObject.h" 4 | #include 5 | #include 6 | // this for trigger collisions 7 | class GE_API Collision 8 | { 9 | public: 10 | void AddGameObject(GameObject *gameObject); 11 | void RemoveGameObject(GameObject *gameObject); 12 | void CheckForCollisions(); 13 | void CheckForBorderCollisions() const; 14 | GameObject* GetCollidedObject(Vector2 position); 15 | ~Collision(); 16 | 17 | private: 18 | std::set> collidingPairs; 19 | std::vector gameObjects; 20 | }; 21 | -------------------------------------------------------------------------------- /Source/Core/Component/AI/AIMovement.cpp: -------------------------------------------------------------------------------- 1 | #include "AIMovement.h" 2 | #include "../../../Core/GameObject.h" 3 | #include "../../../Core/Scene.h" 4 | void AIMovement::Init() 5 | { 6 | UpdatePath(); 7 | } 8 | void AIMovement::Update(float deltaTime) 9 | { 10 | if (m_TargetTransform != nullptr) 11 | { 12 | if (m_Path.size() > m_CurrentPathCorner + 2) 13 | { 14 | Vector2 moveDirection = (Vector2(m_Path[m_CurrentPathCorner + 1].x, m_Path[m_CurrentPathCorner + 1].y) - owner->transform.Position); 15 | if (moveDirection == Vector2(0, 0) || moveDirection.Length() < 1.f) 16 | { 17 | m_CurrentPathCorner++; 18 | } 19 | moveDirection.Normalize(); 20 | owner->transform.MovePosition(moveDirection.X * deltaTime * m_MoveSpeed, moveDirection.Y * deltaTime * m_MoveSpeed); 21 | } 22 | m_TimeElapsedSincePathUpdate += deltaTime; 23 | if (m_TimeElapsedSincePathUpdate > m_UpdatePathDuration) 24 | { 25 | if (m_TargetLastPosition != m_TargetTransform->Position) 26 | { 27 | UpdatePath(); 28 | m_CurrentPathCorner = 0; 29 | m_TimeElapsedSincePathUpdate = 0; 30 | } 31 | m_TargetLastPosition = m_TargetTransform->Position; 32 | } 33 | } 34 | 35 | } 36 | 37 | void AIMovement::UpdatePath() 38 | { 39 | PathNode ownerNode(owner->transform.Position.X, owner->transform.Position.Y); 40 | PathNode targetNode(m_TargetTransform->GetCenterPosition().X, m_TargetTransform->GetCenterPosition().Y); 41 | m_Path = owner->GetCurrentScene().pathfinder->FindPath(ownerNode, targetNode); 42 | } 43 | -------------------------------------------------------------------------------- /Source/Core/Component/AI/AIMovement.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../CoreStructs/Transform.h" 3 | #include "Pathfinder.h" 4 | #include "../../../Core/Component/Component.h" 5 | class AIMovement : public Component 6 | { 7 | public: 8 | AIMovement(GameObject& gameObject) : Component(gameObject) 9 | { 10 | 11 | } 12 | 13 | void SetTargetTransform(const Transform* transform) 14 | { 15 | m_TargetTransform = transform; 16 | } 17 | void Init() override; 18 | void Update(float deltaTime) override; 19 | 20 | 21 | private: 22 | float m_MoveSpeed = 3; 23 | float m_TimeElapsedSincePathUpdate = 0; 24 | float m_UpdatePathDuration = 0.5f; 25 | int m_CurrentPathCorner = 0; 26 | 27 | Vector2 m_TargetLastPosition; 28 | 29 | private: 30 | const Transform *m_TargetTransform; 31 | std::vector m_Path; 32 | private: 33 | void UpdatePath(); 34 | }; -------------------------------------------------------------------------------- /Source/Core/Component/AI/Pathfinder.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | struct PathNode 8 | { 9 | int x, y; 10 | int f, g, h; 11 | 12 | PathNode(int r, int c); 13 | 14 | bool operator>(const PathNode &other) const; 15 | bool operator==(const PathNode &other) const; 16 | }; 17 | class Pathfinder 18 | { 19 | public: 20 | Pathfinder(const class Scene &scene); 21 | std::vector FindPath(const PathNode &start, const PathNode &goal); 22 | 23 | private: 24 | const Scene &m_Scene; 25 | std::vector> m_Map; 26 | int m_FinderGameObjectWidth = 4; 27 | int m_FinderGameObjectHeight = 4; 28 | 29 | }; 30 | -------------------------------------------------------------------------------- /Source/Core/Component/Component.cpp: -------------------------------------------------------------------------------- 1 | #include "Component.h" 2 | -------------------------------------------------------------------------------- /Source/Core/Component/Component.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | class Component 3 | { 4 | public: 5 | Component(class GameObject& go) : owner(&go) { owner = &go; } 6 | virtual ~Component() 7 | { 8 | 9 | } 10 | virtual void Init() {}; 11 | virtual void Update(float deltaTime) {}; 12 | protected: 13 | GameObject* owner; 14 | }; 15 | 16 | -------------------------------------------------------------------------------- /Source/Core/Component/DestructionEffectComponent.cpp: -------------------------------------------------------------------------------- 1 | #include "DestructionEffectComponent.h" 2 | #include "../ParticleSystem/ParticleSource.h" 3 | #include "../GameObject.h" 4 | #include "../../CoreStructs/Vector.h" 5 | 6 | void DestructionEffectComponent::Init() 7 | { 8 | particleSource = new ParticleSource(*owner); 9 | } 10 | 11 | void DestructionEffectComponent::Update(float deltaTime) 12 | { 13 | } 14 | 15 | void DestructionEffectComponent::StartEffect() 16 | { 17 | for (int j = 0; j < owner->GetHeight(); j++) { 18 | for (int i = 0; i < owner->GetWidth(); i++) { 19 | owner->sprite[j][i] = 0; 20 | particleSource->EmitParticle(1, BRICKPARTICLE, Vector2(i, j)); //TODO: give color of the owner 21 | } 22 | } 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Source/Core/Component/DestructionEffectComponent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../Core.h" 3 | #include "Component.h" 4 | class GE_API DestructionEffectComponent : public Component 5 | { 6 | public: 7 | DestructionEffectComponent(GameObject& owner) : Component(owner) 8 | { 9 | 10 | } 11 | void Init() override; 12 | void Update(float deltaTime) override; 13 | void StartEffect(); 14 | private: 15 | class ParticleSource* particleSource; 16 | }; -------------------------------------------------------------------------------- /Source/Core/Component/PlayerController.cpp: -------------------------------------------------------------------------------- 1 | #include "PlayerController.h" 2 | #include "../GameObject.h" 3 | 4 | #include "../Input.h" 5 | #include "../Scene.h" 6 | PlayerController::PlayerController(GameObject &go, int playerNo) : Component(go) 7 | { 8 | this->playerNo = playerNo; 9 | } 10 | 11 | void PlayerController::Init() 12 | { 13 | inputEvent = std::bind(&PlayerController::OnKeyDown, this, std::placeholders::_1); 14 | Input::AddListener(inputEvent); 15 | 16 | } 17 | 18 | void PlayerController::Update(float deltaTime) 19 | { 20 | } 21 | 22 | void PlayerController::OnKeyDown(int input) 23 | { 24 | if(owner) 25 | { 26 | if (!owner->GetCurrentScene().isPaused) 27 | { 28 | if (playerNo == 0) 29 | { 30 | if (tolower(Input::GetKeyDown()) == 'd') 31 | { 32 | owner->transform.MovePosition(moveSpeed, 0); 33 | } 34 | if (tolower(Input::GetKeyDown()) == 'a') 35 | { 36 | owner->transform.MovePosition(-moveSpeed, 0); 37 | } 38 | if (tolower(Input::GetKeyDown()) == 's') 39 | { 40 | owner->transform.MovePosition(0, moveSpeed); 41 | } 42 | if (tolower(Input::GetKeyDown()) == 'w') 43 | { 44 | owner->transform.MovePosition(0, -moveSpeed); 45 | } 46 | } 47 | if (playerNo == 1) 48 | { 49 | if (tolower(Input::GetKeyDown()) == 'k') 50 | { 51 | owner->transform.MovePosition(1, 0); 52 | } 53 | if (tolower(Input::GetKeyDown()) == 'h') 54 | { 55 | owner->transform.MovePosition(-1, 0); 56 | } 57 | if (tolower(Input::GetKeyDown()) == 'j') 58 | { 59 | owner->transform.MovePosition(0, 1); 60 | } 61 | if (tolower(Input::GetKeyDown()) == 'u') 62 | { 63 | owner->transform.MovePosition(0, -1); 64 | } 65 | } 66 | } 67 | } 68 | } 69 | 70 | void PlayerController::RemoveListenerForInput() 71 | { 72 | Input::RemoveListener(inputEvent); 73 | } 74 | -------------------------------------------------------------------------------- /Source/Core/Component/PlayerController.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Component.h" 3 | #include "../../Core.h" 4 | #include 5 | class GE_API PlayerController : public Component 6 | { 7 | public: 8 | PlayerController(class GameObject& go, int playerNo); 9 | void Init() override; 10 | void Update(float deltaTime) override; 11 | void OnKeyDown(int input); 12 | void RemoveListenerForInput(); 13 | void SetMoveSpeed(int newSpeed) 14 | { 15 | moveSpeed = newSpeed; 16 | } 17 | private: 18 | int playerNo = 0; 19 | int moveSpeed = 1; 20 | std::function inputEvent; 21 | }; 22 | 23 | -------------------------------------------------------------------------------- /Source/Core/Component/RigidbodyComponent.cpp: -------------------------------------------------------------------------------- 1 | #include "RigidbodyComponent.h" 2 | #include "../Scene.h" 3 | void Rigidbody::Init() 4 | { 5 | // owner->GetCurrentScene().collisionResolver->AddRigidbody(this); 6 | // Create dynamic body 7 | b2BodyDef bodyDef; 8 | bodyDef.fixedRotation = true; // freeze rotation 9 | bodyDef.type = b2_dynamicBody; 10 | bodyDef.position.Set(owner->transform.Position.X, owner->transform.Position.Y); 11 | 12 | body = owner->GetCurrentScene().world->CreateBody(&bodyDef); 13 | b2PolygonShape dynamicBox; 14 | dynamicBox.SetAsBox(owner->GetWidth() , owner->GetHeight()); 15 | 16 | b2FixtureDef fixtureDef; 17 | fixtureDef.shape = &dynamicBox; 18 | fixtureDef.density = 1; 19 | fixtureDef.friction = 0.1f; 20 | fixtureDef.restitution = 0.8f; 21 | body->CreateFixture(&fixtureDef); 22 | 23 | debugUI.position = Vector2(0, 29); 24 | debugUIPtr = std::make_shared(debugUI); 25 | owner->GetCurrentScene().uiHandler->AddString(debugUIPtr); 26 | } 27 | Rigidbody::~Rigidbody() 28 | { 29 | owner->GetCurrentScene().collisionResolver->RemoveRigidbody(this); 30 | } 31 | void Rigidbody::Update(float deltaTime) 32 | { 33 | elapsedSinceCollision += deltaTime; 34 | owner->transform.SetPosition(body->GetPosition().x, body->GetPosition().y); 35 | // body->ApplyForce(b2Vec2(500, 0), body->GetWorldCenter() , true); 36 | // ProcessForce(deltaTime); 37 | // ProcessTorque(deltaTime); 38 | } 39 | 40 | void Rigidbody::ProcessForce(float deltaTime) 41 | { 42 | } 43 | void Rigidbody::ProcessTorque(float deltaTime) 44 | { 45 | } 46 | void Rigidbody::AddTorque(float torque) 47 | { 48 | } 49 | void Rigidbody::OnCollided(Rigidbody &other) 50 | { 51 | 52 | if (elapsedSinceCollision <= collisionFreq) 53 | return; 54 | elapsedSinceCollision = 0; 55 | } 56 | 57 | void Rigidbody::OnCollidedBorder(int border) 58 | { 59 | } 60 | 61 | void Rigidbody::AddForce(Vector2 force) 62 | { 63 | 64 | body->ApplyForce(b2Vec2(force.X, force.Y), body->GetWorldCenter(), true); 65 | } 66 | 67 | void Rigidbody::AddPulse(Vector2 pulse) 68 | { 69 | body->ApplyLinearImpulse(b2Vec2(pulse.X, pulse.Y), body->GetWorldCenter(), true); 70 | } 71 | -------------------------------------------------------------------------------- /Source/Core/Component/RigidbodyComponent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Component.h" 3 | #include "../GameObject.h" 4 | #include "../../CoreStructs/Vector.h" 5 | #include "../UIHandler.h" 6 | #include "../../Core.h" 7 | class b2Body; 8 | class GE_API Rigidbody : public Component 9 | { 10 | 11 | public: 12 | Rigidbody(GameObject &gameObject) : Component(gameObject) 13 | { 14 | } 15 | ~Rigidbody(); 16 | void Init() override; 17 | void Update(float deltaTime) override; 18 | void AddTorque(float torque); 19 | void OnCollided(Rigidbody &other); 20 | void OnCollidedBorder(int border); 21 | void AddForce(Vector2 force); 22 | void AddPulse(Vector2 pulse); 23 | 24 | Transform &GetTransform() 25 | { 26 | 27 | return owner->transform; 28 | }; 29 | float GetMass() const 30 | { 31 | return mass; 32 | } 33 | int GetWidth() const 34 | { 35 | return owner->GetWidth(); 36 | } 37 | int GetHeight() const 38 | { 39 | return owner->GetHeight(); 40 | } 41 | Vector2 GetCenterPosition() 42 | { 43 | return Vector2(owner->transform.Position.X + GetWidth() / 2, owner->transform.Position.Y + GetHeight() / 2); 44 | } 45 | b2Body *GetBody() const 46 | { 47 | return body; 48 | } 49 | Vector2 gravity = Vector2(0, 0); 50 | Vector2 velocity = Vector2(0.00001f, 0); // To prevent clear flag issues 51 | 52 | bool isKinematic = false; 53 | bool canCollide = true; 54 | bool canCollideBorder = true; 55 | bool isDebug = false; 56 | float drag = 0.9; 57 | 58 | private: 59 | std::shared_ptr debugUIPtr; 60 | UIData debugUI; 61 | 62 | float mass = 1; 63 | 64 | float momentOfInertia = 0; 65 | float angularVelocity = 0; 66 | float angularDrag = 0.3; 67 | b2Body *body; 68 | Vector2 force; 69 | Vector2 pulse; 70 | 71 | float elapsedSinceCollision = 0; 72 | float collisionFreq = 0.1f; 73 | 74 | void ProcessForce(float deltaTime); 75 | void ProcessTorque(float deltaTime); 76 | }; 77 | -------------------------------------------------------------------------------- /Source/Core/Component/SpringComponent.cpp: -------------------------------------------------------------------------------- 1 | #include "SpringComponent.h" 2 | 3 | SpringComponent::~SpringComponent() 4 | { 5 | } 6 | 7 | void SpringComponent::Init() 8 | { 9 | torq = 50; 10 | } 11 | 12 | void SpringComponent::Update(float deltaTime) 13 | { 14 | if (firstAttachment != nullptr && secondAttachment != nullptr) 15 | { 16 | Vector2 force = firstAttachment->GetCenterPosition() - secondAttachment->GetCenterPosition(); //TODO: this causes starting bug 17 | force = (force - startOffset) * stiffness; 18 | secondAttachment->AddForce(force / 2); 19 | firstAttachment->AddForce((force * -1) / 2); 20 | 21 | UpdateRotation(deltaTime); 22 | } 23 | } 24 | 25 | void SpringComponent::AddTorque(float _torque) 26 | { 27 | // if (this != nullptr) 28 | this->torq += _torque; 29 | } 30 | 31 | void SpringComponent::UpdateRotation(float deltaTime) 32 | { 33 | angularVelocity += torq * deltaTime; 34 | angularVelocity *= (1 - 0.6 * deltaTime) ; 35 | float springAngle = angularVelocity * (3.14159f / 180.0f) * deltaTime; 36 | // std::cout<< angularVelocity << '\n'; 37 | float newX = std::cos(springAngle) * startOffset.X - std::sin(springAngle) * startOffset.Y; 38 | float newY = std::sin(springAngle) * startOffset.X + std::cos(springAngle) * startOffset.Y; 39 | startOffset.X = newX; 40 | startOffset.Y = newY; 41 | torq = 0; 42 | } 43 | -------------------------------------------------------------------------------- /Source/Core/Component/SpringComponent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Component.h" 3 | #include "../GameObject.h" 4 | #include "../Component/RigidbodyComponent.h" 5 | #include "../../CoreStructs/Vector.h" 6 | class GE_API SpringComponent : public Component 7 | { 8 | public: 9 | SpringComponent(GameObject &owner, float stiffness) : Component(owner), stiffness(stiffness) 10 | { 11 | } 12 | ~SpringComponent(); 13 | void Init() override; 14 | void Update(float deltaTime) override; 15 | void SetAttachments(Rigidbody *first, Rigidbody *second) 16 | { 17 | firstAttachment = first; 18 | secondAttachment = second; 19 | startOffset = first->GetCenterPosition() - second->GetCenterPosition(); 20 | } 21 | 22 | float stiffness = 400; 23 | float torq = 0; 24 | void AddTorque(float _torque); 25 | 26 | private: 27 | 28 | void UpdateRotation(float deltaTime); 29 | 30 | Rigidbody *firstAttachment = nullptr; 31 | Rigidbody *secondAttachment = nullptr; 32 | 33 | float angularVelocity = 0; 34 | float angularDrag = 0.5f; 35 | 36 | Vector2 startOffset; 37 | }; -------------------------------------------------------------------------------- /Source/Core/EntryPoint.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Core/Renderer.h" 4 | #include "Core/Scene.h" 5 | #include "Core/SceneManager.h" 6 | #include 7 | class Engine 8 | { 9 | public: 10 | std::vector scenes; 11 | 12 | void StartGame() 13 | { 14 | SceneManager::ChangeScene(scenes[0]); 15 | auto startTime = std::chrono::high_resolution_clock::now(); 16 | auto prevTime = startTime; 17 | 18 | while (Input::GetKeyDown() != 27) 19 | { 20 | if (SceneManager::CurrentScene != nullptr) 21 | { 22 | auto endTime = std::chrono::high_resolution_clock::now(); 23 | auto deltaTimeInSeconds = std::chrono::duration_cast>(endTime - prevTime).count(); 24 | // double deltaTimeMilliseconds = deltaTimeInSeconds * 1000.0; 25 | 26 | if (SceneManager::CurrentScene->hasGameOver) 27 | return; 28 | UpdateInput(); 29 | Update(deltaTimeInSeconds); 30 | Render(); 31 | prevTime = endTime; 32 | } 33 | } 34 | } 35 | void Clean() 36 | { 37 | delete SceneManager::CurrentScene; 38 | Input::Cleanup(); 39 | } 40 | void InitScene(Scene *scene) 41 | { 42 | scene->Init(); 43 | scene->Start(); 44 | } 45 | 46 | private: 47 | Renderer renderer; 48 | void UpdateInput() 49 | { 50 | Input::Update(); 51 | } 52 | void Update(float deltaTime) 53 | { 54 | SceneManager::CurrentScene->Update(deltaTime); 55 | SceneManager::CurrentScene->SpawnQueuedGameObjects(); 56 | } 57 | void UpdateCamera(float deltaTime) 58 | { 59 | SceneManager::CurrentScene->UpdateCamera(deltaTime); 60 | } 61 | void Render() 62 | { 63 | renderer.Render(*SceneManager::CurrentScene); 64 | } 65 | void SetCurrentScene(Scene *scene) 66 | { 67 | #ifdef __GNUC__ 68 | system("clear"); 69 | #else 70 | system("cls"); 71 | #endif 72 | // currentScene = scene; 73 | InitScene(SceneManager::CurrentScene); 74 | } 75 | }; -------------------------------------------------------------------------------- /Source/Core/Event.cpp: -------------------------------------------------------------------------------- 1 | #include "Event.h" 2 | -------------------------------------------------------------------------------- /Source/Core/Event.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | enum class EventType 3 | { 4 | None = 0, 5 | OnEnemySpawned = 1, 6 | OnEnemyKilled = 2, 7 | }; 8 | class Event 9 | { 10 | public: 11 | Event(EventType eventType) 12 | { 13 | this->eventType = eventType; 14 | } 15 | EventType GetEventType() const 16 | { 17 | return eventType; 18 | } 19 | 20 | private: 21 | EventType eventType; 22 | }; 23 | 24 | -------------------------------------------------------------------------------- /Source/Core/EventDispatcher.cpp: -------------------------------------------------------------------------------- 1 | #include "EventDispatcher.h" 2 | #include 3 | std::vector> listeners; 4 | 5 | void EventDispatcher::AddListener(std::function& func) 6 | { 7 | listeners.push_back(func); 8 | } 9 | void EventDispatcher::RemoveListener(std::function func) 10 | { 11 | auto it = std::remove_if(listeners.begin(), listeners.end(), 12 | [func](const auto& listener) { 13 | return listener.target_type() == func.target_type(); 14 | }); 15 | listeners.erase(it, listeners.end()); 16 | } 17 | 18 | void EventDispatcher::CallEvent(Event& event) 19 | { 20 | for (auto& listener : listeners) 21 | { 22 | listener(event); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Source/Core/EventDispatcher.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Core.h" 3 | #include 4 | #include 5 | #include "Event.h" 6 | class GE_API EventDispatcher 7 | { 8 | public: 9 | static void AddListener(std::function& func); 10 | static void RemoveListener(std::function func); 11 | static void CallEvent(Event& event); 12 | private: 13 | 14 | }; 15 | 16 | -------------------------------------------------------------------------------- /Source/Core/GameObject.cpp: -------------------------------------------------------------------------------- 1 | #include "GameObject.h" 2 | 3 | -------------------------------------------------------------------------------- /Source/Core/Input.cpp: -------------------------------------------------------------------------------- 1 | #ifdef __GNUC__ 2 | #include "../conio.h" 3 | #else 4 | #include 5 | #endif 6 | #include "Input.h" 7 | #include 8 | #include 9 | int Input::keyDown = 0; // Initialize the static member variable 10 | 11 | Input::Input() {} 12 | std::vector> Input::listeners; 13 | void Input::Update() 14 | { 15 | #ifdef __GNUC__ 16 | if (kbhit()) 17 | #else 18 | if(_kbhit()) 19 | #endif 20 | { 21 | #ifdef __GNUC__ 22 | keyDown = getch(); 23 | #else 24 | keyDown = _getch(); 25 | #endif 26 | 27 | for (auto &listener:listeners) 28 | { 29 | listener(keyDown); 30 | } 31 | } 32 | else 33 | { 34 | keyDown = 0; 35 | } 36 | } 37 | 38 | int Input::GetKeyDown() 39 | { 40 | return keyDown; 41 | } 42 | 43 | void Input::AddListener(std::function func) 44 | { 45 | listeners.push_back(func); 46 | } 47 | 48 | void Input::RemoveListener(std::function func) 49 | { 50 | auto it = std::remove_if(listeners.begin(), listeners.end(), 51 | [func](const auto& listener) { 52 | return listener.target_type() == func.target_type(); 53 | }); 54 | listeners.erase(it, listeners.end()); 55 | } 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Source/Core/Input.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Core.h" 3 | #include 4 | #include 5 | const int SPACEBAR = 32; 6 | const int KEY1 = 49; 7 | const int KEY2 = 50; 8 | const int KEY3 = 51; 9 | #define BIND_EVENT_FN(x) std::bind(&x, this, std::placeholders::_1) 10 | class GE_API Input 11 | { 12 | public: 13 | Input(); 14 | static void Update(); 15 | static int GetKeyDown(); 16 | static void AddListener(std::function func); 17 | static void RemoveListener(std::function func); 18 | static void Cleanup() 19 | { 20 | listeners.clear(); 21 | } 22 | 23 | 24 | 25 | private: 26 | 27 | static int keyDown; 28 | static std::vector> listeners; 29 | }; -------------------------------------------------------------------------------- /Source/Core/LineDrawer.cpp: -------------------------------------------------------------------------------- 1 | #include "LineDrawer.h" 2 | #include "Scene.h" 3 | 4 | void LineDrawer::DrawLine(Vector2 startPosition, Vector2 endPosition) 5 | { 6 | Vector2 direction = endPosition - startPosition; 7 | int length = direction.LengthInt(); 8 | if(length == 0) return; 9 | direction.Normalize(); 10 | if (length < lines.size()) 11 | { 12 | for (size_t i = 0; i < length; i++) 13 | { 14 | 15 | Vector2 newPosition = direction * i + startPosition; 16 | if (drawingParticleIndex < lines.size()) 17 | { 18 | lines[drawingParticleIndex]->transform.SetPosition(newPosition.X, newPosition.Y); 19 | lines[drawingParticleIndex]->isRenderable = true; 20 | drawingParticleIndex++; 21 | } 22 | } 23 | } 24 | } 25 | 26 | void LineDrawer::CreateLineParticles(int amount, int color) 27 | { 28 | for (size_t i = 0; i < amount; i++) 29 | { 30 | LineParticle *polygonLineParticle = new LineParticle(scene); 31 | lines.push_back(polygonLineParticle); 32 | scene.AddGameObject(polygonLineParticle); 33 | polygonLineParticle->overrideColor = color; 34 | polygonLineParticle->isRenderable = true; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Source/Core/LineDrawer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "../CoreStructs/Vector.h" 4 | #include "Physics/Polygon/LineParticle.h" 5 | class LineDrawer 6 | { 7 | public: 8 | LineDrawer(class Scene &scene) : scene(scene) 9 | { 10 | } 11 | ~LineDrawer() 12 | { 13 | lines.clear(); 14 | } 15 | void ResetDrawingParticleIndex() // call this every frame (after drawing multiple line) 16 | { 17 | for (size_t i = drawingParticleIndex; i < lines.size(); i++) 18 | { 19 | if (!lines[i]->isRenderable) 20 | continue; 21 | lines[i]->SetRenderable(false); 22 | } 23 | drawingParticleIndex = 0; 24 | } 25 | void DrawLine(Vector2 startPosition, Vector2 endPosition); 26 | void CreateLineParticles(int amount, int color); 27 | void ClearLines() 28 | { 29 | for (auto lineParticles : lines) 30 | { 31 | lineParticles->Destroy(); // TODO: include lineparticle 32 | } 33 | lines.clear(); 34 | } 35 | int drawingParticleIndex = 0; 36 | 37 | private: 38 | std::vector lines; 39 | Vector2 lastDrawnLinePosition; 40 | Scene &scene; 41 | }; -------------------------------------------------------------------------------- /Source/Core/ParticleSystem/BlastParticle.cpp: -------------------------------------------------------------------------------- 1 | #include "BlastParticle.h" 2 | 3 | void BlastParticle::Update(float deltaTime) 4 | { 5 | transform.MovePosition(velocity.X * deltaTime, velocity.Y * deltaTime); 6 | velocity.Y += gravity * deltaTime; 7 | 8 | if (lastPosition != transform.Position.ToInt()) 9 | { 10 | lastPosition = transform.Position.ToInt(); 11 | trailPositions[trailCount] = lastPosition; 12 | //GetCurrentScene(todo particle trail).AddGameObject() 13 | } 14 | } 15 | 16 | void BlastParticle::OnCollidedBorder(int border) 17 | { 18 | Destroy(); 19 | } 20 | -------------------------------------------------------------------------------- /Source/Core/ParticleSystem/BlastParticle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ParticleObject.h" 3 | #include "../../Core.h" 4 | class GE_API BlastParticle : public ParticleObject 5 | { 6 | public: 7 | BlastParticle(Scene& scene, int particleType, class Vector2 startVelocity) : 8 | ParticleObject(scene, particleType) { 9 | this->velocity = startVelocity; 10 | }; 11 | void Update(float deltaTime) override; 12 | void OnCollidedBorder(int border) override; 13 | private: 14 | Vector2 velocity; 15 | }; 16 | 17 | -------------------------------------------------------------------------------- /Source/Core/ParticleSystem/ParticleObject.cpp: -------------------------------------------------------------------------------- 1 | #include "ParticleObject.h" 2 | #include "../../CoreStructs/Vector.h" 3 | 4 | ParticleObject::ParticleObject(Scene &scene, int particleType) : GameObject("Particle", scene) 5 | { 6 | 7 | switch (particleType) 8 | { 9 | case 0: 10 | sprite = fireParticle; 11 | break; 12 | case 1: 13 | sprite = enemyParticle; 14 | break; 15 | case 2: 16 | sprite = brickParticle; 17 | break; 18 | case 3: 19 | sprite = fireParticle; 20 | break; 21 | } 22 | symbol = '#'; 23 | } 24 | 25 | void ParticleObject::Init() 26 | { 27 | lastPosition = transform.Position.ToInt(); 28 | } 29 | 30 | void ParticleObject::Update(float deltaTime) 31 | { 32 | } 33 | 34 | void ParticleObject::OnCollidedBorder(int border) 35 | { 36 | } 37 | -------------------------------------------------------------------------------- /Source/Core/ParticleSystem/ParticleObject.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../Core.h" 3 | #include "../GameObject.h" 4 | #include 5 | 6 | class GE_API ParticleObject : public GameObject 7 | { 8 | public: 9 | ParticleObject(Scene& scene, int particleType); 10 | ~ParticleObject() 11 | { 12 | 13 | brickParticle.clear(); 14 | enemyParticle.clear(); 15 | fireParticle.clear(); 16 | 17 | } 18 | void Init() override; 19 | void Update(float deltaTime) override; 20 | void OnCollidedBorder(int border) override; 21 | protected: 22 | float gravity = 50; 23 | 24 | std::array trailPositions; 25 | int trailCount = 0; 26 | Vector2 lastPosition; 27 | std::vector> fireParticle = { {0,2}, {0,0} }; 28 | std::vector> enemyParticle = { {0,6}, {4,0} }; 29 | std::vector> brickParticle = { {2} }; 30 | }; 31 | 32 | -------------------------------------------------------------------------------- /Source/Core/ParticleSystem/ParticleSource.cpp: -------------------------------------------------------------------------------- 1 | #include "ParticleSource.h" 2 | #include "../Scene.h" 3 | #include "BlastParticle.h" 4 | #include "StaticBlastParticle.h" 5 | #include "WaveParticle.h" 6 | #include "../../CoreStructs/Transform.h" 7 | 8 | void ParticleSource::Init() 9 | { 10 | } 11 | 12 | void ParticleSource::Update(float deltaTime) 13 | { 14 | } 15 | 16 | void ParticleSource::EmitParticle(int count, int particleType, Vector2 localPosition) 17 | { 18 | if (particleType != STATICBLASTPARTICLE) 19 | { 20 | for (int i = 0; i < count; i++) 21 | { 22 | double randomAngle = static_cast(rand()) / RAND_MAX * 2 * 3.14f; 23 | 24 | double speed = 25.0; 25 | double randomVelocityX = speed * std::cos(randomAngle); 26 | double randomVelocityY = speed * std::sin(randomAngle); 27 | 28 | Vector2 randomVelocity(randomVelocityX, randomVelocityY); 29 | owner->GetCurrentScene().AddGameObject(new BlastParticle(owner->GetCurrentScene(), particleType, randomVelocity), owner->transform.Position + localPosition); 30 | } 31 | } 32 | else 33 | { 34 | owner->GetCurrentScene().AddGameObject(new StaticBlastParticle(owner->GetCurrentScene(), particleType), owner->transform.Position + localPosition); 35 | } 36 | } 37 | 38 | void ParticleSource::EmitWaveParticle(Transform &endTransform, Vector2 localPosition) 39 | { 40 | int distance = 70; 41 | for (int i = 0; i < distance; i++) 42 | { 43 | auto waveParticle = new WaveParticle(owner->GetCurrentScene(), 44 | ENEMYTYPEPARTICLE, endTransform, owner->transform, i); 45 | waveParticles.push_back(waveParticle); 46 | owner->GetCurrentScene().AddGameObject(waveParticle); 47 | } 48 | } 49 | 50 | void ParticleSource::ClearWaveParticles() 51 | { 52 | for (auto &waveParticle : waveParticles) 53 | { 54 | waveParticle->Destroy(); 55 | } 56 | waveParticles.clear(); 57 | } 58 | -------------------------------------------------------------------------------- /Source/Core/ParticleSystem/ParticleSource.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Component/Component.h" 3 | #include "../../Core.h" 4 | #include "../../CoreStructs/Vector.h" 5 | #include 6 | const int FIRETYPEPARTICLE = 0; 7 | const int ENEMYTYPEPARTICLE = 1; 8 | const int BRICKPARTICLE = 2; 9 | const int STATICBLASTPARTICLE = 3; 10 | struct Vector2; 11 | class GE_API ParticleSource : public Component 12 | { 13 | public: 14 | ParticleSource(class GameObject& owner) : Component(owner){} 15 | void Init() override; 16 | void Update(float deltaTime) override; 17 | void EmitParticle(int count, int particleType, Vector2 localPosition = Vector2(0,0)); 18 | void EmitWaveParticle(class Transform& endTransform, Vector2 localPosition); 19 | void ClearWaveParticles(); 20 | private: 21 | std::vector waveParticles; 22 | 23 | }; 24 | 25 | -------------------------------------------------------------------------------- /Source/Core/ParticleSystem/StaticBlastParticle.cpp: -------------------------------------------------------------------------------- 1 | #include "StaticBlastParticle.h" 2 | 3 | void StaticBlastParticle::Update(float deltaTime) 4 | { 5 | elapsedTime += deltaTime; 6 | if (elapsedTime > duration) 7 | { 8 | Destroy(); 9 | } 10 | if (elapsedTime > stage03Time) 11 | { 12 | sprite = { 13 | {1, 1}, 14 | {1, 1}}; 15 | } 16 | else if (elapsedTime > stage02Time) 17 | { 18 | sprite = { 19 | {1}, 20 | {1}}; 21 | } 22 | else if (elapsedTime > stage01Time) 23 | { 24 | sprite = { 25 | {1} 26 | 27 | }; 28 | } 29 | } 30 | 31 | void StaticBlastParticle::OnCollidedBorder(int border) 32 | { 33 | Destroy(); 34 | } 35 | -------------------------------------------------------------------------------- /Source/Core/ParticleSystem/StaticBlastParticle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ParticleObject.h" 3 | #include "../../Core.h" 4 | class GE_API StaticBlastParticle : public ParticleObject 5 | { 6 | public: 7 | StaticBlastParticle(Scene& scene, int particleType) : 8 | ParticleObject(scene, particleType) { 9 | 10 | }; 11 | void Update(float deltaTime) override; 12 | void OnCollidedBorder(int border) override; 13 | private: 14 | float duration = 0.3; 15 | float elapsedTime = 0; 16 | float stage01Time = 0.0f; 17 | float stage02Time = 0.1f; 18 | float stage03Time = 0.2f; 19 | }; 20 | 21 | -------------------------------------------------------------------------------- /Source/Core/ParticleSystem/WaveParticle.cpp: -------------------------------------------------------------------------------- 1 | #include "WaveParticle.h" 2 | #include "../../Core/Scene.h" 3 | void WaveParticle::Init() 4 | { 5 | } 6 | 7 | void WaveParticle::Update(float deltaTime) 8 | { 9 | duration += deltaTime; 10 | //speed += deltaTime * 10; 11 | speed += 4 * deltaTime; 12 | // amplitude -= deltaTime / 2 ; 13 | Vector2 direction = endTransform->Position - startTransform->Position; 14 | direction.Normalize(); 15 | direction = direction * index; 16 | 17 | float timeOffset = speed * duration; 18 | 19 | if (index < Vector2::Distance(endTransform->Position, startTransform->Position)) 20 | { 21 | float yOffset = amplitude * sin(frequency * index + timeOffset); 22 | transform.SetPosition(direction.X + startTransform->Position.X + 2, direction.Y + startTransform->Position.Y + yOffset + 2); 23 | isRenderable = true; 24 | } 25 | else 26 | { 27 | isRenderable = false; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Source/Core/ParticleSystem/WaveParticle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ParticleObject.h" 3 | 4 | class WaveParticle : public ParticleObject 5 | { 6 | public: 7 | WaveParticle(Scene& scene, int particleType, Transform& endTransform, 8 | Transform& startTransform, 9 | int index) : 10 | ParticleObject(scene, particleType) 11 | { 12 | sprite = { {10} }; 13 | symbol = '='; 14 | this->startTransform = &startTransform; 15 | this->endTransform = &endTransform; 16 | this->index = index; 17 | 18 | 19 | } 20 | ~WaveParticle() override 21 | { 22 | /*delete endTransform; 23 | delete startTransform;*///TODO: fix 24 | } 25 | void Init() override; 26 | void Update(float deltaTime); 27 | private: 28 | float duration = 0; 29 | 30 | float frequency = 0.2f; 31 | float amplitude = 2.f; 32 | float speed = 15.0f; 33 | 34 | Transform* endTransform; 35 | Transform* startTransform; 36 | int index; 37 | }; 38 | 39 | -------------------------------------------------------------------------------- /Source/Core/Physics/CollisionResolver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../Core.h" 3 | #include "../Component/RigidbodyComponent.h" 4 | #include 5 | #include "../../CoreStructs/Vector.h" 6 | #include "../UIHandler.h" 7 | 8 | class GE_API CollisionResolver 9 | { 10 | public: 11 | CollisionResolver(class Scene &scene); 12 | ~CollisionResolver() 13 | { 14 | rigidbodies.clear(); 15 | } 16 | void AddRigidbody(Rigidbody *rb); 17 | void RemoveRigidbody(Rigidbody *rb); 18 | void UpdateResolve(float deltaTime); 19 | bool HasOverlapped(Rigidbody *&first, Rigidbody *&second); 20 | void UpdateBorderResolve(float deltaTime); 21 | void SeparateObjects(Rigidbody *first, Rigidbody *second, float deltaTime); 22 | 23 | private: 24 | void ResolveCollision(Rigidbody *first, Rigidbody *second, float deltaTime); 25 | 26 | Vector2 Velocity; 27 | float Restitution; // Coefficient of restitution (elasticity) 28 | 29 | std::shared_ptr debugUIPtr; 30 | UIData debugUI; 31 | 32 | std::vector rigidbodies; 33 | }; -------------------------------------------------------------------------------- /Source/Core/Physics/JointRenderer.cpp: -------------------------------------------------------------------------------- 1 | #include "JointRenderer.h" 2 | #include "../LineDrawer.h" 3 | #include "../Scene.h" 4 | JointRenderer::JointRenderer(Scene &scene) : scene(scene) 5 | { 6 | lineDrawer = new LineDrawer(scene); 7 | int color = 1; 8 | lineDrawer->CreateLineParticles(100, color); 9 | } 10 | JointRenderer::~JointRenderer() 11 | { 12 | delete lineDrawer; 13 | lineDrawer = nullptr; 14 | } 15 | void JointRenderer::Update(float deltaTime) 16 | { 17 | lineDrawer->ResetDrawingParticleIndex(); 18 | for (b2Joint *joint = scene.world->GetJointList(); joint; joint = joint->GetNext()) 19 | { 20 | b2Vec2 anchorA = joint->GetAnchorA(); 21 | b2Vec2 anchorB = joint->GetAnchorB(); 22 | 23 | Vector2 startPosition(anchorA.x, anchorA.y); 24 | Vector2 endPosition(anchorB.x, anchorB.y); 25 | 26 | lineDrawer->DrawLine(startPosition, endPosition); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Source/Core/Physics/JointRenderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | class JointRenderer 3 | { 4 | public: 5 | JointRenderer(class Scene &scene); 6 | ~JointRenderer(); 7 | void Update(float deltaTime); 8 | private: 9 | Scene& scene; 10 | class LineDrawer* lineDrawer; 11 | }; -------------------------------------------------------------------------------- /Source/Core/Physics/Polygon/JointCreator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../../Core.h" 4 | #include "../../../CoreStructs/Vector.h" 5 | #include "PolygonCreator/PolygonCursor.h" 6 | class b2Body; 7 | class Scene; 8 | 9 | class GE_API JointCreator 10 | { 11 | public: 12 | JointCreator(Scene &scene); 13 | ~JointCreator() 14 | { 15 | 16 | } 17 | void SetBody(b2Body *body); 18 | 19 | private: 20 | b2Body *firstBody; 21 | Scene &scene; 22 | b2Body *secondBody; 23 | class Cursor* cursor; 24 | 25 | private: 26 | void OnInput(int input); 27 | void SetFirstBody(b2Body *firstBody); 28 | void SetSecondBody(b2Body *secondBody); 29 | void CreateDistanceJoint(); 30 | void CreateRevoluteJoint(); 31 | }; 32 | -------------------------------------------------------------------------------- /Source/Core/Physics/Polygon/LineParticle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../GameObject.h" 3 | #include "../../../Core.h" 4 | class GE_API LineParticle : public GameObject 5 | { 6 | public: 7 | LineParticle(class Scene &scene) : GameObject("LineParticle", scene) 8 | { 9 | hasCollider = false; 10 | isNavIgnore = true; 11 | } 12 | void Init() override 13 | { 14 | sprite = {{1}}; 15 | symbol = "o"; 16 | } 17 | bool isFixed = false; 18 | }; -------------------------------------------------------------------------------- /Source/Core/Physics/Polygon/Polygon.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../CoreStructs/Vector.h" 3 | #include "../../../Core.h" 4 | #include "../../../box2d/include/box2d/b2_body.h" 5 | #include "../../../box2d/include/box2d/b2_polygon_shape.h" 6 | #include "../../UIHandler.h" 7 | #include "../../LineDrawer.h" 8 | #include "PolygonParticle.h" 9 | #include "PolygonHelper.h" 10 | #include 11 | #include 12 | class GE_API Polygon // This is not registered in scene. So use this with shared ptr to clear yourself. 13 | { 14 | const int maxParticlesToDraw = 50; 15 | 16 | public: 17 | Polygon(class Scene &scene, bool isClosed = false, int color = 1) : scene(scene), isClosed(isClosed), color(color) 18 | { 19 | lineDrawer = new LineDrawer(scene); 20 | CreateLineParticles(maxParticlesToDraw, color); 21 | debugUIPtr = std::make_shared(); 22 | debugUIPtr->position = Vector2(5, 29); 23 | } 24 | ~Polygon() 25 | { 26 | particles.clear(); 27 | lineDrawer->ClearLines(); 28 | delete lineDrawer; 29 | lineDrawer = nullptr; 30 | 31 | } 32 | 33 | public: 34 | void AddParticle(Vector2 position, bool isFixed = false); 35 | void Init(); 36 | void UpdateLines(float deltaTime); 37 | void OnCollided(Vector2 overlap, int particleIndex); 38 | void AddTorque(float torque); 39 | b2Body* GetBody() const 40 | { 41 | return polygonBody; 42 | } 43 | 44 | std::vector particles; 45 | std::vector edges; 46 | 47 | Vector2 GetCenter() 48 | { 49 | return GetCenterPointWorldPosition(particles); 50 | } 51 | 52 | bool isClosed = false; 53 | 54 | private: 55 | Scene &scene; 56 | bool isReady = false; 57 | 58 | int color; 59 | 60 | b2PolygonShape polygonBox; 61 | b2Body* polygonBody; 62 | LineDrawer* lineDrawer; 63 | Vector2 lastDrawnLinePosition; 64 | std::shared_ptr debugUIPtr; 65 | 66 | private: 67 | void CreateLineParticles(int amount, int color); 68 | void DrawLine(Vector2 startPosition, Vector2 endPosition); 69 | 70 | void ClearParticles() 71 | { 72 | for (auto particle : particles) 73 | { 74 | particle->Destroy(); 75 | } 76 | particles.clear(); 77 | } 78 | 79 | 80 | }; -------------------------------------------------------------------------------- /Source/Core/Physics/Polygon/PolygonCollision.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../Core.h" 3 | #include "Polygon.h" 4 | #include 5 | class GE_API PolygonCollision 6 | { 7 | public: 8 | PolygonCollision() 9 | { 10 | } 11 | void UpdateResolve(float deltaTime); 12 | void AddPolygon(Polygon *polygon) 13 | { 14 | 15 | polygons.push_back(polygon); 16 | } 17 | 18 | void RemovePolygon(Polygon *polygon) 19 | { 20 | polygons.erase(std::remove(polygons.begin(), polygons.end(), polygon), polygons.end()); 21 | } 22 | 23 | private: 24 | std::vector polygons; 25 | float PointToLineDistance(const Vector2 &point, const Vector2 &lineStart, const Vector2 &lineEnd); 26 | void ResolveCollisionForClosedPolygons(Polygon *polygonA, Polygon *polygonB, float deltaTime, int particleIndex); 27 | void ResolveCollision(Polygon *polygonA, Polygon *polygonB, float deltaTime, float overlapDepth, int particleIndex); 28 | bool CheckCollisionForClosedPolygon(Polygon *polygonA, Polygon *polygonB, int &particleIndex); 29 | bool CheckCollisionForEdgeToPoint(Polygon *polygonA, Polygon *polgonB, float &overlapDepth, int &particleIndex); 30 | bool IntersectPolygons(Polygon *polygonA, Polygon *polygonB, Vector2 &collisionNormal, float &depth); 31 | void ProjectPolygonOntoAxis(Polygon *polygon, const Vector2 &axis, float &min, float &max); 32 | }; -------------------------------------------------------------------------------- /Source/Core/Physics/Polygon/PolygonCreator/PolygonCreator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../../Core.h" 3 | #include "../../../../CoreStructs/Vector.h" 4 | #include "PolygonCursor.h" 5 | #include 6 | class GE_API PolygonCreator //Created polygons' owner ship is moved to scene!!! 7 | { 8 | public: 9 | PolygonCreator(class Scene& scene); 10 | ~PolygonCreator() 11 | { 12 | 13 | } 14 | class Polygon* ApplyAndGetPolygon(); 15 | 16 | void StartCreating(Vector2 position, int color); 17 | void SetPosition(Vector2 position); 18 | Polygon* CreateSquarePolygon(Vector2 position, int size, float rotationAngle, int color = 1); 19 | Polygon* CreateRectanglePolygon(Transform transform, int color = 1); 20 | Polygon* CreateCircle(Vector2 position, float radius, int numSegments, int color = 1); 21 | private: 22 | Polygon* currentPolygon; 23 | Scene& scene; 24 | 25 | class Cursor* cursor; 26 | 27 | bool isCreating = false; 28 | int particleIndex = 0; 29 | 30 | void OnInput(int input); 31 | }; 32 | -------------------------------------------------------------------------------- /Source/Core/Physics/Polygon/PolygonCreator/PolygonCursor.cpp: -------------------------------------------------------------------------------- 1 | #include "PolygonCursor.h" 2 | #include "PolygonCreator.h" 3 | #include "../../../Input.h" 4 | #include "../../../Component/PlayerController.h" 5 | Cursor::Cursor(class Scene &scene) : GameObject("PolygonCursor", scene) 6 | { 7 | sprite = {{1}}; 8 | } 9 | 10 | void Cursor::Init() 11 | { 12 | AddComponent(new PlayerController(*this, 0)); 13 | 14 | } 15 | 16 | 17 | -------------------------------------------------------------------------------- /Source/Core/Physics/Polygon/PolygonCreator/PolygonCursor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../GameObject.h" 3 | #include "../../../../Core.h" 4 | class GE_API Cursor : public GameObject 5 | { 6 | public: 7 | Cursor(class Scene &scene); 8 | ~Cursor(){} 9 | 10 | void Init() override; 11 | private: 12 | 13 | }; -------------------------------------------------------------------------------- /Source/Core/Physics/Polygon/PolygonHelper.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "PolygonParticle.h" 4 | #include "../../../CoreStructs/Vector.h" 5 | #include "../../GameObject.h" 6 | 7 | static Vector2 GetCenterPointWorldPosition(std::vector &vertices) 8 | { 9 | int vertexCount = vertices.size(); 10 | Vector2 centroid = {0, 0}; 11 | double signedArea = 0.0; 12 | double x0 = 0.0; // Current vertex X 13 | double y0 = 0.0; // Current vertex Y 14 | double x1 = 0.0; // Next vertex X 15 | double y1 = 0.0; // Next vertex Y 16 | double a = 0.0; // Partial signed area 17 | 18 | // For all vertices except last 19 | int i = 0; 20 | for (i = 0; i < vertexCount - 1; ++i) 21 | { 22 | x0 = vertices[i]->transform.Position.X; 23 | y0 = vertices[i]->transform.Position.Y; 24 | x1 = vertices[i + 1]->transform.Position.X; 25 | y1 = vertices[i + 1]->transform.Position.Y; 26 | a = x0 * y1 - x1 * y0; 27 | signedArea += a; 28 | centroid.X += (x0 + x1) * a; 29 | centroid.Y += (y0 + y1) * a; 30 | } 31 | 32 | // Do last vertex separately to avoid performing an expensive 33 | // modulus operation in each iteration. 34 | x0 = vertices[i]->transform.Position.X; 35 | y0 = vertices[i]->transform.Position.Y; 36 | x1 = vertices[0]->transform.Position.X; 37 | y1 = vertices[0]->transform.Position.Y; 38 | a = x0 * y1 - x1 * y0; 39 | signedArea += a; 40 | centroid.X += (x0 + x1) * a; 41 | centroid.Y += (y0 + y1) * a; 42 | 43 | signedArea *= 0.5; 44 | centroid.X /= (6.0 * signedArea); 45 | centroid.Y /= (6.0 * signedArea); 46 | 47 | return centroid; 48 | } -------------------------------------------------------------------------------- /Source/Core/Physics/Polygon/PolygonParticle.cpp: -------------------------------------------------------------------------------- 1 | #include "PolygonParticle.h" 2 | 3 | void PolygonParticle::Init() 4 | { 5 | } 6 | 7 | void PolygonParticle::OnReady() 8 | { 9 | Rigidbody *rb = new Rigidbody(*this); 10 | AddComponent(rb); 11 | } 12 | -------------------------------------------------------------------------------- /Source/Core/Physics/Polygon/PolygonParticle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../Core.h" 3 | #include "../../GameObject.h" 4 | #include "../../Component/RigidbodyComponent.h" 5 | #include "../../Component/SpringComponent.h" 6 | class GE_API PolygonParticle : public GameObject 7 | { 8 | public: 9 | PolygonParticle(class Scene &scene) : GameObject("PolygonParticle", scene) 10 | { 11 | sprite = {{1}}; 12 | symbol = "X"; 13 | } 14 | virtual ~PolygonParticle() {} 15 | void Init() override; 16 | bool isFixed = false; 17 | void OnReady(); 18 | }; -------------------------------------------------------------------------------- /Source/Core/Physics/Raycaster.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Raycaster.h" 3 | #include "../Collision.h" 4 | #include "../Scene.h" 5 | #include "../GameObject.h" 6 | 7 | bool Raycaster::RayCast(Vector2 startPosition, Vector2 direction, RayHit &rayHit, int distance, const std::string& targetName) 8 | { 9 | int dist = 0; 10 | bool hasHit = false; 11 | while (!hasHit && dist <= distance) 12 | { 13 | auto hit = scene.collision.GetCollidedObject(startPosition + direction * dist); 14 | if (hit != nullptr && hit->name == targetName) 15 | { 16 | hasHit = true; 17 | rayHit.gameObject = hit; 18 | return true; 19 | } 20 | dist++; 21 | } 22 | return false; 23 | } 24 | -------------------------------------------------------------------------------- /Source/Core/Physics/Raycaster.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../CoreStructs/Vector.h" 3 | struct RayHit 4 | { 5 | public: 6 | class GameObject* gameObject; 7 | }; 8 | 9 | class Raycaster 10 | { 11 | public: 12 | Raycaster(class Scene& scene) : scene(scene) 13 | { 14 | 15 | } 16 | 17 | bool RayCast(Vector2 startPosition, Vector2 direction, RayHit& rayHit, int distance, const std::string& targetName); 18 | private: 19 | Scene& scene; 20 | }; -------------------------------------------------------------------------------- /Source/Core/Physics/RopeParticle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../GameObject.h" 3 | 4 | class RopeParticle : public GameObject 5 | { 6 | public: 7 | RopeParticle(class Scene &scene) : GameObject("RopeParticle", scene) {} 8 | void Init() override 9 | { 10 | sprite = {{1}}; 11 | symbol = "O"; 12 | } 13 | bool isFixed = false; 14 | }; -------------------------------------------------------------------------------- /Source/Core/Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Core.h" 3 | #include "Scene.h" 4 | #ifdef __GNUC__ 5 | #include "ncurses.h" 6 | #else 7 | #include 8 | #endif 9 | class GE_API Renderer 10 | { 11 | public: 12 | Renderer(); 13 | void Render(Scene &scene); 14 | 15 | static std::vector> RotateSprite(const std::vector> &sprite); 16 | 17 | private: 18 | void FixConsoleWindow() 19 | { 20 | // HWND consoleWindow = GetConsoleWindow(); 21 | // LONG style = GetWindowLong(consoleWindow, GWL_STYLE); 22 | // style = style & ~(WS_MAXIMIZEBOX) & ~(WS_THICKFRAME); 23 | // SetWindowLong(consoleWindow, GWL_STYLE, style); 24 | } 25 | void GoToXY(int x, int y) 26 | { 27 | #ifdef __GNUC__ 28 | 29 | std::cout << "\033[" << y << ";" << x << "H"; 30 | #else 31 | 32 | COORD coord; 33 | coord.X = x; 34 | coord.Y = y; 35 | SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 36 | #endif 37 | } 38 | void HideCursor() 39 | { 40 | #ifdef __GNUC__ 41 | curs_set(0); 42 | #else 43 | HANDLE fd = GetStdHandle(STD_OUTPUT_HANDLE); 44 | CONSOLE_CURSOR_INFO cinfo; 45 | cinfo.bVisible = 0; 46 | cinfo.dwSize = 1; 47 | SetConsoleCursorInfo(fd, &cinfo); 48 | #endif 49 | } 50 | 51 | void ClearDestroyedObject(GameObject &go, Scene &scene); 52 | void DrawObjects(GameObject &go, Scene &scene); 53 | void ClearMovedObjectsTrail(GameObject &go, Scene &scene); 54 | void ClearObjectTrailAfterCameraMove(GameObject &fgo, Scene &scene); 55 | void DrawUI(const Scene &scene); 56 | void SetConsoleColor(int color); 57 | }; 58 | -------------------------------------------------------------------------------- /Source/Core/Scene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Core.h" 3 | #include 4 | #include 5 | #include "GameObject.h" 6 | #include "Physics/Polygon/Polygon.h" 7 | #include "Physics/CollisionResolver.h" 8 | #include "Component/AI/Pathfinder.h" 9 | #include "Physics/Polygon/PolygonCollision.h" 10 | #include "../box2d/include/box2d/box2d.h" 11 | #include "Physics/Raycaster.h" 12 | #include "Collision.h" 13 | #include "Camera.h" 14 | #include 15 | #include 16 | class GE_API Scene 17 | { 18 | protected: 19 | std::vector m_GameObjects; 20 | std::queue m_GameObjectsToSpawn; 21 | std::map m_NameToGameObjectMap; 22 | std::vector m_Polygons; 23 | std::queue m_PolygonsToSpawn; 24 | 25 | public: 26 | Scene(); 27 | 28 | virtual ~Scene(); 29 | virtual void Init(){}; 30 | virtual void Start(){}; 31 | 32 | void AddGameObject(GameObject *gameObject); 33 | void AddGameObject(GameObject *gameObject, Vector2 position); 34 | void RemoveGameObject(GameObject *gameObject); 35 | void AddPolygon(Polygon *polygon); 36 | void RemovePolygon(Polygon *polygon); 37 | void UpdateCamera(float deltaTime); 38 | virtual void Update(float deltaTime); //!!ALWAYS UPDATE BASE HERE 39 | void SpawnQueuedGameObjects(); 40 | GameObject *FindNearestGameObject(Transform transform, std::string gameObjectName); 41 | GameObject *FindSecondNearestGameObject(Transform transform, std::string gameObjectName); 42 | GameObject *FindGameObject(std::string gameObjectName); 43 | const std::vector &GetGameObjects() const 44 | { 45 | return m_GameObjects; 46 | } 47 | 48 | GameObject *GetGameObject(std::string name) 49 | { 50 | return m_NameToGameObjectMap[name]; 51 | } 52 | 53 | public: 54 | bool hasGameOver = false; 55 | bool isPaused = false; 56 | 57 | Camera *camera; 58 | class UIHandler *uiHandler; 59 | CollisionResolver *collisionResolver; // Reached from rigidbodies to be added 60 | 61 | Collision collision; 62 | b2World *world = new b2World(b2Vec2(0, 20)); 63 | Raycaster *raycaster = new Raycaster(*this); 64 | Pathfinder *pathfinder = new Pathfinder(*this); 65 | 66 | 67 | private: 68 | void CreateBoxBorder(); 69 | void InitializeGameObject(GameObject *go); 70 | void InitializePolygon(Polygon *p); 71 | 72 | private: 73 | PolygonCollision polygonCollision; 74 | std::shared_ptr debugUIPtr; 75 | 76 | int32 velocityIterations = 6; 77 | int32 positionIterations = 2; 78 | }; -------------------------------------------------------------------------------- /Source/Core/SceneManager.cpp: -------------------------------------------------------------------------------- 1 | #include "SceneManager.h" 2 | Scene *SceneManager::CurrentScene = nullptr; 3 | SceneManager::SceneManager() 4 | { 5 | CurrentScene = nullptr; 6 | } 7 | 8 | void SceneManager::ChangeScene(Scene *scene) 9 | { 10 | if(CurrentScene) 11 | { 12 | delete CurrentScene; 13 | CurrentScene = nullptr; 14 | } 15 | CurrentScene = scene; 16 | CurrentScene->Init(); 17 | CurrentScene->Start(); 18 | #ifdef __GNUC__ 19 | system("clear"); 20 | #else 21 | system("cls"); 22 | #endif 23 | } 24 | -------------------------------------------------------------------------------- /Source/Core/SceneManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Core.h" 3 | #include "Scene.h" 4 | class GE_API SceneManager 5 | { 6 | public: 7 | static Scene* CurrentScene; 8 | static void ChangeScene(Scene* scene); 9 | SceneManager(); 10 | private: 11 | 12 | 13 | }; -------------------------------------------------------------------------------- /Source/Core/UIHandler.cpp: -------------------------------------------------------------------------------- 1 | #include "UIHandler.h" 2 | -------------------------------------------------------------------------------- /Source/CoreStructs/Transform.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Vector.h" 3 | #include 4 | #include 5 | #include "../Core.h" 6 | 7 | const int LEFTBORDER = 0; 8 | const int TOPBORDER = 1; 9 | const int RIGHTBORDER = 2; 10 | const int BOTTOMBORDER = 3; 11 | 12 | struct Transform 13 | { 14 | public: 15 | Transform() 16 | { 17 | Position = Vector2(0, 0); 18 | Rotation = 0; 19 | PreviousPosition = Vector2(0, 0); 20 | }; 21 | Transform(Vector2 position, Vector2 size, float rotation) : Position(position), Size(size), Rotation(rotation){} 22 | Vector2 Position; 23 | Vector2 PreviousPosition; 24 | Vector2 RopePreviousPosition; 25 | Vector2 Size; 26 | Transform* Parent = nullptr; 27 | 28 | float Rotation; 29 | bool IsStatic = false; 30 | bool HasClearedFlag = false; // If removed its previous position from rendering 31 | bool HasMovedThisFrame = true; 32 | bool HasOwnerDestroyed = false; 33 | 34 | void SetChild(Transform &child) 35 | { 36 | children.push_back(&child); 37 | } 38 | void SetParent(Transform &parent) 39 | { 40 | Parent = &parent; 41 | parent.SetChild(*this); 42 | } 43 | void SetPosition(float x, float y) 44 | { 45 | if(IsStatic) return; 46 | PreviousPosition = Position.ToInt(); 47 | Position = Vector2(x, y); 48 | if (PreviousPosition != Position.ToInt()) 49 | { 50 | HasMovedThisFrame = true; 51 | HasClearedFlag = false; 52 | } 53 | else 54 | { 55 | HasMovedThisFrame = false; 56 | } 57 | } 58 | Vector2 GetCenterPosition() const 59 | { 60 | return Position + Size / 2; 61 | } 62 | void MovePosition(float x, float y, bool isCameraMove = false) 63 | { 64 | 65 | PreviousPosition = Position.ToInt(); 66 | 67 | for (auto &child : children) 68 | { 69 | child->MovePosition(x, y); 70 | } 71 | 72 | if(IsStatic) return; 73 | Position.X += x; 74 | Position.Y += y; 75 | if (PreviousPosition != Position.ToInt()) 76 | { 77 | HasMovedThisFrame = true; 78 | HasClearedFlag = false; 79 | } 80 | else 81 | { 82 | HasMovedThisFrame = false; 83 | } 84 | } 85 | 86 | friend std::ostream &operator<<(std::ostream &os, const Transform &transform) 87 | { 88 | os << "Position:" << transform.Position.X << "," << transform.Position.Y; 89 | return os; 90 | } 91 | 92 | private: 93 | std::vector children; 94 | }; 95 | -------------------------------------------------------------------------------- /Source/box2d/include/box2d/b2_api.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_API_H 24 | #define B2_API_H 25 | 26 | #ifdef B2_SHARED 27 | #if defined _WIN32 || defined __CYGWIN__ 28 | #ifdef box2d_EXPORTS 29 | #ifdef __GNUC__ 30 | #define B2_API __attribute__ ((dllexport)) 31 | #else 32 | #define B2_API __declspec(dllexport) 33 | #endif 34 | #else 35 | #ifdef __GNUC__ 36 | #define B2_API __attribute__ ((dllimport)) 37 | #else 38 | #define B2_API __declspec(dllimport) 39 | #endif 40 | #endif 41 | #else 42 | #if __GNUC__ >= 4 43 | #define B2_API __attribute__ ((visibility ("default"))) 44 | #else 45 | #define B2_API 46 | #endif 47 | #endif 48 | #else 49 | #define B2_API 50 | #endif 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /Source/box2d/include/box2d/b2_block_allocator.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_BLOCK_ALLOCATOR_H 24 | #define B2_BLOCK_ALLOCATOR_H 25 | 26 | #include "b2_api.h" 27 | #include "b2_settings.h" 28 | 29 | const int32 b2_blockSizeCount = 14; 30 | 31 | struct b2Block; 32 | struct b2Chunk; 33 | 34 | /// This is a small object allocator used for allocating small 35 | /// objects that persist for more than one time step. 36 | /// See: http://www.codeproject.com/useritems/Small_Block_Allocator.asp 37 | class B2_API b2BlockAllocator 38 | { 39 | public: 40 | b2BlockAllocator(); 41 | ~b2BlockAllocator(); 42 | 43 | /// Allocate memory. This will use b2Alloc if the size is larger than b2_maxBlockSize. 44 | void* Allocate(int32 size); 45 | 46 | /// Free memory. This will use b2Free if the size is larger than b2_maxBlockSize. 47 | void Free(void* p, int32 size); 48 | 49 | void Clear(); 50 | 51 | private: 52 | 53 | b2Chunk* m_chunks; 54 | int32 m_chunkCount; 55 | int32 m_chunkSpace; 56 | 57 | b2Block* m_freeLists[b2_blockSizeCount]; 58 | }; 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /Source/box2d/include/box2d/b2_circle_shape.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_CIRCLE_SHAPE_H 24 | #define B2_CIRCLE_SHAPE_H 25 | 26 | #include "b2_api.h" 27 | #include "b2_shape.h" 28 | 29 | /// A solid circle shape 30 | class B2_API b2CircleShape : public b2Shape 31 | { 32 | public: 33 | b2CircleShape(); 34 | 35 | /// Implement b2Shape. 36 | b2Shape* Clone(b2BlockAllocator* allocator) const override; 37 | 38 | /// @see b2Shape::GetChildCount 39 | int32 GetChildCount() const override; 40 | 41 | /// Implement b2Shape. 42 | bool TestPoint(const b2Transform& transform, const b2Vec2& p) const override; 43 | 44 | /// Implement b2Shape. 45 | /// @note because the circle is solid, rays that start inside do not hit because the normal is 46 | /// not defined. 47 | bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input, 48 | const b2Transform& transform, int32 childIndex) const override; 49 | 50 | /// @see b2Shape::ComputeAABB 51 | void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const override; 52 | 53 | /// @see b2Shape::ComputeMass 54 | void ComputeMass(b2MassData* massData, float density) const override; 55 | 56 | /// Position 57 | b2Vec2 m_p; 58 | }; 59 | 60 | inline b2CircleShape::b2CircleShape() 61 | { 62 | m_type = e_circle; 63 | m_radius = 0.0f; 64 | m_p.SetZero(); 65 | } 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /Source/box2d/include/box2d/b2_contact_manager.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_CONTACT_MANAGER_H 24 | #define B2_CONTACT_MANAGER_H 25 | 26 | #include "b2_api.h" 27 | #include "b2_broad_phase.h" 28 | 29 | class b2Contact; 30 | class b2ContactFilter; 31 | class b2ContactListener; 32 | class b2BlockAllocator; 33 | 34 | // Delegate of b2World. 35 | class B2_API b2ContactManager 36 | { 37 | public: 38 | b2ContactManager(); 39 | 40 | // Broad-phase callback. 41 | void AddPair(void* proxyUserDataA, void* proxyUserDataB); 42 | 43 | void FindNewContacts(); 44 | 45 | void Destroy(b2Contact* c); 46 | 47 | void Collide(); 48 | 49 | b2BroadPhase m_broadPhase; 50 | b2Contact* m_contactList; 51 | int32 m_contactCount; 52 | b2ContactFilter* m_contactFilter; 53 | b2ContactListener* m_contactListener; 54 | b2BlockAllocator* m_allocator; 55 | }; 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /Source/box2d/include/box2d/b2_stack_allocator.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_STACK_ALLOCATOR_H 24 | #define B2_STACK_ALLOCATOR_H 25 | 26 | #include "b2_api.h" 27 | #include "b2_settings.h" 28 | 29 | const int32 b2_stackSize = 100 * 1024; // 100k 30 | const int32 b2_maxStackEntries = 32; 31 | 32 | struct B2_API b2StackEntry 33 | { 34 | char* data; 35 | int32 size; 36 | bool usedMalloc; 37 | }; 38 | 39 | // This is a stack allocator used for fast per step allocations. 40 | // You must nest allocate/free pairs. The code will assert 41 | // if you try to interleave multiple allocate/free pairs. 42 | class B2_API b2StackAllocator 43 | { 44 | public: 45 | b2StackAllocator(); 46 | ~b2StackAllocator(); 47 | 48 | void* Allocate(int32 size); 49 | void Free(void* p); 50 | 51 | int32 GetMaxAllocation() const; 52 | 53 | private: 54 | 55 | char m_data[b2_stackSize]; 56 | int32 m_index; 57 | 58 | int32 m_allocation; 59 | int32 m_maxAllocation; 60 | 61 | b2StackEntry m_entries[b2_maxStackEntries]; 62 | int32 m_entryCount; 63 | }; 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /Source/box2d/include/box2d/b2_time_of_impact.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_TIME_OF_IMPACT_H 24 | #define B2_TIME_OF_IMPACT_H 25 | 26 | #include "b2_api.h" 27 | #include "b2_math.h" 28 | #include "b2_distance.h" 29 | 30 | /// Input parameters for b2TimeOfImpact 31 | struct B2_API b2TOIInput 32 | { 33 | b2DistanceProxy proxyA; 34 | b2DistanceProxy proxyB; 35 | b2Sweep sweepA; 36 | b2Sweep sweepB; 37 | float tMax; // defines sweep interval [0, tMax] 38 | }; 39 | 40 | /// Output parameters for b2TimeOfImpact. 41 | struct B2_API b2TOIOutput 42 | { 43 | enum State 44 | { 45 | e_unknown, 46 | e_failed, 47 | e_overlapped, 48 | e_touching, 49 | e_separated 50 | }; 51 | 52 | State state; 53 | float t; 54 | }; 55 | 56 | /// Compute the upper bound on time before two shapes penetrate. Time is represented as 57 | /// a fraction between [0,tMax]. This uses a swept separating axis and may miss some intermediate, 58 | /// non-tunneling collisions. If you change the time interval, you should call this function 59 | /// again. 60 | /// Note: use b2Distance to compute the contact point and normal at the time of impact. 61 | B2_API void b2TimeOfImpact(b2TOIOutput* output, const b2TOIInput* input); 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /Source/box2d/include/box2d/b2_time_step.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | #ifndef B2_TIME_STEP_H 23 | #define B2_TIME_STEP_H 24 | 25 | #include "b2_api.h" 26 | #include "b2_math.h" 27 | 28 | /// Profiling data. Times are in milliseconds. 29 | struct B2_API b2Profile 30 | { 31 | float step; 32 | float collide; 33 | float solve; 34 | float solveInit; 35 | float solveVelocity; 36 | float solvePosition; 37 | float broadphase; 38 | float solveTOI; 39 | }; 40 | 41 | /// This is an internal structure. 42 | struct B2_API b2TimeStep 43 | { 44 | float dt; // time step 45 | float inv_dt; // inverse time step (0 if dt == 0). 46 | float dtRatio; // dt * inv_dt0 47 | int32 velocityIterations; 48 | int32 positionIterations; 49 | bool warmStarting; 50 | }; 51 | 52 | /// This is an internal structure. 53 | struct B2_API b2Position 54 | { 55 | b2Vec2 c; 56 | float a; 57 | }; 58 | 59 | /// This is an internal structure. 60 | struct B2_API b2Velocity 61 | { 62 | b2Vec2 v; 63 | float w; 64 | }; 65 | 66 | /// Solver Data 67 | struct B2_API b2SolverData 68 | { 69 | b2TimeStep step; 70 | b2Position* positions; 71 | b2Velocity* velocities; 72 | }; 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /Source/box2d/include/box2d/b2_timer.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_TIMER_H 24 | #define B2_TIMER_H 25 | 26 | #include "b2_api.h" 27 | #include "b2_settings.h" 28 | 29 | /// Timer for profiling. This has platform specific code and may 30 | /// not work on every platform. 31 | class B2_API b2Timer 32 | { 33 | public: 34 | 35 | /// Constructor 36 | b2Timer(); 37 | 38 | /// Reset the timer. 39 | void Reset(); 40 | 41 | /// Get the time since construction or the last reset. 42 | float GetMilliseconds() const; 43 | 44 | private: 45 | 46 | #if defined(_WIN32) 47 | double m_start; 48 | static double s_invFrequency; 49 | #elif defined(__linux__) || defined (__APPLE__) 50 | unsigned long long m_start_sec; 51 | unsigned long long m_start_usec; 52 | #endif 53 | }; 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /Source/box2d/include/box2d/b2_types.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2020 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_TYPES_H 24 | #define B2_TYPES_H 25 | 26 | typedef signed char int8; 27 | typedef signed short int16; 28 | typedef signed int int32; 29 | typedef unsigned char uint8; 30 | typedef unsigned short uint16; 31 | typedef unsigned int uint32; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /Source/box2d/include/box2d/box2d.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef BOX2D_H 24 | #define BOX2D_H 25 | 26 | // These include files constitute the main Box2D API 27 | 28 | #include "b2_settings.h" 29 | #include "b2_draw.h" 30 | #include "b2_timer.h" 31 | 32 | #include "b2_chain_shape.h" 33 | #include "b2_circle_shape.h" 34 | #include "b2_edge_shape.h" 35 | #include "b2_polygon_shape.h" 36 | 37 | #include "b2_broad_phase.h" 38 | #include "b2_dynamic_tree.h" 39 | 40 | #include "b2_body.h" 41 | #include "b2_contact.h" 42 | #include "b2_fixture.h" 43 | #include "b2_time_step.h" 44 | #include "b2_world.h" 45 | #include "b2_world_callbacks.h" 46 | 47 | #include "b2_distance_joint.h" 48 | #include "b2_friction_joint.h" 49 | #include "b2_gear_joint.h" 50 | #include "b2_motor_joint.h" 51 | #include "b2_mouse_joint.h" 52 | #include "b2_prismatic_joint.h" 53 | #include "b2_pulley_joint.h" 54 | #include "b2_revolute_joint.h" 55 | #include "b2_weld_joint.h" 56 | #include "b2_wheel_joint.h" 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /Source/box2d/src/common/b2_draw.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | #include "../../include/box2d//b2_draw.h" 23 | 24 | b2Draw::b2Draw() 25 | { 26 | m_drawFlags = 0; 27 | } 28 | 29 | void b2Draw::SetFlags(uint32 flags) 30 | { 31 | m_drawFlags = flags; 32 | } 33 | 34 | uint32 b2Draw::GetFlags() const 35 | { 36 | return m_drawFlags; 37 | } 38 | 39 | void b2Draw::AppendFlags(uint32 flags) 40 | { 41 | m_drawFlags |= flags; 42 | } 43 | 44 | void b2Draw::ClearFlags(uint32 flags) 45 | { 46 | m_drawFlags &= ~flags; 47 | } 48 | -------------------------------------------------------------------------------- /Source/box2d/src/common/b2_settings.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #define _CRT_SECURE_NO_WARNINGS 24 | 25 | #include "../../include/box2d//b2_settings.h" 26 | #include 27 | #include 28 | #include 29 | 30 | b2Version b2_version = {2, 4, 1}; 31 | 32 | // Memory allocators. Modify these to use your own allocator. 33 | void* b2Alloc_Default(int32 size) 34 | { 35 | return malloc(size); 36 | } 37 | 38 | void b2Free_Default(void* mem) 39 | { 40 | free(mem); 41 | } 42 | 43 | // You can modify this to use your logging facility. 44 | void b2Log_Default(const char* string, va_list args) 45 | { 46 | vprintf(string, args); 47 | } 48 | 49 | FILE* b2_dumpFile = nullptr; 50 | 51 | void b2OpenDump(const char* fileName) 52 | { 53 | b2Assert(b2_dumpFile == nullptr); 54 | b2_dumpFile = fopen(fileName, "w"); 55 | } 56 | 57 | void b2Dump(const char* string, ...) 58 | { 59 | if (b2_dumpFile == nullptr) 60 | { 61 | return; 62 | } 63 | 64 | va_list args; 65 | va_start(args, string); 66 | vfprintf(b2_dumpFile, string, args); 67 | va_end(args); 68 | } 69 | 70 | void b2CloseDump() 71 | { 72 | fclose(b2_dumpFile); 73 | b2_dumpFile = nullptr; 74 | } 75 | -------------------------------------------------------------------------------- /Source/box2d/src/dynamics/b2_chain_circle_contact.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_CHAIN_AND_CIRCLE_CONTACT_H 24 | #define B2_CHAIN_AND_CIRCLE_CONTACT_H 25 | 26 | #include "../../include/box2d//b2_contact.h" 27 | 28 | class b2BlockAllocator; 29 | 30 | class b2ChainAndCircleContact : public b2Contact 31 | { 32 | public: 33 | static b2Contact* Create( b2Fixture* fixtureA, int32 indexA, 34 | b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator); 35 | static void Destroy(b2Contact* contact, b2BlockAllocator* allocator); 36 | 37 | b2ChainAndCircleContact(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB); 38 | ~b2ChainAndCircleContact() {} 39 | 40 | void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) override; 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /Source/box2d/src/dynamics/b2_chain_polygon_contact.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_CHAIN_AND_POLYGON_CONTACT_H 24 | #define B2_CHAIN_AND_POLYGON_CONTACT_H 25 | 26 | #include "../../include/box2d//b2_contact.h" 27 | 28 | class b2BlockAllocator; 29 | 30 | class b2ChainAndPolygonContact : public b2Contact 31 | { 32 | public: 33 | static b2Contact* Create( b2Fixture* fixtureA, int32 indexA, 34 | b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator); 35 | static void Destroy(b2Contact* contact, b2BlockAllocator* allocator); 36 | 37 | b2ChainAndPolygonContact(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB); 38 | ~b2ChainAndPolygonContact() {} 39 | 40 | void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) override; 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /Source/box2d/src/dynamics/b2_circle_contact.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_CIRCLE_CONTACT_H 24 | #define B2_CIRCLE_CONTACT_H 25 | 26 | #include "../../include/box2d//b2_contact.h" 27 | 28 | class b2BlockAllocator; 29 | 30 | class b2CircleContact : public b2Contact 31 | { 32 | public: 33 | static b2Contact* Create( b2Fixture* fixtureA, int32 indexA, 34 | b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator); 35 | static void Destroy(b2Contact* contact, b2BlockAllocator* allocator); 36 | 37 | b2CircleContact(b2Fixture* fixtureA, b2Fixture* fixtureB); 38 | ~b2CircleContact() {} 39 | 40 | void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) override; 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /Source/box2d/src/dynamics/b2_edge_circle_contact.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #include "b2_edge_circle_contact.h" 24 | 25 | #include "../../include/box2d//b2_block_allocator.h" 26 | #include "../../include/box2d//b2_fixture.h" 27 | 28 | #include 29 | 30 | b2Contact* b2EdgeAndCircleContact::Create(b2Fixture* fixtureA, int32, b2Fixture* fixtureB, int32, b2BlockAllocator* allocator) 31 | { 32 | void* mem = allocator->Allocate(sizeof(b2EdgeAndCircleContact)); 33 | return new (mem) b2EdgeAndCircleContact(fixtureA, fixtureB); 34 | } 35 | 36 | void b2EdgeAndCircleContact::Destroy(b2Contact* contact, b2BlockAllocator* allocator) 37 | { 38 | ((b2EdgeAndCircleContact*)contact)->~b2EdgeAndCircleContact(); 39 | allocator->Free(contact, sizeof(b2EdgeAndCircleContact)); 40 | } 41 | 42 | b2EdgeAndCircleContact::b2EdgeAndCircleContact(b2Fixture* fixtureA, b2Fixture* fixtureB) 43 | : b2Contact(fixtureA, 0, fixtureB, 0) 44 | { 45 | b2Assert(m_fixtureA->GetType() == b2Shape::e_edge); 46 | b2Assert(m_fixtureB->GetType() == b2Shape::e_circle); 47 | } 48 | 49 | void b2EdgeAndCircleContact::Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) 50 | { 51 | b2CollideEdgeAndCircle( manifold, 52 | (b2EdgeShape*)m_fixtureA->GetShape(), xfA, 53 | (b2CircleShape*)m_fixtureB->GetShape(), xfB); 54 | } 55 | -------------------------------------------------------------------------------- /Source/box2d/src/dynamics/b2_edge_circle_contact.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_EDGE_AND_CIRCLE_CONTACT_H 24 | #define B2_EDGE_AND_CIRCLE_CONTACT_H 25 | 26 | #include "../../include/box2d//b2_contact.h" 27 | 28 | class b2BlockAllocator; 29 | 30 | class b2EdgeAndCircleContact : public b2Contact 31 | { 32 | public: 33 | static b2Contact* Create( b2Fixture* fixtureA, int32 indexA, 34 | b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator); 35 | static void Destroy(b2Contact* contact, b2BlockAllocator* allocator); 36 | 37 | b2EdgeAndCircleContact(b2Fixture* fixtureA, b2Fixture* fixtureB); 38 | ~b2EdgeAndCircleContact() {} 39 | 40 | void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) override; 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /Source/box2d/src/dynamics/b2_edge_polygon_contact.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #include "b2_edge_polygon_contact.h" 24 | 25 | #include "../../include/box2d//b2_block_allocator.h" 26 | #include "../../include/box2d//b2_fixture.h" 27 | 28 | #include 29 | 30 | b2Contact* b2EdgeAndPolygonContact::Create(b2Fixture* fixtureA, int32, b2Fixture* fixtureB, int32, b2BlockAllocator* allocator) 31 | { 32 | void* mem = allocator->Allocate(sizeof(b2EdgeAndPolygonContact)); 33 | return new (mem) b2EdgeAndPolygonContact(fixtureA, fixtureB); 34 | } 35 | 36 | void b2EdgeAndPolygonContact::Destroy(b2Contact* contact, b2BlockAllocator* allocator) 37 | { 38 | ((b2EdgeAndPolygonContact*)contact)->~b2EdgeAndPolygonContact(); 39 | allocator->Free(contact, sizeof(b2EdgeAndPolygonContact)); 40 | } 41 | 42 | b2EdgeAndPolygonContact::b2EdgeAndPolygonContact(b2Fixture* fixtureA, b2Fixture* fixtureB) 43 | : b2Contact(fixtureA, 0, fixtureB, 0) 44 | { 45 | b2Assert(m_fixtureA->GetType() == b2Shape::e_edge); 46 | b2Assert(m_fixtureB->GetType() == b2Shape::e_polygon); 47 | } 48 | 49 | void b2EdgeAndPolygonContact::Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) 50 | { 51 | b2CollideEdgeAndPolygon( manifold, 52 | (b2EdgeShape*)m_fixtureA->GetShape(), xfA, 53 | (b2PolygonShape*)m_fixtureB->GetShape(), xfB); 54 | } 55 | -------------------------------------------------------------------------------- /Source/box2d/src/dynamics/b2_edge_polygon_contact.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_EDGE_AND_POLYGON_CONTACT_H 24 | #define B2_EDGE_AND_POLYGON_CONTACT_H 25 | 26 | #include "../../include/box2d//b2_contact.h" 27 | 28 | class b2BlockAllocator; 29 | 30 | class b2EdgeAndPolygonContact : public b2Contact 31 | { 32 | public: 33 | static b2Contact* Create( b2Fixture* fixtureA, int32 indexA, 34 | b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator); 35 | static void Destroy(b2Contact* contact, b2BlockAllocator* allocator); 36 | 37 | b2EdgeAndPolygonContact(b2Fixture* fixtureA, b2Fixture* fixtureB); 38 | ~b2EdgeAndPolygonContact() {} 39 | 40 | void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) override; 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /Source/box2d/src/dynamics/b2_polygon_circle_contact.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #include "b2_polygon_circle_contact.h" 24 | 25 | #include "../../include/box2d//b2_block_allocator.h" 26 | #include "../../include/box2d//b2_fixture.h" 27 | 28 | #include 29 | 30 | b2Contact* b2PolygonAndCircleContact::Create(b2Fixture* fixtureA, int32, b2Fixture* fixtureB, int32, b2BlockAllocator* allocator) 31 | { 32 | void* mem = allocator->Allocate(sizeof(b2PolygonAndCircleContact)); 33 | return new (mem) b2PolygonAndCircleContact(fixtureA, fixtureB); 34 | } 35 | 36 | void b2PolygonAndCircleContact::Destroy(b2Contact* contact, b2BlockAllocator* allocator) 37 | { 38 | ((b2PolygonAndCircleContact*)contact)->~b2PolygonAndCircleContact(); 39 | allocator->Free(contact, sizeof(b2PolygonAndCircleContact)); 40 | } 41 | 42 | b2PolygonAndCircleContact::b2PolygonAndCircleContact(b2Fixture* fixtureA, b2Fixture* fixtureB) 43 | : b2Contact(fixtureA, 0, fixtureB, 0) 44 | { 45 | b2Assert(m_fixtureA->GetType() == b2Shape::e_polygon); 46 | b2Assert(m_fixtureB->GetType() == b2Shape::e_circle); 47 | } 48 | 49 | void b2PolygonAndCircleContact::Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) 50 | { 51 | b2CollidePolygonAndCircle( manifold, 52 | (b2PolygonShape*)m_fixtureA->GetShape(), xfA, 53 | (b2CircleShape*)m_fixtureB->GetShape(), xfB); 54 | } 55 | -------------------------------------------------------------------------------- /Source/box2d/src/dynamics/b2_polygon_circle_contact.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_POLYGON_AND_CIRCLE_CONTACT_H 24 | #define B2_POLYGON_AND_CIRCLE_CONTACT_H 25 | 26 | #include "../../include/box2d//b2_contact.h" 27 | 28 | class b2BlockAllocator; 29 | 30 | class b2PolygonAndCircleContact : public b2Contact 31 | { 32 | public: 33 | static b2Contact* Create(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator); 34 | static void Destroy(b2Contact* contact, b2BlockAllocator* allocator); 35 | 36 | b2PolygonAndCircleContact(b2Fixture* fixtureA, b2Fixture* fixtureB); 37 | ~b2PolygonAndCircleContact() {} 38 | 39 | void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) override; 40 | }; 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /Source/box2d/src/dynamics/b2_polygon_contact.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef B2_POLYGON_CONTACT_H 24 | #define B2_POLYGON_CONTACT_H 25 | 26 | #include "../../include/box2d//b2_contact.h" 27 | 28 | class b2BlockAllocator; 29 | 30 | class b2PolygonContact : public b2Contact 31 | { 32 | public: 33 | static b2Contact* Create( b2Fixture* fixtureA, int32 indexA, 34 | b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator); 35 | static void Destroy(b2Contact* contact, b2BlockAllocator* allocator); 36 | 37 | b2PolygonContact(b2Fixture* fixtureA, b2Fixture* fixtureB); 38 | ~b2PolygonContact() {} 39 | 40 | void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) override; 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /Source/box2d/src/dynamics/b2_world_callbacks.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Erin Catto 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #include "../../include/box2d//b2_fixture.h" 24 | #include "../../include/box2d//b2_world_callbacks.h" 25 | 26 | // Return true if contact calculations should be performed between these two shapes. 27 | // If you implement your own collision filter you may want to build from this implementation. 28 | bool b2ContactFilter::ShouldCollide(b2Fixture* fixtureA, b2Fixture* fixtureB) 29 | { 30 | const b2Filter& filterA = fixtureA->GetFilterData(); 31 | const b2Filter& filterB = fixtureB->GetFilterData(); 32 | 33 | if (filterA.groupIndex == filterB.groupIndex && filterA.groupIndex != 0) 34 | { 35 | return filterA.groupIndex > 0; 36 | } 37 | 38 | bool collide = (filterA.maskBits & filterB.categoryBits) != 0 && (filterA.categoryBits & filterB.maskBits) != 0; 39 | return collide; 40 | } 41 | --------------------------------------------------------------------------------