Skip to content

Commit 3512960

Browse files
author
Alessio Linares
committed
Forcearea added
1 parent 6218470 commit 3512960

File tree

6 files changed

+69
-18
lines changed

6 files changed

+69
-18
lines changed

HB/Tmx/TmxScene.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ m_post_init(std::move(post_init))
7777
for (int x = 0; x < layer->GetWidth(); ++x)
7878
{
7979
if (layer->GetTileTilesetIndex(x, y) == -1) continue;
80-
int gid = layer->GetTileId(x, y);
80+
int gid = layer->GetTileGid(x, y);
8181
const Tmx::Tileset *tileset = map->FindTileset(gid);
8282
int lid = gid - tileset->GetFirstGid();
8383
std::vector<int> anim;

main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ int main(int argc, char const *argv[])
6666
hb::TmxRegisterFactory("Bridge", makeBridge);
6767
hb::TmxRegisterFactory("Door", makeDoor);
6868
hb::TmxRegisterFactory("DynamicBody", makeDynamicBody);
69+
hb::TmxRegisterFactory("ForceArea", makeForceArea);
6970

7071
hb::Physics2d::getB2World()->SetGravity(b2Vec2(0, 10));
7172

makeForceArea.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "makeObjects.h"
2+
3+
void makeForceArea(hb::GameObject* go, const Tmx::Map* map, int obj_grp, int obj_id)
4+
{
5+
const Tmx::Object* object = map->GetObjectGroup(obj_grp)->GetObject(obj_id);
6+
b2BodyDef def;
7+
def.position = b2Vec2(go->getPosition().x + object->GetWidth()/64., go->getPosition().y + object->GetHeight()/64.);
8+
auto rigid_body = new hb::RigidBody2dComponent(def);
9+
hb::Material2d mat;
10+
mat.friction = object->GetProperties().GetFloatProperty("friction", 0.);
11+
mat.bounciness = object->GetProperties().GetFloatProperty("bounciness", 0.);
12+
mat.density = object->GetProperties().GetFloatProperty("density", 1.);
13+
if (object->GetEllipse() != 0)
14+
{
15+
hb::CircleCollider2d collider(
16+
mat,
17+
hb::Vector2d(),
18+
object->GetWidth()/64.,
19+
true);
20+
rigid_body->addCollider(collider);
21+
22+
}
23+
else
24+
{
25+
hb::BoxCollider2d collider(
26+
mat,
27+
hb::Vector2d(object->GetWidth()/32., object->GetHeight()/32.),
28+
hb::Vector2d(),
29+
object->GetRot(),
30+
true);
31+
rigid_body->addCollider(collider);
32+
}
33+
34+
auto fc = new hb::FunctionComponent();
35+
go->addComponents({rigid_body, fc});
36+
37+
b2Vec2 force (object->GetProperties().GetFloatProperty("force_x", 0.0), object->GetProperties().GetFloatProperty("force_y", 0.0));
38+
fc->addListener("sensorStay", [=](hb::DataRepository& data)
39+
{
40+
b2Body* other_body = data.getPointer<b2Fixture>("otherFixture")->GetBody();
41+
other_body->ApplyForceToCenter(force, true);
42+
});
43+
}

makeObjects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ void makeSwitch(hb::GameObject* go, const Tmx::Map* map, int obj_grp, int obj_id
1111
void makeBridge(hb::GameObject* go, const Tmx::Map* map, int obj_grp, int obj_id);
1212
void makeDoor(hb::GameObject* go, const Tmx::Map* map, int obj_grp, int obj_id);
1313
void makeDynamicBody(hb::GameObject* go, const Tmx::Map* map, int obj_grp, int obj_id);
14+
void makeForceArea(hb::GameObject* go, const Tmx::Map* map, int obj_grp, int obj_id);

makePlayer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void makePlayer(hb::GameObject* player, const Tmx::Map* map, int obj_grp, int ob
2121
b2BodyDef body_def;
2222
body_def.type = b2_dynamicBody;
2323
body_def.position = b2Vec2(player->getPosition().x+0.5, player->getPosition().y-0.5);
24+
body_def.linearDamping = 0.5f;
2425
//body_def.fixedRotation = true;
2526
auto rigid_body = new hb::RigidBody2dComponent(body_def);
2627
// Define the player GameObject

res/levels/physics.tmx

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="30" height="20" tilewidth="32" tileheight="32" backgroundcolor="#787878" nextobjectid="37">
2+
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="30" height="20" tilewidth="32" tileheight="32" backgroundcolor="#787878" nextobjectid="41">
33
<objectgroup name="Object Layer 1">
44
<object id="3" type="Wall" x="0" y="544" width="960" height="32">
55
<properties>
@@ -16,12 +16,12 @@
1616
<property name="friction" value="0.5"/>
1717
</properties>
1818
</object>
19-
<object id="6" type="DynamicBody" x="477.333" y="224" width="32" height="32">
19+
<object id="6" type="DynamicBody" x="725.333" y="17.3333" width="32" height="32">
2020
<properties>
2121
<property name="friction" value="0.5"/>
2222
</properties>
2323
</object>
24-
<object id="7" type="DynamicBody" x="362.667" y="332" width="32" height="32">
24+
<object id="7" type="DynamicBody" x="610.667" y="125.333" width="32" height="32">
2525
<properties>
2626
<property name="friction" value="0.5"/>
2727
</properties>
@@ -31,7 +31,7 @@
3131
<property name="friction" value="0.5"/>
3232
</properties>
3333
</object>
34-
<object id="9" type="DynamicBody" x="450.667" y="256" width="32" height="32">
34+
<object id="9" type="DynamicBody" x="698.667" y="49.3333" width="32" height="32">
3535
<properties>
3636
<property name="friction" value="0.5"/>
3737
</properties>
@@ -41,7 +41,7 @@
4141
<property name="friction" value="0.5"/>
4242
</properties>
4343
</object>
44-
<object id="11" type="DynamicBody" x="545.333" y="230.667" width="30.6667" height="102.667" rotation="25">
44+
<object id="11" type="DynamicBody" x="793.333" y="24.0003" width="30.6667" height="102.667" rotation="25">
4545
<properties>
4646
<property name="friction" value="0.5"/>
4747
</properties>
@@ -56,17 +56,17 @@
5656
<property name="friction" value="0.5"/>
5757
</properties>
5858
</object>
59-
<object id="14" type="DynamicBody" x="382.667" y="277.333" width="116" height="28" rotation="25">
59+
<object id="14" type="DynamicBody" x="630.667" y="70.6663" width="116" height="28" rotation="25">
6060
<properties>
6161
<property name="friction" value="0.5"/>
6262
</properties>
6363
</object>
64-
<object id="16" type="DynamicBody" x="556" y="372" width="32" height="32">
64+
<object id="16" type="DynamicBody" x="804" y="165.333" width="32" height="32">
6565
<properties>
6666
<property name="friction" value="0.5"/>
6767
</properties>
6868
</object>
69-
<object id="17" type="DynamicBody" x="438.667" y="412" width="32" height="32">
69+
<object id="17" type="DynamicBody" x="686.667" y="205.333" width="32" height="32">
7070
<properties>
7171
<property name="friction" value="0.5"/>
7272
</properties>
@@ -76,17 +76,17 @@
7676
<property name="friction" value="0.5"/>
7777
</properties>
7878
</object>
79-
<object id="19" type="Player" x="365.333" y="461.333" width="32" height="32" rotation="25">
79+
<object id="19" type="Player" x="613.333" y="254.666" width="32" height="32" rotation="25">
8080
<properties>
8181
<property name="friction" value="0.5"/>
8282
</properties>
8383
</object>
84-
<object id="20" type="DynamicBody" x="301.688" y="426.656" width="32" height="32" rotation="-311.604">
84+
<object id="20" type="DynamicBody" x="549.688" y="219.989" width="32" height="32" rotation="-311.604">
8585
<properties>
8686
<property name="friction" value="0.5"/>
8787
</properties>
8888
</object>
89-
<object id="21" type="DynamicBody" x="250.634" y="443.46" width="32" height="32" rotation="-286.604">
89+
<object id="21" type="DynamicBody" x="498.634" y="236.793" width="32" height="32" rotation="-286.604">
9090
<properties>
9191
<property name="friction" value="0.5"/>
9292
</properties>
@@ -96,7 +96,7 @@
9696
<property name="friction" value="0.5"/>
9797
</properties>
9898
</object>
99-
<object id="23" type="DynamicBody" x="236.606" y="377.459" width="32" height="32" rotation="11.4">
99+
<object id="23" type="DynamicBody" x="484.606" y="170.792" width="32" height="32" rotation="11.4">
100100
<properties>
101101
<property name="friction" value="0.5"/>
102102
</properties>
@@ -106,7 +106,7 @@
106106
<property name="friction" value="0.5"/>
107107
</properties>
108108
</object>
109-
<object id="25" type="DynamicBody" x="395.242" y="222.625" width="32" height="32" rotation="11.4">
109+
<object id="25" type="DynamicBody" x="643.242" y="15.9583" width="32" height="32" rotation="11.4">
110110
<properties>
111111
<property name="friction" value="0.5"/>
112112
</properties>
@@ -118,7 +118,7 @@
118118
</properties>
119119
<ellipse/>
120120
</object>
121-
<object id="27" type="DynamicBody" x="577.333" y="214.667" width="32" height="32">
121+
<object id="27" type="DynamicBody" x="825.333" y="8.00033" width="32" height="32">
122122
<properties>
123123
<property name="friction" value="0.5"/>
124124
</properties>
@@ -136,19 +136,19 @@
136136
</properties>
137137
<ellipse/>
138138
</object>
139-
<object id="30" type="DynamicBody" x="500" y="453.333" width="32" height="32">
139+
<object id="30" type="DynamicBody" x="748" y="246.666" width="32" height="32">
140140
<properties>
141141
<property name="friction" value="0.5"/>
142142
</properties>
143143
<ellipse/>
144144
</object>
145-
<object id="31" type="DynamicBody" x="616" y="454.667" width="32" height="32">
145+
<object id="31" type="DynamicBody" x="864" y="248" width="32" height="32">
146146
<properties>
147147
<property name="friction" value="0.5"/>
148148
</properties>
149149
<ellipse/>
150150
</object>
151-
<object id="32" type="DynamicBody" x="354.667" y="381.333" width="32" height="32">
151+
<object id="32" type="DynamicBody" x="602.667" y="174.666" width="32" height="32">
152152
<properties>
153153
<property name="friction" value="0.5"/>
154154
</properties>
@@ -170,5 +170,10 @@
170170
<property name="friction" value="0.5"/>
171171
</properties>
172172
</object>
173+
<object id="37" type="ForceArea" x="25.3333" y="388" width="916" height="166.667">
174+
<properties>
175+
<property name="force_y" value="-20"/>
176+
</properties>
177+
</object>
173178
</objectgroup>
174179
</map>

0 commit comments

Comments
 (0)