Skip to main content

Core API

Essential GameByte API (~2000 tokens).

Setup​

import { createGame, BaseScene, UIButton } from '@gamebyte/framework';

const game = createGame();
await game.initialize(canvas, '2d'); // '2d' | '3d' | 'hybrid'
game.start();

Services​

// Get services
const renderer = game.make('renderer');
const sceneManager = game.make('scene.manager');
const input = game.make('input');
const assets = game.make('assets');

Scenes​

class GameScene extends BaseScene {
constructor() {
super('game', 'Game Scene');
}

async initialize(): Promise<void> {
await super.initialize();
// Setup code
}

update(deltaTime: number): void {
super.update(deltaTime);
// Game logic
}
}

// Register and switch
sceneManager.add(new GameScene());
await sceneManager.switchTo('game');

UI Components​

Button​

const btn = new UIButton({
text: 'PLAY',
width: 200,
height: 60,
backgroundColor: 0x4CAF50
});
btn.setPosition(300, 270);
btn.on('click', () => console.log('clicked'));
scene.container.addChild(btn.getContainer());

TopBar​

const topBar = new TopBar({
width: 800,
items: [
{ id: 'coins', type: TopBarItemType.RESOURCE, icon: coinTexture, value: 0 }
]
});
topBar.updateItem('coins', 100, true);

ProgressBar​

const health = new UIProgressBar({
width: 150, height: 16,
value: 100, maxValue: 100,
color: 0x22c55e
});
health.setValue(75, true);

Input​

// Keyboard
input.keyboard.on('Space', (pressed) => { if (pressed) jump(); });

// Touch
input.touch.on('tap', (e) => handleTap(e.x, e.y));

// Polling
if (input.keyboard.isPressed('KeyW')) moveForward();

Layout (Flexbox)​

import { LayoutPresets, GameLayoutPresets, createFlexRow } from '@gamebyte/framework';

// Enable flexbox on container
container.layout = {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
gap: 20
};

// Use presets
topBar.layout = LayoutPresets.topBar;
screen.layout = GameLayoutPresets.gameScreen;

// Helper functions
row.layout = createFlexRow({ gap: 16, justify: 'space-between' });

Physics​

// 2D
Physics.create2DWorld({ gravity: { x: 0, y: 1 } });
const body = Physics.createBody({ x: 100, y: 100, width: 32, height: 48 });
Physics.onCollision('player', 'enemy', (a, b) => takeDamage());

// Update in loop
Physics.update(deltaTime);

Audio​

// Music
Music.play('bgm', { loop: true, volume: 0.7 });

// SFX
SFX.play('click');

// Volume
Audio.setMasterVolume(0.8);

Assets​

await Assets.load([
{ key: 'player', url: 'player.png', type: 'texture' },
{ key: 'bgm', url: 'music.mp3', type: 'audio' }
]);
const texture = Assets.get('player');

Transitions​

await sceneManager.switchTo('game', {
type: 'fade',
duration: 500
});

Key Types​

type RenderMode = '2d' | '3d' | 'hybrid';
type TransitionType = 'none' | 'fade' | 'slide' | 'zoom';

interface Vector2 { x: number; y: number; }
interface Vector3 { x: number; y: number; z: number; }