-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisionController.hpp
More file actions
125 lines (110 loc) · 3.24 KB
/
Copy pathCollisionController.hpp
File metadata and controls
125 lines (110 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#ifndef COLLISION_CONTROLLER_H_
#define COLLISION_CONTROLLER_H_
#include <memory>
#include <vector>
#include <set>
#include <cstdint>
#include <cmath>
#include <inttypes.h>
#include <Box2D/Box2D.h>
#include "Controller.hpp"
#include "TileMap.hpp"
#include "SFMLDebugDraw.h"
#include "Pathfinding.hpp"
#include "Agent.hpp"
struct CollisionManager : public Controller
{
enum collisionType_e
{
COIN = 3,
FOOT
};
struct FootContactListener : public b2ContactListener
{
FootContactListener()
: numFootContacts(0)
{
}
void BeginContact(b2Contact* contact) override
{
// check if fixture A was the foot sensor
long dataA =
(long)reinterpret_cast<long*>(contact->GetFixtureA()->GetUserData());
long dataB =
(long)reinterpret_cast<long*>(contact->GetFixtureB()->GetUserData());
if (dataA != collisionType_e::COIN && dataB != collisionType_e::COIN) {
if (dataA == collisionType_e::FOOT || dataB == collisionType_e::FOOT) {
numFootContacts++;
}
} else {
b2Body* body;
if (dataA == collisionType_e::COIN) {
body = contact->GetFixtureA()->GetBody();
} else {
body = contact->GetFixtureB()->GetBody();
}
b2Vec2 position = body->GetPosition();
int tileX = (position.x * sfdd::SCALE) / tileSize;
int tileY = (position.y * sfdd::SCALE) / tileSize;
tileArr[tileY * mapWidth + tileX] = 0;
tileMap->initTiles(sf::Vector2u(tileSize, tileSize), tileArr, mapWidth,
mapHeight);
controller->scheduleDelete(body);
}
}
void EndContact(b2Contact* contact) override
{
int dataA =
(long)reinterpret_cast<int*>(contact->GetFixtureA()->GetUserData());
int dataB =
(long)reinterpret_cast<int*>(contact->GetFixtureB()->GetUserData());
if (dataA != collisionType_e::COIN && dataB != collisionType_e::COIN) {
if (dataA == collisionType_e::FOOT || dataB == collisionType_e::FOOT) {
numFootContacts--;
}
} else {
}
}
bool isOnGround() { return numFootContacts > 0; }
int numFootContacts;
int* tileArr;
TileMap* tileMap;
int tileSize;
int mapWidth;
int mapHeight;
CollisionManager* controller;
};
int* map;
TileMap* objMap;
TileMap* tileMap;
int* tileObjArr;
int tileSize;
int mapWidth;
int mapHeight;
int mapX;
int mapY;
int points;
FootContactListener listener;
Agent* agent;
b2Vec2 gravity;
std::unique_ptr<b2World> world;
b2Body* agentBody;
std::vector<b2BodyDef> bodyDefs;
std::set<b2Body*> bodiesToDelete;
Pathfinder pathFinder;
std::vector<std::vector<sf::Vertex>> paths;
CollisionManager();
virtual void update(sf::Time timeDelta, Agent* agent);
void initPhysics();
void initPhysics(Agent* agent);
void draw();
bool isPassable(int tileX, int tileY);
void scheduleDelete(b2Body* body);
int getPoints() const;
std::vector<sf::Vertex> getJumpPath(sf::Vector2i start, sf::Vector2i target);
std::vector<sf::Vertex> getSearchPath(sf::Vector2i start,
sf::Vector2i target);
bool canJumpBetween(sf::Vector2i start, sf::Vector2i target);
void updatePaths();
};
#endif