TG Engine is a lightweight game engine built with StencilJS, designed for creating 2D sprite-based games and animations in the browser. It provides a set of web components that handle sprite rendering, animation, collision detection, camera control, and touch input.
npm install tg-engineDisplays a single sprite from a sprite sheet.
src: String - URL or base64 string of the sprite imagewidth: Number - Width of a single sprite frame in pixelsheight: Number - Height of a single sprite frame in pixelsscale: Number - Scale factor for the sprite (default: 1)frame: Number - Initial frame to display (default: 0)hFrames: Number - Number of horizontal frames in the sprite sheetvFrames: Number - Number of vertical frames in the sprite sheetvFlip: Boolean - Whether to flip the sprite vertically (default: false)hFlip: Boolean - Whether to flip the sprite horizontally (default: false)
<tg-sprite
src="path/to/sprite.png"
width={16}
height={16}
hFrames={2}
vFrames={4}
scale={5}
frame={0}
/>Animates a tg-sprite component using predefined animations.
animations: Object - Dictionary of animations{ [animationName: string]: { frames: number[], duration: number } }
play: String - Name of the animation to playstate: 'running' | 'paused' - Controls animation playbackiterationCount: 'infinite' | number - Number of times to play the animation
<tg-sprite-animator
animations={{
'walk': { frames: [0, 1, 2, 3], duration: 100 },
'idle': { frames: [4, 5], duration: 400 }
}}
play="walk"
state="running"
iterationCount="infinite"
>
<tg-sprite
src="path/to/sprite.png"
width={16}
height={16}
hFrames={2}
vFrames={4}
scale={5}
/>
</tg-sprite-animator>Renders a tile-based map using a sprite sheet.
src: String - URL or base64 string of the tile sheetwidth: Number - Width of the map in tilesheight: Number - Height of the map in tilestileWidth: Number - Width of each tile in pixelstileHeight: Number - Height of each tile in pixelshFrames: Number - Number of horizontal frames in the tile sheetvFrames: Number - Number of vertical frames in the tile sheetscale: Number - Scale factor for the tiles (default: 1)tiles: Array - Array of tile objectsinterface ITile { x: number; y: number; frames: number[]; duration?: number; }
<tg-sprite-map
src="path/to/tiles.png"
width={8}
height={8}
tileWidth={16}
tileHeight={16}
hFrames={10}
vFrames={10}
scale={5}
tiles={[
{ x: 0, y: 0, frames: [0] },
{ x: 1, y: 0, frames: [1] },
{ x: 0, y: 1, frames: [2] },
{ x: 1, y: 1, frames: [3] }
]}
/>Follows a target element with smooth interpolation.
width: Number - Width of the camera viewportheight: Number - Height of the camera viewporttarget: HTMLElement - Element to followsmooth: Number - Smoothing factor for camera movement (default: 0.1)
<tg-camera width={400} height={400} target={targetElement} smooth={0.1}>
<div class="game-world">
<!-- Your game content here -->
</div>
</tg-camera>Detects collisions between elements.
name: String - Unique identifier for the colliderwidth: Number - Width of the collision boxheight: Number - Height of the collision boxx: Number - X position offset (default: 0)y: Number - Y position offset (default: 0)
<div class="player">
<tg-collider name="player" width={32} height={32} />
</div>Handles touch input with swipe detection.
onSwipeUp: Function - Called when swiping uponSwipeDown: Function - Called when swiping downonSwipeLeft: Function - Called when swiping leftonSwipeRight: Function - Called when swiping right
<tg-touch-controller
onSwipeUp={() => handleSwipe('up')}
onSwipeDown={() => handleSwipe('down')}
onSwipeLeft={() => handleSwipe('left')}
onSwipeRight={() => handleSwipe('right')}
>
<div class="touch-area">
<!-- Touch area content -->
</div>
</tg-touch-controller>- Install the package:
npm install tg-engine- Import the components in your project:
import { defineCustomElements } from 'tg-engine/loader';
defineCustomElements();- Use the components in your HTML/JSX:
<tg-sprite-map
src="path/to/tiles.png"
width={8}
height={8}
tileWidth={16}
tileHeight={16}
hFrames={10}
vFrames={10}
scale={5}
tiles={tiles}
/>Contributions are welcome! Please read our contributing guidelines to get started.
This project is licensed under the MIT License - see the LICENSE file for details.
- Adjust the example code blocks to match the actual properties and behavior of your components.
- Ensure that all examples are simple and clear to help users understand how to use each component effectively.
- If you have detailed documentation or a demo site, consider linking to these resources for more comprehensive guidance.