KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
collision.hpp
1/*
2 KTech, Kaup's C++ 2D terminal game engine library.
3 Copyright (C) 2023-2025 Ethan Kaufman (AKA Kaup)
4
5 This file is part of KTech.
6
7 KTech is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 any later version.
11
12 KTech is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with KTech. If not, see <https://www.gnu.org/licenses/>.
19*/
20
21#pragma once
22
23#define KTECH_DEFINITION
24#include "../ktech.hpp"
25#undef KTECH_DEFINITION
26#include "../world/object.hpp"
27
28#include <vector>
29
34{
35public:
36 enum class CR : uint8_t
37 {
38 B,
39 P,
40 O
41 };
42
43 std::vector<std::vector<CR>> colliderTypes{
44 { CR::B, CR::P, CR::O }, // Unpushable - 0
45 { CR::B, CR::P, CR::O }, // Pushable - 1
46 { CR::O, CR::O, CR::O } // Overlapping - 2
47 };
48
49 auto MoveObject(const ID<Object>& object, Point direction) -> bool;
50
51private:
52 Engine& engine;
53
54 struct CollisionData{
55 ID<Object> activeObject;
56 ID<Object> passiveObject;
57 size_t activeCollider;
58 size_t passiveCollider;
59 };
60
61 inline Collision(Engine& engine)
62 : engine(engine) {};
63
64 auto GetPotentialCollisionResult(uint8_t type1, uint8_t type2) -> CR;
65 // Warning: `position1` and `position2` override `collider1.m_rPos` and `collider2.m_rPos` respectively
66 static auto AreCollidersOverlapping(const Collider& collider1, const Point& position1, const Collider& collider2, const Point& position2) -> bool;
67 static auto AreSimpleCollidersOverlapping(const Collider& collider1, const Point& position1, const Collider& collider2, const Point& position2) -> bool;
68 static auto AreSimpleAndComplexCollidersOverlapping(const Collider& complex, const Point& complexPosition, const Collider& simple, const Point& simplePosition) -> bool;
69 static auto AreComplexCollidersOverlapping(const Collider& collider1, const Point& position1, const Collider& collider2, const Point& position2) -> bool;
70 void ExpandMovementTree(const ID<Object>& thisObject, Point direction,
71 std::vector<CollisionData>& pushData,
72 std::vector<CollisionData>& blockData,
73 std::vector<CollisionData>& overlapData,
74 std::vector<CollisionData>& exitOverlapData);
75
76 friend class Engine;
77};
Engine component responsible for processing Object movement and collision.
Definition collision.hpp:34
CR
Collision result.
Definition collision.hpp:37
@ P
Push; the moving collider can make way by moving the passive collider with it.
@ B
Block; the moving collider can't move because the passive collider can't be moved.
@ O
Overlap; the moving collider can move into the passive collider.
std::vector< std::vector< CR > > colliderTypes
Matrix representing collider types and their potential collision results.
Definition collision.hpp:43
auto MoveObject(const ID< Object > &object, Point direction) -> bool
Move an Object in relation to other Objects from the same Layer.
Definition collision.cpp:89
Complete engine containing all engine components.
Definition engine.hpp:41
Serializable world structure identifier.
Definition id.hpp:38
2D vector, mostly used to store positions and directions.
Definition point.hpp:30