Skip to content

Commit

Permalink
added TileInfo.frame for cleaner animation support
Browse files Browse the repository at this point in the history
  • Loading branch information
KilledByAPixel committed Aug 7, 2024
1 parent 639de26 commit 387b343
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 9 deletions.
7 changes: 6 additions & 1 deletion dist/littlejs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1011,11 +1011,16 @@ declare module "littlejs.esm" {
size: Vector2;
/** @property {Number} - Texture index to use */
textureIndex: number;
/** Returns an offset copy of this tile, useful for animation
/** Returns a copy of this tile offset by a vector
* @param {Vector2} offset - Offset to apply in pixels
* @return {TileInfo}
*/
offset(offset: Vector2): TileInfo;
/** Returns a copy of this tile offset by a number of animation frames
* @param {Number} frame - Offset to apply in animation frames
* @return {TileInfo}
*/
frame(frame: number): TileInfo;
/** Returns the texture info for this tile
* @return {TextureInfo}
*/
Expand Down
12 changes: 11 additions & 1 deletion dist/littlejs.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2141,13 +2141,23 @@ class TileInfo
this.textureIndex = textureIndex;
}

/** Returns an offset copy of this tile, useful for animation
/** Returns a copy of this tile offset by a vector
* @param {Vector2} offset - Offset to apply in pixels
* @return {TileInfo}
*/
offset(offset)
{ return new TileInfo(this.pos.add(offset), this.size, this.textureIndex); }

/** Returns a copy of this tile offset by a number of animation frames
* @param {Number} frame - Offset to apply in animation frames
* @return {TileInfo}
*/
frame(frame)
{
ASSERT(typeof frame == 'number');
return this.offset(vec2(frame*this.size.x, 0));
}

/** Returns the texture info for this tile
* @return {TextureInfo}
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/littlejs.esm.min.js

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion dist/littlejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2141,13 +2141,23 @@ class TileInfo
this.textureIndex = textureIndex;
}

/** Returns an offset copy of this tile, useful for animation
/** Returns a copy of this tile offset by a vector
* @param {Vector2} offset - Offset to apply in pixels
* @return {TileInfo}
*/
offset(offset)
{ return new TileInfo(this.pos.add(offset), this.size, this.textureIndex); }

/** Returns a copy of this tile offset by a number of animation frames
* @param {Number} frame - Offset to apply in animation frames
* @return {TileInfo}
*/
frame(frame)
{
ASSERT(typeof frame == 'number');
return this.offset(vec2(frame*this.size.x, 0));
}

/** Returns the texture info for this tile
* @return {TextureInfo}
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/littlejs.min.js

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion dist/littlejs.release.js
Original file line number Diff line number Diff line change
Expand Up @@ -1755,13 +1755,23 @@ class TileInfo
this.textureIndex = textureIndex;
}

/** Returns an offset copy of this tile, useful for animation
/** Returns a copy of this tile offset by a vector
* @param {Vector2} offset - Offset to apply in pixels
* @return {TileInfo}
*/
offset(offset)
{ return new TileInfo(this.pos.add(offset), this.size, this.textureIndex); }

/** Returns a copy of this tile offset by a number of animation frames
* @param {Number} frame - Offset to apply in animation frames
* @return {TileInfo}
*/
frame(frame)
{
ASSERT(typeof frame == 'number');
return this.offset(vec2(frame*this.size.x, 0));
}

/** Returns the texture info for this tile
* @return {TextureInfo}
*/
Expand Down
3 changes: 1 addition & 2 deletions examples/platformer/gameCharacter.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ class Character extends GameObject
const animationFrame = this.isDead() ? 0 :
this.climbingLadder || this.groundTimer.active() ?
2*this.walkCyclePercent|0 : 1;
const playerTile = spriteAtlas.player;
this.tileInfo.pos.x = playerTile.pos.x + playerTile.size.x*animationFrame;
this.tileInfo = spriteAtlas.player.frame(animationFrame);

let bodyPos = this.pos;
if (!this.isDead())
Expand Down
12 changes: 11 additions & 1 deletion src/engineDraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,23 @@ class TileInfo
this.textureIndex = textureIndex;
}

/** Returns an offset copy of this tile, useful for animation
/** Returns a copy of this tile offset by a vector
* @param {Vector2} offset - Offset to apply in pixels
* @return {TileInfo}
*/
offset(offset)
{ return new TileInfo(this.pos.add(offset), this.size, this.textureIndex); }

/** Returns a copy of this tile offset by a number of animation frames
* @param {Number} frame - Offset to apply in animation frames
* @return {TileInfo}
*/
frame(frame)
{
ASSERT(typeof frame == 'number');
return this.offset(vec2(frame*this.size.x, 0));
}

/** Returns the texture info for this tile
* @return {TextureInfo}
*/
Expand Down

0 comments on commit 387b343

Please sign in to comment.