rngine 0.3.1 → 0.4.0
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/README.md +19 -16
- package/android/build.gradle +6 -4
- package/android/kls_database.db +0 -0
- package/android/src/main/cpp/GameLoopJNI.cpp +20 -9
- package/android/src/main/java/com/margelo/nitro/rngine/Asset.kt +9 -0
- package/android/src/main/java/com/margelo/nitro/rngine/GameAssets.kt +47 -0
- package/android/src/main/java/com/margelo/nitro/rngine/GameView.kt +46 -8
- package/android/src/main/java/com/margelo/nitro/rngine/Rect.kt +7 -5
- package/android/src/main/java/com/margelo/nitro/rngine/RectSerializer.kt +8 -6
- package/lib/module/GameAssets.nitro.js +5 -0
- package/lib/module/GameAssets.nitro.js.map +1 -0
- package/lib/module/GameMethods.js +1 -31
- package/lib/module/GameMethods.js.map +1 -1
- package/lib/module/GameMethods.nitro.js +2 -1
- package/lib/module/GameMethods.nitro.js.map +1 -1
- package/lib/module/Shared.js +72 -0
- package/lib/module/Shared.js.map +1 -0
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/nativeTypes.js +2 -0
- package/lib/module/nativeTypes.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/typescript/src/GameAssets.nitro.d.ts +12 -0
- package/lib/typescript/src/GameAssets.nitro.d.ts.map +1 -0
- package/lib/typescript/src/GameMethods.d.ts +0 -7
- package/lib/typescript/src/GameMethods.d.ts.map +1 -1
- package/lib/typescript/src/GameMethods.nitro.d.ts +5 -3
- package/lib/typescript/src/GameMethods.nitro.d.ts.map +1 -1
- package/lib/typescript/src/Shared.d.ts +8 -0
- package/lib/typescript/src/Shared.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +2 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/nativeTypes.d.ts +79 -0
- package/lib/typescript/src/nativeTypes.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +13 -49
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/nitro.json +10 -0
- package/nitrogen/generated/android/c++/JHybridGameAssetsSpec.cpp +61 -0
- package/nitrogen/generated/android/c++/JHybridGameAssetsSpec.hpp +65 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/rngine/HybridGameAssetsSpec.kt +62 -0
- package/nitrogen/generated/android/rngine+autolinking.cmake +2 -0
- package/nitrogen/generated/android/rngineOnLoad.cpp +16 -0
- package/nitrogen/generated/ios/Rngine-Swift-Cxx-Bridge.cpp +17 -0
- package/nitrogen/generated/ios/Rngine-Swift-Cxx-Bridge.hpp +37 -0
- package/nitrogen/generated/ios/Rngine-Swift-Cxx-Umbrella.hpp +8 -0
- package/nitrogen/generated/ios/RngineAutolinking.mm +8 -0
- package/nitrogen/generated/ios/RngineAutolinking.swift +12 -0
- package/nitrogen/generated/ios/c++/HybridGameAssetsSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridGameAssetsSpecSwift.hpp +94 -0
- package/nitrogen/generated/ios/swift/HybridGameAssetsSpec.swift +57 -0
- package/nitrogen/generated/ios/swift/HybridGameAssetsSpec_cxx.swift +160 -0
- package/nitrogen/generated/shared/c++/{World.hpp → Collision.hpp} +23 -23
- package/nitrogen/generated/shared/c++/CollisionPair.hpp +87 -0
- package/nitrogen/generated/shared/c++/Entity.hpp +13 -5
- package/nitrogen/generated/shared/c++/EntityUpdate.hpp +9 -1
- package/nitrogen/generated/shared/c++/HybridGameAssetsSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridGameAssetsSpec.hpp +64 -0
- package/nitrogen/generated/shared/c++/HybridGameMethodsSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridGameMethodsSpec.hpp +4 -4
- package/nitrogen/generated/shared/c++/Screen.hpp +100 -0
- package/nitrogen/generated/shared/c++/System.hpp +20 -9
- package/package.json +1 -1
- package/shared/ColorUtils.hpp +29 -17
- package/shared/GameLoop.cpp +77 -14
- package/shared/GameLoop.hpp +5 -3
- package/shared/GameMethods.cpp +24 -11
- package/shared/GameMethods.hpp +1 -1
- package/shared/Rect.hpp +8 -5
- package/src/GameAssets.nitro.ts +14 -0
- package/src/GameMethods.nitro.ts +7 -3
- package/src/GameMethods.ts +1 -34
- package/src/Shared.ts +83 -0
- package/src/index.tsx +2 -0
- package/src/nativeTypes.ts +83 -0
- package/src/types.ts +19 -51
package/src/Shared.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Image } from 'react-native';
|
|
2
|
+
import { gameAssets } from './GameAssets.nitro';
|
|
3
|
+
import { gameMethods } from './GameMethods.nitro';
|
|
4
|
+
import type {
|
|
5
|
+
Screen as NativeScreen,
|
|
6
|
+
Entity as NativeEntity,
|
|
7
|
+
EntityUpdate as NativeEntityUpdate,
|
|
8
|
+
} from './nativeTypes';
|
|
9
|
+
import type { Asset, Config, Entity, EntityUpdate } from './types';
|
|
10
|
+
|
|
11
|
+
const assetCache = new WeakMap<object, number>();
|
|
12
|
+
let nextId = -1;
|
|
13
|
+
|
|
14
|
+
const registerAssetIfNotExist = (asset?: Asset) => {
|
|
15
|
+
switch (typeof asset) {
|
|
16
|
+
case 'object':
|
|
17
|
+
if (assetCache.has(asset)) {
|
|
18
|
+
return assetCache.get(asset);
|
|
19
|
+
}
|
|
20
|
+
const assetId = nextId--;
|
|
21
|
+
assetCache.set(asset, assetId);
|
|
22
|
+
gameAssets.registerLottie(assetId, JSON.stringify(asset));
|
|
23
|
+
return assetId;
|
|
24
|
+
|
|
25
|
+
case 'number':
|
|
26
|
+
if (gameAssets.isAssetRegistered(asset)) {
|
|
27
|
+
return asset;
|
|
28
|
+
}
|
|
29
|
+
const assetUri = Image.resolveAssetSource(asset)?.uri;
|
|
30
|
+
if (!assetUri) {
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
gameAssets.registerSvg(asset, assetUri);
|
|
34
|
+
return asset;
|
|
35
|
+
}
|
|
36
|
+
return;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** Sets up the game engine with the given configuration. Call this before anything else. */
|
|
40
|
+
export const configure = ({
|
|
41
|
+
tickRate,
|
|
42
|
+
screen,
|
|
43
|
+
entities = [],
|
|
44
|
+
systems = [],
|
|
45
|
+
paused = true,
|
|
46
|
+
}: Config) => {
|
|
47
|
+
screen.asset = registerAssetIfNotExist(screen.asset);
|
|
48
|
+
for (const entity of entities) {
|
|
49
|
+
entity.asset = registerAssetIfNotExist(entity.asset);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
gameMethods.setTickRate(tickRate);
|
|
53
|
+
gameMethods.setScreen(screen as NativeScreen);
|
|
54
|
+
gameMethods.setEntities(entities as NativeEntity[]);
|
|
55
|
+
gameMethods.setSystems(systems);
|
|
56
|
+
if (paused) {
|
|
57
|
+
gameMethods.pause();
|
|
58
|
+
} else {
|
|
59
|
+
gameMethods.resume();
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** Spawns one or more entities into the world. Skips duplicates by id. */
|
|
64
|
+
export const spawn = (entities: Entity | Entity[]) => {
|
|
65
|
+
const entityArray = Array.isArray(entities) ? entities : [entities];
|
|
66
|
+
|
|
67
|
+
for (const entity of entityArray) {
|
|
68
|
+
entity.asset = registerAssetIfNotExist(entity.asset);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
gameMethods.spawn(entityArray as NativeEntity[]);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/** Updates one or more entities. Only the provided fields are changed. */
|
|
75
|
+
export const update = (updates: EntityUpdate | EntityUpdate[]) => {
|
|
76
|
+
const entityUpdateArray = Array.isArray(updates) ? updates : [updates];
|
|
77
|
+
|
|
78
|
+
for (const entityUpdate of entityUpdateArray) {
|
|
79
|
+
entityUpdate.asset = registerAssetIfNotExist(entityUpdate.asset);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
gameMethods.update(entityUpdateArray as NativeEntityUpdate[]);
|
|
83
|
+
};
|
package/src/index.tsx
CHANGED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
export type Screen = {
|
|
2
|
+
/** Width of the screen in game units. */
|
|
3
|
+
width: number;
|
|
4
|
+
/** Height of the screen in game units. */
|
|
5
|
+
height: number;
|
|
6
|
+
/** Background color of the screen as a hex string e.g. `'#1a1a1a'`. */
|
|
7
|
+
color?: string;
|
|
8
|
+
/** Asset to render, use `require` with the file path to get the id. */
|
|
9
|
+
asset?: number;
|
|
10
|
+
/** Animation progress between 0 and 1, automatically advanced each tick for Lottie assets. */
|
|
11
|
+
progress?: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type Entity = {
|
|
15
|
+
/** Unique identifier. Use `_` as a separator for group queries e.g. `'enemy_1'`. */
|
|
16
|
+
id: string;
|
|
17
|
+
/** X position in game units. */
|
|
18
|
+
px: number;
|
|
19
|
+
/** Y position in game units. */
|
|
20
|
+
py: number;
|
|
21
|
+
/** Width in game units. */
|
|
22
|
+
width: number;
|
|
23
|
+
/** Height in game units. */
|
|
24
|
+
height: number;
|
|
25
|
+
/** Fill color as a hex string e.g. `'#00ff00'`. */
|
|
26
|
+
color?: string;
|
|
27
|
+
/** Asset to render, use `require` with the file path to get the id. */
|
|
28
|
+
asset?: number;
|
|
29
|
+
/** Animation progress between 0 and 1, automatically advanced each tick for Lottie assets. */
|
|
30
|
+
progress?: number;
|
|
31
|
+
/** Velocity on the X axis in game units per second. */
|
|
32
|
+
vx?: number;
|
|
33
|
+
/** Velocity on the Y axis in game units per second. */
|
|
34
|
+
vy?: number;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type CollisionPair = {
|
|
38
|
+
/** Entity id or prefix of the first entity to watch. */
|
|
39
|
+
a: string;
|
|
40
|
+
/** Entity id or prefix of the second entity to watch. */
|
|
41
|
+
b: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type Collision = {
|
|
45
|
+
/** Id of the first entity involved in the collision. */
|
|
46
|
+
a: string;
|
|
47
|
+
/** Id of the second entity involved in the collision. */
|
|
48
|
+
b: string;
|
|
49
|
+
/** Penetration depth of the collision. */
|
|
50
|
+
depth: number;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type System = {
|
|
54
|
+
/** Entity ids or prefixes this system subscribes to. */
|
|
55
|
+
entities?: string[];
|
|
56
|
+
/** Collision pairs to watch for every tick. */
|
|
57
|
+
collisions?: CollisionPair[];
|
|
58
|
+
/** Called every tick with the resolved entities and collisions. */
|
|
59
|
+
onTick: (entities: Entity[], collisions: Collision[]) => void;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export type EntityUpdate = {
|
|
63
|
+
/** Unique identifier or prefix of the entities to update. */
|
|
64
|
+
id: string;
|
|
65
|
+
/** X position in game units. */
|
|
66
|
+
px?: number;
|
|
67
|
+
/** Y position in game units. */
|
|
68
|
+
py?: number;
|
|
69
|
+
/** Width in game units. */
|
|
70
|
+
width?: number;
|
|
71
|
+
/** Height in game units. */
|
|
72
|
+
height?: number;
|
|
73
|
+
/** Fill color as a hex string e.g. `'#00ff00'`. */
|
|
74
|
+
color?: string;
|
|
75
|
+
/** Asset to render, use `require` with the file path to get the id. */
|
|
76
|
+
asset?: number;
|
|
77
|
+
/** Animation progress between 0 and 1, automatically advanced each tick for Lottie assets. */
|
|
78
|
+
progress?: number;
|
|
79
|
+
/** Velocity on the X axis in game units per second. */
|
|
80
|
+
vx?: number;
|
|
81
|
+
/** Velocity on the Y axis in game units per second. */
|
|
82
|
+
vy?: number;
|
|
83
|
+
};
|
package/src/types.ts
CHANGED
|
@@ -1,62 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
color: string;
|
|
8
|
-
};
|
|
1
|
+
import type {
|
|
2
|
+
Screen as NativeScreen,
|
|
3
|
+
Entity as NativeEntity,
|
|
4
|
+
EntityUpdate as NativeEntityUpdate,
|
|
5
|
+
System,
|
|
6
|
+
} from './nativeTypes';
|
|
9
7
|
|
|
10
|
-
export type
|
|
11
|
-
/** Unique identifier. Use `_` as a separator for group queries e.g. `'enemy_1'`. */
|
|
12
|
-
id: string;
|
|
13
|
-
/** X position in game units. */
|
|
14
|
-
px: number;
|
|
15
|
-
/** Y position in game units. */
|
|
16
|
-
py: number;
|
|
17
|
-
/** Width in game units. */
|
|
18
|
-
width: number;
|
|
19
|
-
/** Height in game units. */
|
|
20
|
-
height: number;
|
|
21
|
-
/** Fill color as a hex string e.g. `'#00ff00'`. */
|
|
22
|
-
color: string;
|
|
23
|
-
/** Velocity on the X axis in game units per second. */
|
|
24
|
-
vx?: number;
|
|
25
|
-
/** Velocity on the Y axis in game units per second. */
|
|
26
|
-
vy?: number;
|
|
27
|
-
};
|
|
8
|
+
export type Asset = number | object;
|
|
28
9
|
|
|
29
|
-
export type
|
|
30
|
-
/**
|
|
31
|
-
|
|
32
|
-
/** Called every tick with the resolved entities. */
|
|
33
|
-
onTick: (entities: Entity[]) => void;
|
|
10
|
+
export type Screen = Omit<NativeScreen, 'asset'> & {
|
|
11
|
+
/** Asset to render, use `require` with the file path to get the id. */
|
|
12
|
+
asset?: Asset;
|
|
34
13
|
};
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
py?: number;
|
|
43
|
-
/** Width in game units. */
|
|
44
|
-
width?: number;
|
|
45
|
-
/** Height in game units. */
|
|
46
|
-
height?: number;
|
|
47
|
-
/** Fill color as a hex string e.g. `'#00ff00'`. */
|
|
48
|
-
color?: string;
|
|
49
|
-
/** Velocity on the X axis in game units per second. */
|
|
50
|
-
vx?: number;
|
|
51
|
-
/** Velocity on the Y axis in game units per second. */
|
|
52
|
-
vy?: number;
|
|
14
|
+
export type Entity = Omit<NativeEntity, 'asset'> & {
|
|
15
|
+
/** Asset to render, use `require` with the file path to get the id. */
|
|
16
|
+
asset?: Asset;
|
|
17
|
+
};
|
|
18
|
+
export type EntityUpdate = Omit<NativeEntityUpdate, 'asset'> & {
|
|
19
|
+
/** Asset to render, use `require` with the file path to get the id. */
|
|
20
|
+
asset?: Asset;
|
|
53
21
|
};
|
|
54
22
|
|
|
55
23
|
export type Config = {
|
|
56
24
|
/** Number of game logic updates per second. */
|
|
57
25
|
tickRate: number;
|
|
58
|
-
/**
|
|
59
|
-
|
|
26
|
+
/** Screen dimensions and background color. */
|
|
27
|
+
screen: Screen;
|
|
60
28
|
/** Initial entities to spawn. */
|
|
61
29
|
entities?: Entity[];
|
|
62
30
|
/**
|