Skip to content

feat(examples): Add animated sprite example using SpriteComponent#8676

Merged
willeastcott merged 4 commits into
mainfrom
example/animated-sprite
May 4, 2026
Merged

feat(examples): Add animated sprite example using SpriteComponent#8676
willeastcott merged 4 commits into
mainfrom
example/animated-sprite

Conversation

@willeastcott

@willeastcott willeastcott commented May 3, 2026

Copy link
Copy Markdown
Contributor

Summary

2026-05-03.23-16-26.mp4

Adds a new engine example demonstrating SpriteComponent with multiple animation clips, driven by keyboard input as a small platformer-style controller. The example also illustrates how to build a TextureAtlas at runtime from a regular-grid spritesheet, and how to reuse the same sprite construction helper to spawn a row of ground tiles from a separate tileset texture.

Lives at examples/src/examples/misc/animated-sprite.example.mjs.

Controls

  • Left/Right arrows: walk (sprite is flipped horizontally via entity scale for left)
  • Space: jump (simple gravity-based arc, only when grounded)
  • Z: roll (one-shot, locks input and moves forward in the facing direction)
  • X: attack (one-shot, locks input)

What it covers

  • Loading a texture spritesheet and configuring it for crisp pixel-art rendering (clamp + nearest)
  • Building a TextureAtlas programmatically from a regular grid, with frame rects derived from texture.width / texture.height at runtime
  • Tuning sprite pivots (FEET_OFFSET_PX) so the entity origin aligns with the character's feet
  • Wrapping a Sprite resource in a sprite asset that can be referenced by name or id (no JSON files required)
  • Adding multiple SpriteAnimationClips to a SpriteComponent from a definition table
  • Switching clips based on a small input/state machine, including handling of one-shot non-looping clips via the 'end' event
  • Using the same atlas-building pattern for a tileset and spawning a strip of SPRITETYPE_SIMPLE tiles to act as a ground

Assets

Two new pixel-art textures are added to examples/assets/sprites/:

  • caveman.png (6x7 character spritesheet)
  • prehistoric-tileset.png (one 48x48 ground tile is referenced)

Other changes

  • examples/src/static/styles.css: added text-align: center; to #descriptionPanel. The panel itself was already horizontally centered via left: 50% + transform: translateX(-50%), but its text wasn't, so short // @config DESCRIPTION ... strings appeared left-aligned within the panel.

Public API changes

None.

Test plan

  • npm run lint (engine + examples) passes
  • Open the new example via npm run develop in examples/; verify idle plays on load
  • Walk left/right; sprite flips and the walk cycle plays
  • Jump; jump animation plays in air, returns to idle/walk on landing
  • Roll (Z); plays once, moves forward, then returns to idle/walk
  • Attack (X); plays once, locks horizontal movement, then returns to idle/walk
  • Caveman feet sit flush on the ground tiles
  • Description text appears centered at the bottom of the example viewer

A new example under `misc/` that demonstrates `SpriteComponent` with
multiple animation clips, plus a small platformer-style controller:

- Arrow keys walk left/right (entity scale flip for left)
- Space jumps with simple gravity
- Z rolls (one-shot, locked-in forward motion)
- X attacks (one-shot)

Also shows how to build a `TextureAtlas` at runtime from a regular grid
spritesheet, and reuses the same `createSpriteAsset` helper to spawn a
row of ground tiles from a separate tileset texture.

Adds two pixel-art assets to `examples/assets/sprites/`:

- `caveman.png` (6x7 character spritesheet)
- `prehistoric-tileset.png` (one 48x48 ground tile is referenced)

Also adds `text-align: center` to the example viewer's description
panel so short descriptions are centered horizontally within the
already-centered panel.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new interactive engine example demonstrating SpriteComponent-driven 2D sprite animation (with keyboard-controlled state machine) and runtime TextureAtlas construction, plus minor viewer styling and associated assets/thumbnails.

Changes:

  • New animated-sprite example showcasing animated clips, one-shot actions, sprite flipping, and runtime atlas generation from grid spritesheets/tilesets.
  • Add new sprite textures + example thumbnails for the example browser.
  • Center-align description panel text in the examples viewer.

Reviewed changes

Copilot reviewed 2 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
examples/src/examples/misc/animated-sprite.example.mjs New animated sprite example implementing runtime atlas creation and input-driven clip switching.
examples/src/static/styles.css Centers the description panel text for improved presentation.
examples/assets/sprites/caveman.png New character spritesheet asset for the example.
examples/assets/sprites/prehistoric-tileset.png New tileset texture used to generate ground tiles in the example.
examples/thumbnails/misc_animated-sprite_small.webp New thumbnail for the example picker (small).
examples/thumbnails/misc_animated-sprite_large.webp New thumbnail for the example picker (large).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread examples/src/examples/misc/animated-sprite.example.mjs Outdated
Comment thread examples/src/examples/misc/animated-sprite.example.mjs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@willeastcott willeastcott merged commit ab0d0a3 into main May 4, 2026
8 checks passed
@willeastcott willeastcott deleted the example/animated-sprite branch May 4, 2026 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: examples enhancement Request for a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants