pixi-fusion 1.0.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/LICENSE +21 -0
- package/README.md +115 -0
- package/dist/src/assets-manager/AssetsManager.context.d.ts +15 -0
- package/dist/src/assets-manager/AssetsManager.context.js +6 -0
- package/dist/src/assets-manager/AssetsManager.d.ts +2 -0
- package/dist/src/assets-manager/AssetsManager.js +43 -0
- package/dist/src/assets-manager/index.d.ts +3 -0
- package/dist/src/assets-manager/index.js +3 -0
- package/dist/src/assets-manager/useAssetManager.d.ts +1 -0
- package/dist/src/assets-manager/useAssetManager.js +5 -0
- package/dist/src/camera/Camera.context.d.ts +13 -0
- package/dist/src/camera/Camera.context.js +2 -0
- package/dist/src/camera/Camera.d.ts +7 -0
- package/dist/src/camera/Camera.js +99 -0
- package/dist/src/camera/index.d.ts +2 -0
- package/dist/src/camera/index.js +2 -0
- package/dist/src/game-context/Game.context.d.ts +39 -0
- package/dist/src/game-context/Game.context.js +85 -0
- package/dist/src/game-context/index.d.ts +1 -0
- package/dist/src/game-context/index.js +1 -0
- package/dist/src/game-objects/GameObjectPhysicalObjectConfig.d.ts +4 -0
- package/dist/src/game-objects/GameObjectPhysicalObjectConfig.js +1 -0
- package/dist/src/game-objects/index.d.ts +2 -0
- package/dist/src/game-objects/index.js +2 -0
- package/dist/src/game-objects/usePhysicalObject.d.ts +7 -0
- package/dist/src/game-objects/usePhysicalObject.js +17 -0
- package/dist/src/game-objects/usePhysicalObjectFromConfig.d.ts +6 -0
- package/dist/src/game-objects/usePhysicalObjectFromConfig.js +13 -0
- package/dist/src/game-objects/useWalls.d.ts +11 -0
- package/dist/src/game-objects/useWalls.js +59 -0
- package/dist/src/hooks/index.d.ts +12 -0
- package/dist/src/hooks/index.js +12 -0
- package/dist/src/hooks/useAnimatedSprite.d.ts +10 -0
- package/dist/src/hooks/useAnimatedSprite.js +41 -0
- package/dist/src/hooks/useCamera.d.ts +1 -0
- package/dist/src/hooks/useCamera.js +5 -0
- package/dist/src/hooks/useCollisionDetection.d.ts +9 -0
- package/dist/src/hooks/useCollisionDetection.js +21 -0
- package/dist/src/hooks/useGame.d.ts +1 -0
- package/dist/src/hooks/useGame.js +5 -0
- package/dist/src/hooks/useGlobalEventHandler.d.ts +5 -0
- package/dist/src/hooks/useGlobalEventHandler.js +15 -0
- package/dist/src/hooks/useLayerContext.d.ts +1 -0
- package/dist/src/hooks/useLayerContext.js +5 -0
- package/dist/src/hooks/useObject.d.ts +6 -0
- package/dist/src/hooks/useObject.js +54 -0
- package/dist/src/hooks/useSprite.d.ts +6 -0
- package/dist/src/hooks/useSprite.js +21 -0
- package/dist/src/hooks/useStage.d.ts +1 -0
- package/dist/src/hooks/useStage.js +5 -0
- package/dist/src/hooks/useTexture.d.ts +9 -0
- package/dist/src/hooks/useTexture.js +14 -0
- package/dist/src/hooks/useTickerCallback.d.ts +5 -0
- package/dist/src/hooks/useTickerCallback.js +14 -0
- package/dist/src/hooks/useTilingSprite.d.ts +6 -0
- package/dist/src/hooks/useTilingSprite.js +26 -0
- package/dist/src/hooks/useWorld.d.ts +1 -0
- package/dist/src/hooks/useWorld.js +5 -0
- package/dist/src/index.d.ts +10 -0
- package/dist/src/index.js +10 -0
- package/dist/src/layer/Layer.context.d.ts +6 -0
- package/dist/src/layer/Layer.context.js +2 -0
- package/dist/src/layer/Layer.d.ts +6 -0
- package/dist/src/layer/Layer.js +34 -0
- package/dist/src/layer/index.d.ts +1 -0
- package/dist/src/layer/index.js +1 -0
- package/dist/src/physics/MatterPhysics.context.d.ts +8 -0
- package/dist/src/physics/MatterPhysics.context.js +74 -0
- package/dist/src/physics/index.d.ts +4 -0
- package/dist/src/physics/index.js +4 -0
- package/dist/src/physics/types.d.ts +24 -0
- package/dist/src/physics/types.js +1 -0
- package/dist/src/physics/useCollisionEventHandler.d.ts +7 -0
- package/dist/src/physics/useCollisionEventHandler.js +15 -0
- package/dist/src/physics/usePhysicsEngineEventHandler.d.ts +8 -0
- package/dist/src/physics/usePhysicsEngineEventHandler.js +15 -0
- package/dist/src/physics/usePhysicsTickerCallback.d.ts +7 -0
- package/dist/src/physics/usePhysicsTickerCallback.js +15 -0
- package/dist/src/stage/Stage.context.d.ts +6 -0
- package/dist/src/stage/Stage.context.js +2 -0
- package/dist/src/stage/Stage.d.ts +2 -0
- package/dist/src/stage/Stage.js +29 -0
- package/dist/src/stage/index.d.ts +1 -0
- package/dist/src/stage/index.js +1 -0
- package/dist/src/types.d.ts +7 -0
- package/dist/src/types.js +7 -0
- package/dist/src/world/World.context.d.ts +10 -0
- package/dist/src/world/World.context.js +2 -0
- package/dist/src/world/World.d.ts +11 -0
- package/dist/src/world/World.js +66 -0
- package/dist/src/world/index.d.ts +2 -0
- package/dist/src/world/index.js +2 -0
- package/package.json +52 -0
- package/src/assets-manager/AssetsManager.context.tsx +23 -0
- package/src/assets-manager/AssetsManager.tsx +54 -0
- package/src/assets-manager/index.ts +3 -0
- package/src/assets-manager/useAssetManager.ts +7 -0
- package/src/camera/Camera.context.tsx +17 -0
- package/src/camera/Camera.tsx +153 -0
- package/src/camera/index.ts +2 -0
- package/src/game-context/Game.context.tsx +133 -0
- package/src/game-context/index.ts +1 -0
- package/src/game-objects/GameObjectPhysicalObjectConfig.ts +18 -0
- package/src/game-objects/index.ts +2 -0
- package/src/game-objects/usePhysicalObject.ts +24 -0
- package/src/game-objects/usePhysicalObjectFromConfig.ts +22 -0
- package/src/game-objects/useWalls.ts +93 -0
- package/src/hooks/index.ts +12 -0
- package/src/hooks/useAnimatedSprite.ts +65 -0
- package/src/hooks/useCamera.ts +6 -0
- package/src/hooks/useCollisionDetection.ts +35 -0
- package/src/hooks/useGame.ts +6 -0
- package/src/hooks/useGlobalEventHandler.ts +27 -0
- package/src/hooks/useLayerContext.ts +6 -0
- package/src/hooks/useObject.ts +81 -0
- package/src/hooks/useSprite.ts +33 -0
- package/src/hooks/useStage.ts +7 -0
- package/src/hooks/useTexture.ts +22 -0
- package/src/hooks/useTickerCallback.ts +25 -0
- package/src/hooks/useTilingSprite.ts +39 -0
- package/src/hooks/useWorld.ts +7 -0
- package/src/index.ts +10 -0
- package/src/layer/Layer.context.tsx +9 -0
- package/src/layer/Layer.tsx +57 -0
- package/src/layer/index.ts +1 -0
- package/src/physics/MatterPhysics.context.tsx +100 -0
- package/src/physics/index.ts +4 -0
- package/src/physics/types.ts +27 -0
- package/src/physics/useCollisionEventHandler.ts +26 -0
- package/src/physics/usePhysicsEngineEventHandler.ts +30 -0
- package/src/physics/usePhysicsTickerCallback.ts +25 -0
- package/src/stage/Stage.context.tsx +9 -0
- package/src/stage/Stage.tsx +50 -0
- package/src/stage/index.ts +1 -0
- package/src/types.ts +8 -0
- package/src/world/World.context.tsx +13 -0
- package/src/world/World.tsx +93 -0
- package/src/world/index.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014 Blake Embrey (hello@blakeembrey.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
[](https://github.com/laverve/fusion/actions/workflows/main.yaml)
|
|
2
|
+
|
|
3
|
+
# pixi-fusion - The lightweight Game Engine
|
|
4
|
+
|
|
5
|
+
This project aims to provide a frwmework for games built on top of [PixiJS](https://pixijs.com/) and [matter-js](https://brm.io/matter-js/).
|
|
6
|
+
|
|
7
|
+
It is designed to be used with [React](https://react.dev/) framework v18.2.0 or above.
|
|
8
|
+
|
|
9
|
+
# Installation
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm i --save pixi-fusion
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
# Basic Usage Example
|
|
16
|
+
|
|
17
|
+
1. Create a component with `GameContextProvider` and `World`.
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
<GameContextProvider timeout={10}>
|
|
21
|
+
<World size={{width: 300, height: 300}} eventMode="dynamic">
|
|
22
|
+
<layer>
|
|
23
|
+
<MyAwesomeStage />
|
|
24
|
+
</Layer>
|
|
25
|
+
</World>
|
|
26
|
+
</GameContextProvider>
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
2. Create a component `MyAwesomeStage`.
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
export const MyAwesomeStage: React.FC<MazeHeroProps> = () => {
|
|
33
|
+
const { status } = useGame();
|
|
34
|
+
const displayObjectConfig = useMemo(
|
|
35
|
+
() => ({
|
|
36
|
+
type: DisplayObjectType.SPRITE,
|
|
37
|
+
url: "https://image_url",
|
|
38
|
+
anchor: { x: 0.5, y: 0.5 },
|
|
39
|
+
}),
|
|
40
|
+
[]
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const [position, setPosition] = useState({x: 0, y: 0});
|
|
44
|
+
|
|
45
|
+
useDisplayObject({
|
|
46
|
+
...displayObjectConfig,
|
|
47
|
+
position: position
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const onKeyDown = useCallback(
|
|
51
|
+
(e: KeyboardEvent) => {
|
|
52
|
+
switch (e.key) {
|
|
53
|
+
case "ArrowUp":
|
|
54
|
+
setPosition({x: position.x, y: position.y - 1})
|
|
55
|
+
break;
|
|
56
|
+
case "ArrowDown":
|
|
57
|
+
setPosition({x: position.x, y: position.y + 1})
|
|
58
|
+
break;
|
|
59
|
+
case "ArrowRight":
|
|
60
|
+
setPosition({x: position.x + 1, y: position.y})
|
|
61
|
+
setDirection(90);
|
|
62
|
+
break;
|
|
63
|
+
case "ArrowLeft":
|
|
64
|
+
setPosition({x: position.x - 1, y: position.y})
|
|
65
|
+
break;
|
|
66
|
+
default:
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
[position]
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
useGlobalEventHandler({ event: "keydown", callback: onKeyDown, isEnabled: status === GameStatus.IN_PROGRESS });
|
|
74
|
+
|
|
75
|
+
return null;
|
|
76
|
+
};
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
# Demos
|
|
80
|
+
|
|
81
|
+
- [Maze](https://laverve.github.io/fusion/?path=/story/games-maze-gameplay--maze-story)
|
|
82
|
+
- [WordSearch](https://laverve.github.io/fusion/?path=/story/games-wordsearch-gameplay--word-search-story)
|
|
83
|
+
- [Spelling spree](https://laverve.github.io/fusion/?path=/story/games-spelling-spree-gameplay--spelling-spree)
|
|
84
|
+
|
|
85
|
+
# Contribution guidelines
|
|
86
|
+
|
|
87
|
+
You are encouraged to contribute to this project as soon as you see any defects or issues.
|
|
88
|
+
|
|
89
|
+
## Code Editor Configuration
|
|
90
|
+
|
|
91
|
+
This monorepo contains configuration for VSCode editor. It doesn't mean that you cannot use a different tool. Feel free to push configurations for your favourite code editor.
|
|
92
|
+
|
|
93
|
+
## Commands
|
|
94
|
+
|
|
95
|
+
1. To run tests use: `npm run test`
|
|
96
|
+
2. To build monorepo use: `npm run build`
|
|
97
|
+
3. To lint monorepo use: `npm run lint`
|
|
98
|
+
4. To initialize pre-commit hooks use: `npm run prepare`
|
|
99
|
+
5. To run storybook `npm run storybook`
|
|
100
|
+
|
|
101
|
+
## Workflow
|
|
102
|
+
|
|
103
|
+
Before you get started make sure that there is noone working on an issue you are going to address.
|
|
104
|
+
|
|
105
|
+
As a first step, clone the repository and run `npm i && npm run prepare` command to install all dependencies and initialize pre-commit hooks.
|
|
106
|
+
|
|
107
|
+
Create a branch to work on your code changes.
|
|
108
|
+
|
|
109
|
+
Once you are done with coding, create pull request and get an approval.
|
|
110
|
+
|
|
111
|
+
Lastly, enjoy your changes.
|
|
112
|
+
|
|
113
|
+
# License
|
|
114
|
+
|
|
115
|
+
This content is released under the ([http://opensource.org/licenses/MIT](http://opensource.org/licenses/MIT)) MIT License.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Texture } from "pixi.js";
|
|
2
|
+
export type Asset = {
|
|
3
|
+
src: string;
|
|
4
|
+
alias: string;
|
|
5
|
+
};
|
|
6
|
+
export type AssetsManagerContextValue = {
|
|
7
|
+
isFetching: boolean;
|
|
8
|
+
isFetched: boolean;
|
|
9
|
+
isError: boolean;
|
|
10
|
+
error?: unknown;
|
|
11
|
+
load: (groupId: string, asset: Asset[]) => Promise<void>;
|
|
12
|
+
unload: (groupIdOrAlias: string) => void;
|
|
13
|
+
getAsset: (alias: string) => Texture | undefined;
|
|
14
|
+
};
|
|
15
|
+
export declare const AssetsManagerContext: import("react").Context<AssetsManagerContextValue>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
+
import { AssetsManagerContext } from "./AssetsManager.context";
|
|
3
|
+
import { Assets } from "pixi.js";
|
|
4
|
+
export const AssetsManager = ({ children }) => {
|
|
5
|
+
const [isFetching, setIsFetching] = useState(false);
|
|
6
|
+
const [isFetched, setIsFetched] = useState(false);
|
|
7
|
+
const [isError, setIsError] = useState(false);
|
|
8
|
+
const [error, setError] = useState();
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
Assets.cache.reset();
|
|
11
|
+
}, []);
|
|
12
|
+
const load = useCallback(async (groupId, localAsset) => {
|
|
13
|
+
try {
|
|
14
|
+
Assets.addBundle(groupId, localAsset);
|
|
15
|
+
await Assets.loadBundle(groupId);
|
|
16
|
+
setIsFetched(true);
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
setIsError(true);
|
|
20
|
+
setIsFetched(false);
|
|
21
|
+
setError(e);
|
|
22
|
+
}
|
|
23
|
+
finally {
|
|
24
|
+
setIsFetching(false);
|
|
25
|
+
}
|
|
26
|
+
}, [isFetching, isError, isFetched, error]);
|
|
27
|
+
const unload = useCallback((groupId) => {
|
|
28
|
+
Assets.unloadBundle(groupId);
|
|
29
|
+
}, []);
|
|
30
|
+
const getAsset = useCallback((alias) => {
|
|
31
|
+
return Assets.get(alias);
|
|
32
|
+
}, []);
|
|
33
|
+
const conextValue = useMemo(() => ({
|
|
34
|
+
load,
|
|
35
|
+
unload,
|
|
36
|
+
getAsset,
|
|
37
|
+
isFetching,
|
|
38
|
+
isFetched,
|
|
39
|
+
isError,
|
|
40
|
+
error
|
|
41
|
+
}), [load, unload, getAsset, isFetching, isFetched, isError, error]);
|
|
42
|
+
return React.createElement(AssetsManagerContext.Provider, { value: conextValue }, children);
|
|
43
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useAssetManager: () => import("./AssetsManager.context").AssetsManagerContextValue;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Container } from "pixi.js";
|
|
2
|
+
export type EnsureVisibleOptions = {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
resizeToFit?: boolean;
|
|
8
|
+
};
|
|
9
|
+
export type CameraContextValue = {
|
|
10
|
+
follow: (object: Container) => void;
|
|
11
|
+
ensureVisible: (options: EnsureVisibleOptions) => void;
|
|
12
|
+
};
|
|
13
|
+
export declare const CameraContext: import("react").Context<CameraContextValue>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import React, { PropsWithChildren } from "react";
|
|
2
|
+
import { Viewport as ViewportContainer } from "pixi-viewport";
|
|
3
|
+
type CameraProps = {
|
|
4
|
+
clampZoom?: Parameters<ViewportContainer["clampZoom"]>[0];
|
|
5
|
+
};
|
|
6
|
+
export declare const Camera: React.FC<CameraProps & PropsWithChildren>;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
+
import { Viewport as ViewportContainer } from "pixi-viewport";
|
|
3
|
+
import { Layer } from "../layer";
|
|
4
|
+
import { useStage, useWorld } from "../hooks";
|
|
5
|
+
import { LayerContext } from "../layer/Layer.context";
|
|
6
|
+
import { CameraContext } from "./Camera.context";
|
|
7
|
+
export const Camera = ({ children, clampZoom }) => {
|
|
8
|
+
const [isReady, setIsReady] = useState(false);
|
|
9
|
+
const [followContainer, setFollowContainer] = useState(null);
|
|
10
|
+
const [ensureVisibleOptions, setEnsureVisibleOptions] = useState(null);
|
|
11
|
+
const { application, isInitialized, size } = useWorld();
|
|
12
|
+
const { addObject: addObjectToStage, removeObject: removeObjectFromStage } = useStage();
|
|
13
|
+
const [things, setThings] = useState([]);
|
|
14
|
+
const viewport = useMemo(() => isInitialized
|
|
15
|
+
? new ViewportContainer({
|
|
16
|
+
worldHeight: size.height,
|
|
17
|
+
worldWidth: size.width,
|
|
18
|
+
screenHeight: application.canvas.height,
|
|
19
|
+
screenWidth: application.canvas.width,
|
|
20
|
+
events: application?.renderer?.events
|
|
21
|
+
})
|
|
22
|
+
: null, [size, application, isInitialized]);
|
|
23
|
+
const addObject = useCallback((thing) => {
|
|
24
|
+
setThings((oldThings) => [...oldThings, thing]);
|
|
25
|
+
}, [things]);
|
|
26
|
+
const removeObject = useCallback((thing) => {
|
|
27
|
+
setThings((oldThings) => oldThings.filter(({ uid }) => uid === thing.uid));
|
|
28
|
+
}, [application]);
|
|
29
|
+
const conextValue = useMemo(() => ({
|
|
30
|
+
addObject,
|
|
31
|
+
removeObject
|
|
32
|
+
}), [addObject, removeObject]);
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
if (viewport && clampZoom) {
|
|
35
|
+
viewport.clampZoom(clampZoom);
|
|
36
|
+
}
|
|
37
|
+
}, [clampZoom, isInitialized]);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
if (viewport) {
|
|
40
|
+
if (size.width) {
|
|
41
|
+
viewport.width = size.width;
|
|
42
|
+
}
|
|
43
|
+
if (size.height) {
|
|
44
|
+
viewport.height = size.height;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}, [size, viewport?.uid, isInitialized]);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (!isInitialized || !viewport) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
viewport?.moveCenter(0, 0);
|
|
53
|
+
}, [viewport, size, isInitialized]);
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
if (!viewport) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
addObjectToStage(viewport);
|
|
59
|
+
setIsReady(true);
|
|
60
|
+
return () => {
|
|
61
|
+
removeObjectFromStage(viewport);
|
|
62
|
+
setIsReady(false);
|
|
63
|
+
};
|
|
64
|
+
}, [application, isInitialized]);
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (!viewport || !isReady) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
viewport.addChild(...things);
|
|
70
|
+
return () => {
|
|
71
|
+
viewport.removeChild(...things);
|
|
72
|
+
};
|
|
73
|
+
}, [things, viewport, isReady]);
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
if (isReady && viewport && followContainer) {
|
|
76
|
+
viewport.follow(followContainer, { radius: 300 });
|
|
77
|
+
}
|
|
78
|
+
}, [isReady, viewport, followContainer]);
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
if (isReady && viewport && ensureVisibleOptions) {
|
|
81
|
+
viewport.ensureVisible(ensureVisibleOptions.x, ensureVisibleOptions.y, ensureVisibleOptions.width, ensureVisibleOptions.height, ensureVisibleOptions.resizeToFit);
|
|
82
|
+
}
|
|
83
|
+
}, [isReady, viewport, ensureVisibleOptions]);
|
|
84
|
+
const followCallback = useCallback((container) => {
|
|
85
|
+
setFollowContainer(container);
|
|
86
|
+
}, [followContainer]);
|
|
87
|
+
const ensureVisibleCallback = useCallback((options) => {
|
|
88
|
+
setEnsureVisibleOptions(options);
|
|
89
|
+
}, [followContainer]);
|
|
90
|
+
const value = useMemo(() => {
|
|
91
|
+
return {
|
|
92
|
+
follow: followCallback,
|
|
93
|
+
ensureVisible: ensureVisibleCallback
|
|
94
|
+
};
|
|
95
|
+
}, [followCallback]);
|
|
96
|
+
return (React.createElement(CameraContext.Provider, { value: value },
|
|
97
|
+
React.createElement(LayerContext.Provider, { value: conextValue },
|
|
98
|
+
React.createElement(Layer, null, children))));
|
|
99
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import React, { PropsWithChildren } from "react";
|
|
2
|
+
import { GameStatus } from "../types";
|
|
3
|
+
export type GameContextValue = {
|
|
4
|
+
readonly reset: () => void;
|
|
5
|
+
readonly start: () => void;
|
|
6
|
+
readonly stop: () => void;
|
|
7
|
+
readonly timeout: number;
|
|
8
|
+
readonly status: GameStatus;
|
|
9
|
+
readonly startTime: number | null;
|
|
10
|
+
readonly endTime: number | null;
|
|
11
|
+
};
|
|
12
|
+
export type GameContextProviderEvents = {
|
|
13
|
+
onStart?: (event: {
|
|
14
|
+
startTime: number | null;
|
|
15
|
+
status: GameStatus;
|
|
16
|
+
endTime: number | null;
|
|
17
|
+
}) => unknown;
|
|
18
|
+
onStop?: (event: {
|
|
19
|
+
startTime: number | null;
|
|
20
|
+
status: GameStatus;
|
|
21
|
+
endTime: number | null;
|
|
22
|
+
}) => unknown;
|
|
23
|
+
onTimedOut?: (event: {
|
|
24
|
+
startTime: number | null;
|
|
25
|
+
status: GameStatus;
|
|
26
|
+
endTime: number | null;
|
|
27
|
+
}) => unknown;
|
|
28
|
+
onReset?: (event: {
|
|
29
|
+
startTime: number | null;
|
|
30
|
+
status: GameStatus;
|
|
31
|
+
endTime: number | null;
|
|
32
|
+
}) => unknown;
|
|
33
|
+
};
|
|
34
|
+
export declare const GameContext: React.Context<GameContextValue>;
|
|
35
|
+
export type GameContextProviderProps = PropsWithChildren & {
|
|
36
|
+
timeout?: number;
|
|
37
|
+
events?: GameContextProviderEvents;
|
|
38
|
+
};
|
|
39
|
+
export declare const GameContextProvider: React.FC<GameContextProviderProps>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import React, { createContext, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { GameStatus } from "../types";
|
|
3
|
+
export const GameContext = createContext({
|
|
4
|
+
reset: () => { },
|
|
5
|
+
start: () => { },
|
|
6
|
+
stop: () => { },
|
|
7
|
+
timeout: 0,
|
|
8
|
+
status: GameStatus.READY,
|
|
9
|
+
startTime: null,
|
|
10
|
+
endTime: null
|
|
11
|
+
});
|
|
12
|
+
export const GameContextProvider = ({ children, timeout: inputTimeout = 0, events }) => {
|
|
13
|
+
const timerRef = useRef();
|
|
14
|
+
const timeout = Math.max(0, inputTimeout);
|
|
15
|
+
const [status, setStatus] = useState(GameStatus.READY);
|
|
16
|
+
const [startTime, setStartTime] = useState(null);
|
|
17
|
+
const [endTime, setEndTime] = useState(null);
|
|
18
|
+
const onTimedOut = () => {
|
|
19
|
+
const newEndTime = +new Date();
|
|
20
|
+
const newStatus = GameStatus.TIMEDOUT;
|
|
21
|
+
setEndTime(newEndTime);
|
|
22
|
+
setStatus(GameStatus.TIMEDOUT);
|
|
23
|
+
events?.onTimedOut?.({
|
|
24
|
+
startTime,
|
|
25
|
+
endTime: newEndTime,
|
|
26
|
+
status: newStatus
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
const start = () => {
|
|
30
|
+
const newStartTime = +new Date();
|
|
31
|
+
const newStatus = GameStatus.IN_PROGRESS;
|
|
32
|
+
const newEndTime = null;
|
|
33
|
+
setStatus(newStatus);
|
|
34
|
+
setStartTime(newStartTime);
|
|
35
|
+
setEndTime(newEndTime);
|
|
36
|
+
if (timeout !== 0) {
|
|
37
|
+
timerRef.current = setTimeout(() => {
|
|
38
|
+
onTimedOut();
|
|
39
|
+
}, timeout * 1000);
|
|
40
|
+
}
|
|
41
|
+
events?.onStart?.({
|
|
42
|
+
startTime: newStartTime,
|
|
43
|
+
endTime: newEndTime,
|
|
44
|
+
status: newStatus
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
const reset = () => {
|
|
48
|
+
const newStartTime = null;
|
|
49
|
+
const newStatus = GameStatus.READY;
|
|
50
|
+
const newEndTime = null;
|
|
51
|
+
setStatus(newStatus);
|
|
52
|
+
setStartTime(newStartTime);
|
|
53
|
+
setEndTime(newEndTime);
|
|
54
|
+
if (timerRef.current) {
|
|
55
|
+
clearTimeout(timerRef.current);
|
|
56
|
+
}
|
|
57
|
+
events?.onReset?.({
|
|
58
|
+
startTime: newStartTime,
|
|
59
|
+
endTime: newEndTime,
|
|
60
|
+
status: newStatus
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
const stop = () => {
|
|
64
|
+
const newStatus = GameStatus.COMPLETED;
|
|
65
|
+
const newEndTime = +new Date();
|
|
66
|
+
clearTimeout(timerRef.current);
|
|
67
|
+
setEndTime(+new Date());
|
|
68
|
+
setStatus(GameStatus.COMPLETED);
|
|
69
|
+
events?.onStop?.({
|
|
70
|
+
startTime,
|
|
71
|
+
endTime: newEndTime,
|
|
72
|
+
status: newStatus
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
const contextValue = useMemo(() => ({
|
|
76
|
+
status,
|
|
77
|
+
startTime,
|
|
78
|
+
endTime,
|
|
79
|
+
timeout,
|
|
80
|
+
stop,
|
|
81
|
+
start,
|
|
82
|
+
reset
|
|
83
|
+
}), [timeout, status, startTime, endTime]);
|
|
84
|
+
return React.createElement(GameContext.Provider, { value: contextValue }, children);
|
|
85
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Game.context";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Game.context";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { IBodyDefinition } from "matter-js";
|
|
2
|
+
type PhysicalObjectConfig = Pick<IBodyDefinition, "vertices" | "label" | "inertia" | "position" | "friction" | "frictionAir" | "frictionStatic" | "parts" | "isStatic" | "isSensor" | "collisionFilter">;
|
|
3
|
+
export type GameObjectPhysicalObjectConfig = PhysicalObjectConfig;
|
|
4
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useContext, useEffect } from "react";
|
|
2
|
+
import { MatterPhysicsContext } from "../physics";
|
|
3
|
+
export const usePhysicalObject = ({ physicalObject }) => {
|
|
4
|
+
const { addBody, removeBody } = useContext(MatterPhysicsContext);
|
|
5
|
+
useEffect(() => {
|
|
6
|
+
if (!physicalObject) {
|
|
7
|
+
return () => { };
|
|
8
|
+
}
|
|
9
|
+
addBody(physicalObject);
|
|
10
|
+
return () => {
|
|
11
|
+
removeBody(physicalObject);
|
|
12
|
+
};
|
|
13
|
+
}, [physicalObject?.id]);
|
|
14
|
+
return {
|
|
15
|
+
physicalObject
|
|
16
|
+
};
|
|
17
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Body } from "matter-js";
|
|
2
|
+
import { GameObjectPhysicalObjectConfig } from "./GameObjectPhysicalObjectConfig";
|
|
3
|
+
import { Nullable } from "../types";
|
|
4
|
+
export declare const usePhysicalObjectFromConfig: <PhysicalObjectType extends Nullable<Body> = Body>({ position, ...physicalObjectConfig }: GameObjectPhysicalObjectConfig) => {
|
|
5
|
+
physicalObject: PhysicalObjectType;
|
|
6
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Body } from "matter-js";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
import { usePhysicalObject } from "./usePhysicalObject";
|
|
4
|
+
export const usePhysicalObjectFromConfig = ({ position = { x: 0, y: 0 }, ...physicalObjectConfig }) => {
|
|
5
|
+
const physicalObject = useMemo(() => {
|
|
6
|
+
if (physicalObjectConfig) {
|
|
7
|
+
return Body.create({ position, ...physicalObjectConfig });
|
|
8
|
+
}
|
|
9
|
+
return null;
|
|
10
|
+
}, []);
|
|
11
|
+
usePhysicalObject({ physicalObject });
|
|
12
|
+
return { physicalObject };
|
|
13
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type WallOptions = {
|
|
2
|
+
thikness: number;
|
|
3
|
+
};
|
|
4
|
+
type UseWallsOptions = {
|
|
5
|
+
left?: Partial<WallOptions> | boolean;
|
|
6
|
+
right?: Partial<WallOptions> | boolean;
|
|
7
|
+
top?: Partial<WallOptions> | boolean;
|
|
8
|
+
bottom?: Partial<WallOptions> | boolean;
|
|
9
|
+
};
|
|
10
|
+
export declare const useWalls: ({ left, right, top, bottom }?: UseWallsOptions) => void;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { useContext, useEffect, useMemo } from "react";
|
|
2
|
+
import { Bodies, Composite } from "matter-js";
|
|
3
|
+
import { MatterPhysicsContext } from "../physics/MatterPhysics.context";
|
|
4
|
+
const DEFAULT_WALL_CONFIG = {
|
|
5
|
+
thikness: 10
|
|
6
|
+
};
|
|
7
|
+
export const useWalls = ({ left = true, right = true, top = true, bottom = true } = {}) => {
|
|
8
|
+
const { config } = useContext(MatterPhysicsContext);
|
|
9
|
+
const { height, width } = config.world;
|
|
10
|
+
const center = {
|
|
11
|
+
x: width / 2,
|
|
12
|
+
y: height / 2
|
|
13
|
+
};
|
|
14
|
+
const { addBody, removeBody } = useContext(MatterPhysicsContext);
|
|
15
|
+
const body = useMemo(() => {
|
|
16
|
+
const walls = Composite.create();
|
|
17
|
+
const parts = [];
|
|
18
|
+
if (left) {
|
|
19
|
+
const leftWallConfig = Object.assign(DEFAULT_WALL_CONFIG, typeof left === "boolean" ? {} : { ...left });
|
|
20
|
+
const leftWallPosition = { x: 0, y: center.y };
|
|
21
|
+
parts.push(Bodies.rectangle(leftWallPosition.x, leftWallPosition.y, leftWallConfig.thikness, height, {
|
|
22
|
+
isStatic: true,
|
|
23
|
+
label: "left-wall"
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
if (right) {
|
|
27
|
+
const rightWallConfig = Object.assign(DEFAULT_WALL_CONFIG, typeof right === "boolean" ? {} : { ...right });
|
|
28
|
+
const rightWallPosition = { x: width, y: center.y };
|
|
29
|
+
parts.push(Bodies.rectangle(rightWallPosition.x, rightWallPosition.y, rightWallConfig.thikness, height, {
|
|
30
|
+
isStatic: true,
|
|
31
|
+
label: "right-wall"
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
if (top) {
|
|
35
|
+
const topWallConfig = Object.assign(DEFAULT_WALL_CONFIG, typeof top === "boolean" ? {} : { ...top });
|
|
36
|
+
const topWallPosition = { x: center.x, y: 0 };
|
|
37
|
+
parts.push(Bodies.rectangle(topWallPosition.x, topWallPosition.y, width, topWallConfig.thikness, {
|
|
38
|
+
isStatic: true,
|
|
39
|
+
label: "top-wall"
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
if (bottom) {
|
|
43
|
+
const bottomWallConfig = Object.assign(DEFAULT_WALL_CONFIG, typeof bottom === "boolean" ? {} : { ...bottom });
|
|
44
|
+
const bottomWallPosition = { x: center.x, y: height };
|
|
45
|
+
parts.push(Bodies.rectangle(bottomWallPosition.x, bottomWallPosition.y, width, bottomWallConfig.thikness, {
|
|
46
|
+
isStatic: true,
|
|
47
|
+
label: "bottom-wall"
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
Composite.add(walls, parts);
|
|
51
|
+
return walls;
|
|
52
|
+
}, [width, height]);
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
addBody(body);
|
|
55
|
+
return () => {
|
|
56
|
+
removeBody(body);
|
|
57
|
+
};
|
|
58
|
+
}, [body.id]);
|
|
59
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from "./useGlobalEventHandler";
|
|
2
|
+
export * from "./useStage";
|
|
3
|
+
export * from "./useTickerCallback";
|
|
4
|
+
export * from "./useWorld";
|
|
5
|
+
export * from "./useLayerContext";
|
|
6
|
+
export * from "./useSprite";
|
|
7
|
+
export * from "./useAnimatedSprite";
|
|
8
|
+
export * from "./useTexture";
|
|
9
|
+
export * from "./useGame";
|
|
10
|
+
export * from "./useTilingSprite";
|
|
11
|
+
export * from "./useCamera";
|
|
12
|
+
export * from "./useCollisionDetection";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from "./useGlobalEventHandler";
|
|
2
|
+
export * from "./useStage";
|
|
3
|
+
export * from "./useTickerCallback";
|
|
4
|
+
export * from "./useWorld";
|
|
5
|
+
export * from "./useLayerContext";
|
|
6
|
+
export * from "./useSprite";
|
|
7
|
+
export * from "./useAnimatedSprite";
|
|
8
|
+
export * from "./useTexture";
|
|
9
|
+
export * from "./useGame";
|
|
10
|
+
export * from "./useTilingSprite";
|
|
11
|
+
export * from "./useCamera";
|
|
12
|
+
export * from "./useCollisionDetection";
|