react-simple-game-engine 0.0.1
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 +46 -0
- package/lib/classes/animations/animation.js +35 -0
- package/lib/classes/animations/avatar.animation.js +63 -0
- package/lib/classes/animations/color.animation.js +33 -0
- package/lib/classes/camera.js +34 -0
- package/lib/classes/entities/circle.entity.js +42 -0
- package/lib/classes/entities/entity.js +90 -0
- package/lib/classes/entities/rect.entity.js +44 -0
- package/lib/classes/logic-component.js +13 -0
- package/lib/classes/scene-management.js +38 -0
- package/lib/classes/scene.js +122 -0
- package/lib/classes/sprites/avatar.sprite.js +38 -0
- package/lib/classes/sprites/color.sprite.js +40 -0
- package/lib/classes/sprites/sprite.js +70 -0
- package/lib/classes/world-management.js +45 -0
- package/lib/decorators/entity-tag.decor.js +5 -0
- package/lib/decorators/scene-tag.decor.js +5 -0
- package/lib/decorators/scene-ui.decor.js +5 -0
- package/lib/export-types.js +1 -0
- package/lib/index.js +15 -0
- package/lib/ui-components/SceneRunner.js +35 -0
- package/lib/ui-components/ScenesProcess.js +24 -0
- package/lib/ui-components/Sketch.js +46 -0
- package/lib/utils.js +75 -0
- package/package.json +44 -0
package/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Getting Started with Create React App
|
2
|
+
|
3
|
+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
|
4
|
+
|
5
|
+
## Available Scripts
|
6
|
+
|
7
|
+
In the project directory, you can run:
|
8
|
+
|
9
|
+
### `yarn start`
|
10
|
+
|
11
|
+
Runs the app in the development mode.\
|
12
|
+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
|
13
|
+
|
14
|
+
The page will reload if you make edits.\
|
15
|
+
You will also see any lint errors in the console.
|
16
|
+
|
17
|
+
### `yarn test`
|
18
|
+
|
19
|
+
Launches the test runner in the interactive watch mode.\
|
20
|
+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
|
21
|
+
|
22
|
+
### `yarn build`
|
23
|
+
|
24
|
+
Builds the app for production to the `build` folder.\
|
25
|
+
It correctly bundles React in production mode and optimizes the build for the best performance.
|
26
|
+
|
27
|
+
The build is minified and the filenames include the hashes.\
|
28
|
+
Your app is ready to be deployed!
|
29
|
+
|
30
|
+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
|
31
|
+
|
32
|
+
### `yarn eject`
|
33
|
+
|
34
|
+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
|
35
|
+
|
36
|
+
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
|
37
|
+
|
38
|
+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
|
39
|
+
|
40
|
+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
|
41
|
+
|
42
|
+
## Learn More
|
43
|
+
|
44
|
+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
|
45
|
+
|
46
|
+
To learn React, check out the [React documentation](https://reactjs.org/).
|
@@ -0,0 +1,35 @@
|
|
1
|
+
var AnimationSprite = /** @class */ (function () {
|
2
|
+
function AnimationSprite() {
|
3
|
+
this.currentFrame = 0;
|
4
|
+
this._isRunning = true;
|
5
|
+
this.timeCounter = 0;
|
6
|
+
this.delatime = 1 / 30;
|
7
|
+
}
|
8
|
+
Object.defineProperty(AnimationSprite.prototype, "isRunning", {
|
9
|
+
set: function (_isRunning) {
|
10
|
+
this._isRunning = _isRunning;
|
11
|
+
if (!_isRunning) {
|
12
|
+
// stop
|
13
|
+
this.timeCounter = 0;
|
14
|
+
this.currentFrame = 0;
|
15
|
+
}
|
16
|
+
},
|
17
|
+
enumerable: false,
|
18
|
+
configurable: true
|
19
|
+
});
|
20
|
+
AnimationSprite.prototype.draw = function () {
|
21
|
+
if (this.checkFrameMax()) {
|
22
|
+
this.currentFrame = 0;
|
23
|
+
}
|
24
|
+
this.onDraw();
|
25
|
+
if (this.timeCounter / 1000 >= this.delatime) {
|
26
|
+
this.timeCounter = 0;
|
27
|
+
if (this._isRunning) {
|
28
|
+
this.currentFrame++;
|
29
|
+
}
|
30
|
+
}
|
31
|
+
this.timeCounter += Renderer.deltaTime;
|
32
|
+
};
|
33
|
+
return AnimationSprite;
|
34
|
+
}());
|
35
|
+
export { AnimationSprite };
|
@@ -0,0 +1,63 @@
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
2
|
+
var extendStatics = function (d, b) {
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
6
|
+
return extendStatics(d, b);
|
7
|
+
};
|
8
|
+
return function (d, b) {
|
9
|
+
if (typeof b !== "function" && b !== null)
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
11
|
+
extendStatics(d, b);
|
12
|
+
function __() { this.constructor = d; }
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
14
|
+
};
|
15
|
+
})();
|
16
|
+
var __assign = (this && this.__assign) || function () {
|
17
|
+
__assign = Object.assign || function(t) {
|
18
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
19
|
+
s = arguments[i];
|
20
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
21
|
+
t[p] = s[p];
|
22
|
+
}
|
23
|
+
return t;
|
24
|
+
};
|
25
|
+
return __assign.apply(this, arguments);
|
26
|
+
};
|
27
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
28
|
+
var t = {};
|
29
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
30
|
+
t[p] = s[p];
|
31
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
32
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
33
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
34
|
+
t[p[i]] = s[p[i]];
|
35
|
+
}
|
36
|
+
return t;
|
37
|
+
};
|
38
|
+
import { AnimationSprite } from "./animation";
|
39
|
+
var AvatarAnimationSprite = /** @class */ (function (_super) {
|
40
|
+
__extends(AvatarAnimationSprite, _super);
|
41
|
+
function AvatarAnimationSprite() {
|
42
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
43
|
+
}
|
44
|
+
AvatarAnimationSprite.prototype.initial = function (_a) {
|
45
|
+
var _b = _a.offsetX, offsetX = _b === void 0 ? 0 : _b, _c = _a.offsetY, offsetY = _c === void 0 ? 0 : _c, options = __rest(_a, ["offsetX", "offsetY"]);
|
46
|
+
this.options = __assign({ offsetX: offsetX, offsetY: offsetY }, options);
|
47
|
+
};
|
48
|
+
AvatarAnimationSprite.prototype.checkFrameMax = function () {
|
49
|
+
var _a = this.options, offsetWidth = _a.offsetWidth, offsetX = _a.offsetX;
|
50
|
+
return (this.currentFrame * offsetWidth + offsetX >= this.source.sprite.width);
|
51
|
+
};
|
52
|
+
AvatarAnimationSprite.prototype.onDraw = function () {
|
53
|
+
var source = this.source;
|
54
|
+
var _a = this.options, offsetWidth = _a.offsetWidth, offsetHeight = _a.offsetHeight, offsetX = _a.offsetX, offsetY = _a.offsetY;
|
55
|
+
Renderer.image(source.sprite,
|
56
|
+
// position on canvas
|
57
|
+
0, 0, source.width, source.height,
|
58
|
+
//crop on source image
|
59
|
+
this.currentFrame * offsetWidth + offsetX, offsetY, offsetWidth, offsetHeight);
|
60
|
+
};
|
61
|
+
return AvatarAnimationSprite;
|
62
|
+
}(AnimationSprite));
|
63
|
+
export { AvatarAnimationSprite };
|
@@ -0,0 +1,33 @@
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
2
|
+
var extendStatics = function (d, b) {
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
6
|
+
return extendStatics(d, b);
|
7
|
+
};
|
8
|
+
return function (d, b) {
|
9
|
+
if (typeof b !== "function" && b !== null)
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
11
|
+
extendStatics(d, b);
|
12
|
+
function __() { this.constructor = d; }
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
14
|
+
};
|
15
|
+
})();
|
16
|
+
import { AnimationSprite } from "./animation";
|
17
|
+
var ColorAnimationSprite = /** @class */ (function (_super) {
|
18
|
+
__extends(ColorAnimationSprite, _super);
|
19
|
+
function ColorAnimationSprite() {
|
20
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
21
|
+
}
|
22
|
+
ColorAnimationSprite.prototype.initial = function (colors) {
|
23
|
+
this.colors = colors;
|
24
|
+
};
|
25
|
+
ColorAnimationSprite.prototype.checkFrameMax = function () {
|
26
|
+
return this.currentFrame >= this.colors.length;
|
27
|
+
};
|
28
|
+
ColorAnimationSprite.prototype.onDraw = function () {
|
29
|
+
Renderer.fill.apply(Renderer, this.colors[this.currentFrame]);
|
30
|
+
};
|
31
|
+
return ColorAnimationSprite;
|
32
|
+
}(AnimationSprite));
|
33
|
+
export { ColorAnimationSprite };
|
@@ -0,0 +1,34 @@
|
|
1
|
+
var Camera = /** @class */ (function () {
|
2
|
+
function Camera(_width, _height) {
|
3
|
+
this._width = _width;
|
4
|
+
this._height = _height;
|
5
|
+
this.x = 0;
|
6
|
+
this.y = 0;
|
7
|
+
this.scaleX = 1;
|
8
|
+
this.scaleY = 1;
|
9
|
+
}
|
10
|
+
Object.defineProperty(Camera.prototype, "width", {
|
11
|
+
get: function () {
|
12
|
+
return this._width;
|
13
|
+
},
|
14
|
+
set: function (width) {
|
15
|
+
this._width = width;
|
16
|
+
Renderer.resizeCanvas(this._width, this._height);
|
17
|
+
},
|
18
|
+
enumerable: false,
|
19
|
+
configurable: true
|
20
|
+
});
|
21
|
+
Object.defineProperty(Camera.prototype, "height", {
|
22
|
+
get: function () {
|
23
|
+
return this._height;
|
24
|
+
},
|
25
|
+
set: function (height) {
|
26
|
+
this._height = height;
|
27
|
+
Renderer.resizeCanvas(this._width, this._height);
|
28
|
+
},
|
29
|
+
enumerable: false,
|
30
|
+
configurable: true
|
31
|
+
});
|
32
|
+
return Camera;
|
33
|
+
}());
|
34
|
+
export { Camera };
|
@@ -0,0 +1,42 @@
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
2
|
+
var extendStatics = function (d, b) {
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
6
|
+
return extendStatics(d, b);
|
7
|
+
};
|
8
|
+
return function (d, b) {
|
9
|
+
if (typeof b !== "function" && b !== null)
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
11
|
+
extendStatics(d, b);
|
12
|
+
function __() { this.constructor = d; }
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
14
|
+
};
|
15
|
+
})();
|
16
|
+
import { Bodies } from "matter-js";
|
17
|
+
import { Entity } from "./entity";
|
18
|
+
var CircleEntity = /** @class */ (function (_super) {
|
19
|
+
__extends(CircleEntity, _super);
|
20
|
+
function CircleEntity() {
|
21
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
22
|
+
}
|
23
|
+
CircleEntity.prototype.onSpriteWidthHeightBinding = function () {
|
24
|
+
return {
|
25
|
+
width: this.radius * 2,
|
26
|
+
height: this.radius * 2,
|
27
|
+
};
|
28
|
+
};
|
29
|
+
CircleEntity.prototype.onInitial = function () {
|
30
|
+
return {
|
31
|
+
transform: {
|
32
|
+
radius: 5,
|
33
|
+
},
|
34
|
+
};
|
35
|
+
};
|
36
|
+
CircleEntity.prototype.onCreateBody = function (transform, options) {
|
37
|
+
this.radius = transform.radius;
|
38
|
+
return Bodies.circle(transform.x, transform.y, transform.radius, options);
|
39
|
+
};
|
40
|
+
return CircleEntity;
|
41
|
+
}(Entity));
|
42
|
+
export { CircleEntity };
|
@@ -0,0 +1,90 @@
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
2
|
+
__assign = Object.assign || function(t) {
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4
|
+
s = arguments[i];
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6
|
+
t[p] = s[p];
|
7
|
+
}
|
8
|
+
return t;
|
9
|
+
};
|
10
|
+
return __assign.apply(this, arguments);
|
11
|
+
};
|
12
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
13
|
+
var t = {};
|
14
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
15
|
+
t[p] = s[p];
|
16
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
17
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
18
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
19
|
+
t[p[i]] = s[p[i]];
|
20
|
+
}
|
21
|
+
return t;
|
22
|
+
};
|
23
|
+
import { ColorSprite } from "../sprites/color.sprite";
|
24
|
+
var Entity = /** @class */ (function () {
|
25
|
+
function Entity() {
|
26
|
+
this.id = "".concat(Math.random(), "-").concat(new Date().getTime());
|
27
|
+
this.tag = this.constructor.tag;
|
28
|
+
}
|
29
|
+
Object.defineProperty(Entity.prototype, "sprite", {
|
30
|
+
get: function () {
|
31
|
+
return this._sprite;
|
32
|
+
},
|
33
|
+
set: function (sprite) {
|
34
|
+
this._sprite = sprite;
|
35
|
+
this._sprite.entity = this;
|
36
|
+
},
|
37
|
+
enumerable: false,
|
38
|
+
configurable: true
|
39
|
+
});
|
40
|
+
Object.defineProperty(Entity.prototype, "position", {
|
41
|
+
get: function () {
|
42
|
+
return this._body.position;
|
43
|
+
},
|
44
|
+
enumerable: false,
|
45
|
+
configurable: true
|
46
|
+
});
|
47
|
+
Object.defineProperty(Entity.prototype, "body", {
|
48
|
+
get: function () {
|
49
|
+
return this._body;
|
50
|
+
},
|
51
|
+
enumerable: false,
|
52
|
+
configurable: true
|
53
|
+
});
|
54
|
+
Entity.prototype.active = function (worldManagement) {
|
55
|
+
console.log("Initted ".concat(this.tag, " entity (id : ").concat(this.id, ")"));
|
56
|
+
this.worldManagement = worldManagement;
|
57
|
+
this.onActive();
|
58
|
+
};
|
59
|
+
Entity.prototype.onActive = function () { };
|
60
|
+
Entity.prototype.createBody = function (transform, options) {
|
61
|
+
this._body = this.onCreateBody(transform, options);
|
62
|
+
this._body.entity = this;
|
63
|
+
return this._body;
|
64
|
+
};
|
65
|
+
Entity.prototype.initial = function (_a) {
|
66
|
+
var sound = _a.sound, _b = _a.transform, transform = _b === void 0 ? {} : _b, spriteComponent = _a.sprite, _c = _a.bodyOptions, bodyOptions = _c === void 0 ? {} : _c;
|
67
|
+
var _d = this.onInitial(), _e = _d.transform, _f = _e === void 0 ? {} : _e, _g = _f.x, x = _g === void 0 ? 0 : _g, _h = _f.y, y = _h === void 0 ? 0 : _h, dfTransform = __rest(_f, ["x", "y"]), dfBodyOptions = _d.bodyOptions, dfSpriteComponent = _d.sprite;
|
68
|
+
var _j = this.onPrepare(), transformAlt = _j.transform, bodyOptionsAlt = _j.bodyOptions, spriteComponentAlt = _j.sprite;
|
69
|
+
this.createBody(__assign(__assign(__assign({ x: x, y: y }, dfTransform), transform), transformAlt), __assign(__assign(__assign({}, dfBodyOptions), bodyOptions), bodyOptionsAlt));
|
70
|
+
this.sound = sound;
|
71
|
+
this.sprite =
|
72
|
+
(dfSpriteComponent === null || dfSpriteComponent === void 0 ? void 0 : dfSpriteComponent.output()) ||
|
73
|
+
(spriteComponent === null || spriteComponent === void 0 ? void 0 : spriteComponent.output()) ||
|
74
|
+
(spriteComponentAlt === null || spriteComponentAlt === void 0 ? void 0 : spriteComponentAlt.output()) ||
|
75
|
+
new ColorSprite();
|
76
|
+
};
|
77
|
+
Entity.prototype.onPrepare = function () {
|
78
|
+
return {};
|
79
|
+
};
|
80
|
+
Entity.prototype.update = function () {
|
81
|
+
this.onUpdate();
|
82
|
+
};
|
83
|
+
Entity.prototype.onUpdate = function () { };
|
84
|
+
Entity.prototype.draw = function () {
|
85
|
+
this.sprite.draw();
|
86
|
+
};
|
87
|
+
Entity.prototype.onCollision = function (target) { };
|
88
|
+
return Entity;
|
89
|
+
}());
|
90
|
+
export { Entity };
|
@@ -0,0 +1,44 @@
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
2
|
+
var extendStatics = function (d, b) {
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
6
|
+
return extendStatics(d, b);
|
7
|
+
};
|
8
|
+
return function (d, b) {
|
9
|
+
if (typeof b !== "function" && b !== null)
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
11
|
+
extendStatics(d, b);
|
12
|
+
function __() { this.constructor = d; }
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
14
|
+
};
|
15
|
+
})();
|
16
|
+
import { Bodies } from "matter-js";
|
17
|
+
import { Entity } from "./entity";
|
18
|
+
var RectEntity = /** @class */ (function (_super) {
|
19
|
+
__extends(RectEntity, _super);
|
20
|
+
function RectEntity() {
|
21
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
22
|
+
}
|
23
|
+
RectEntity.prototype.onSpriteWidthHeightBinding = function () {
|
24
|
+
return {
|
25
|
+
width: this.width,
|
26
|
+
height: this.height,
|
27
|
+
};
|
28
|
+
};
|
29
|
+
RectEntity.prototype.onInitial = function () {
|
30
|
+
return {
|
31
|
+
transform: {
|
32
|
+
width: 10,
|
33
|
+
height: 10,
|
34
|
+
},
|
35
|
+
};
|
36
|
+
};
|
37
|
+
RectEntity.prototype.onCreateBody = function (transform, options) {
|
38
|
+
this.width = transform.width;
|
39
|
+
this.height = transform.height;
|
40
|
+
return Bodies.rectangle(transform.x, transform.y, transform.width, transform.height, options);
|
41
|
+
};
|
42
|
+
return RectEntity;
|
43
|
+
}(Entity));
|
44
|
+
export { RectEntity };
|
@@ -0,0 +1,13 @@
|
|
1
|
+
var LogicComponent = /** @class */ (function () {
|
2
|
+
function LogicComponent(configale) {
|
3
|
+
var _a = Array.isArray(configale) ? configale : [configale], Class = _a[0], params = _a[1];
|
4
|
+
var c = new Class();
|
5
|
+
c.initial(params);
|
6
|
+
this.instance = c;
|
7
|
+
}
|
8
|
+
LogicComponent.prototype.output = function () {
|
9
|
+
return this.instance;
|
10
|
+
};
|
11
|
+
return LogicComponent;
|
12
|
+
}());
|
13
|
+
export { LogicComponent };
|
@@ -0,0 +1,38 @@
|
|
1
|
+
var SceneManagement = /** @class */ (function () {
|
2
|
+
function SceneManagement(Scenes) {
|
3
|
+
this.Scenes = Scenes;
|
4
|
+
this._currentScene = new Scenes[0]();
|
5
|
+
this._currentScene.manager = this;
|
6
|
+
}
|
7
|
+
SceneManagement.getTag = function (Scene) {
|
8
|
+
return Scene.tag;
|
9
|
+
};
|
10
|
+
Object.defineProperty(SceneManagement.prototype, "currentScene", {
|
11
|
+
get: function () {
|
12
|
+
return this._currentScene;
|
13
|
+
},
|
14
|
+
enumerable: false,
|
15
|
+
configurable: true
|
16
|
+
});
|
17
|
+
SceneManagement.prototype.onChangeScene = function (func) {
|
18
|
+
this.changeSceneListener = func;
|
19
|
+
};
|
20
|
+
SceneManagement.prototype.gotoScene = function (tag) {
|
21
|
+
var _a;
|
22
|
+
for (var _i = 0, _b = this.Scenes; _i < _b.length; _i++) {
|
23
|
+
var Scene_1 = _b[_i];
|
24
|
+
var _tag = SceneManagement.getTag(Scene_1);
|
25
|
+
if (_tag === tag) {
|
26
|
+
if (this._currentScene) {
|
27
|
+
this._currentScene.destructor();
|
28
|
+
}
|
29
|
+
this._currentScene = new Scene_1();
|
30
|
+
this._currentScene.manager = this;
|
31
|
+
(_a = this.changeSceneListener) === null || _a === void 0 ? void 0 : _a.call(this, this._currentScene);
|
32
|
+
break;
|
33
|
+
}
|
34
|
+
}
|
35
|
+
};
|
36
|
+
return SceneManagement;
|
37
|
+
}());
|
38
|
+
export { SceneManagement };
|
@@ -0,0 +1,122 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
12
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
14
|
+
function step(op) {
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
16
|
+
while (_) try {
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
19
|
+
switch (op[0]) {
|
20
|
+
case 0: case 1: t = op; break;
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
24
|
+
default:
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
29
|
+
if (t[2]) _.ops.pop();
|
30
|
+
_.trys.pop(); continue;
|
31
|
+
}
|
32
|
+
op = body.call(thisArg, _);
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
35
|
+
}
|
36
|
+
};
|
37
|
+
import { WorldManagement } from "./world-management";
|
38
|
+
var Scene = /** @class */ (function () {
|
39
|
+
function Scene() {
|
40
|
+
this.sessionId = "".concat(Math.random(), "-").concat(new Date().getTime());
|
41
|
+
this.tag = this.constructor.tag;
|
42
|
+
this.ui = this.constructor.ui;
|
43
|
+
this._loadedAssets = false;
|
44
|
+
}
|
45
|
+
Object.defineProperty(Scene.prototype, "UI", {
|
46
|
+
get: function () {
|
47
|
+
var Ui = this.ui || (function () { return null; });
|
48
|
+
return Ui;
|
49
|
+
},
|
50
|
+
enumerable: false,
|
51
|
+
configurable: true
|
52
|
+
});
|
53
|
+
Object.defineProperty(Scene.prototype, "UIProps", {
|
54
|
+
get: function () {
|
55
|
+
return this.getUIProps();
|
56
|
+
},
|
57
|
+
enumerable: false,
|
58
|
+
configurable: true
|
59
|
+
});
|
60
|
+
Scene.prototype.getUIProps = function () {
|
61
|
+
return {};
|
62
|
+
};
|
63
|
+
Object.defineProperty(Scene.prototype, "loadedAssets", {
|
64
|
+
get: function () {
|
65
|
+
return this._loadedAssets;
|
66
|
+
},
|
67
|
+
enumerable: false,
|
68
|
+
configurable: true
|
69
|
+
});
|
70
|
+
Scene.prototype.onLoadAssetNotify = function (func) {
|
71
|
+
this.loadAssetsListener = func;
|
72
|
+
};
|
73
|
+
Scene.prototype.destructor = function () {
|
74
|
+
this.worldManagement.destructor();
|
75
|
+
};
|
76
|
+
Scene.prototype.setLoadAssetStatus = function (loadedAssets) {
|
77
|
+
var _a;
|
78
|
+
this._loadedAssets = loadedAssets;
|
79
|
+
(_a = this.loadAssetsListener) === null || _a === void 0 ? void 0 : _a.call(this, loadedAssets);
|
80
|
+
};
|
81
|
+
Scene.prototype.switchToScene = function (tag) {
|
82
|
+
this.manager.gotoScene(tag);
|
83
|
+
};
|
84
|
+
Scene.prototype.loadAssets = function () {
|
85
|
+
return __awaiter(this, void 0, void 0, function () {
|
86
|
+
return __generator(this, function (_a) {
|
87
|
+
switch (_a.label) {
|
88
|
+
case 0:
|
89
|
+
this.setLoadAssetStatus(false);
|
90
|
+
return [4 /*yield*/, this.onLoadAssets().catch(function (err) {
|
91
|
+
console.warn("Load assets fail", err.toString());
|
92
|
+
})];
|
93
|
+
case 1:
|
94
|
+
_a.sent();
|
95
|
+
this.setLoadAssetStatus(true);
|
96
|
+
return [2 /*return*/];
|
97
|
+
}
|
98
|
+
});
|
99
|
+
});
|
100
|
+
};
|
101
|
+
Scene.prototype.onLoadAssets = function () {
|
102
|
+
return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
|
103
|
+
return [2 /*return*/];
|
104
|
+
}); });
|
105
|
+
};
|
106
|
+
Scene.prototype.bootstrap = function (camera) {
|
107
|
+
this.worldManagement = new WorldManagement();
|
108
|
+
var components = this.getComponents();
|
109
|
+
for (var _i = 0, components_1 = components; _i < components_1.length; _i++) {
|
110
|
+
var component = components_1[_i];
|
111
|
+
var entity = component.output();
|
112
|
+
entity.camera = camera;
|
113
|
+
this.worldManagement.addEntity(entity);
|
114
|
+
}
|
115
|
+
};
|
116
|
+
Scene.prototype.action = function () {
|
117
|
+
this.worldManagement.update();
|
118
|
+
this.worldManagement.draw();
|
119
|
+
};
|
120
|
+
return Scene;
|
121
|
+
}());
|
122
|
+
export { Scene };
|
@@ -0,0 +1,38 @@
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
2
|
+
var extendStatics = function (d, b) {
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
6
|
+
return extendStatics(d, b);
|
7
|
+
};
|
8
|
+
return function (d, b) {
|
9
|
+
if (typeof b !== "function" && b !== null)
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
11
|
+
extendStatics(d, b);
|
12
|
+
function __() { this.constructor = d; }
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
14
|
+
};
|
15
|
+
})();
|
16
|
+
import { Sprite } from "./sprite";
|
17
|
+
var AvatarSprite = /** @class */ (function (_super) {
|
18
|
+
__extends(AvatarSprite, _super);
|
19
|
+
function AvatarSprite() {
|
20
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
21
|
+
}
|
22
|
+
AvatarSprite.prototype.onDraw = function () {
|
23
|
+
if (this.sprite) {
|
24
|
+
if (this.animation) {
|
25
|
+
this.animation.draw();
|
26
|
+
}
|
27
|
+
else {
|
28
|
+
Renderer.image(this.sprite,
|
29
|
+
// position on canvas
|
30
|
+
0, 0, this.width, this.height,
|
31
|
+
//crop on source image
|
32
|
+
0, 0, this.sprite.width, this.sprite.height);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
};
|
36
|
+
return AvatarSprite;
|
37
|
+
}(Sprite));
|
38
|
+
export { AvatarSprite };
|
@@ -0,0 +1,40 @@
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
2
|
+
var extendStatics = function (d, b) {
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
6
|
+
return extendStatics(d, b);
|
7
|
+
};
|
8
|
+
return function (d, b) {
|
9
|
+
if (typeof b !== "function" && b !== null)
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
11
|
+
extendStatics(d, b);
|
12
|
+
function __() { this.constructor = d; }
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
14
|
+
};
|
15
|
+
})();
|
16
|
+
import { Sprite } from "./sprite";
|
17
|
+
var ColorSprite = /** @class */ (function (_super) {
|
18
|
+
__extends(ColorSprite, _super);
|
19
|
+
function ColorSprite() {
|
20
|
+
var _this = _super.call(this) || this;
|
21
|
+
_this.sprite = [255, 255, 255];
|
22
|
+
return _this;
|
23
|
+
}
|
24
|
+
ColorSprite.prototype.onDraw = function () {
|
25
|
+
if (this.animation) {
|
26
|
+
this.animation.draw();
|
27
|
+
}
|
28
|
+
else {
|
29
|
+
Renderer.fill.apply(Renderer, this.sprite);
|
30
|
+
}
|
31
|
+
if (this.entity.width != null) {
|
32
|
+
Renderer.rect(0, 0, this.width, this.height);
|
33
|
+
}
|
34
|
+
else {
|
35
|
+
Renderer.circle(0, 0, this.width);
|
36
|
+
}
|
37
|
+
};
|
38
|
+
return ColorSprite;
|
39
|
+
}(Sprite));
|
40
|
+
export { ColorSprite };
|
@@ -0,0 +1,70 @@
|
|
1
|
+
import { LogicComponent } from "../logic-component";
|
2
|
+
var Sprite = /** @class */ (function () {
|
3
|
+
function Sprite() {
|
4
|
+
}
|
5
|
+
Object.defineProperty(Sprite.prototype, "animation", {
|
6
|
+
get: function () {
|
7
|
+
return this._animation;
|
8
|
+
},
|
9
|
+
set: function (ani) {
|
10
|
+
this._animation = ani;
|
11
|
+
this._animation.source = this;
|
12
|
+
},
|
13
|
+
enumerable: false,
|
14
|
+
configurable: true
|
15
|
+
});
|
16
|
+
Object.defineProperty(Sprite.prototype, "entity", {
|
17
|
+
get: function () {
|
18
|
+
return this._entity;
|
19
|
+
},
|
20
|
+
set: function (entity) {
|
21
|
+
this._entity = entity;
|
22
|
+
var _a = this._entity.onSpriteWidthHeightBinding(), width = _a.width, height = _a.height;
|
23
|
+
this._width = width;
|
24
|
+
this._height = height;
|
25
|
+
},
|
26
|
+
enumerable: false,
|
27
|
+
configurable: true
|
28
|
+
});
|
29
|
+
Object.defineProperty(Sprite.prototype, "width", {
|
30
|
+
get: function () {
|
31
|
+
return this._width;
|
32
|
+
},
|
33
|
+
enumerable: false,
|
34
|
+
configurable: true
|
35
|
+
});
|
36
|
+
Object.defineProperty(Sprite.prototype, "height", {
|
37
|
+
get: function () {
|
38
|
+
return this._height;
|
39
|
+
},
|
40
|
+
enumerable: false,
|
41
|
+
configurable: true
|
42
|
+
});
|
43
|
+
Sprite.prototype.draw = function () {
|
44
|
+
var _a = this.entity, body = _a.body, position = _a.position, camera = _a.camera;
|
45
|
+
Renderer.push();
|
46
|
+
Renderer.noStroke();
|
47
|
+
Renderer.translate(position.x - camera.x, position.y - camera.y);
|
48
|
+
Renderer.rotate(body.angle);
|
49
|
+
this.onDraw();
|
50
|
+
Renderer.pop();
|
51
|
+
};
|
52
|
+
Sprite.prototype.initial = function (params) {
|
53
|
+
if (params) {
|
54
|
+
for (var key in params) {
|
55
|
+
// @ts-ignore
|
56
|
+
var P = params[key];
|
57
|
+
if (P instanceof LogicComponent) {
|
58
|
+
// @ts-ignore
|
59
|
+
this[key] = P.output();
|
60
|
+
}
|
61
|
+
else {
|
62
|
+
// @ts-ignore
|
63
|
+
this[key] = P;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
}
|
67
|
+
};
|
68
|
+
return Sprite;
|
69
|
+
}());
|
70
|
+
export { Sprite };
|
@@ -0,0 +1,45 @@
|
|
1
|
+
import { Engine, World, Events } from "matter-js";
|
2
|
+
var WorldManagement = /** @class */ (function () {
|
3
|
+
function WorldManagement() {
|
4
|
+
this.entities = [];
|
5
|
+
this.engine = Engine.create();
|
6
|
+
Events.on(this.engine, "collisionStart", function (event) {
|
7
|
+
var pairs = event.pairs;
|
8
|
+
for (var _i = 0, pairs_1 = pairs; _i < pairs_1.length; _i++) {
|
9
|
+
var pair = pairs_1[_i];
|
10
|
+
var _a = pair, bodyA = _a.bodyA, bodyB = _a.bodyB;
|
11
|
+
bodyA.entity.onCollision(bodyB.entity);
|
12
|
+
bodyB.entity.onCollision(bodyA.entity);
|
13
|
+
}
|
14
|
+
});
|
15
|
+
}
|
16
|
+
WorldManagement.prototype.destructor = function () {
|
17
|
+
World.clear(this.engine.world, false);
|
18
|
+
Engine.clear(this.engine);
|
19
|
+
};
|
20
|
+
WorldManagement.prototype.addEntity = function (entity) {
|
21
|
+
this.entities.push(entity);
|
22
|
+
World.add(this.engine.world, entity.body);
|
23
|
+
entity.active(this);
|
24
|
+
};
|
25
|
+
WorldManagement.prototype.removeEntity = function (entity) {
|
26
|
+
World.remove(this.engine.world, entity.body);
|
27
|
+
this.entities.splice(this.entities.indexOf(entity), 1);
|
28
|
+
};
|
29
|
+
WorldManagement.prototype.update = function () {
|
30
|
+
Engine.update(this.engine);
|
31
|
+
for (var _i = 0, _a = this.entities; _i < _a.length; _i++) {
|
32
|
+
var entity = _a[_i];
|
33
|
+
entity.update();
|
34
|
+
}
|
35
|
+
};
|
36
|
+
WorldManagement.prototype.draw = function () {
|
37
|
+
Renderer.background(41, 41, 41);
|
38
|
+
for (var _i = 0, _a = this.entities; _i < _a.length; _i++) {
|
39
|
+
var entity = _a[_i];
|
40
|
+
entity.draw();
|
41
|
+
}
|
42
|
+
};
|
43
|
+
return WorldManagement;
|
44
|
+
}());
|
45
|
+
export { WorldManagement };
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
package/lib/index.js
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
export { createAssetImage, createAssetSound } from "./utils";
|
2
|
+
export { AvatarAnimationSprite } from "./classes/animations/avatar.animation";
|
3
|
+
export { ColorAnimationSprite } from "./classes/animations/color.animation";
|
4
|
+
export { CircleEntity } from "./classes/entities/circle.entity";
|
5
|
+
export { RectEntity } from "./classes/entities/rect.entity";
|
6
|
+
export { LogicComponent } from "./classes/logic-component";
|
7
|
+
export { AvatarSprite } from "./classes/sprites/avatar.sprite";
|
8
|
+
export { ColorSprite } from "./classes/sprites/color.sprite";
|
9
|
+
export { Scene } from "./classes/scene";
|
10
|
+
export { EntityTag } from "./decorators/entity-tag.decor";
|
11
|
+
export { SceneTag } from "./decorators/scene-tag.decor";
|
12
|
+
export { SceneUI } from "./decorators/scene-ui.decor";
|
13
|
+
export { ScenesProcess } from "./ui-components/ScenesProcess";
|
14
|
+
export * from "./export-types";
|
15
|
+
export var Renderer = window.Renderer;
|
@@ -0,0 +1,35 @@
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
2
|
+
__assign = Object.assign || function(t) {
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4
|
+
s = arguments[i];
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6
|
+
t[p] = s[p];
|
7
|
+
}
|
8
|
+
return t;
|
9
|
+
};
|
10
|
+
return __assign.apply(this, arguments);
|
11
|
+
};
|
12
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
13
|
+
import { useEffect } from "react";
|
14
|
+
import { Sketch } from "./Sketch";
|
15
|
+
export function SceneRunner(_a) {
|
16
|
+
var current = _a.current, width = _a.width, height = _a.height;
|
17
|
+
useEffect(function () {
|
18
|
+
current.loadAssets();
|
19
|
+
}, [current]);
|
20
|
+
var setup = function (camera) {
|
21
|
+
current.bootstrap(camera);
|
22
|
+
};
|
23
|
+
var draw = function () {
|
24
|
+
current.action();
|
25
|
+
};
|
26
|
+
return current.loadedAssets ? (_jsxs("div", __assign({ style: { width: width, height: height, position: "relative" } }, { children: [_jsx(Sketch, { width: width, height: height, onSetup: setup, onDraw: draw }), _jsx("div", __assign({ style: {
|
27
|
+
position: "absolute",
|
28
|
+
top: 0,
|
29
|
+
bottom: 0,
|
30
|
+
left: 0,
|
31
|
+
right: 0,
|
32
|
+
userSelect: "none",
|
33
|
+
zIndex: 2,
|
34
|
+
} }, { children: _jsx(current.UI, __assign({}, current.UIProps)) }))] }))) : (_jsx("div", { children: "Assets loading..." }));
|
35
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
2
|
+
import { useEffect, useMemo, useState } from "react";
|
3
|
+
import { SceneManagement } from "../classes/scene-management";
|
4
|
+
import { SceneRunner } from "./SceneRunner";
|
5
|
+
export function ScenesProcess(_a) {
|
6
|
+
var list = _a.list, width = _a.width, height = _a.height;
|
7
|
+
var sceneManagement = useMemo(function () {
|
8
|
+
return new SceneManagement(list);
|
9
|
+
}, [list]);
|
10
|
+
var _b = useState(sceneManagement.currentScene), currentScene = _b[0], setCurrentScene = _b[1];
|
11
|
+
var _c = useState(currentScene.loadedAssets), setLoadedAssets = _c[1];
|
12
|
+
useEffect(function () {
|
13
|
+
sceneManagement.onChangeScene(function (scene) {
|
14
|
+
setCurrentScene(scene);
|
15
|
+
setLoadedAssets(scene.loadedAssets);
|
16
|
+
});
|
17
|
+
}, [sceneManagement]);
|
18
|
+
useEffect(function () {
|
19
|
+
currentScene.onLoadAssetNotify(function (isLoaded) {
|
20
|
+
setLoadedAssets(isLoaded);
|
21
|
+
});
|
22
|
+
}, [currentScene]);
|
23
|
+
return (_jsx(SceneRunner, { current: currentScene, width: width, height: height }, currentScene.sessionId));
|
24
|
+
}
|
@@ -0,0 +1,46 @@
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
2
|
+
import p5 from "p5";
|
3
|
+
import { useEffect, useRef } from "react";
|
4
|
+
import { Camera } from "../classes/camera";
|
5
|
+
export function Sketch(_a) {
|
6
|
+
var onSetup = _a.onSetup, onDraw = _a.onDraw, onPreload = _a.onPreload, width = _a.width, height = _a.height;
|
7
|
+
var refContainer = useRef(null);
|
8
|
+
useEffect(function () {
|
9
|
+
var sketch = function (s) {
|
10
|
+
window.Renderer = s;
|
11
|
+
var camera;
|
12
|
+
s.preload = function () {
|
13
|
+
return onPreload === null || onPreload === void 0 ? void 0 : onPreload();
|
14
|
+
};
|
15
|
+
s.setup = function () {
|
16
|
+
camera = new Camera(width, height);
|
17
|
+
s.createCanvas(width, height).parent(refContainer.current);
|
18
|
+
onSetup(camera);
|
19
|
+
//@ts-ignore
|
20
|
+
window.camera = camera;
|
21
|
+
};
|
22
|
+
s.draw = function () {
|
23
|
+
s.scale(camera.scaleX, camera.scaleY);
|
24
|
+
s.background(0);
|
25
|
+
s.imageMode(s.CENTER);
|
26
|
+
s.rectMode(s.CENTER);
|
27
|
+
onDraw();
|
28
|
+
// if (s.keyIsDown(s.DOWN_ARROW)) {
|
29
|
+
// camera.y += 2;
|
30
|
+
// }
|
31
|
+
// if (s.keyIsDown(s.UP_ARROW)) {
|
32
|
+
// camera.y -= 2;
|
33
|
+
// }
|
34
|
+
// if (s.keyIsDown(s.LEFT_ARROW)) {
|
35
|
+
// camera.x -= 2;
|
36
|
+
// }
|
37
|
+
// if (s.keyIsDown(s.RIGHT_ARROW)) {
|
38
|
+
// camera.x += 2;
|
39
|
+
// }
|
40
|
+
};
|
41
|
+
};
|
42
|
+
new p5(sketch);
|
43
|
+
// eslint-disable-next-line
|
44
|
+
}, []);
|
45
|
+
return _jsx("div", { style: { backgroundColor: "#000" }, ref: refContainer });
|
46
|
+
}
|
package/lib/utils.js
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
12
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
14
|
+
function step(op) {
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
16
|
+
while (_) try {
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
19
|
+
switch (op[0]) {
|
20
|
+
case 0: case 1: t = op; break;
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
24
|
+
default:
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
29
|
+
if (t[2]) _.ops.pop();
|
30
|
+
_.trys.pop(); continue;
|
31
|
+
}
|
32
|
+
op = body.call(thisArg, _);
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
35
|
+
}
|
36
|
+
};
|
37
|
+
import OutofScopeP5 from "p5";
|
38
|
+
var outofScopeP5;
|
39
|
+
document.addEventListener("DOMContentLoaded", function () {
|
40
|
+
var noop = document.createElement("div");
|
41
|
+
noop.style.position = "absolute";
|
42
|
+
noop.style.zIndex = "-1";
|
43
|
+
noop.style.top = "-100%";
|
44
|
+
noop.style.left = "-100%";
|
45
|
+
document.body.appendChild(noop);
|
46
|
+
setTimeout(function () {
|
47
|
+
outofScopeP5 = new OutofScopeP5(function (skt) { }, noop);
|
48
|
+
}, 0);
|
49
|
+
});
|
50
|
+
export function createAssetImage(src) {
|
51
|
+
return __awaiter(this, void 0, void 0, function () {
|
52
|
+
return __generator(this, function (_a) {
|
53
|
+
return [2 /*return*/, new Promise(function (res, rej) {
|
54
|
+
outofScopeP5.loadImage(src, res, rej);
|
55
|
+
})];
|
56
|
+
});
|
57
|
+
});
|
58
|
+
}
|
59
|
+
export function createAssetSound(src) {
|
60
|
+
return __awaiter(this, void 0, void 0, function () {
|
61
|
+
var media;
|
62
|
+
return __generator(this, function (_a) {
|
63
|
+
media = new Audio();
|
64
|
+
return [2 /*return*/, new Promise(function (res, rej) {
|
65
|
+
media.onloadedmetadata = function () {
|
66
|
+
res(media);
|
67
|
+
};
|
68
|
+
media.onerror = function (error) {
|
69
|
+
rej(error);
|
70
|
+
};
|
71
|
+
media.src = src;
|
72
|
+
})];
|
73
|
+
});
|
74
|
+
});
|
75
|
+
}
|
package/package.json
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
{
|
2
|
+
"name": "react-simple-game-engine",
|
3
|
+
"version": "0.0.1",
|
4
|
+
"description": "",
|
5
|
+
"main": "lib/index.js",
|
6
|
+
"types": "lib",
|
7
|
+
"repository": "https://github.com/phucsang0spt/react-simple-game-engine",
|
8
|
+
"dependencies": {
|
9
|
+
"matter-js": "^0.18.0",
|
10
|
+
"p5": "^1.4.1",
|
11
|
+
"react": "^18.0.0",
|
12
|
+
"react-dom": "^18.0.0"
|
13
|
+
},
|
14
|
+
"scripts": {
|
15
|
+
"build": "rm -rf lib && tsc -p .",
|
16
|
+
"publish": "npm publish"
|
17
|
+
},
|
18
|
+
"files": [
|
19
|
+
"/lib"
|
20
|
+
],
|
21
|
+
"keywords": [
|
22
|
+
"game engine",
|
23
|
+
"matter.js game",
|
24
|
+
"p5.js game",
|
25
|
+
"matter js game",
|
26
|
+
"p5 js game",
|
27
|
+
"js game",
|
28
|
+
"canvas game",
|
29
|
+
"react game",
|
30
|
+
"react game engine",
|
31
|
+
"react simple game engine",
|
32
|
+
"reactjs game",
|
33
|
+
"reactjs game engine",
|
34
|
+
"reactjs simple game engine"
|
35
|
+
],
|
36
|
+
"devDependencies": {
|
37
|
+
"typescript": "^4.4.2",
|
38
|
+
"@types/matter-js": "^0.17.7",
|
39
|
+
"@types/node": "^16.7.13",
|
40
|
+
"@types/p5": "^1.4.2",
|
41
|
+
"@types/react": "^18.0.0",
|
42
|
+
"@types/react-dom": "^18.0.0"
|
43
|
+
}
|
44
|
+
}
|