pixi-fusion 2.0.1 → 2.2.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/dist/src/assets-manager/AssetsManager.js +2 -2
- package/dist/src/camera/Camera.js +8 -8
- package/dist/src/game-context/Game.context.d.ts +1 -0
- package/dist/src/game-context/Game.context.js +17 -13
- package/dist/src/game-objects/usePhysicalObject.js +1 -1
- package/dist/src/game-objects/usePhysicalObjectFromConfig.d.ts +1 -1
- package/dist/src/game-objects/usePhysicalObjectFromConfig.js +1 -1
- package/dist/src/game-objects/useWalls.js +2 -2
- package/dist/src/hooks/useAnimatedSprite.js +4 -4
- package/dist/src/hooks/useCollisionDetection.js +1 -1
- package/dist/src/hooks/useObject.js +10 -10
- package/dist/src/hooks/useSprite.js +1 -1
- package/dist/src/hooks/useTexture.js +1 -1
- package/dist/src/hooks/useTilingSprite.js +2 -2
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.js +0 -1
- package/dist/src/layer/Layer.js +4 -4
- package/dist/src/physics/MatterPhysics.context.js +11 -11
- package/dist/src/physics/useCollisionEventHandler.js +1 -1
- package/dist/src/physics/usePhysicsEngineEventHandler.js +1 -1
- package/dist/src/physics/usePhysicsTickerCallback.js +1 -1
- package/dist/src/stage/Stage.js +3 -3
- package/dist/src/world/World.js +31 -34
- package/package.json +27 -14
- package/src/assets-manager/AssetsManager.tsx +14 -17
- package/src/camera/Camera.tsx +16 -28
- package/src/game-context/Game.context.tsx +18 -13
- package/src/game-objects/usePhysicalObject.ts +1 -1
- package/src/game-objects/usePhysicalObjectFromConfig.ts +2 -2
- package/src/game-objects/useWalls.ts +2 -2
- package/src/hooks/useAnimatedSprite.ts +4 -4
- package/src/hooks/useCollisionDetection.ts +1 -1
- package/src/hooks/useObject.ts +10 -10
- package/src/hooks/useSprite.ts +1 -1
- package/src/hooks/useTexture.ts +1 -1
- package/src/hooks/useTilingSprite.ts +2 -2
- package/src/index.ts +0 -1
- package/src/layer/Layer.tsx +4 -4
- package/src/physics/MatterPhysics.context.tsx +23 -17
- package/src/physics/useCollisionEventHandler.ts +1 -1
- package/src/physics/usePhysicsEngineEventHandler.ts +1 -1
- package/src/physics/usePhysicsTickerCallback.ts +1 -1
- package/src/stage/Stage.tsx +7 -13
- package/src/world/World.tsx +37 -37
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
-
import { AssetsManagerContext } from "./AssetsManager.context";
|
|
3
2
|
import { Assets } from "pixi.js";
|
|
3
|
+
import { AssetsManagerContext } from "./AssetsManager.context";
|
|
4
4
|
export const AssetsManager = ({ children }) => {
|
|
5
5
|
const [isFetching, setIsFetching] = useState(false);
|
|
6
6
|
const [isFetched, setIsFetched] = useState(false);
|
|
@@ -23,7 +23,7 @@ export const AssetsManager = ({ children }) => {
|
|
|
23
23
|
finally {
|
|
24
24
|
setIsFetching(false);
|
|
25
25
|
}
|
|
26
|
-
}, [
|
|
26
|
+
}, []);
|
|
27
27
|
const unload = useCallback((groupId) => {
|
|
28
28
|
Assets.unloadBundle(groupId);
|
|
29
29
|
}, []);
|
|
@@ -22,10 +22,10 @@ export const Camera = ({ children, clampZoom }) => {
|
|
|
22
22
|
: null, [size, application]);
|
|
23
23
|
const addObject = useCallback((thing) => {
|
|
24
24
|
setThings((oldThings) => [...oldThings, thing]);
|
|
25
|
-
}, [
|
|
25
|
+
}, []);
|
|
26
26
|
const removeObject = useCallback((thing) => {
|
|
27
27
|
setThings((oldThings) => oldThings.filter(({ uid }) => uid === thing.uid));
|
|
28
|
-
}, [
|
|
28
|
+
}, []);
|
|
29
29
|
const conextValue = useMemo(() => ({
|
|
30
30
|
addObject,
|
|
31
31
|
removeObject
|
|
@@ -34,7 +34,7 @@ export const Camera = ({ children, clampZoom }) => {
|
|
|
34
34
|
if (viewport && clampZoom && application) {
|
|
35
35
|
viewport.clampZoom(clampZoom);
|
|
36
36
|
}
|
|
37
|
-
}, [clampZoom]);
|
|
37
|
+
}, [clampZoom, application, viewport]);
|
|
38
38
|
useEffect(() => {
|
|
39
39
|
if (viewport) {
|
|
40
40
|
if (size.width) {
|
|
@@ -44,7 +44,7 @@ export const Camera = ({ children, clampZoom }) => {
|
|
|
44
44
|
viewport.height = size.height;
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
-
}, [size, viewport
|
|
47
|
+
}, [size, viewport, application]);
|
|
48
48
|
useEffect(() => {
|
|
49
49
|
if (!application || !viewport) {
|
|
50
50
|
return;
|
|
@@ -61,7 +61,7 @@ export const Camera = ({ children, clampZoom }) => {
|
|
|
61
61
|
removeObjectFromStage(viewport);
|
|
62
62
|
setIsReady(false);
|
|
63
63
|
};
|
|
64
|
-
}, [viewport
|
|
64
|
+
}, [viewport, addObjectToStage, removeObjectFromStage]);
|
|
65
65
|
useEffect(() => {
|
|
66
66
|
if (!viewport || !isReady) {
|
|
67
67
|
return;
|
|
@@ -83,16 +83,16 @@ export const Camera = ({ children, clampZoom }) => {
|
|
|
83
83
|
}, [isReady, viewport, ensureVisibleOptions]);
|
|
84
84
|
const followCallback = useCallback((container) => {
|
|
85
85
|
setFollowContainer(container);
|
|
86
|
-
}, [
|
|
86
|
+
}, []);
|
|
87
87
|
const ensureVisibleCallback = useCallback((options) => {
|
|
88
88
|
setEnsureVisibleOptions(options);
|
|
89
|
-
}, [
|
|
89
|
+
}, []);
|
|
90
90
|
const value = useMemo(() => {
|
|
91
91
|
return {
|
|
92
92
|
follow: followCallback,
|
|
93
93
|
ensureVisible: ensureVisibleCallback
|
|
94
94
|
};
|
|
95
|
-
}, [followCallback]);
|
|
95
|
+
}, [followCallback, ensureVisibleCallback]);
|
|
96
96
|
return (React.createElement(CameraContext.Provider, { value: value },
|
|
97
97
|
React.createElement(LayerContext.Provider, { value: conextValue },
|
|
98
98
|
React.createElement(Layer, null, children))));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { createContext, useMemo, useRef, useState } from "react";
|
|
1
|
+
import React, { createContext, useCallback, useMemo, useRef, useState } from "react";
|
|
2
2
|
import { GameStatus } from "../types";
|
|
3
3
|
export const GameContext = createContext({
|
|
4
4
|
reset: () => { },
|
|
@@ -6,6 +6,7 @@ export const GameContext = createContext({
|
|
|
6
6
|
start: () => { },
|
|
7
7
|
stop: () => { },
|
|
8
8
|
timeout: 0,
|
|
9
|
+
gameId: 0,
|
|
9
10
|
status: GameStatus.READY,
|
|
10
11
|
startTime: null,
|
|
11
12
|
pauseTime: 0,
|
|
@@ -14,12 +15,13 @@ export const GameContext = createContext({
|
|
|
14
15
|
export const GameContextProvider = ({ children, timeout: inputTimeout = 0, events }) => {
|
|
15
16
|
const timerRef = useRef(null);
|
|
16
17
|
const timeout = Math.max(0, inputTimeout);
|
|
18
|
+
const [gameId, setGemaId] = useState(0);
|
|
17
19
|
const [status, setStatus] = useState(GameStatus.READY);
|
|
18
20
|
const [startTime, setStartTime] = useState(null);
|
|
19
21
|
const [endTime, setEndTime] = useState(null);
|
|
20
22
|
const [pauseTime, setPauseTime] = useState(0);
|
|
21
23
|
const [pausedAtTime, setPausedAtTime] = useState(null);
|
|
22
|
-
const onTimedOut = () => {
|
|
24
|
+
const onTimedOut = useCallback(() => {
|
|
23
25
|
const newEndTime = +new Date();
|
|
24
26
|
const newStatus = GameStatus.TIMEDOUT;
|
|
25
27
|
setEndTime(newEndTime);
|
|
@@ -31,8 +33,8 @@ export const GameContextProvider = ({ children, timeout: inputTimeout = 0, event
|
|
|
31
33
|
pausedAtTime: null,
|
|
32
34
|
pauseTime
|
|
33
35
|
});
|
|
34
|
-
};
|
|
35
|
-
const start = () => {
|
|
36
|
+
}, [events, pauseTime, startTime]);
|
|
37
|
+
const start = useCallback(() => {
|
|
36
38
|
const newStartTime = startTime || +new Date();
|
|
37
39
|
const newStatus = GameStatus.IN_PROGRESS;
|
|
38
40
|
const newEndTime = null;
|
|
@@ -54,8 +56,8 @@ export const GameContextProvider = ({ children, timeout: inputTimeout = 0, event
|
|
|
54
56
|
pausedAtTime: null,
|
|
55
57
|
pauseTime: newPauseTime
|
|
56
58
|
});
|
|
57
|
-
};
|
|
58
|
-
const reset = () => {
|
|
59
|
+
}, [events, onTimedOut, pauseTime, pausedAtTime, startTime, timeout]);
|
|
60
|
+
const reset = useCallback(() => {
|
|
59
61
|
const newStartTime = null;
|
|
60
62
|
const newStatus = GameStatus.READY;
|
|
61
63
|
const newEndTime = null;
|
|
@@ -66,6 +68,7 @@ export const GameContextProvider = ({ children, timeout: inputTimeout = 0, event
|
|
|
66
68
|
setEndTime(newEndTime);
|
|
67
69
|
setPauseTime(newPauseTime);
|
|
68
70
|
setPausedAtTime(newPausedAtTime);
|
|
71
|
+
setGemaId((current) => current++);
|
|
69
72
|
if (timerRef.current) {
|
|
70
73
|
clearTimeout(timerRef.current);
|
|
71
74
|
}
|
|
@@ -76,8 +79,8 @@ export const GameContextProvider = ({ children, timeout: inputTimeout = 0, event
|
|
|
76
79
|
pausedAtTime: newPausedAtTime,
|
|
77
80
|
pauseTime: newPauseTime
|
|
78
81
|
});
|
|
79
|
-
};
|
|
80
|
-
const stop = () => {
|
|
82
|
+
}, [events]);
|
|
83
|
+
const stop = useCallback(() => {
|
|
81
84
|
const newStatus = GameStatus.COMPLETED;
|
|
82
85
|
const newEndTime = +new Date();
|
|
83
86
|
if (timerRef.current) {
|
|
@@ -92,8 +95,8 @@ export const GameContextProvider = ({ children, timeout: inputTimeout = 0, event
|
|
|
92
95
|
pausedAtTime,
|
|
93
96
|
pauseTime
|
|
94
97
|
});
|
|
95
|
-
};
|
|
96
|
-
const pause = () => {
|
|
98
|
+
}, [events, pauseTime, pausedAtTime, startTime]);
|
|
99
|
+
const pause = useCallback(() => {
|
|
97
100
|
const newStatus = GameStatus.PAUSED;
|
|
98
101
|
const newPausedAtTime = +new Date();
|
|
99
102
|
if (timerRef.current) {
|
|
@@ -108,7 +111,7 @@ export const GameContextProvider = ({ children, timeout: inputTimeout = 0, event
|
|
|
108
111
|
status: newStatus,
|
|
109
112
|
pauseTime
|
|
110
113
|
});
|
|
111
|
-
};
|
|
114
|
+
}, [events, pauseTime, startTime]);
|
|
112
115
|
const contextValue = useMemo(() => ({
|
|
113
116
|
status,
|
|
114
117
|
startTime,
|
|
@@ -118,7 +121,8 @@ export const GameContextProvider = ({ children, timeout: inputTimeout = 0, event
|
|
|
118
121
|
stop,
|
|
119
122
|
start,
|
|
120
123
|
reset,
|
|
121
|
-
pause
|
|
122
|
-
|
|
124
|
+
pause,
|
|
125
|
+
gameId
|
|
126
|
+
}), [timeout, status, startTime, endTime, pauseTime, pause, reset, start, stop, gameId]);
|
|
123
127
|
return React.createElement(GameContext.Provider, { value: contextValue }, children);
|
|
124
128
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Body } from "matter-js";
|
|
2
|
-
import { GameObjectPhysicalObjectConfig } from "./GameObjectPhysicalObjectConfig";
|
|
3
2
|
import { Nullable } from "../types";
|
|
3
|
+
import { GameObjectPhysicalObjectConfig } from "./GameObjectPhysicalObjectConfig";
|
|
4
4
|
export declare const usePhysicalObjectFromConfig: <PhysicalObjectType extends Nullable<Body> = Body>({ position, ...physicalObjectConfig }: GameObjectPhysicalObjectConfig) => {
|
|
5
5
|
physicalObject: PhysicalObjectType;
|
|
6
6
|
};
|
|
@@ -7,7 +7,7 @@ export const usePhysicalObjectFromConfig = ({ position = { x: 0, y: 0 }, ...phys
|
|
|
7
7
|
return Body.create({ position, ...physicalObjectConfig });
|
|
8
8
|
}
|
|
9
9
|
return null;
|
|
10
|
-
}, []);
|
|
10
|
+
}, [physicalObjectConfig, position]);
|
|
11
11
|
usePhysicalObject({ physicalObject });
|
|
12
12
|
return { physicalObject };
|
|
13
13
|
};
|
|
@@ -49,11 +49,11 @@ export const useWalls = ({ left = true, right = true, top = true, bottom = true
|
|
|
49
49
|
}
|
|
50
50
|
Composite.add(walls, parts);
|
|
51
51
|
return walls;
|
|
52
|
-
}, [width, height]);
|
|
52
|
+
}, [width, height, bottom, center.x, center.y, left, right, top]);
|
|
53
53
|
useEffect(() => {
|
|
54
54
|
addBody(body);
|
|
55
55
|
return () => {
|
|
56
56
|
removeBody(body);
|
|
57
57
|
};
|
|
58
|
-
}, [body
|
|
58
|
+
}, [body, addBody, removeBody]);
|
|
59
59
|
};
|
|
@@ -14,19 +14,19 @@ export const useAnimatedSprite = ({ texture, spritesheet: spritesheetJSON, anima
|
|
|
14
14
|
return sheet;
|
|
15
15
|
}
|
|
16
16
|
return null;
|
|
17
|
-
}, [spritesheetJSON]);
|
|
17
|
+
}, [spritesheetJSON, isFetched, textures]);
|
|
18
18
|
const sprite = useMemo(() => {
|
|
19
19
|
if (spritesheet?.animations?.[animation]) {
|
|
20
20
|
return new AnimatedSprite({ textures: spritesheet?.animations?.[animation] });
|
|
21
21
|
}
|
|
22
22
|
return null;
|
|
23
|
-
}, [
|
|
23
|
+
}, [animation, spritesheet?.animations]);
|
|
24
24
|
useObject({ object: sprite, ...options });
|
|
25
25
|
useEffect(() => {
|
|
26
26
|
if (sprite) {
|
|
27
27
|
sprite.animationSpeed = animationSpeed;
|
|
28
28
|
}
|
|
29
|
-
}, [animationSpeed, sprite
|
|
29
|
+
}, [animationSpeed, sprite]);
|
|
30
30
|
useEffect(() => {
|
|
31
31
|
if (sprite) {
|
|
32
32
|
if (isPlaying) {
|
|
@@ -36,6 +36,6 @@ export const useAnimatedSprite = ({ texture, spritesheet: spritesheetJSON, anima
|
|
|
36
36
|
sprite.stop();
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
-
}, [isPlaying, sprite
|
|
39
|
+
}, [isPlaying, sprite]);
|
|
40
40
|
return sprite;
|
|
41
41
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { useTickerCallback } from "./useTickerCallback";
|
|
2
1
|
import { useCallback } from "react";
|
|
2
|
+
import { useTickerCallback } from "./useTickerCallback";
|
|
3
3
|
export const useCollisionDetection = ({ objectA, objectB, onCollide, isEnabled }) => {
|
|
4
4
|
const testForAABB = useCallback(() => {
|
|
5
5
|
if (!objectA || !objectB) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { useLayerContext } from "./useLayerContext";
|
|
2
1
|
import { useEffect } from "react";
|
|
2
|
+
import { useLayerContext } from "./useLayerContext";
|
|
3
3
|
export const useObject = ({ object, anchor, position, skew, scale, width, angle, alpha, visible = true }) => {
|
|
4
4
|
const { addObject, removeObject } = useLayerContext();
|
|
5
5
|
useEffect(() => {
|
|
@@ -10,45 +10,45 @@ export const useObject = ({ object, anchor, position, skew, scale, width, angle,
|
|
|
10
10
|
return () => {
|
|
11
11
|
removeObject(object);
|
|
12
12
|
};
|
|
13
|
-
}, [object
|
|
13
|
+
}, [object, addObject, removeObject]);
|
|
14
14
|
useEffect(() => {
|
|
15
15
|
if (anchor !== undefined && object) {
|
|
16
16
|
object.anchor = anchor;
|
|
17
17
|
}
|
|
18
|
-
}, [anchor, object
|
|
18
|
+
}, [anchor, object]);
|
|
19
19
|
useEffect(() => {
|
|
20
20
|
if (angle !== undefined && object) {
|
|
21
21
|
object.angle = angle;
|
|
22
22
|
}
|
|
23
|
-
}, [angle, object
|
|
23
|
+
}, [angle, object]);
|
|
24
24
|
useEffect(() => {
|
|
25
25
|
if (object && position !== undefined) {
|
|
26
26
|
object.position = position;
|
|
27
27
|
}
|
|
28
|
-
}, [position, object
|
|
28
|
+
}, [position, object]);
|
|
29
29
|
useEffect(() => {
|
|
30
30
|
if (object && skew !== undefined) {
|
|
31
31
|
object.skew = skew;
|
|
32
32
|
}
|
|
33
|
-
}, [skew, object
|
|
33
|
+
}, [skew, object]);
|
|
34
34
|
useEffect(() => {
|
|
35
35
|
if (object && alpha !== undefined) {
|
|
36
36
|
object.alpha = alpha;
|
|
37
37
|
}
|
|
38
|
-
}, [alpha, object
|
|
38
|
+
}, [alpha, object]);
|
|
39
39
|
useEffect(() => {
|
|
40
40
|
if (object && scale !== undefined) {
|
|
41
41
|
object.scale = scale;
|
|
42
42
|
}
|
|
43
|
-
}, [scale, object
|
|
43
|
+
}, [scale, object]);
|
|
44
44
|
useEffect(() => {
|
|
45
45
|
if (object) {
|
|
46
46
|
object.visible = visible;
|
|
47
47
|
}
|
|
48
|
-
}, [visible, object
|
|
48
|
+
}, [visible, object]);
|
|
49
49
|
useEffect(() => {
|
|
50
50
|
if (object && width !== undefined) {
|
|
51
51
|
object.width = width;
|
|
52
52
|
}
|
|
53
|
-
}, [width, object
|
|
53
|
+
}, [width, object]);
|
|
54
54
|
};
|
|
@@ -4,7 +4,7 @@ export const useTextures = ({ keys = [] }) => {
|
|
|
4
4
|
const { getAsset, isFetched, isFetching, isError } = useAssetManager();
|
|
5
5
|
const textures = useMemo(() => {
|
|
6
6
|
return isFetched ? keys.map((key) => getAsset(key)) : [];
|
|
7
|
-
}, [keys, isFetched,
|
|
7
|
+
}, [keys, isFetched, getAsset]);
|
|
8
8
|
return useMemo(() => ({
|
|
9
9
|
textures: textures,
|
|
10
10
|
isFetched,
|
|
@@ -15,12 +15,12 @@ export const useTilingSprite = ({ texture = "", tilePosition, ...options }) => {
|
|
|
15
15
|
return TilingSprite.from(texture);
|
|
16
16
|
}
|
|
17
17
|
return new TilingSprite({});
|
|
18
|
-
}, [texture,
|
|
18
|
+
}, [texture, isFetched]);
|
|
19
19
|
useObject({ object: sprite, ...options });
|
|
20
20
|
useEffect(() => {
|
|
21
21
|
if (tilePosition && sprite) {
|
|
22
22
|
sprite.tilePosition = tilePosition;
|
|
23
23
|
}
|
|
24
|
-
}, [tilePosition, sprite
|
|
24
|
+
}, [tilePosition, sprite]);
|
|
25
25
|
return sprite;
|
|
26
26
|
};
|
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.js
CHANGED
package/dist/src/layer/Layer.js
CHANGED
|
@@ -19,16 +19,16 @@ export const Layer = ({ options, children }) => {
|
|
|
19
19
|
return () => {
|
|
20
20
|
removeObjectFromStaeg(container);
|
|
21
21
|
};
|
|
22
|
-
}, [container
|
|
22
|
+
}, [container, addObjectIntoParent, addObjectIntoStage, removeObjectFromParent, removeObjectFromStaeg]);
|
|
23
23
|
const addObject = useCallback((thing) => {
|
|
24
24
|
container.addChild(thing);
|
|
25
|
-
}, [container
|
|
25
|
+
}, [container]);
|
|
26
26
|
const removeObject = useCallback((thing) => {
|
|
27
27
|
container.removeChild(thing);
|
|
28
|
-
}, [container
|
|
28
|
+
}, [container]);
|
|
29
29
|
const conextValue = useMemo(() => ({
|
|
30
30
|
addObject,
|
|
31
31
|
removeObject
|
|
32
|
-
}), [
|
|
32
|
+
}), [addObject, removeObject]);
|
|
33
33
|
return React.createElement(LayerContext.Provider, { value: conextValue }, children);
|
|
34
34
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { createContext, useEffect, useMemo, useRef, useState } from "react";
|
|
1
|
+
import React, { createContext, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
2
|
import { Engine, Runner, World } from "matter-js";
|
|
3
3
|
export const MatterPhysicsContext = createContext({
|
|
4
4
|
config: {
|
|
@@ -31,28 +31,28 @@ export const MatterPhysicsContextProvider = ({ isRunning = false, children, conf
|
|
|
31
31
|
});
|
|
32
32
|
}, [world]);
|
|
33
33
|
const runner = useMemo(() => Runner.create(), []);
|
|
34
|
-
const addBody = (body) => {
|
|
34
|
+
const addBody = useCallback((body) => {
|
|
35
35
|
if (engine?.world) {
|
|
36
36
|
World.add(engine?.world, body);
|
|
37
37
|
}
|
|
38
|
-
};
|
|
39
|
-
const removeBody = (body) => {
|
|
38
|
+
}, [engine]);
|
|
39
|
+
const removeBody = useCallback((body) => {
|
|
40
40
|
if (engine?.world) {
|
|
41
41
|
World.remove(engine?.world, body);
|
|
42
42
|
}
|
|
43
|
-
};
|
|
44
|
-
const run = () => {
|
|
43
|
+
}, [engine]);
|
|
44
|
+
const run = useCallback(() => {
|
|
45
45
|
if (!isRunningRef.current) {
|
|
46
46
|
Runner.run(runner, engine);
|
|
47
47
|
isRunningRef.current = true;
|
|
48
48
|
}
|
|
49
|
-
};
|
|
50
|
-
const stop = () => {
|
|
49
|
+
}, [runner, engine]);
|
|
50
|
+
const stop = useCallback(() => {
|
|
51
51
|
if (isRunningRef.current) {
|
|
52
52
|
Runner.stop(runner);
|
|
53
53
|
isRunningRef.current = false;
|
|
54
54
|
}
|
|
55
|
-
};
|
|
55
|
+
}, [runner]);
|
|
56
56
|
const conextValue = useMemo(() => ({
|
|
57
57
|
config: localConfig,
|
|
58
58
|
runner,
|
|
@@ -61,7 +61,7 @@ export const MatterPhysicsContextProvider = ({ isRunning = false, children, conf
|
|
|
61
61
|
stop,
|
|
62
62
|
addBody,
|
|
63
63
|
removeBody
|
|
64
|
-
}), [runner, engine, localConfig]);
|
|
64
|
+
}), [runner, engine, localConfig, addBody, removeBody, run, stop]);
|
|
65
65
|
useEffect(() => {
|
|
66
66
|
if (isRunning) {
|
|
67
67
|
run();
|
|
@@ -69,6 +69,6 @@ export const MatterPhysicsContextProvider = ({ isRunning = false, children, conf
|
|
|
69
69
|
return () => {
|
|
70
70
|
stop();
|
|
71
71
|
};
|
|
72
|
-
}, [isRunning]);
|
|
72
|
+
}, [isRunning, run, stop]);
|
|
73
73
|
return React.createElement(MatterPhysicsContext.Provider, { value: conextValue }, children);
|
|
74
74
|
};
|
package/dist/src/stage/Stage.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
-
import { StageContext } from "./Stage.context";
|
|
3
2
|
import { useWorld } from "../hooks";
|
|
4
3
|
import { Layer } from "../layer";
|
|
4
|
+
import { StageContext } from "./Stage.context";
|
|
5
5
|
export const Stage = ({ children }) => {
|
|
6
6
|
const { application } = useWorld();
|
|
7
7
|
const [things, setThings] = useState([]);
|
|
8
8
|
const addObject = useCallback((thing) => {
|
|
9
9
|
setThings((oldThings) => [...oldThings, thing]);
|
|
10
|
-
}, [
|
|
10
|
+
}, []);
|
|
11
11
|
const removeObject = useCallback((thing) => {
|
|
12
12
|
setThings((oldThings) => oldThings.filter(({ uid }) => uid === thing.uid));
|
|
13
|
-
}, [
|
|
13
|
+
}, []);
|
|
14
14
|
const conextValue = useMemo(() => ({
|
|
15
15
|
addObject,
|
|
16
16
|
removeObject
|
package/dist/src/world/World.js
CHANGED
|
@@ -1,21 +1,36 @@
|
|
|
1
|
-
import React, { createRef, useEffect, useMemo, useState } from "react";
|
|
1
|
+
import React, { createRef, useCallback, useEffect, useLayoutEffect, useMemo, useState } from "react";
|
|
2
2
|
import { Application } from "pixi.js";
|
|
3
|
-
import { WorldContext } from "./World.context";
|
|
4
3
|
import { Stage } from "../stage";
|
|
5
4
|
import { AssetsManager } from "../assets-manager";
|
|
5
|
+
import { WorldContext } from "./World.context";
|
|
6
6
|
export const World = ({ children, eventMode, size }) => {
|
|
7
7
|
const canvasRef = createRef();
|
|
8
8
|
const [applicationRef, setApplicationRef] = useState(null);
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
9
|
+
const [canvasSize, setCanvasSize] = useState(null);
|
|
10
|
+
const onCanvasRef = useCallback((ref) => {
|
|
11
|
+
const application = new Application();
|
|
12
|
+
application.init({ eventMode }).then(() => {
|
|
13
|
+
canvasRef.current = ref;
|
|
14
|
+
application.resizeTo = canvasRef.current;
|
|
15
|
+
application.resize();
|
|
16
|
+
setApplicationRef(application);
|
|
17
|
+
});
|
|
18
|
+
}, [canvasRef, eventMode]);
|
|
19
|
+
useLayoutEffect(() => {
|
|
13
20
|
if (canvasRef?.current) {
|
|
14
21
|
const { width, height } = canvasRef.current.getBoundingClientRect();
|
|
15
|
-
|
|
22
|
+
setCanvasSize({
|
|
16
23
|
width,
|
|
17
24
|
height
|
|
18
|
-
};
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}, [canvasRef]);
|
|
28
|
+
const worldSize = useMemo(() => {
|
|
29
|
+
if (size) {
|
|
30
|
+
return { ...size };
|
|
31
|
+
}
|
|
32
|
+
if (canvasSize) {
|
|
33
|
+
return canvasSize;
|
|
19
34
|
}
|
|
20
35
|
if (!applicationRef) {
|
|
21
36
|
return {
|
|
@@ -24,24 +39,10 @@ export const World = ({ children, eventMode, size }) => {
|
|
|
24
39
|
};
|
|
25
40
|
}
|
|
26
41
|
return {
|
|
27
|
-
width: applicationRef
|
|
28
|
-
height: applicationRef
|
|
42
|
+
width: applicationRef?.screen.width,
|
|
43
|
+
height: applicationRef?.screen.height
|
|
29
44
|
};
|
|
30
|
-
}, [size,
|
|
31
|
-
useEffect(() => {
|
|
32
|
-
if (!applicationRef) {
|
|
33
|
-
const application = new Application();
|
|
34
|
-
application.init().then(() => {
|
|
35
|
-
setApplicationRef(application);
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
}, []);
|
|
39
|
-
useEffect(() => {
|
|
40
|
-
if (applicationRef && canvasRef.current) {
|
|
41
|
-
applicationRef.resizeTo = canvasRef.current;
|
|
42
|
-
applicationRef.resize();
|
|
43
|
-
}
|
|
44
|
-
}, [canvasRef.current, applicationRef]);
|
|
45
|
+
}, [size, canvasSize, applicationRef]);
|
|
45
46
|
const conextValue = useMemo(() => ({
|
|
46
47
|
application: applicationRef,
|
|
47
48
|
size: worldSize
|
|
@@ -50,20 +51,16 @@ export const World = ({ children, eventMode, size }) => {
|
|
|
50
51
|
if (!applicationRef) {
|
|
51
52
|
return;
|
|
52
53
|
}
|
|
53
|
-
canvasRef.current
|
|
54
|
+
const canvas = canvasRef.current;
|
|
55
|
+
canvas?.appendChild(applicationRef.canvas);
|
|
54
56
|
return () => {
|
|
55
57
|
if (applicationRef) {
|
|
56
|
-
|
|
58
|
+
canvas?.removeChild(applicationRef.canvas);
|
|
57
59
|
}
|
|
58
60
|
};
|
|
59
|
-
}, [canvasRef
|
|
60
|
-
useEffect(() => {
|
|
61
|
-
if (applicationRef) {
|
|
62
|
-
applicationRef.stage.eventMode = eventMode;
|
|
63
|
-
}
|
|
64
|
-
}, [eventMode, applicationRef]);
|
|
61
|
+
}, [canvasRef, applicationRef]);
|
|
65
62
|
return (React.createElement(WorldContext.Provider, { value: conextValue },
|
|
66
63
|
React.createElement(AssetsManager, null,
|
|
67
64
|
React.createElement(Stage, null, children),
|
|
68
|
-
React.createElement("div", { style: { width: "100%", height: "100%" }, ref:
|
|
65
|
+
React.createElement("div", { style: { width: "100%", height: "100%" }, ref: onCanvasRef }))));
|
|
69
66
|
};
|