-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.cpp
89 lines (75 loc) · 2.53 KB
/
World.cpp
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
#include "World.h"
#include "MotionFilter.h"
World::World(QGraphicsScene* scene, QGraphicsView* view):
_scene(scene),
_view(view),
_engine(new QtBox2DEngine),
_myContactListener(new MyContactListener(_engine, _view)),
_cave(new Cave(_scene, _engine)),
_player_ship(new PlayerShip(_scene, _engine)) {
// Detect user/scene input
SceneMotionFilter* scene_motion_filter = new SceneMotionFilter(_player_ship);
// scene event filter required to get mouse events
_scene->installEventFilter(scene_motion_filter);
_view->installEventFilter(this);
for (int i = 0; i < 10; ++i) {
new EnemyShip(_scene, _engine, _cave);
}
for (int i = 0; i < 30; ++i) {
new Diamond(_scene, _engine, _cave);
}
_engine->setContactListener(_myContactListener);
_engine->setGravity(0);
_engine->setInterval(60);
connect(_engine, &QtBox2DEngine::step, this, &World::centerViewOnPlayer);
connect(_engine, &QtBox2DEngine::step, this, &World::updateDrag);
#ifdef __ANDROID_API__
ts.start();
connect(_engine, &QtBox2DEngine::step, this, &World::updateTitle);
#endif
}
void World::Start() {
_engine->start();
}
void World::centerViewOnPlayer() {
_view->centerOn(_player_ship->GetBody()->GetPosition().x, _view->height()/2);
}
// TODO(dek): move event filter to World
bool World::eventFilter(QObject *obj, QEvent *event) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
switch (keyEvent->key()) {
case Qt::Key_Left:
_player_ship->GetBody()->ApplyForceToCenter(b2Vec2(-1., 0), true);
break;
case Qt::Key_Right:
_player_ship->GetBody()->ApplyForceToCenter(b2Vec2(1., 0), true);
break;
case Qt::Key_Up:
_player_ship->GetBody()->ApplyForceToCenter(b2Vec2(0, -1.), true);
break;
case Qt::Key_Down:
_player_ship->GetBody()->ApplyForceToCenter(b2Vec2(0, 1.), true);
break;
}
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
return true;
}
void World::updateDrag() {
// Apply drag
// drag coefficient
float Cd = 0.05f;
b2Vec2 lv = _player_ship->GetBody()->GetLinearVelocity();
float v = lv.x * lv.x;
float dragForce = 0.5f * v * Cd;
float dragAngle = atan2f(lv.y, lv.x) + M_PI/2.f;
b2Vec2 appliedDrag(-sinf(dragAngle)*dragForce, 0.);
_player_ship->GetBody()->ApplyForceToCenter(appliedDrag, true);
}
void World::updateTilt() {
b2Vec2 tilt(ts.reading()->xRotation()/360. * 10., -ts.reading()->yRotation()/360.);
_player_ship->GetBody()->ApplyForceToCenter(tilt, true);
}