Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [#2468] add clone() method to ex.SpriteSheet #2469

Merged
merged 3 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

- Added the emitted particle transform style as part of `ex.ParticleEmitter({particleTransform: ex.ParticleTransform.Global})`, [[ParticleTransform.Global]] is the default and emits particles as if they were world space objects, useful for most effects. If set to [[ParticleTransform.Local]] particles are children of the emitter and move relative to the emitter as they would in a parent/child actor relationship.
- Added `wasButtonReleased` and `wasButtonPressed` methods to [[ex.Input.Gamepad]]
- Added `clone()` method to `ex.SpriteSheet`

### Fixed

Expand Down
8 changes: 8 additions & 0 deletions src/engine/Graphics/SpriteSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,12 @@ export class SpriteSheet {
columns: cols
});
}

public clone(): SpriteSheet {
return new SpriteSheet({
sprites: this.sprites.map(sprite => sprite.clone()),
rows: this.rows,
columns: this.columns
});
}
}
20 changes: 20 additions & 0 deletions src/spec/SpriteSheetSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,24 @@ describe('A SpriteSheet for Graphics', () => {
expect(ss.getSprite(1, -1)).toBeNull();
expect(logger.warn).toHaveBeenCalledWith('No sprite exists in the SpriteSheet at (1, -1), y: -1 should be between 0 and 3');
});

it('can be cloned', async () => {
const image = new ex.ImageSource('src/spec/images/GraphicsTextSpec/spritefont.png');

await image.load();
const ss = new ex.SpriteSheet({
sprites: [ex.Sprite.from(image)],
rows: 5,
columns: 5
});

const clone = ss.clone();

expect(ss).not.toBe(clone);
expect(ss.getSprite(0, 0)).not.toBe(clone.getSprite(0,0));
expect(ss.sprites.length).toBe(clone.sprites.length);
expect(ss.rows).toBe(clone.rows);
expect(ss.columns).toBe(clone.columns);

});
});