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
package/src/stage/Stage.tsx
CHANGED
|
@@ -1,26 +1,20 @@
|
|
|
1
1
|
import React, { PropsWithChildren, useCallback, useEffect, useMemo, useState } from "react";
|
|
2
2
|
import { Container } from "pixi.js";
|
|
3
|
-
import { StageContext, StageContextValue } from "./Stage.context";
|
|
4
3
|
import { useWorld } from "../hooks";
|
|
5
4
|
import { Layer } from "../layer";
|
|
5
|
+
import { StageContext, StageContextValue } from "./Stage.context";
|
|
6
6
|
|
|
7
7
|
export const Stage: React.FC<PropsWithChildren> = ({ children }) => {
|
|
8
8
|
const { application } = useWorld();
|
|
9
9
|
const [things, setThings] = useState<Container[]>([]);
|
|
10
10
|
|
|
11
|
-
const addObject = useCallback(
|
|
12
|
-
(
|
|
13
|
-
|
|
14
|
-
},
|
|
15
|
-
[things]
|
|
16
|
-
);
|
|
11
|
+
const addObject = useCallback((thing: Container) => {
|
|
12
|
+
setThings((oldThings) => [...oldThings, thing]);
|
|
13
|
+
}, []);
|
|
17
14
|
|
|
18
|
-
const removeObject = useCallback(
|
|
19
|
-
(
|
|
20
|
-
|
|
21
|
-
},
|
|
22
|
-
[application]
|
|
23
|
-
);
|
|
15
|
+
const removeObject = useCallback((thing: Container) => {
|
|
16
|
+
setThings((oldThings) => oldThings.filter(({ uid }) => uid === thing.uid));
|
|
17
|
+
}, []);
|
|
24
18
|
|
|
25
19
|
const conextValue = useMemo<StageContextValue>(
|
|
26
20
|
() => ({
|
package/src/world/World.tsx
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import React, { PropsWithChildren, createRef, useEffect, useMemo, useState } from "react";
|
|
1
|
+
import React, { PropsWithChildren, createRef, useCallback, useEffect, useLayoutEffect, useMemo, useState } from "react";
|
|
2
2
|
import { Application, EventMode } from "pixi.js";
|
|
3
|
-
import { WorldContextValue, WorldContext } from "./World.context";
|
|
4
3
|
import { Stage } from "../stage";
|
|
5
4
|
import { AssetsManager } from "../assets-manager";
|
|
5
|
+
import { WorldContextValue, WorldContext } from "./World.context";
|
|
6
6
|
|
|
7
7
|
type WorldProps = {
|
|
8
8
|
eventMode?: EventMode;
|
|
@@ -17,17 +17,38 @@ export const World: React.FC<PropsWithChildren & WorldProps> = ({ children, even
|
|
|
17
17
|
|
|
18
18
|
const [applicationRef, setApplicationRef] = useState<Application | null>(null);
|
|
19
19
|
|
|
20
|
-
const
|
|
21
|
-
if (size) {
|
|
22
|
-
return { ...size };
|
|
23
|
-
}
|
|
20
|
+
const [canvasSize, setCanvasSize] = useState<null | { width: number; height: number }>(null);
|
|
24
21
|
|
|
22
|
+
const onCanvasRef = useCallback(
|
|
23
|
+
(ref: HTMLDivElement) => {
|
|
24
|
+
const application = new Application();
|
|
25
|
+
application.init({ eventMode }).then(() => {
|
|
26
|
+
canvasRef.current = ref;
|
|
27
|
+
application.resizeTo = canvasRef.current;
|
|
28
|
+
application.resize();
|
|
29
|
+
setApplicationRef(application);
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
[canvasRef, eventMode]
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
useLayoutEffect(() => {
|
|
25
36
|
if (canvasRef?.current) {
|
|
26
37
|
const { width, height } = canvasRef.current.getBoundingClientRect();
|
|
27
|
-
|
|
38
|
+
setCanvasSize({
|
|
28
39
|
width,
|
|
29
40
|
height
|
|
30
|
-
};
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}, [canvasRef]);
|
|
44
|
+
|
|
45
|
+
const worldSize = useMemo(() => {
|
|
46
|
+
if (size) {
|
|
47
|
+
return { ...size };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (canvasSize) {
|
|
51
|
+
return canvasSize;
|
|
31
52
|
}
|
|
32
53
|
|
|
33
54
|
if (!applicationRef) {
|
|
@@ -38,26 +59,10 @@ export const World: React.FC<PropsWithChildren & WorldProps> = ({ children, even
|
|
|
38
59
|
}
|
|
39
60
|
|
|
40
61
|
return {
|
|
41
|
-
width: applicationRef
|
|
42
|
-
height: applicationRef
|
|
62
|
+
width: applicationRef?.screen.width,
|
|
63
|
+
height: applicationRef?.screen.height
|
|
43
64
|
};
|
|
44
|
-
}, [size,
|
|
45
|
-
|
|
46
|
-
useEffect(() => {
|
|
47
|
-
if (!applicationRef) {
|
|
48
|
-
const application = new Application();
|
|
49
|
-
application.init().then(() => {
|
|
50
|
-
setApplicationRef(application);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}, []);
|
|
54
|
-
|
|
55
|
-
useEffect(() => {
|
|
56
|
-
if (applicationRef && canvasRef.current) {
|
|
57
|
-
applicationRef.resizeTo = canvasRef.current;
|
|
58
|
-
applicationRef.resize();
|
|
59
|
-
}
|
|
60
|
-
}, [canvasRef.current, applicationRef]);
|
|
65
|
+
}, [size, canvasSize, applicationRef]);
|
|
61
66
|
|
|
62
67
|
const conextValue = useMemo<WorldContextValue>(
|
|
63
68
|
() => ({
|
|
@@ -71,26 +76,21 @@ export const World: React.FC<PropsWithChildren & WorldProps> = ({ children, even
|
|
|
71
76
|
if (!applicationRef) {
|
|
72
77
|
return;
|
|
73
78
|
}
|
|
74
|
-
canvasRef.current
|
|
79
|
+
const canvas = canvasRef.current;
|
|
80
|
+
canvas?.appendChild(applicationRef.canvas);
|
|
75
81
|
|
|
76
82
|
return () => {
|
|
77
83
|
if (applicationRef) {
|
|
78
|
-
|
|
84
|
+
canvas?.removeChild(applicationRef.canvas);
|
|
79
85
|
}
|
|
80
86
|
};
|
|
81
|
-
}, [canvasRef
|
|
82
|
-
|
|
83
|
-
useEffect(() => {
|
|
84
|
-
if (applicationRef) {
|
|
85
|
-
applicationRef.stage.eventMode = eventMode;
|
|
86
|
-
}
|
|
87
|
-
}, [eventMode, applicationRef]);
|
|
87
|
+
}, [canvasRef, applicationRef]);
|
|
88
88
|
|
|
89
89
|
return (
|
|
90
90
|
<WorldContext.Provider value={conextValue}>
|
|
91
91
|
<AssetsManager>
|
|
92
92
|
<Stage>{children}</Stage>
|
|
93
|
-
<div style={{ width: "100%", height: "100%" }} ref={
|
|
93
|
+
<div style={{ width: "100%", height: "100%" }} ref={onCanvasRef} />
|
|
94
94
|
</AssetsManager>
|
|
95
95
|
</WorldContext.Provider>
|
|
96
96
|
);
|