rngine 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/build.gradle +5 -1
- package/lib/module/Shared.js +14 -1
- package/lib/module/Shared.js.map +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/Shared.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +0 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/nativeTypes.d.ts +1 -1
- package/lib/typescript/src/nativeTypes.d.ts.map +1 -1
- package/lib/typescript/src/types.d.ts +5 -1
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/nitrogen/generated/shared/c++/System.hpp +6 -5
- package/package.json +1 -1
- package/shared/GameLoop.cpp +45 -21
- package/shared/GameLoop.hpp +9 -0
- package/shared/GameMethods.cpp +6 -1
- package/src/Shared.ts +17 -1
- package/src/index.tsx +0 -1
- package/src/nativeTypes.ts +1 -1
- package/src/types.ts +7 -1
package/android/build.gradle
CHANGED
|
@@ -35,7 +35,11 @@ apply plugin: "com.android.library"
|
|
|
35
35
|
apply plugin: "kotlin-android"
|
|
36
36
|
apply from: '../nitrogen/generated/android/rngine+autolinking.gradle'
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
if (rootProject.name != "rngine.example") {
|
|
39
|
+
apply plugin: "com.facebook.react"
|
|
40
|
+
} else {
|
|
41
|
+
println("\u001B[33m⚠️ Skipping React Native Gradle plugin in library (example build detected)\u001B[0m")
|
|
42
|
+
}
|
|
39
43
|
|
|
40
44
|
android {
|
|
41
45
|
namespace "com.margelo.nitro.rngine"
|
package/lib/module/Shared.js
CHANGED
|
@@ -41,10 +41,23 @@ export const configure = ({
|
|
|
41
41
|
for (const entity of entities) {
|
|
42
42
|
entity.asset = registerAssetIfNotExist(entity.asset);
|
|
43
43
|
}
|
|
44
|
+
let nativeSystems = [];
|
|
45
|
+
for (const system of systems) {
|
|
46
|
+
nativeSystems.push({
|
|
47
|
+
entities: system.entities,
|
|
48
|
+
collisions: system.collisions,
|
|
49
|
+
onTick: (e, c) => {
|
|
50
|
+
const start = Date.now();
|
|
51
|
+
system.onTick(e, c);
|
|
52
|
+
const finish = Date.now();
|
|
53
|
+
return finish - start;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
44
57
|
gameMethods.setTickRate(tickRate);
|
|
45
58
|
gameMethods.setScreen(screen);
|
|
46
59
|
gameMethods.setEntities(entities);
|
|
47
|
-
gameMethods.setSystems(
|
|
60
|
+
gameMethods.setSystems(nativeSystems);
|
|
48
61
|
if (paused) {
|
|
49
62
|
gameMethods.pause();
|
|
50
63
|
} else {
|
package/lib/module/Shared.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Image","gameAssets","gameMethods","assetCache","WeakMap","nextId","registerAssetIfNotExist","asset","has","get","assetId","set","registerLottie","JSON","stringify","isAssetRegistered","assetUri","resolveAssetSource","uri","registerSvg","configure","tickRate","screen","entities","systems","paused","entity","setTickRate","setScreen","setEntities","setSystems","pause","resume","spawn","entityArray","Array","isArray","update","updates","entityUpdateArray","entityUpdate"],"sourceRoot":"../../src","sources":["Shared.ts"],"mappings":";;AAAA,SAASA,KAAK,QAAQ,cAAc;AACpC,SAASC,UAAU,QAAQ,uBAAoB;AAC/C,SAASC,WAAW,QAAQ,wBAAqB;
|
|
1
|
+
{"version":3,"names":["Image","gameAssets","gameMethods","assetCache","WeakMap","nextId","registerAssetIfNotExist","asset","has","get","assetId","set","registerLottie","JSON","stringify","isAssetRegistered","assetUri","resolveAssetSource","uri","registerSvg","configure","tickRate","screen","entities","systems","paused","entity","nativeSystems","system","push","collisions","onTick","e","c","start","Date","now","finish","setTickRate","setScreen","setEntities","setSystems","pause","resume","spawn","entityArray","Array","isArray","update","updates","entityUpdateArray","entityUpdate"],"sourceRoot":"../../src","sources":["Shared.ts"],"mappings":";;AAAA,SAASA,KAAK,QAAQ,cAAc;AACpC,S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CAACC,OAAO,CAACE,OAAO,CAAC,GAAGA,OAAO,GAAG,CAACA,OAAO,CAAC;EAEtE,KAAK,MAAME,YAAY,IAAID,iBAAiB,EAAE;IAC5CC,YAAY,CAAC5C,KAAK,GAAGD,uBAAuB,CAAC6C,YAAY,CAAC5C,KAAK,CAAC;EAClE;EAEAL,WAAW,CAAC8C,MAAM,CAACE,iBAAyC,CAAC;AAC/D,CAAC","ignoreList":[]}
|
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["GameEngine"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,OAAOA,UAAU,MAAM,iBAAc;AAErC,cAAc,kBAAe;AAC7B,cAAc,aAAU;
|
|
1
|
+
{"version":3,"names":["GameEngine"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,OAAOA,UAAU,MAAM,iBAAc;AAErC,cAAc,kBAAe;AAC7B,cAAc,aAAU;AACxB,cAAc,YAAS;AACvB,SAASA,UAAU","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Shared.d.ts","sourceRoot":"","sources":["../../../src/Shared.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Shared.d.ts","sourceRoot":"","sources":["../../../src/Shared.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAS,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AA8BnE,4FAA4F;AAC5F,eAAO,MAAM,SAAS,GAAI,kDAMvB,MAAM,SA8BR,CAAC;AAEF,0EAA0E;AAC1E,eAAO,MAAM,KAAK,GAAI,UAAU,MAAM,GAAG,MAAM,EAAE,SAQhD,CAAC;AAEF,0EAA0E;AAC1E,eAAO,MAAM,MAAM,GAAI,SAAS,YAAY,GAAG,YAAY,EAAE,SAQ5D,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,cAAc,CAAC;AAEtC,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,cAAc,CAAC;AAEtC,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -52,7 +52,7 @@ export type System = {
|
|
|
52
52
|
/** Collision pairs to watch for every tick. */
|
|
53
53
|
collisions?: CollisionPair[];
|
|
54
54
|
/** Called every tick with the resolved entities and collisions. */
|
|
55
|
-
onTick: (entities: Entity[], collisions: Collision[]) =>
|
|
55
|
+
onTick: (entities: Entity[], collisions: Collision[]) => number;
|
|
56
56
|
};
|
|
57
57
|
export type EntityUpdate = {
|
|
58
58
|
/** Unique identifier or prefix of the entities to update. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nativeTypes.d.ts","sourceRoot":"","sources":["../../../src/nativeTypes.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,GAAG;IACnB,yCAAyC;IACzC,KAAK,EAAE,MAAM,CAAC;IACd,0CAA0C;IAC1C,MAAM,EAAE,MAAM,CAAC;IACf,uEAAuE;IACvE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,uEAAuE;IACvE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,8FAA8F;IAC9F,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,oFAAoF;IACpF,EAAE,EAAE,MAAM,CAAC;IACX,gCAAgC;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,gCAAgC;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,2BAA2B;IAC3B,KAAK,EAAE,M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AChC,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,2BAA2B;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,4BAA4B;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mDAAmD;IACnD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,uEAAuE;IACvE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,8FAA8F;IAC9F,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uDAAuD;IACvD,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,uDAAuD;IACvD,EAAE,CAAC,EAAE,MAAM,CAAC;CACb,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Screen as NativeScreen, Entity as NativeEntity, EntityUpdate as NativeEntityUpdate, System } from './nativeTypes';
|
|
1
|
+
import type { Screen as NativeScreen, Entity as NativeEntity, EntityUpdate as NativeEntityUpdate, System as NativeSystem, Collision } from './nativeTypes';
|
|
2
2
|
export type Asset = number | object;
|
|
3
3
|
export type Screen = Omit<NativeScreen, 'asset'> & {
|
|
4
4
|
/** Asset to render, use `require` with the file path to get the id. */
|
|
@@ -12,6 +12,10 @@ export type EntityUpdate = Omit<NativeEntityUpdate, 'asset'> & {
|
|
|
12
12
|
/** Asset to render, use `require` with the file path to get the id. */
|
|
13
13
|
asset?: Asset;
|
|
14
14
|
};
|
|
15
|
+
export type System = Omit<NativeSystem, 'onTick'> & {
|
|
16
|
+
/** Called every tick with the resolved entities and collisions. */
|
|
17
|
+
onTick: (entities: NativeEntity[], collisions: Collision[]) => void;
|
|
18
|
+
};
|
|
15
19
|
export type Config = {
|
|
16
20
|
/** Number of game logic updates per second. */
|
|
17
21
|
tickRate: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,IAAI,YAAY,EACtB,MAAM,IAAI,YAAY,EAC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+39,7 @@ namespace margelo::nitro::rngine { struct Collision; }
|
|
|
39
39
|
#include <vector>
|
|
40
40
|
#include <optional>
|
|
41
41
|
#include "CollisionPair.hpp"
|
|
42
|
+
#include <NitroModules/Promise.hpp>
|
|
42
43
|
#include "Entity.hpp"
|
|
43
44
|
#include "Collision.hpp"
|
|
44
45
|
#include <functional>
|
|
@@ -52,11 +53,11 @@ namespace margelo::nitro::rngine {
|
|
|
52
53
|
public:
|
|
53
54
|
std::optional<std::vector<std::string>> entities SWIFT_PRIVATE;
|
|
54
55
|
std::optional<std::vector<CollisionPair>> collisions SWIFT_PRIVATE;
|
|
55
|
-
std::function<
|
|
56
|
+
std::function<std::shared_ptr<Promise<double>>(const std::vector<Entity>& /* entities */, const std::vector<Collision>& /* collisions */)> onTick SWIFT_PRIVATE;
|
|
56
57
|
|
|
57
58
|
public:
|
|
58
59
|
System() = default;
|
|
59
|
-
explicit System(std::optional<std::vector<std::string>> entities, std::optional<std::vector<CollisionPair>> collisions, std::function<
|
|
60
|
+
explicit System(std::optional<std::vector<std::string>> entities, std::optional<std::vector<CollisionPair>> collisions, std::function<std::shared_ptr<Promise<double>>(const std::vector<Entity>& /* entities */, const std::vector<Collision>& /* collisions */)> onTick): entities(entities), collisions(collisions), onTick(onTick) {}
|
|
60
61
|
|
|
61
62
|
public:
|
|
62
63
|
// System is not equatable because these properties are not equatable: onTick
|
|
@@ -74,14 +75,14 @@ namespace margelo::nitro {
|
|
|
74
75
|
return margelo::nitro::rngine::System(
|
|
75
76
|
JSIConverter<std::optional<std::vector<std::string>>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "entities"))),
|
|
76
77
|
JSIConverter<std::optional<std::vector<margelo::nitro::rngine::CollisionPair>>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "collisions"))),
|
|
77
|
-
JSIConverter<std::function<
|
|
78
|
+
JSIConverter<std::function<std::shared_ptr<Promise<double>>(const std::vector<margelo::nitro::rngine::Entity>&, const std::vector<margelo::nitro::rngine::Collision>&)>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "onTick")))
|
|
78
79
|
);
|
|
79
80
|
}
|
|
80
81
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::rngine::System& arg) {
|
|
81
82
|
jsi::Object obj(runtime);
|
|
82
83
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "entities"), JSIConverter<std::optional<std::vector<std::string>>>::toJSI(runtime, arg.entities));
|
|
83
84
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "collisions"), JSIConverter<std::optional<std::vector<margelo::nitro::rngine::CollisionPair>>>::toJSI(runtime, arg.collisions));
|
|
84
|
-
obj.setProperty(runtime, PropNameIDCache::get(runtime, "onTick"), JSIConverter<std::function<
|
|
85
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "onTick"), JSIConverter<std::function<std::shared_ptr<Promise<double>>(const std::vector<margelo::nitro::rngine::Entity>&, const std::vector<margelo::nitro::rngine::Collision>&)>>::toJSI(runtime, arg.onTick));
|
|
85
86
|
return obj;
|
|
86
87
|
}
|
|
87
88
|
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
@@ -94,7 +95,7 @@ namespace margelo::nitro {
|
|
|
94
95
|
}
|
|
95
96
|
if (!JSIConverter<std::optional<std::vector<std::string>>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "entities")))) return false;
|
|
96
97
|
if (!JSIConverter<std::optional<std::vector<margelo::nitro::rngine::CollisionPair>>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "collisions")))) return false;
|
|
97
|
-
if (!JSIConverter<std::function<
|
|
98
|
+
if (!JSIConverter<std::function<std::shared_ptr<Promise<double>>(const std::vector<margelo::nitro::rngine::Entity>&, const std::vector<margelo::nitro::rngine::Collision>&)>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "onTick")))) return false;
|
|
98
99
|
return true;
|
|
99
100
|
}
|
|
100
101
|
};
|
package/package.json
CHANGED
package/shared/GameLoop.cpp
CHANGED
|
@@ -61,29 +61,32 @@ void GameLoop::registerLottieDuration(double id, double duration) {
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
std::vector<Rect> GameLoop::getRectsSnapshot() {
|
|
64
|
-
std::lock_guard<std::mutex>
|
|
64
|
+
std::lock_guard<std::mutex> snapshotLock(_snapshotMutex);
|
|
65
65
|
std::vector<Rect> rects;
|
|
66
|
-
rects.reserve(
|
|
67
|
-
|
|
68
|
-
rects.push_back({0, static_cast<float>(
|
|
69
|
-
static_cast<float>(
|
|
70
|
-
static_cast<float>(
|
|
71
|
-
parseHexColor(
|
|
72
|
-
static_cast<int32_t>(
|
|
73
|
-
|
|
74
|
-
for (const auto &[id,
|
|
75
|
-
if (
|
|
76
|
-
|
|
66
|
+
rects.reserve(_entitiesSnapshot.size() + 1);
|
|
67
|
+
|
|
68
|
+
rects.push_back({0, static_cast<float>(_screenSnapshot.width), 0,
|
|
69
|
+
static_cast<float>(_screenSnapshot.height),
|
|
70
|
+
static_cast<float>(_screenSnapshot.progress.value_or(0)),
|
|
71
|
+
parseHexColor(_screenSnapshot.color),
|
|
72
|
+
static_cast<int32_t>(_screenSnapshot.asset.value_or(0))});
|
|
73
|
+
|
|
74
|
+
for (const auto &[id, entitySnapshot] : _entitiesSnapshot) {
|
|
75
|
+
if (entitySnapshot.px + entitySnapshot.width < 0 ||
|
|
76
|
+
entitySnapshot.px > _screenSnapshot.width ||
|
|
77
|
+
entitySnapshot.py + entitySnapshot.height < 0 ||
|
|
78
|
+
entitySnapshot.py > _screenSnapshot.height) {
|
|
77
79
|
continue;
|
|
78
80
|
}
|
|
79
81
|
|
|
80
|
-
rects.push_back(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
82
|
+
rects.push_back(
|
|
83
|
+
{static_cast<float>(entitySnapshot.px),
|
|
84
|
+
static_cast<float>(entitySnapshot.px + entitySnapshot.width),
|
|
85
|
+
static_cast<float>(entitySnapshot.py),
|
|
86
|
+
static_cast<float>(entitySnapshot.py + entitySnapshot.height),
|
|
87
|
+
static_cast<float>(entitySnapshot.progress.value_or(0)),
|
|
88
|
+
parseHexColor(entitySnapshot.color),
|
|
89
|
+
static_cast<int32_t>(entitySnapshot.asset.value_or(0))});
|
|
87
90
|
}
|
|
88
91
|
return rects;
|
|
89
92
|
}
|
|
@@ -122,7 +125,8 @@ void GameLoop::runGameLoop() {
|
|
|
122
125
|
}
|
|
123
126
|
|
|
124
127
|
void GameLoop::runSystems() {
|
|
125
|
-
for (
|
|
128
|
+
for (size_t systemIndex = 0; systemIndex < _systems.size(); ++systemIndex) {
|
|
129
|
+
auto &system = _systems[systemIndex];
|
|
126
130
|
std::vector<Entity> entities;
|
|
127
131
|
std::vector<Collision> collisions;
|
|
128
132
|
|
|
@@ -170,14 +174,34 @@ void GameLoop::runSystems() {
|
|
|
170
174
|
}
|
|
171
175
|
}
|
|
172
176
|
|
|
173
|
-
|
|
177
|
+
__android_log_print(
|
|
178
|
+
ANDROID_LOG_DEBUG, "GameLoop",
|
|
179
|
+
"System Index: %zu, Entities: %zu, Collisions: %zu, Calling onTick",
|
|
180
|
+
systemIndex, entities.size(), collisions.size());
|
|
181
|
+
|
|
182
|
+
std::shared_ptr<Promise<double>> promise =
|
|
183
|
+
system.onTick(entities, collisions);
|
|
184
|
+
std::future<double> future = promise->await();
|
|
185
|
+
double duration = future.get();
|
|
186
|
+
|
|
187
|
+
__android_log_print(ANDROID_LOG_DEBUG, "GameLoop",
|
|
188
|
+
"System Index: %zu, onTick Complete, Duration: %.2fms",
|
|
189
|
+
systemIndex, duration);
|
|
174
190
|
}
|
|
175
191
|
}
|
|
176
192
|
|
|
193
|
+
void GameLoop::captureSnapshot() {
|
|
194
|
+
std::lock_guard<std::mutex> snapshotLock(_snapshotMutex);
|
|
195
|
+
std::lock_guard<std::mutex> lock(_mutex);
|
|
196
|
+
_entitiesSnapshot = _entities;
|
|
197
|
+
_screenSnapshot = _screen;
|
|
198
|
+
}
|
|
199
|
+
|
|
177
200
|
void GameLoop::update(double deltaTime) {
|
|
178
201
|
updateStats(deltaTime);
|
|
179
202
|
updateEntities(deltaTime);
|
|
180
203
|
runSystems();
|
|
204
|
+
captureSnapshot();
|
|
181
205
|
}
|
|
182
206
|
|
|
183
207
|
void GameLoop::updateStats(double deltaTime) {
|
package/shared/GameLoop.hpp
CHANGED
|
@@ -22,10 +22,15 @@ public:
|
|
|
22
22
|
~GameLoop();
|
|
23
23
|
|
|
24
24
|
std::mutex &getMutexInternal() { return _mutex; };
|
|
25
|
+
std::mutex &getSnapshotMutexInternal() { return _snapshotMutex; };
|
|
25
26
|
std::map<string, Entity> &getEntitiesInternal() { return _entities; };
|
|
27
|
+
std::map<string, Entity> &getEntitiesSnapshotInternal() {
|
|
28
|
+
return _entitiesSnapshot;
|
|
29
|
+
};
|
|
26
30
|
std::vector<System> &getSystemsInternal() { return _systems; };
|
|
27
31
|
std::atomic<bool> &getIsPausedInternal() { return _isPaused; };
|
|
28
32
|
Screen &getScreenInternal() { return _screen; };
|
|
33
|
+
Screen &getScreenSnapshotInternal() { return _screenSnapshot; };
|
|
29
34
|
|
|
30
35
|
std::vector<Entity *> resolveEntitiesInternal(const std::string &prefix);
|
|
31
36
|
void registerLottieDuration(double, double);
|
|
@@ -36,18 +41,22 @@ public:
|
|
|
36
41
|
private:
|
|
37
42
|
explicit GameLoop();
|
|
38
43
|
std::mutex _mutex;
|
|
44
|
+
std::mutex _snapshotMutex;
|
|
39
45
|
std::map<std::string, Entity> _entities;
|
|
46
|
+
std::map<std::string, Entity> _entitiesSnapshot;
|
|
40
47
|
std::map<double, double> _lottieDurations;
|
|
41
48
|
std::vector<System> _systems{};
|
|
42
49
|
std::atomic<bool> _isRunning{true};
|
|
43
50
|
std::atomic<bool> _isPaused{true};
|
|
44
51
|
std::atomic<double> _tickRate{60.0};
|
|
45
52
|
Screen _screen;
|
|
53
|
+
Screen _screenSnapshot;
|
|
46
54
|
GameStats _gameStats;
|
|
47
55
|
std::unique_ptr<std::thread> _gameThread;
|
|
48
56
|
|
|
49
57
|
void runGameLoop();
|
|
50
58
|
void runSystems();
|
|
59
|
+
void captureSnapshot();
|
|
51
60
|
void update(double deltaTime);
|
|
52
61
|
void updateStats(double deltaTime);
|
|
53
62
|
void updateEntities(double deltaTime);
|
package/shared/GameMethods.cpp
CHANGED
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
namespace margelo::nitro::rngine {
|
|
7
7
|
void GameMethods::setTickRate(double tickRate) {
|
|
8
8
|
auto &instance = GameLoop::getInstance();
|
|
9
|
-
std::lock_guard<std::mutex> lock(instance.getMutexInternal());
|
|
10
9
|
instance.setTickRate(tickRate);
|
|
11
10
|
__android_log_print(ANDROID_LOG_INFO, "GameMethods",
|
|
12
11
|
"setTickRate: tickRate=%.1f", tickRate);
|
|
@@ -15,7 +14,10 @@ void GameMethods::setTickRate(double tickRate) {
|
|
|
15
14
|
void GameMethods::setScreen(const Screen &screen) {
|
|
16
15
|
auto &instance = GameLoop::getInstance();
|
|
17
16
|
std::lock_guard<std::mutex> lock(instance.getMutexInternal());
|
|
17
|
+
std::lock_guard<std::mutex> snapshotLock(instance.getSnapshotMutexInternal());
|
|
18
18
|
instance.getScreenInternal() = screen;
|
|
19
|
+
instance.getScreenSnapshotInternal() = screen;
|
|
20
|
+
|
|
19
21
|
__android_log_print(ANDROID_LOG_INFO, "GameMethods",
|
|
20
22
|
"setWorld: width=%.0f height=%.0f", screen.width,
|
|
21
23
|
screen.height);
|
|
@@ -24,9 +26,12 @@ void GameMethods::setScreen(const Screen &screen) {
|
|
|
24
26
|
void GameMethods::setEntities(const std::vector<Entity> &entities) {
|
|
25
27
|
auto &instance = GameLoop::getInstance();
|
|
26
28
|
std::lock_guard<std::mutex> lock(instance.getMutexInternal());
|
|
29
|
+
std::lock_guard<std::mutex> snapshotLock(instance.getSnapshotMutexInternal());
|
|
27
30
|
auto &entitiesInternal = instance.getEntitiesInternal();
|
|
31
|
+
auto &entitiesSnapshotInternal = instance.getEntitiesSnapshotInternal();
|
|
28
32
|
for (auto &entity : entities) {
|
|
29
33
|
entitiesInternal[entity.id] = entity;
|
|
34
|
+
entitiesSnapshotInternal[entity.id] = entity;
|
|
30
35
|
}
|
|
31
36
|
__android_log_print(ANDROID_LOG_INFO, "GameMethods",
|
|
32
37
|
"setEntities: loaded %zu entities", entities.size());
|
package/src/Shared.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
Screen as NativeScreen,
|
|
6
6
|
Entity as NativeEntity,
|
|
7
7
|
EntityUpdate as NativeEntityUpdate,
|
|
8
|
+
System as NativeSystem,
|
|
8
9
|
} from './nativeTypes';
|
|
9
10
|
import type { Asset, Config, Entity, EntityUpdate } from './types';
|
|
10
11
|
|
|
@@ -49,10 +50,25 @@ export const configure = ({
|
|
|
49
50
|
entity.asset = registerAssetIfNotExist(entity.asset);
|
|
50
51
|
}
|
|
51
52
|
|
|
53
|
+
let nativeSystems: NativeSystem[] = [];
|
|
54
|
+
|
|
55
|
+
for (const system of systems) {
|
|
56
|
+
nativeSystems.push({
|
|
57
|
+
entities: system.entities,
|
|
58
|
+
collisions: system.collisions,
|
|
59
|
+
onTick: (e, c) => {
|
|
60
|
+
const start = Date.now();
|
|
61
|
+
system.onTick(e, c);
|
|
62
|
+
const finish = Date.now();
|
|
63
|
+
return finish - start;
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
52
68
|
gameMethods.setTickRate(tickRate);
|
|
53
69
|
gameMethods.setScreen(screen as NativeScreen);
|
|
54
70
|
gameMethods.setEntities(entities as NativeEntity[]);
|
|
55
|
-
gameMethods.setSystems(
|
|
71
|
+
gameMethods.setSystems(nativeSystems);
|
|
56
72
|
if (paused) {
|
|
57
73
|
gameMethods.pause();
|
|
58
74
|
} else {
|
package/src/index.tsx
CHANGED
package/src/nativeTypes.ts
CHANGED
|
@@ -56,7 +56,7 @@ export type System = {
|
|
|
56
56
|
/** Collision pairs to watch for every tick. */
|
|
57
57
|
collisions?: CollisionPair[];
|
|
58
58
|
/** Called every tick with the resolved entities and collisions. */
|
|
59
|
-
onTick: (entities: Entity[], collisions: Collision[]) =>
|
|
59
|
+
onTick: (entities: Entity[], collisions: Collision[]) => number;
|
|
60
60
|
};
|
|
61
61
|
|
|
62
62
|
export type EntityUpdate = {
|
package/src/types.ts
CHANGED
|
@@ -2,7 +2,8 @@ import type {
|
|
|
2
2
|
Screen as NativeScreen,
|
|
3
3
|
Entity as NativeEntity,
|
|
4
4
|
EntityUpdate as NativeEntityUpdate,
|
|
5
|
-
System,
|
|
5
|
+
System as NativeSystem,
|
|
6
|
+
Collision,
|
|
6
7
|
} from './nativeTypes';
|
|
7
8
|
|
|
8
9
|
export type Asset = number | object;
|
|
@@ -20,6 +21,11 @@ export type EntityUpdate = Omit<NativeEntityUpdate, 'asset'> & {
|
|
|
20
21
|
asset?: Asset;
|
|
21
22
|
};
|
|
22
23
|
|
|
24
|
+
export type System = Omit<NativeSystem, 'onTick'> & {
|
|
25
|
+
/** Called every tick with the resolved entities and collisions. */
|
|
26
|
+
onTick: (entities: NativeEntity[], collisions: Collision[]) => void;
|
|
27
|
+
};
|
|
28
|
+
|
|
23
29
|
export type Config = {
|
|
24
30
|
/** Number of game logic updates per second. */
|
|
25
31
|
tickRate: number;
|