Skip to content

Commit

Permalink
fix: lifespan using async (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel committed Jan 15, 2025
1 parent 7f34947 commit 70a6948
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 54 deletions.
44 changes: 38 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,50 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Changed default behaviour of `kaplay({ tagsAsComponents: false })` to `false`.
- Now if you pass a nullish value to `.use()` it throws an error

## [3001.0.9] - 2025-01-15

### Added

- Added new option in `LoadSpriteOpt` for loading sprites in an individual
spritesheet - @chqs-git
```js
loadSprite(
"player",
"sprites/player.png",
{
singular: true,
},
);
```
- **(examples)** Added a new `particle` example! - @lajbel

### Changed

- Improved `lifespan()` explanation - @lajbel
- **(examples)** `particle` example renamed to `lifespan` - @lajbel

### Fixed

- Fixed a bug where `lifespan()` was working incorrectly - @lajbel

## [3001.0.8] - 2025-01-15

### Fixed

- Fixed a bug where alpha channel wasn't correctly setted - @mflerackers

## [3001.0.7] - 2025-01-15

### Added

- Added `kaplay({ spriteAtlasPadding })` for setting the space between the
- Added `kaplay({ sprit`e`AtlasPadding })` for setting the space between the
sprites in the sprite atlas - @marianyp

```js
kaplay({
spriteAtlasPadding: 10, // 10 pixels of space between each sprite
});
```
```js
kaplay({
spriteAtlasPadding: 10, // 10 pixels of space between each sprite
});
```

### Changed

Expand Down
41 changes: 41 additions & 0 deletions examples/lifespan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// @ts-check


kaplay();

const sprites = [
"apple",
"heart",
"coin",
"meat",
"lightening",
];

sprites.forEach((spr) => {
loadSprite(spr, `/sprites/${spr}.png`);
});

setGravity(800);

// Spawn one object every 0.1 second
loop(0.1, () => {
// Compose object properties with components
const item = add([
pos(center()),
sprite(choose(sprites)),
anchor("center"),
scale(rand(0.5, 1)),
area({ collisionIgnore: ["fruit"] }),
body(),
// lifespan() comp destroys the object after desired seconds
lifespan(1, {
// it will fade after 0.5 seconds
fade: 0.5
}),
opacity(1),
move(choose([LEFT, RIGHT]), rand(60, 240)),
"fruit",
]);

item.jump(rand(320, 640));
});
70 changes: 38 additions & 32 deletions examples/particle.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
// @ts-check

// Particle spawning
// Creating particles using Particle Component

kaplay();

const sprites = [
"apple",
"heart",
"coin",
"meat",
"lightening",
];
loadSprite("star", "./examples/sprites/particle_star_filled.png");

sprites.forEach((spr) => {
loadSprite(spr, `/sprites/${spr}.png`);
onLoad(() => {
go("game")
});

setGravity(800);

// Spawn one particle every 0.1 second
loop(0.1, () => {
// TODO: they are resolving collision with each other for some reason
// Compose particle properties with components
const item = add([
pos(mousePos()),
sprite(choose(sprites)),
anchor("center"),
scale(rand(0.5, 1)),
area({ collisionIgnore: ["particle"] }),
body(),
lifespan(1, { fade: 0.5 }),
opacity(1),
move(choose([LEFT, RIGHT]), rand(60, 240)),
"particle",
function woah() {
const parts = add([
pos(center()),
particles({
max: 20,
speed: [50, 100],
angle: [0, 360],
angularVelocity: [45, 90],
lifeTime: [1.0, 1.5],
colors: [rgb(128, 128, 255), WHITE],
opacities: [0.1, 1.0, 0.0],
scales: [1, 2, 1],
texture: getSprite("star").data.tex,
quads: [getSprite("star").data.frames[0]],
}, {
lifetime: 1.5,
rate: 0,
direction: -90,
spread: 40,
}),
]);

item.onCollide("particle", (p) => {
console.log("dea");
parts.emit(20);
}

scene("game", () => {
onKeyPress("space", () => {
woah();
});

onMousePress(() => {
woah();
});

item.jump(rand(320, 640));
add([
text("press space for particles"),
]);
});

32 changes: 19 additions & 13 deletions src/components/misc/lifespan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,25 @@ export function lifespan(time: number, opt: LifespanCompOpt = {}): EmptyComp {
return {
id: "lifespan",
require: ["opacity"],
async add(this: GameObj<OpacityComp>) {
await _k.game.root.wait(time);
this.opacity = this.opacity ?? 1;
if (fade > 0) {
await _k.game.root.tween(
this.opacity,
0,
fade,
(a) => this.opacity = a,
easings.linear,
);
}
this.destroy();
add(this: GameObj<OpacityComp>) {
_k.game.root.wait(time, () => {
this.opacity = this.opacity ?? 1;

if (fade > 0) {
_k.game.root.tween(
this.opacity,
0,
fade,
(a) => this.opacity = a,
easings.linear,
).onEnd(() => {
this.destroy();
});
}
else {
this.destroy();
}
});
},
};
}
8 changes: 5 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1222,11 +1222,13 @@ export interface KAPLAYCtx<
*
* @example
* ```js
* // spawn an explosion, destroy after 1 seconds, start fading away after 0.5 second
* // spawn an explosion, destroy after 1.5 seconds (time + fade)
* add([
* sprite("explosion", { anim: "burst", }),
* lifespan(1, { fade: 0.5 }),
* ])
* lifespan(1, {
* fade: 0.5 // it start fading 0.5 second after time
* }),
* ]);
* ```
*
* @returns The lifespan comp.
Expand Down

0 comments on commit 70a6948

Please sign in to comment.