generated from mrousavy/react-native-jsi-library-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJSIBox2dBody.h
118 lines (97 loc) · 4.2 KB
/
JSIBox2dBody.h
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
//
// Created by Hanno Gödecke on 15.07.22.
//
#pragma once
#include "box2d/b2_body.h"
#include "JSIBox2dVec2.h"
#include "JSIBox2dFixtureDef.h"
#include <jsi/jsi.h>
#include "jsi/JsiWrappingHostObjects.h"
#include "utils.h"
namespace Box2d {
using namespace facebook;
class JSIBox2dBody : public JsiWrappingHostObject<b2Body *> {
public:
/**
* Constructor
*/
JSIBox2dBody(b2Body *body)
: JsiWrappingHostObject<b2Body *>(body) {}
JSI_HOST_FUNCTION(GetPosition) {
return JSIBox2dVec2::toValue(runtime, getObject()->GetPosition());
}
JSI_HOST_FUNCTION(GetAngle) {
return jsi::Value((double) getObject()->GetAngle());
}
JSI_HOST_FUNCTION(CreateFixture) {
if (arguments[0].isObject()) {
const auto obj = arguments[0].asObject(runtime);
if (obj.isHostObject<JSIBox2dFixtureDef>(runtime)) {
getObject()->CreateFixture(
JSIBox2dFixtureDef::fromValue(runtime, arguments[0]).get());
return jsi::Value::undefined();
}
}
throw jsi::JSError(runtime, "Unsupported shape type");
}
JSI_HOST_FUNCTION(CreateFixture2) {
b2Shape *shape = Utils::getShape(runtime, arguments[0]);
getObject()->CreateFixture(shape, arguments[1].asNumber());
return jsi::Value::undefined();
}
JSI_HOST_FUNCTION(SetLinearVelocity) {
auto vector = JSIBox2dVec2::fromValue(runtime, arguments[0]).get();
getObject()->SetLinearVelocity(*vector);
return jsi::Value::undefined();
}
JSI_HOST_FUNCTION(GetLinearVelocity) {
return JSIBox2dVec2::toValue(runtime, getObject()->GetLinearVelocity());
}
JSI_HOST_FUNCTION(SetLinearDamping) {
auto linearDamping = arguments[0].asNumber();
getObject()->SetLinearDamping(linearDamping);
return jsi::Value::undefined();
}
JSI_HOST_FUNCTION(SetTransform) {
auto position = JSIBox2dVec2::fromValue(runtime, arguments[0]).get();
auto angle = arguments[1].asNumber();
getObject()->SetTransform(*position, angle);
return jsi::Value::undefined();
}
// JSI_HOST_FUNCTION(GetTransform) {
// b2Transform transform;
// getObject()->GetTransform(&transform);
// return JSIBox2dTransform::toValue(runtime, transform);
// }
JSI_HOST_FUNCTION(ApplyForceToCenter) {
auto vector = JSIBox2dVec2::fromValue(runtime, arguments[0]).get();
getObject()->ApplyForceToCenter(*vector, arguments[1].getBool());
return jsi::Value::undefined();
}
JSI_HOST_FUNCTION(ApplyLinearImpulseToCenter) {
auto vector = JSIBox2dVec2::fromValue(runtime, arguments[0]).get();
getObject()->ApplyLinearImpulseToCenter(*vector, arguments[1].getBool());
return jsi::Value::undefined();
}
JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JSIBox2dBody, GetAngle),
JSI_EXPORT_FUNC(JSIBox2dBody, GetPosition),
JSI_EXPORT_FUNC(JSIBox2dBody, CreateFixture),
JSI_EXPORT_FUNC(JSIBox2dBody, CreateFixture2),
JSI_EXPORT_FUNC(JSIBox2dBody, SetLinearVelocity),
JSI_EXPORT_FUNC(JSIBox2dBody, GetLinearVelocity),
JSI_EXPORT_FUNC(JSIBox2dBody, SetLinearDamping),
JSI_EXPORT_FUNC(JSIBox2dBody, SetTransform),
JSI_EXPORT_FUNC(JSIBox2dBody, ApplyForceToCenter),
JSI_EXPORT_FUNC(JSIBox2dBody, ApplyLinearImpulseToCenter),
);
/**
* Returns the underlying object from a host object of this type
*/
static b2Body* fromValue(jsi::Runtime &runtime,
const jsi::Value &obj) {
return obj.asObject(runtime)
.asHostObject<JSIBox2dBody>(runtime)
->getObject();
}
};
}