Forge2djs is a simple JavaScript framework for creating 2D games or interactive applications using HTML5 Canvas. It allows you to easily add objects, apply physics, manage the camera, and handle game logic with an intuitive API.
To get started, create a new instance of Forge2djs by calling Forge.int() with configuration options such as canvas background color and screen dimensions. Example:
<script src="https://forge2djs.vercel.app/forge.js"></script>
// Create a new Forge2djs game instance
const game = Forge.int({ color: '#eaeaea', screen: [800, 600] });
This creates a new Forge2djs game instance with a white background and screen dimensions of 800x600 pixels.
You can create different objects such as cubes or text, and then add them to the game scene using addEntry().
Example: Create and add a cube:
// Create a red cube and add it to the game
const cube = game.cube({ width: 100, height: 100, color: 'red' });
game.addEntry(cube);
This creates a red cube of size 100x100 and adds it to the game scene.
Example: Create and add text:
// Create a text object and add it to the game
const text = game.addText({ text: 'Welcome to Forge2djs!', x: 200, y: 100 });
game.addEntry(text);
This creates a text object that says "Welcome to Forge2djs!" at position (200, 100) on the canvas.
The game loop is where all updates and rendering happen. Call game.loop() inside a loop to update and render the objects. Example:
function gameLoop() {
game.loop(); // Update and render the game
requestAnimationFrame(gameLoop); // Repeat the game loop
}
gameLoop(); // Start the game loop
This sets up the game loop, which updates the scene every frame and calls the game.loop() method.
Forge2djs supports simple physics like gravity and friction. You can enable physics for objects by passing physics: true when creating them.
Example: Create a cube with physics enabled:
// Create a cube with physics enabled
const physicsCube = game.cube({ width: 50, height: 50, color: 'blue', physics: true });
game.addEntry(physicsCube);
This cube will fall due to gravity and apply friction when moving.
To enable camera movement, use game.setCamera() to change the camera's position. This can be useful for scrolling or panning the scene.
Example: Move the camera:
// Set the camera position to (100, 200)
game.setCamera(100, 200);
This moves the camera to coordinates (100, 200), effectively shifting the view of the canvas.
Use the isCollidingWith() method to check if two objects collide. This method uses axis-aligned bounding box (AABB) collision detection.
Example: Check for collision between two objects:
if (cube.isCollidingWith(physicsCube)) {
console.log('The cube is colliding with the physics cube!');
}
This checks if cube is colliding with physicsCube and logs a message if they are.
Forge2djs offers several advanced features:
gameObject.shape.Forge2djs supports both radial and directional light sources. You can add light sources to the scene using addLightSource and addDirectionalLight.
Example: Add a radial light source:
// Add a radial light source at position (100, 100) with a radius of 50
game.addLightSource({ x: 100, y: 100, radius: 50, color: 'rgba(255, 255, 255, 0.5)' });
Example: Add a directional light source:
// Add a directional light source at position (200, 200) with an angle of 45 degrees
game.addDirectionalLight({ x: 200, y: 200, angle: 45, distance: 200, color: 'rgba(255, 255, 255, 0.5)' });
These light sources will be rendered in the game loop, creating dynamic lighting effects.
Forge2djs includes a particle system for creating effects like explosions, smoke, or fire. You can create a particle system using addParticleSystem.
Example: Create a particle system:
// Create a particle system at position (300, 300) with 10 particles
game.addParticleSystem({ x: 300, y: 300, count: 10, colors: ['white', 'red'], sizeRange: [1, 3], lifeRange: [50, 100] });
This creates a particle system that generates particles with random colors, sizes, and life durations.
You can define custom shapes for game objects using the shape property. This allows you to create complex shapes beyond simple rectangles.
Example: Create a custom shape:
// Create a custom shape using an array of points
const customShape = game.cube({ width: 100, height: 100, color: 'green', shape: [[0, 0], [50, 50], [100, 0]] });
game.addEntry(customShape);
This creates a custom triangular shape using the provided points.
You can remove objects from the game scene using the removeEntry method. This is useful for cleaning up objects that are no longer needed.
Example: Remove an object from the game:
// Remove the cube object from the game
game.removeEntry(cube);
This removes the cube object from the game, stopping it from being rendered or updated.
Forge2djs supports applying textures to game objects. You can specify a texture when creating an object using the texture property.
Example: Create a cube with a texture:
// Create a cube with a texture
const texturedCube = game.cube({ width: 100, height: 100, texture: 'path/to/texture.png' });
game.addEntry(texturedCube);
This creates a cube with the specified texture applied to it. The texture will be rendered on the cube in the game loop.
Here’s a full example that demonstrates how to create a simple Forge2djs game with all the features discussed:
// Create a new Forge2djs game instance
const game = Forge.int({ color: '#eaeaea', screen: [800, 600] });
// Create a red cube and add it to the game
const cube = game.cube({ width: 100, height: 100, color: 'red' });
game.addEntry(cube);
// Create a text object and add it to the game
const text = game.addText({ text: 'Welcome to Forge2djs!', x: 200, y: 100 });
game.addEntry(text);
// Create a cube with physics enabled
const physicsCube = game.cube({ width: 50, height: 50, color: 'blue', physics: true });
game.addEntry(physicsCube);
// Add a radial light source
game.addLightSource({ x: 100, y: 100, radius: 50, color: 'rgba(255, 255, 255, 0.5)' });
// Add a directional light source
game.addDirectionalLight({ x: 200, y: 200, angle: 45, distance: 200, color: 'rgba(255, 255, 255, 0.5)' });
// Create a particle system
game.addParticleSystem({ x: 300, y: 300, count: 10, colors: ['white', 'red'], sizeRange: [1, 3], lifeRange: [50, 100] });
// Start the game loop
function gameLoop() {
game.loop(); // Update and render the game
requestAnimationFrame(gameLoop); // Repeat the game loop
}
gameLoop(); // Start the game loop