rngine 0.3.0 → 0.3.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/README.md +10 -10
- package/lib/module/GameMethods.js +2 -2
- package/lib/module/GameMethods.js.map +1 -1
- package/lib/module/index.js +3 -2
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/GameMethods.d.ts +1 -1
- package/lib/typescript/src/GameMethods.d.ts.map +1 -1
- package/lib/typescript/src/GameMethods.nitro.d.ts +2 -2
- package/lib/typescript/src/GameMethods.nitro.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +3 -2
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/types.d.ts +6 -6
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/nitrogen/generated/shared/c++/HybridGameMethodsSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridGameMethodsSpec.hpp +4 -4
- package/nitrogen/generated/shared/c++/{World.hpp → Screen.hpp} +11 -11
- package/package.json +1 -1
- package/shared/ColorUtils.hpp +25 -16
- package/shared/GameLoop.cpp +7 -7
- package/shared/GameLoop.hpp +3 -3
- package/shared/GameMethods.cpp +4 -4
- package/shared/GameMethods.hpp +1 -1
- package/src/GameMethods.nitro.ts +2 -2
- package/src/GameMethods.ts +2 -2
- package/src/index.tsx +3 -9
- package/src/types.ts +6 -6
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ import { GameEngine, configure, update } from 'rngine';
|
|
|
17
17
|
|
|
18
18
|
configure({
|
|
19
19
|
tickRate: 60,
|
|
20
|
-
|
|
20
|
+
screen: { width: 800, height: 800, color: '#1a1a1a' },
|
|
21
21
|
entities: [
|
|
22
22
|
{
|
|
23
23
|
id: 'player',
|
|
@@ -52,7 +52,7 @@ configure({
|
|
|
52
52
|
ids: ['enemy'],
|
|
53
53
|
onTick: (enemies) => {
|
|
54
54
|
enemies.forEach((enemy) => {
|
|
55
|
-
// reverse direction when reaching
|
|
55
|
+
// reverse direction when reaching screen edges
|
|
56
56
|
if (enemy.px <= 0 || enemy.px + enemy.width >= 800) {
|
|
57
57
|
update({ id: enemy.id, vx: -enemy.vx });
|
|
58
58
|
}
|
|
@@ -74,7 +74,7 @@ export default function App() {
|
|
|
74
74
|
|
|
75
75
|
**Systems** define your game logic. Each system declares which entities it cares about via `ids` and runs every tick receiving those entities. See [`System`](./src/types.ts).
|
|
76
76
|
|
|
77
|
-
**
|
|
77
|
+
**Screen** defines the viewport dimensions and background color. Entities outside the screen bounds are automatically clipped. See [`Screen`](./src/types.ts).
|
|
78
78
|
|
|
79
79
|
## Entity Querying
|
|
80
80
|
|
|
@@ -99,13 +99,13 @@ This makes it easy to build entity groups naturally through naming. No extra con
|
|
|
99
99
|
|
|
100
100
|
Sets up the game engine. Call this before anything else.
|
|
101
101
|
|
|
102
|
-
| param | required | type | default | description
|
|
103
|
-
| ---------- | -------- | ---------- | ------- |
|
|
104
|
-
| `tickRate` | ✓ | `number` | - | Game logic updates per second
|
|
105
|
-
| `
|
|
106
|
-
| `entities` | | `Entity[]` | `[]` | Initial entities to spawn
|
|
107
|
-
| `systems` | | `System[]` | `[]` | Systems to run each tick
|
|
108
|
-
| `paused` | | `boolean` | `true` | Whether to start paused
|
|
102
|
+
| param | required | type | default | description |
|
|
103
|
+
| ---------- | -------- | ---------- | ------- | -------------------------------------- |
|
|
104
|
+
| `tickRate` | ✓ | `number` | - | Game logic updates per second |
|
|
105
|
+
| `screen` | ✓ | `Screen` | - | Screen dimensions and background color |
|
|
106
|
+
| `entities` | | `Entity[]` | `[]` | Initial entities to spawn |
|
|
107
|
+
| `systems` | | `System[]` | `[]` | Systems to run each tick |
|
|
108
|
+
| `paused` | | `boolean` | `true` | Whether to start paused |
|
|
109
109
|
|
|
110
110
|
### `spawn(entity | entity[])`
|
|
111
111
|
|
|
@@ -6,13 +6,13 @@ const gameMethods = NitroModules.createHybridObject('GameMethods');
|
|
|
6
6
|
/** Sets up the game engine with the given configuration. Call this before anything else. */
|
|
7
7
|
export function configure({
|
|
8
8
|
tickRate,
|
|
9
|
-
|
|
9
|
+
screen,
|
|
10
10
|
entities = [],
|
|
11
11
|
systems = [],
|
|
12
12
|
paused = true
|
|
13
13
|
}) {
|
|
14
14
|
gameMethods.setTickRate(tickRate);
|
|
15
|
-
gameMethods.
|
|
15
|
+
gameMethods.setScreen(screen);
|
|
16
16
|
gameMethods.setEntities(entities);
|
|
17
17
|
gameMethods.setSystems(systems);
|
|
18
18
|
if (paused) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["NitroModules","gameMethods","createHybridObject","configure","tickRate","
|
|
1
|
+
{"version":3,"names":["NitroModules","gameMethods","createHybridObject","configure","tickRate","screen","entities","systems","paused","setTickRate","setScreen","setEntities","setSystems","pause","resume","spawn","Array","isArray","despawn","id","update","updates"],"sourceRoot":"../../src","sources":["GameMethods.ts"],"mappings":";;AAAA,SAASA,YAAY,QAAQ,4BAA4B;AAIzD,MAAMC,WAAW,GAAGD,YAAY,CAACE,kBAAkB,CAAc,aAAa,CAAC;;AAE/E;AACA,OAAO,SAASC,SAASA,CAAC;EACxBC,QAAQ;EACRC,MAAM;EACNC,QAAQ,GAAG,EAAE;EACbC,OAAO,GAAG,EAAE;EACZC,MAAM,GAAG;AACH,CAAC,EAAE;EACTP,WAAW,CAACQ,WAAW,CAACL,QAAQ,CAAC;EACjCH,WAAW,CAACS,SAAS,CAACL,MAAM,CAAC;EAC7BJ,WAAW,CAACU,WAAW,CAACL,QAAQ,CAAC;EACjCL,WAAW,CAACW,UAAU,CAACL,OAAO,CAAC;EAC/B,IAAIC,MAAM,EAAE;IACVP,WAAW,CAACY,KAAK,CAAC,CAAC;EACrB,CAAC,MAAM;IACLZ,WAAW,CAACa,MAAM,CAAC,CAAC;EACtB;AACF;;AAEA;AACA,OAAO,MAAMD,KAAK,GAAGA,CAAA,KAAMZ,WAAW,CAACY,KAAK,CAAC,CAAC;;AAE9C;AACA,OAAO,MAAMC,MAAM,GAAGA,CAAA,KAAMb,WAAW,CAACa,MAAM,CAAC,CAAC;;AAEhD;AACA,OAAO,MAAMC,KAAK,GAAIT,QAA2B,IAAK;EACpDL,WAAW,CAACc,KAAK,CAACC,KAAK,CAACC,OAAO,CAACX,QAAQ,CAAC,GAAGA,QAAQ,GAAG,CAACA,QAAQ,CAAC,CAAC;AACpE,CAAC;;AAED;AACA,OAAO,MAAMY,OAAO,GAAIC,EAAU,IAAKlB,WAAW,CAACiB,OAAO,CAACC,EAAE,CAAC;;AAE9D;AACA,OAAO,MAAMC,MAAM,GAAIC,OAAsC,IAAK;EAChEpB,WAAW,CAACmB,MAAM,CAACJ,KAAK,CAACC,OAAO,CAACI,OAAO,CAAC,GAAGA,OAAO,GAAG,CAACA,OAAO,CAAC,CAAC;AAClE,CAAC","ignoreList":[]}
|
package/lib/module/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
import GameEngine from "./GameEngine.js";
|
|
4
|
-
|
|
5
|
-
export
|
|
4
|
+
export * from "./GameMethods.js";
|
|
5
|
+
export * from "./types.js";
|
|
6
|
+
export { GameEngine };
|
|
6
7
|
//# sourceMappingURL=index.js.map
|
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["GameEngine"
|
|
1
|
+
{"version":3,"names":["GameEngine"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,OAAOA,UAAU,MAAM,iBAAc;AAErC,cAAc,kBAAe;AAC7B,cAAc,YAAS;AACvB,SAASA,UAAU","ignoreList":[]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Config, Entity, EntityUpdate } from './types';
|
|
2
2
|
/** Sets up the game engine with the given configuration. Call this before anything else. */
|
|
3
|
-
export declare function configure({ tickRate,
|
|
3
|
+
export declare function configure({ tickRate, screen, entities, systems, paused, }: Config): void;
|
|
4
4
|
/** Pauses the game loop. Systems will stop running. */
|
|
5
5
|
export declare const pause: () => void;
|
|
6
6
|
/** Resumes the game loop. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GameMethods.d.ts","sourceRoot":"","sources":["../../../src/GameMethods.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAI5D,4FAA4F;AAC5F,wBAAgB,SAAS,CAAC,EACxB,QAAQ,EACR,
|
|
1
|
+
{"version":3,"file":"GameMethods.d.ts","sourceRoot":"","sources":["../../../src/GameMethods.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAI5D,4FAA4F;AAC5F,wBAAgB,SAAS,CAAC,EACxB,QAAQ,EACR,MAAM,EACN,QAAa,EACb,OAAY,EACZ,MAAa,GACd,EAAE,MAAM,QAUR;AAED,uDAAuD;AACvD,eAAO,MAAM,KAAK,YAA4B,CAAC;AAE/C,6BAA6B;AAC7B,eAAO,MAAM,MAAM,YAA6B,CAAC;AAEjD,0EAA0E;AAC1E,eAAO,MAAM,KAAK,GAAI,UAAU,MAAM,GAAG,MAAM,EAAE,SAEhD,CAAC;AAEF,qFAAqF;AACrF,eAAO,MAAM,OAAO,GAAI,IAAI,MAAM,SAA4B,CAAC;AAE/D,0EAA0E;AAC1E,eAAO,MAAM,MAAM,GAAI,SAAS,YAAY,GAAG,YAAY,EAAE,SAE5D,CAAC"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { HybridObject } from 'react-native-nitro-modules';
|
|
2
|
-
import type { Entity, EntityUpdate, System,
|
|
2
|
+
import type { Entity, EntityUpdate, System, Screen } from './types';
|
|
3
3
|
export interface GameMethods extends HybridObject<{
|
|
4
4
|
ios: 'c++';
|
|
5
5
|
android: 'c++';
|
|
6
6
|
}> {
|
|
7
7
|
setTickRate(tickRate: number): void;
|
|
8
|
-
|
|
8
|
+
setScreen(screen: Screen): void;
|
|
9
9
|
setEntities(entities: Entity[]): void;
|
|
10
10
|
setSystems(systems: System[]): void;
|
|
11
11
|
pause(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GameMethods.nitro.d.ts","sourceRoot":"","sources":["../../../src/GameMethods.nitro.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"GameMethods.nitro.d.ts","sourceRoot":"","sources":["../../../src/GameMethods.nitro.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEpE,MAAM,WAAW,WAAY,SAAQ,YAAY,CAAC;IAChD,GAAG,EAAE,KAAK,CAAC;IACX,OAAO,EAAE,KAAK,CAAC;CAChB,CAAC;IACA,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IACtC,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IACpC,KAAK,IAAI,IAAI,CAAC;IACd,MAAM,IAAI,IAAI,CAAC;IACf,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAChC,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,MAAM,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC;CACvC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import GameEngine from './GameEngine';
|
|
2
|
-
|
|
3
|
-
export
|
|
2
|
+
export * from './GameMethods';
|
|
3
|
+
export * from './types';
|
|
4
|
+
export { GameEngine };
|
|
4
5
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,cAAc,CAAC;
|
|
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,SAAS,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export type
|
|
2
|
-
/** Width of the
|
|
1
|
+
export type Screen = {
|
|
2
|
+
/** Width of the screen in game units. */
|
|
3
3
|
width: number;
|
|
4
|
-
/** Height of the
|
|
4
|
+
/** Height of the screen in game units. */
|
|
5
5
|
height: number;
|
|
6
|
-
/** Background color of the
|
|
6
|
+
/** Background color of the screen as a hex string e.g. `'#1a1a1a'`. */
|
|
7
7
|
color: string;
|
|
8
8
|
};
|
|
9
9
|
export type Entity = {
|
|
@@ -51,8 +51,8 @@ export type EntityUpdate = {
|
|
|
51
51
|
export type Config = {
|
|
52
52
|
/** Number of game logic updates per second. */
|
|
53
53
|
tickRate: number;
|
|
54
|
-
/**
|
|
55
|
-
|
|
54
|
+
/** Screen dimensions and background color. */
|
|
55
|
+
screen: Screen;
|
|
56
56
|
/** Initial entities to spawn. */
|
|
57
57
|
entities?: Entity[];
|
|
58
58
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.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,EAAE,MAAM,CAAC;CACf,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,MAAM,CAAC;IACd,4BAA4B;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,mDAAmD;IACnD,KAAK,EAAE,MAAM,CAAC;IACd,uDAAuD;IACvD,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,uDAAuD;IACvD,EAAE,CAAC,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,wDAAwD;IACxD,GAAG,EAAE,MAAM,EAAE,CAAC;IACd,oDAAoD;IACpD,MAAM,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;CACtC,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,6DAA6D;IAC7D,EAAE,EAAE,MAAM,CAAC;IACX,gCAAgC;IAChC,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,gCAAgC;IAChC,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,uDAAuD;IACvD,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,uDAAuD;IACvD,EAAE,CAAC,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,+CAA+C;IAC/C,QAAQ,EAAE,MAAM,CAAC;IACjB,8CAA8C;IAC9C,MAAM,EAAE,MAAM,CAAC;IACf,iCAAiC;IACjC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,iDAAiD;IACjD,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB,CAAC"}
|
|
@@ -15,7 +15,7 @@ namespace margelo::nitro::rngine {
|
|
|
15
15
|
// load custom methods/properties
|
|
16
16
|
registerHybrids(this, [](Prototype& prototype) {
|
|
17
17
|
prototype.registerHybridMethod("setTickRate", &HybridGameMethodsSpec::setTickRate);
|
|
18
|
-
prototype.registerHybridMethod("
|
|
18
|
+
prototype.registerHybridMethod("setScreen", &HybridGameMethodsSpec::setScreen);
|
|
19
19
|
prototype.registerHybridMethod("setEntities", &HybridGameMethodsSpec::setEntities);
|
|
20
20
|
prototype.registerHybridMethod("setSystems", &HybridGameMethodsSpec::setSystems);
|
|
21
21
|
prototype.registerHybridMethod("pause", &HybridGameMethodsSpec::pause);
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
14
|
#endif
|
|
15
15
|
|
|
16
|
-
// Forward declaration of `
|
|
17
|
-
namespace margelo::nitro::rngine { struct
|
|
16
|
+
// Forward declaration of `Screen` to properly resolve imports.
|
|
17
|
+
namespace margelo::nitro::rngine { struct Screen; }
|
|
18
18
|
// Forward declaration of `Entity` to properly resolve imports.
|
|
19
19
|
namespace margelo::nitro::rngine { struct Entity; }
|
|
20
20
|
// Forward declaration of `System` to properly resolve imports.
|
|
@@ -22,7 +22,7 @@ namespace margelo::nitro::rngine { struct System; }
|
|
|
22
22
|
// Forward declaration of `EntityUpdate` to properly resolve imports.
|
|
23
23
|
namespace margelo::nitro::rngine { struct EntityUpdate; }
|
|
24
24
|
|
|
25
|
-
#include "
|
|
25
|
+
#include "Screen.hpp"
|
|
26
26
|
#include "Entity.hpp"
|
|
27
27
|
#include <vector>
|
|
28
28
|
#include "System.hpp"
|
|
@@ -61,7 +61,7 @@ namespace margelo::nitro::rngine {
|
|
|
61
61
|
public:
|
|
62
62
|
// Methods
|
|
63
63
|
virtual void setTickRate(double tickRate) = 0;
|
|
64
|
-
virtual void
|
|
64
|
+
virtual void setScreen(const Screen& screen) = 0;
|
|
65
65
|
virtual void setEntities(const std::vector<Entity>& entities) = 0;
|
|
66
66
|
virtual void setSystems(const std::vector<System>& systems) = 0;
|
|
67
67
|
virtual void pause() = 0;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
///
|
|
2
|
-
///
|
|
2
|
+
/// Screen.hpp
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
5
|
/// Copyright © Marc Rousavy @ Margelo
|
|
@@ -35,38 +35,38 @@
|
|
|
35
35
|
namespace margelo::nitro::rngine {
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* A struct which can be represented as a JavaScript object (
|
|
38
|
+
* A struct which can be represented as a JavaScript object (Screen).
|
|
39
39
|
*/
|
|
40
|
-
struct
|
|
40
|
+
struct Screen final {
|
|
41
41
|
public:
|
|
42
42
|
double width SWIFT_PRIVATE;
|
|
43
43
|
double height SWIFT_PRIVATE;
|
|
44
44
|
std::string color SWIFT_PRIVATE;
|
|
45
45
|
|
|
46
46
|
public:
|
|
47
|
-
|
|
48
|
-
explicit
|
|
47
|
+
Screen() = default;
|
|
48
|
+
explicit Screen(double width, double height, std::string color): width(width), height(height), color(color) {}
|
|
49
49
|
|
|
50
50
|
public:
|
|
51
|
-
friend bool operator==(const
|
|
51
|
+
friend bool operator==(const Screen& lhs, const Screen& rhs) = default;
|
|
52
52
|
};
|
|
53
53
|
|
|
54
54
|
} // namespace margelo::nitro::rngine
|
|
55
55
|
|
|
56
56
|
namespace margelo::nitro {
|
|
57
57
|
|
|
58
|
-
// C++
|
|
58
|
+
// C++ Screen <> JS Screen (object)
|
|
59
59
|
template <>
|
|
60
|
-
struct JSIConverter<margelo::nitro::rngine::
|
|
61
|
-
static inline margelo::nitro::rngine::
|
|
60
|
+
struct JSIConverter<margelo::nitro::rngine::Screen> final {
|
|
61
|
+
static inline margelo::nitro::rngine::Screen fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
62
62
|
jsi::Object obj = arg.asObject(runtime);
|
|
63
|
-
return margelo::nitro::rngine::
|
|
63
|
+
return margelo::nitro::rngine::Screen(
|
|
64
64
|
JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "width"))),
|
|
65
65
|
JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "height"))),
|
|
66
66
|
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "color")))
|
|
67
67
|
);
|
|
68
68
|
}
|
|
69
|
-
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::rngine::
|
|
69
|
+
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::rngine::Screen& arg) {
|
|
70
70
|
jsi::Object obj(runtime);
|
|
71
71
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "width"), JSIConverter<double>::toJSI(runtime, arg.width));
|
|
72
72
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "height"), JSIConverter<double>::toJSI(runtime, arg.height));
|
package/package.json
CHANGED
package/shared/ColorUtils.hpp
CHANGED
|
@@ -1,25 +1,34 @@
|
|
|
1
1
|
#pragma once
|
|
2
|
-
#include <stdexcept>
|
|
3
2
|
#include <string>
|
|
4
3
|
|
|
5
4
|
namespace margelo::nitro::rngine {
|
|
6
5
|
|
|
7
6
|
inline uint32_t parseHexColor(const std::string &color) {
|
|
8
|
-
if (color.empty()
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
7
|
+
if (!color.empty() && color[0] == '#') {
|
|
8
|
+
std::string hex = color.substr(1);
|
|
9
|
+
|
|
10
|
+
if (hex.size() == 3 || hex.size() == 4) {
|
|
11
|
+
std::string expanded;
|
|
12
|
+
expanded.reserve(hex.size() * 2);
|
|
13
|
+
for (char c : hex) {
|
|
14
|
+
expanded += c;
|
|
15
|
+
expanded += c;
|
|
16
|
+
}
|
|
17
|
+
hex = std::move(expanded);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (hex.size() == 6) {
|
|
21
|
+
return 0xFF000000 | std::stoul(hex, nullptr, 16);
|
|
22
|
+
} else if (hex.size() == 8) {
|
|
23
|
+
uint32_t rgba = std::stoul(hex, nullptr, 16);
|
|
24
|
+
uint32_t r = (rgba >> 24) & 0xFF;
|
|
25
|
+
uint32_t g = (rgba >> 16) & 0xFF;
|
|
26
|
+
uint32_t b = (rgba >> 8) & 0xFF;
|
|
27
|
+
uint32_t a = rgba & 0xFF;
|
|
28
|
+
return (a << 24) | (r << 16) | (g << 8) | b;
|
|
29
|
+
}
|
|
21
30
|
}
|
|
22
|
-
throw std::invalid_argument("Invalid hex color: " + color);
|
|
23
|
-
}
|
|
24
31
|
|
|
32
|
+
return 0x00000000;
|
|
33
|
+
}
|
|
25
34
|
} // namespace margelo::nitro::rngine
|
package/shared/GameLoop.cpp
CHANGED
|
@@ -56,18 +56,18 @@ std::vector<Rect> GameLoop::getRectsSnapshot() {
|
|
|
56
56
|
rects.reserve(_entities.size() + 1);
|
|
57
57
|
|
|
58
58
|
rects.push_back(
|
|
59
|
-
{0,
|
|
59
|
+
{0, _screen.width, 0, _screen.height, parseHexColor(_screen.color)});
|
|
60
60
|
|
|
61
61
|
for (const auto &[id, entity] : _entities) {
|
|
62
|
-
if (entity.px + entity.width < 0 || entity.px >
|
|
63
|
-
entity.py + entity.height < 0 || entity.py >
|
|
62
|
+
if (entity.px + entity.width < 0 || entity.px > _screen.width ||
|
|
63
|
+
entity.py + entity.height < 0 || entity.py > _screen.height) {
|
|
64
64
|
continue;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
rects.push_back({std::clamp(entity.px, 0.0,
|
|
68
|
-
std::clamp(entity.px + entity.width, 0.0,
|
|
69
|
-
std::clamp(entity.py, 0.0,
|
|
70
|
-
std::clamp(entity.py + entity.height, 0.0,
|
|
67
|
+
rects.push_back({std::clamp(entity.px, 0.0, _screen.width),
|
|
68
|
+
std::clamp(entity.px + entity.width, 0.0, _screen.width),
|
|
69
|
+
std::clamp(entity.py, 0.0, _screen.height),
|
|
70
|
+
std::clamp(entity.py + entity.height, 0.0, _screen.height),
|
|
71
71
|
parseHexColor(entity.color)});
|
|
72
72
|
}
|
|
73
73
|
return rects;
|
package/shared/GameLoop.hpp
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
#include "Entity.hpp"
|
|
4
4
|
#include "GameStats.hpp"
|
|
5
5
|
#include "Rect.hpp"
|
|
6
|
+
#include "Screen.hpp"
|
|
6
7
|
#include "System.hpp"
|
|
7
|
-
#include "World.hpp"
|
|
8
8
|
#include <atomic>
|
|
9
9
|
#include <memory>
|
|
10
10
|
#include <thread>
|
|
@@ -25,7 +25,7 @@ public:
|
|
|
25
25
|
std::map<string, Entity> &getEntitiesInternal() { return _entities; };
|
|
26
26
|
std::vector<System> &getSystemsInternal() { return _systems; };
|
|
27
27
|
std::atomic<bool> &getIsPausedInternal() { return _isPaused; };
|
|
28
|
-
|
|
28
|
+
Screen &getScreenInternal() { return _screen; };
|
|
29
29
|
|
|
30
30
|
std::vector<Entity *> resolveEntitiesInternal(const std::string &prefix);
|
|
31
31
|
std::vector<Rect> getRectsSnapshot();
|
|
@@ -40,7 +40,7 @@ private:
|
|
|
40
40
|
std::atomic<bool> _isRunning{true};
|
|
41
41
|
std::atomic<bool> _isPaused{true};
|
|
42
42
|
double _tickRate{60.0};
|
|
43
|
-
|
|
43
|
+
Screen _screen;
|
|
44
44
|
GameStats _gameStats;
|
|
45
45
|
std::unique_ptr<std::thread> _gameThread;
|
|
46
46
|
|
package/shared/GameMethods.cpp
CHANGED
|
@@ -12,13 +12,13 @@ void GameMethods::setTickRate(double tickRate) {
|
|
|
12
12
|
"setTickRate: tickRate=%.1f", tickRate);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
void GameMethods::
|
|
15
|
+
void GameMethods::setScreen(const Screen &screen) {
|
|
16
16
|
auto &instance = GameLoop::getInstance();
|
|
17
17
|
std::lock_guard<std::mutex> lock(instance.getMutexInternal());
|
|
18
|
-
instance.
|
|
18
|
+
instance.getScreenInternal() = screen;
|
|
19
19
|
__android_log_print(ANDROID_LOG_INFO, "GameMethods",
|
|
20
|
-
"setWorld: width=%.0f height=%.0f",
|
|
21
|
-
|
|
20
|
+
"setWorld: width=%.0f height=%.0f", screen.width,
|
|
21
|
+
screen.height);
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
void GameMethods::setEntities(const std::vector<Entity> &entities) {
|
package/shared/GameMethods.hpp
CHANGED
|
@@ -7,7 +7,7 @@ class GameMethods : public HybridGameMethodsSpec {
|
|
|
7
7
|
public:
|
|
8
8
|
GameMethods() : HybridObject(TAG) {}
|
|
9
9
|
void setTickRate(double tickRate) override;
|
|
10
|
-
void
|
|
10
|
+
void setScreen(const Screen &screen) override;
|
|
11
11
|
void setEntities(const std::vector<Entity> &entities) override;
|
|
12
12
|
void setSystems(const std::vector<System> &systems) override;
|
|
13
13
|
void pause() override;
|
package/src/GameMethods.nitro.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { HybridObject } from 'react-native-nitro-modules';
|
|
2
|
-
import type { Entity, EntityUpdate, System,
|
|
2
|
+
import type { Entity, EntityUpdate, System, Screen } from './types';
|
|
3
3
|
|
|
4
4
|
export interface GameMethods extends HybridObject<{
|
|
5
5
|
ios: 'c++';
|
|
6
6
|
android: 'c++';
|
|
7
7
|
}> {
|
|
8
8
|
setTickRate(tickRate: number): void;
|
|
9
|
-
|
|
9
|
+
setScreen(screen: Screen): void;
|
|
10
10
|
setEntities(entities: Entity[]): void;
|
|
11
11
|
setSystems(systems: System[]): void;
|
|
12
12
|
pause(): void;
|
package/src/GameMethods.ts
CHANGED
|
@@ -7,13 +7,13 @@ const gameMethods = NitroModules.createHybridObject<GameMethods>('GameMethods');
|
|
|
7
7
|
/** Sets up the game engine with the given configuration. Call this before anything else. */
|
|
8
8
|
export function configure({
|
|
9
9
|
tickRate,
|
|
10
|
-
|
|
10
|
+
screen,
|
|
11
11
|
entities = [],
|
|
12
12
|
systems = [],
|
|
13
13
|
paused = true,
|
|
14
14
|
}: Config) {
|
|
15
15
|
gameMethods.setTickRate(tickRate);
|
|
16
|
-
gameMethods.
|
|
16
|
+
gameMethods.setScreen(screen);
|
|
17
17
|
gameMethods.setEntities(entities);
|
|
18
18
|
gameMethods.setSystems(systems);
|
|
19
19
|
if (paused) {
|
package/src/index.tsx
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
import GameEngine from './GameEngine';
|
|
2
|
-
import {
|
|
3
|
-
configure,
|
|
4
|
-
pause,
|
|
5
|
-
resume,
|
|
6
|
-
spawn,
|
|
7
|
-
despawn,
|
|
8
|
-
update,
|
|
9
|
-
} from './GameMethods';
|
|
10
2
|
|
|
11
|
-
export
|
|
3
|
+
export * from './GameMethods';
|
|
4
|
+
export * from './types';
|
|
5
|
+
export { GameEngine };
|
package/src/types.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export type
|
|
2
|
-
/** Width of the
|
|
1
|
+
export type Screen = {
|
|
2
|
+
/** Width of the screen in game units. */
|
|
3
3
|
width: number;
|
|
4
|
-
/** Height of the
|
|
4
|
+
/** Height of the screen in game units. */
|
|
5
5
|
height: number;
|
|
6
|
-
/** Background color of the
|
|
6
|
+
/** Background color of the screen as a hex string e.g. `'#1a1a1a'`. */
|
|
7
7
|
color: string;
|
|
8
8
|
};
|
|
9
9
|
|
|
@@ -55,8 +55,8 @@ export type EntityUpdate = {
|
|
|
55
55
|
export type Config = {
|
|
56
56
|
/** Number of game logic updates per second. */
|
|
57
57
|
tickRate: number;
|
|
58
|
-
/**
|
|
59
|
-
|
|
58
|
+
/** Screen dimensions and background color. */
|
|
59
|
+
screen: Screen;
|
|
60
60
|
/** Initial entities to spawn. */
|
|
61
61
|
entities?: Entity[];
|
|
62
62
|
/**
|