Skip to content

Commit

Permalink
fix #13 (#67)
Browse files Browse the repository at this point in the history
* wip #13

* wip #13

* wip #13

* wip #13

* wip #13

* wip #13

* wip #13

* wip #13

* wip #13

* wip #13

* wip #13

* wip #13

* wip #13

* wip #13
  • Loading branch information
pagoru authored Jun 25, 2024
1 parent 7aaeb3b commit 4d7e07e
Show file tree
Hide file tree
Showing 64 changed files with 5,099 additions and 395 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/build-library.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ on:
- src/**

jobs:
publish:
name: 'build'
tests:
name: 'tests'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -21,4 +21,5 @@ jobs:
registry-url: 'https://registry.npmjs.org'
- run: yarn install --frozen-lockfile

# Do something here...
- name: run tests
run: yarn test
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jobs:
registry-url: 'https://registry.npmjs.org'
- run: yarn install --frozen-lockfile

- name: run tests
run: yarn test

- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
dist
node_modules
.idea
.vite
.vite
coverage
3 changes: 3 additions & 0 deletions INTERNAL-RULES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# INTERNAL RULES

- Only core components can add more mutable functions (you need to include `getComponent` function and return the new `$mutable` to be able to be visible outside)
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
10 changes: 5 additions & 5 deletions examples/example/src/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ type Mutable = {} & DisplayObjectMutable<Container>;
export const appComponent: AsyncComponent<unknown, Mutable> = async () => {
global.$setVisualHitboxes(false);

const $container = container({ label: "app" });
const $container = await container({ label: "app" });

const $world = world({
const $world = await world({
position: { x: 0, y: 0 },
label: "world2",
props: {
Expand All @@ -28,7 +28,7 @@ export const appComponent: AsyncComponent<unknown, Mutable> = async () => {
},
});

const $plane = plane({
const $plane = await plane({
position: {
x: 0,
y: 1200,
Expand All @@ -40,7 +40,7 @@ export const appComponent: AsyncComponent<unknown, Mutable> = async () => {
});

for (let i = 0; i < 300; i++) {
const _fly = flyComponent({
const _fly = await flyComponent({
label: `ball`,
props: {
color: 0xffffff,
Expand All @@ -56,7 +56,7 @@ export const appComponent: AsyncComponent<unknown, Mutable> = async () => {
}

const $player = await playerComponent();
$player.setPosition({ x: 500, y: 1000 });
await $player.setPosition({ x: 500, y: 1000 });

$world.add($player, $plane);
$container.add($world);
Expand Down
10 changes: 5 additions & 5 deletions examples/example/src/fly.component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
Component,
Container,
DisplayObjectMutable,
EventMode,
ContainerProps,
global,
circle,
AsyncComponent,
} from "@tulib/tulip";
import { GlobalData } from "types";

Expand All @@ -19,8 +19,8 @@ type Props = {

type Mutable = {} & DisplayObjectMutable<Container>;

export const flyComponent: Component<Props, Mutable> = (props) => {
const container = circle({
export const flyComponent: AsyncComponent<Props, Mutable> = async (props) => {
const container = await circle({
...props,
eventMode: EventMode.STATIC,
props: {
Expand All @@ -37,7 +37,7 @@ export const flyComponent: Component<Props, Mutable> = (props) => {
const size = 15;
const position = size - 2;

const circle2 = circle({
const circle2 = await circle({
props: {
size,
color: global.getData<GlobalData>().ballColor,
Expand All @@ -50,7 +50,7 @@ export const flyComponent: Component<Props, Mutable> = (props) => {
},
});

const circle3 = circle({
const circle3 = await circle({
props: {
size,
color: global.getData<GlobalData>().ballColor,
Expand Down
4 changes: 2 additions & 2 deletions examples/example/src/player.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const playerComponent: AsyncComponent<Props, Mutable> = async () => {
$sprite.setFrame(0);
};

const $player = player2D({
const $player = await player2D({
onTick,
maxSpeed: 10,
acceleration: 3,
Expand Down Expand Up @@ -64,7 +64,7 @@ export const playerComponent: AsyncComponent<Props, Mutable> = async () => {
[0, height / 2],
],
});
$player.setBody($body);
await $player.setBody($body);

return $player.getComponent(playerComponent);
};
20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,28 @@
}
],
"dependencies": {
"p2": "0.7.1",
"pixi.js": "8.1.6",
"@tulib/vite-tulip-plugin": "1.0.2",
"howler": "2.2.4",
"@tulib/vite-tulip-plugin": "1.0.2"
"p2": "0.7.1",
"pixi.js": "8.1.6"
},
"devDependencies": {
"@babel/core": "^7.24.7",
"@babel/preset-env": "^7.24.7",
"@babel/preset-typescript": "^7.24.7",
"@types/howler": "2.2.11",
"@types/jest": "29.5.12",
"@types/p2": "0.7.44",
"babel-jest": "^29.7.0",
"jest": "29.7.0",
"prettier": "3.3.0"
},
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage"
},
"jest": {
"verbose": true
}
}
2 changes: 2 additions & 0 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export const application = async ({
pixi: PIXI,
app: application,
};
//@ts-ignore
globalThis.__PIXI_APP__ = application;

// @ts-ignore
if (importMetaHot)
Expand Down
150 changes: 150 additions & 0 deletions src/components/core/animated-sprite.component.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { AnimatedSpriteMutable } from "../../types";
import { animatedSprite } from "./animated-sprite.component";
import { PlayStatus } from "../../enums";
import { expect } from "@jest/globals";

jest.mock("pixi.js", () => {
const originalModule = jest.requireActual("pixi.js");

return {
...originalModule,
AnimatedSprite: jest.fn(() => ({
gotoAndStop: (frame) => mockGotoAndStop(frame),
gotoAndPlay: (frame) => mockGotoAndPlay(frame),
stop: mockStop,
play: mockPlay,
emit: jest.fn(),
on: jest.fn(),
loop: jest.fn(),
})),
Assets: {
load: (args) => mockAssetsLoad(args),
},
};
});

const mockGotoAndStop = jest.fn();
const mockGotoAndPlay = jest.fn();
const mockStop = jest.fn();
const mockPlay = jest.fn();
const mockAssetsLoad = jest.fn(async (args) => ({
id: args,
animations: {
"animation-name": ["texture1"],
"animation-name-2": ["texture2"],
},
}));

describe("components", () => {
describe("core", () => {
describe("animated-sprite", () => {
let $animatedSprite: AnimatedSpriteMutable;

beforeEach(() => {
mockGotoAndStop.mockClear();
mockGotoAndPlay.mockClear();
mockStop.mockClear();
mockPlay.mockClear();

mockAssetsLoad.mockClear();
});
beforeAll(async () => {
$animatedSprite = await animatedSprite({
label: "animated-sprite-label",
spriteSheet: "something.json",
animation: "animation-name",
frame: 1,
playStatus: PlayStatus.STOP,
});
});

test("getId() check that empty is being called", () => {
expect($animatedSprite.getId()).toMatch(
/animated-sprite-label_([0-9]{0,5})/,
);
});
test("getLabel() check that initDisplayObjectMutable(...) is being called", () => {
expect($animatedSprite.getDisplayObject().label).toStrictEqual(
"animated-sprite-label",
);
expect($animatedSprite.getLabel()).toStrictEqual(
"animated-sprite-label",
);
});
test("setSpriteSheet(...) loads the spritesheet and changes textures with animation", () => {
$animatedSprite.setSpriteSheet("testing.json");
expect($animatedSprite.getSpriteSheet()).toStrictEqual("testing.json");
expect(mockAssetsLoad).toHaveBeenCalledWith("testing.json");
});
test("setAnimation(...) changes the animation", () => {
expect($animatedSprite.getAnimation()).toStrictEqual("animation-name");
$animatedSprite.setAnimation("animation-name-2");
expect($animatedSprite.getAnimation()).toStrictEqual(
"animation-name-2",
);
});
test("setFrame(...) sets the frame of the animation", () => {
expect($animatedSprite.getFrame()).toStrictEqual(1);
$animatedSprite.setFrame(3);
expect($animatedSprite.getFrame()).toStrictEqual(3);
expect(mockGotoAndStop).toHaveBeenCalledWith(3);
expect(mockGotoAndPlay).not.toHaveBeenCalled();
});
test("setPlayStatus(...) sets the sate of the current animation", () => {
expect($animatedSprite.getPlayStatus()).toStrictEqual(PlayStatus.STOP);
$animatedSprite.setPlayStatus(PlayStatus.PLAY);
expect($animatedSprite.getPlayStatus()).toStrictEqual(PlayStatus.PLAY);
expect(mockPlay).toHaveBeenCalled();
expect(mockStop).not.toHaveBeenCalled();
expect($animatedSprite.getDisplayObject().loop).toStrictEqual(true);

mockPlay.mockClear();
mockStop.mockClear();

$animatedSprite.setPlayStatus(PlayStatus.STOP);
expect($animatedSprite.getPlayStatus()).toStrictEqual(PlayStatus.STOP);
expect(mockPlay).not.toHaveBeenCalled();
expect(mockStop).toHaveBeenCalled();

mockPlay.mockClear();
mockStop.mockClear();

$animatedSprite.setPlayStatus(PlayStatus.PLAY_AND_STOP);
expect($animatedSprite.getPlayStatus()).toStrictEqual(
PlayStatus.PLAY_AND_STOP,
);
expect(mockPlay).toHaveBeenCalled();
expect(mockStop).not.toHaveBeenCalled();
expect($animatedSprite.getDisplayObject().loop).toStrictEqual(false);
});
test("getProps() returns all the original properties", () => {
expect($animatedSprite.getProps()).toEqual({
animation: "animation-name",
frame: 1,
label: "animated-sprite-label",
playStatus: 1,
spriteSheet: "something.json",
});
});
test("$getRaw() returns everything needed to hot reload", () => {
expect($animatedSprite.$getRaw()).toEqual({
alpha: undefined,
angle: 0,
animation: "animation-name-2",
eventMode: undefined,
frame: 3,
id: $animatedSprite.getId(),
initialData: {},
label: "animated-sprite-label",
pivot: { x: 0, y: 0 },
playStatus: 2,
position: { x: 0, y: 0 },
spriteSheet: "testing.json",
visible: undefined,
zIndex: undefined,
});
});
test.todo("$destroy() destroys everything");
});
});
});
Loading

0 comments on commit 4d7e07e

Please sign in to comment.