-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovement.cpp
More file actions
153 lines (123 loc) · 4.86 KB
/
Copy pathmovement.cpp
File metadata and controls
153 lines (123 loc) · 4.86 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "movement.h"
MovementManager::MovementManager() : map(NULL), world(NULL), CenterBody(NULL), ControlBody(NULL), PhysicsDeltaTime(0.00833333), PixelToMeters(0.03125), speedLimitX(10.0), speedLimitY(100.0), vIter(6), pIter(2)
{
cfg = SingletonConfig::config();
}
int MovementManager::Initialize()
{
if (map != NULL) { return 1; }
map = new MoveEventMap;
MoveEvent * ev;
SDL_Keycode key;
ev = new MoveEvent;
key = SDL_GetKeyFromName((*cfg)["Keys"]["Left"].asCString());
ev->timer = SingletonEvents::RegisterTimer(PhysicsDeltaTime, std::bind<void>([this](double dt, double elapsed) {
this->ControlBody->ApplyLinearImpulse(b2Vec2(1.0,0.0), this->ControlBody->GetWorldCenter(), true);
}, _1, _2));
SingletonEvents::ToggleTimer(ev->timer, false);
ev->key_down = SingletonEvents::Register(SDL_KEYDOWN, std::bind<void>([this, key, ev](SDL_Event &e) {
if (key == e.key.keysym.sym && ControlBody != NULL && !ev->is_down) {
SingletonEvents::ToggleTimer(ev->timer, true);
ev->is_down = true;
}
}, _1));
ev->key_up = SingletonEvents::Register(SDL_KEYUP, std::bind<void>([this, key, ev](SDL_Event &e) {
if (key == e.key.keysym.sym && ControlBody != NULL) {
SingletonEvents::ToggleTimer(ev->timer, false);
ev->is_down = false;
}
}, _1));
map->emplace(key, ev);
ev = new MoveEvent;
key = SDL_GetKeyFromName((*cfg)["Keys"]["Right"].asCString());
ev->timer = SingletonEvents::RegisterTimer(PhysicsDeltaTime, std::bind<void>([this](double dt, double elapsed) {
this->ControlBody->ApplyLinearImpulse(b2Vec2(-1.0,0.0), this->ControlBody->GetWorldCenter(), true);
}, _1, _2));
SingletonEvents::ToggleTimer(ev->timer, false);
ev->key_down = SingletonEvents::Register(SDL_KEYDOWN, std::bind<void>([this, key, ev](SDL_Event &e) {
if (key == e.key.keysym.sym && ControlBody != NULL && !ev->is_down) {
SingletonEvents::ToggleTimer(ev->timer, true);
ev->is_down = true;
}
}, _1));
ev->key_up = SingletonEvents::Register(SDL_KEYUP, std::bind<void>([this, key, ev](SDL_Event &e) {
if (key == e.key.keysym.sym && ControlBody != NULL) {
SingletonEvents::ToggleTimer(ev->timer, false);
ev->is_down = false;
}
}, _1));
map->emplace(key, ev);
ev = new MoveEvent;
key = SDL_GetKeyFromName((*cfg)["Keys"]["Jump"].asCString());
ev->key_down = SingletonEvents::Register(SDL_KEYDOWN, std::bind<void>([this, key, ev](SDL_Event &e) {
if (key == e.key.keysym.sym && ControlBody != NULL && !ev->is_down) {
this->ControlBody->ApplyLinearImpulse(b2Vec2(0.0,-22.0), this->ControlBody->GetWorldCenter(), true);
ev->is_down = true;
}
}, _1));
ev->key_up = SingletonEvents::Register(SDL_KEYUP, std::bind<void>([this, key, ev](SDL_Event &e) {
if (key == e.key.keysym.sym && ControlBody != NULL) {
ev->is_down = false;
}
}, _1));
map->emplace(key, ev);
UpdateTimer = SingletonEvents::RegisterTimer(PhysicsDeltaTime, std::bind(&MovementManager::UpdateDynamicPositions, this, _1, _2));
return 0;
}
void MovementManager::CreateWorld(double gx, double gy)
{
if (world != NULL) {
CenterBody = NULL;
ControlBody = NULL;
delete world;
}
world = new b2World(b2Vec2(gx, gy));
}
b2Body * MovementManager::AddStaticBody(int32_t x, int32_t y, int32_t sx, int32_t sy)
{
if (world == NULL) return NULL;
b2BodyDef def;
def.position.Set(x * PixelToMeters, y * PixelToMeters);
b2Body * body = world->CreateBody(&def);
b2PolygonShape box;
box.SetAsBox(sx * PixelToMeters / 2.0, sy * PixelToMeters / 2.0);
body->CreateFixture(&box, 0.0f);
return body;
}
b2Body * MovementManager::AddDynamicBody(int32_t x, int32_t y, int32_t sx, int32_t sy, float d, float f)
{
if (world == NULL) return NULL;
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(x * PixelToMeters, y * PixelToMeters);
bodyDef.fixedRotation = true;
b2Body * body = world->CreateBody(&bodyDef);
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(sx * PixelToMeters / 2.0, sy * PixelToMeters / 2.0);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = d;
fixtureDef.friction = f;
body->CreateFixture(&fixtureDef);
return body;
}
void MovementManager::UpdateDynamicPositions(double dt, double elapsed)
{
if (world == NULL) return;
if (ControlBody != NULL) {
if (ControlBody->GetLinearVelocity().x >= speedLimitX) {
ControlBody->SetLinearVelocity(b2Vec2(speedLimitX,ControlBody->GetLinearVelocity().y));
} else if (ControlBody->GetLinearVelocity().x < -speedLimitX) {
ControlBody->SetLinearVelocity(b2Vec2(-speedLimitX,ControlBody->GetLinearVelocity().y));
}
}
world->Step(dt, vIter, pIter);
world->ClearForces();
}
void MovementManager::SetToCenterInPixels(int32_t &x, int32_t &y)
{
if (CenterBody == NULL) return;
b2Vec2 center = CenterBody->GetWorldCenter();
x = center.x / PixelToMeters;
y = center.y / PixelToMeters;
}