Platfuse is a lightweight, highly customizable game engine built with TypeScript, designed to make 2D game development in web environments both straightforward and efficient.
To get started with Platfuse, you'll need to have Node.js installed on your system.
Use boilerplate template for your first Platfuse game:
npx platfuse your-game-name
It will create basic game project and install all required dependencies
Navigate to the created game directory:
cd your-game-name
Start the development build:
npm run start
Or build:
npm run build
Define your game configuration and preload assets:
const gameConfig = {
debug: true, // Debug enabled
global: true, // Platfuse as a global `window` object.
pixelPerfect: true, // Whether the `image-rendering` should be `pixelated`
fixedSize: [800, 600], // Fixed `canvas` size
backgroundColor: '#000000',
primaryColor: '#FFFFFF',
/** Available scenes */
scenes: {
MyMainScene
}
}
const preloadAssets = {
player: 'path/to/player.png',
background: 'path/to/background.png'
}
Create your first scene by extending the Scene
class and implementing the required methods.
Initialize the game engine with your configuration and assets:
const myGame = new Game(gameConfig, preloadAssets)
Start your game with your main scene:
await myGame.start('MyMainScene')
The Game
class key features and components:
GameConfig
object.Example:
import { Game } from 'platfuse'
import { MainScene } from './scenes'
import { Enemy, Player } from './models'
import playerImage from './assets/images/player.png'
import enemyImage from './assets/images/enemy.png'
import tilesetImage from './assets/images/tileset.png'
import sound from './assets/sounds/sound.mp3'
const gameConfig = {
fixedSize: [1280, 720], // Optional, can be used to maintain fixed aspect ratio of the game view.
entities: {
// Classes which will be used when creating objects defined in the tmx file.
// Key values should correspond to the object classes defined in the map.
enemy: Enemy,
player: Player
},
scenes: {
MainScene
}
}
const preloadAssets = {
'player.png': playerImage,
'enemy.png': enemyImage,
'tileset.png': tilesetImage,
'sound.mp3' sound
}
const game = new Game(gameConfig, preloadAssets)
await game.start('MainScene')
The Scene class key functionalities include:
Example:
import { Scene } from 'platfuse'
import { CustomLayer } from './layers/custom-layer'
class MainScene extends Scene {
/**
* Initialize scene with map data from *.tmx file.
* Generate tilesets, layers and game objects.
*/
tmxMap = 'map.tmx'
/**
* Set global gravity value for physics
*/
gravity = 0.05
init() {
this.camera.setScale(4) // Set camera scale to 4
this.addLayer(CustomLayer, 1) // Add custom layer with render order 1
this.setTileCollisionLayer(2) // Set tiles collision data from TMX layer #2
console.log('Main Scene initialized', this)
}
/* Scene lifecycle methods */
update() {}
draw() {}
postUpdate() {}
}
The Layer
class is designed for managing layers within a scene, particularly to use tile-based maps. It integrates with the tmx-map-parser
library for handling TMX layer data. Key features and functionalities include:
layerCanvas
specifically for the layer, onto which tile graphics are rendered based on the layer's tile data. This is particularly useful for optimizing rendering performance in web-based applications.Scene
object (representing the overall scene or map) and can contain Entity
objects, allowing for a structured and hierarchical organization of game elements.In addition to handling standard tile and image layers, the Layer
class supports the definition of custom function layers. These layers allow to implement specialized rendering or update logic that goes beyond static tile or image displays. This feature is particularly useful for dynamic content or interactive elements within a scene that cannot be adequately represented by static tiles or images alone.
Key aspects of custom function layers include:
To define a custom function layer, You should extend the Layer
class and override the draw
method with their custom drawing logic. Additionally, any necessary update logic can be implemented in an update
method, which should also be called from the game's main loop.
Example:
class CustomFunctionLayer extends Layer {
constructor(scene: Scene) {
super(scene)
}
// Optional update logic here
update() {}
// Custom rendering logic here
draw() {}
// Optional post-update logic here
postUpdate() {}
}
The Entity
class incorporates a simplified physics model for game development, focusing on movement, collision, and force application. Here's a summary of its physics-related features:
maxSpeed
for entities, ensuring that their movement remains within realistic bounds.collideWithTile
, collideWithTileRaycast
and other entities collideWithObject
, enabling interaction with the game environment and other entities.update
method integrates physics calculations with animation updates, ensuring that entity movements are both visually and physically consistent.This physics model provides a foundation for creating dynamic and interactive game experiences, allowing entities to move, collide, and interact in a realistic manner.
Example:
import { Entity, vec2 } from 'platfuse'
class Player extends Entity {
image = 'player.png' // Asset name to be used for drawing.
size = vec2(1, 1.5) // Entity size (in tiles).
solid = true // Entity is solid and physics affects it.
collideTiles = true // Entity collide with tiles.
collideObjects = true // Entity collite with other solid Entities.
// Custom update logic here.
update() {}
}
For detailed documentation on the Platfuse Game Engine, including advanced configuration options, please refer to the official documentation.
We welcome contributions to the Platfuse Game Engine! Please read our contributing guidelines before submitting pull requests.
Platfuse is licensed under the MIT License - see the LICENSE file for details.
The project is in a state of continuous development and improvement. For the latest changes, please refer to the CHANGELOG.md.