- Notifications
You must be signed in to change notification settings - Fork37
A sandbox framework to fast-prototype tile-based games in HTML5 and JavaScript
License
cstoquer/pixelbox
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Pixelbox takes inspiration from fantasy consoles likePICO8 and game creation frameworks like Unity3D.
From version 2, the Pixelbox package no longer includes the editor. Instead, the editor is now a standalone application that can be downloadedon Itch.io.
A Pixelbox project has the following structure:
assets/ ├── tilesheet.png ├── palette.png └── maps.jsonaudio/build/src/ └── main.jstools/node_modules/project.pixelboxpackage.jsonindex.htmlassets/is where game assets go (images, text files, JSON)audio/is where sounds and music gosrc/is the source code folder, andmain.jsis the entry file of the game
The game entry point is thesrc/main.js file. If you provide anexports.update function, Pixelbox will call it every frame.
Building is done usingbrowserify which gives you access torequire (orimport) andexports to easily modularize your project.
Pixelbox automatically loads all assets at startup, before executing the game code. All supported files added inside theassets/ directory will be available in theassets global object. The structure follows the structure of the directory. For instance, the file located inassets/sprites/player.png will be accessible withassets.sprites.player.
Supported files include:
- images (
.png,.jpg) - JSON formatted data (
.json) - plain text files (
.txt,.css,.vert,.frag)
You have direct access to the content of JSON files.
Because files are loaded inside theassets object and refered wthout their extension, you cannot have a file and a directory with the same name inside the same directory.
Pixelbox exposes the following methods directly on the global scope:
cls()clear screen withpaper colorsprite(n, x, y [,flipH [,flipV [, flipR]]])draw sprite numbernon screen at pixel position(x, y)flipHandflipVcan be used to flip sprites horizontally or vertically,flipRadds a 90 degree clockwise rotationdraw(image, x, y [,flipH [,flipV [, flipR]]])draw anImage,Texture orTileMap on screen at pixel position(x, y)tilesheet(image)change image used as default tilesheetrect(x, y, w, h)stroke a rectangle withpen colorrectf(x, y, w, h)fill a rectangle withpaper colorcamera(x, y)scroll add further drawing by provided position
Pixelbox has a predefined"minitext" bitmap font that you can use to print text on screen or in textures.Minitext is available by default, but can be disabled in the project settings.
print(text, [x, y])if x, y is provided, printtextat pixel position (x,y);else print text at cursor current positionprintln(text)printtextand feed new line;when the cursor reaches the bottom of the screen, vertical scroll is applied(just like it would happen in a terminal)locate(i, j)set cursor position at columnilinejpen(colorId)set text color tocolorIdin color palettepaper(colorId)set paper color tocolorIdin color palette
btnstate of the buttons — by default, available buttons are:up,down,left,right,A,B(buttons names and binding can be configured in the project settings)btnpwhether button has been pressed during current framebtnrwhether button has been released during current frame
sfx('sound');play the sound.mp3 file inaudio/foldermusic('bgm');play the bgm.mp3 file in loop; if another music is already playing, it will cross fade to the new music; if nosoundIdis provided, music stops
AudioManager is the module that handles audioloading and playback. You have access to its instance onaudioManager.
clamp(value, min, max)clip a value between min and maxchr$(n)return a character from codenrandom(n)return a randominteger between 0 and ninherits(Child, Parent)make classChild inherit from classParent
Texture is a canvas that can be drawn, and inside which things can be drawn. In Canvas2D mode, it is implemented with a HTML canvas. In WebGL mode, it is implemented with a GLTexture2D.
The main screen (accessible by the global variable$screen) is an instance of Texture and most of its methods are accessible from the global scope.
To create new texture, you need to require theTexture module:
varTexture=require('pixelbox/Texture');vartexture=newTexture(128,128);// create a new texture of 128 by 128 pixels
texture.resize(width,height);texture.setPalette(palette);texture.pen(colorIndex);// set PEN color index from palette (pen is used for text and stroke)texture.paper(colorIndex);// set PAPER color index from palette (paper is used for fill)texture.setTilesheet(tilesheet);// set tilesheet used for this texture
A tilesheet is an Image containing 256 sprites organized in a 16 x 16 grid (the size of the tilesheet depend of the sprite size you set for your game).
texture.clear();// clear texture (it becomes transparent)texture.cls();// clear screen (the whole texture is filled with the PAPER color)texture.sprite(sprite,x,y,flipH,flipV,flipR);// draw a sprite from current tilesheet in the texturetexture.draw((img,x,y,flipH,flipV,flipR);// draw an image (or Texture or Map) in the texturetexture.rect(x,y,width,height);// stroke a rectangletexture.rectf(x,y,width,height);// fill a rectangle
texture.locate(i,j);// set text cursor to specified locationtexture.print(text,x,y);// print some texttexture.println(text);// print some text and feed a new line
Pixelbox has a built-inTileMap component.A TileMap consist of:
- A name
- A tilesheet — when the tilesheet is changed, the whole map will be redrawn with the new tilesheet
- A grid of sprites from the tilesheet plus few flags to flip or rotate sprites
Once created, a tile map is rendered in one draw call only.
TileMap can be used to render a level made of sprites, or just to store game data.
You can create tile maps from your game code; But usually, you will be using Pixelbox's tools (see the Tools section below) to create and manage your maps as game assets. A map can then be retrieved by its name with Pixelbox'sgetMap function. The tile map can then be drawn on screen (or in another Texture), modified, copied, pasted, resized, etc.
When stored in assets, the map is compressed to Pixelbox format to reduce file size.
varmap=getMap('mapName');// get a tile map by its name
To create new maps, you need to require theMap module:
varTileMap=require('pixelbox/TileMap');varmap=newTileMap(16,16);// create a new tile map of 16 by 16 tiles
map.draw(x,y);// draw map on screen at [x, y] positiondraw(map,x,y);// idem, using the global draw functiontexture.draw(map,x,y);// draw a map in another texturemap.setTilesheet(tilesheet);// set tilesheet to use for this map// The whole map is redrawn when calling this function
map.get(x,y);// returns the Tile at position [x, y]. null if emptymap.set(x,y,tile,flipH,flipV,flipR,flagA,flagB);// add a tilemap.remove(x,y);// remove tile at position [x, y]. (set it to null)map.find(tile,flagA,flagB);// find all tiles with specified properties
map.resize(width,height);// resize the map (size unit is tiles)map.clear();// Reset the whole map content by setting all its tiles to nullvarmapCopy=map.copy(x,y,width,height);// copy this map to a new one// x, y, width, height can be specified to copy only a rectangular part of the mapmap.paste(mapCopy,x,y,merge);// paste map data in the map at position offset [x, y]// if 'merge' flag is set, then null tiles will not overwrite current map tile
Thegamepad module allows for easy access to gamepads if the browser supports it. When the gamepad feature is enabled in the project settings, you get access to these objects on the global scope:
gamepads[id];// get gamepad state. id is a number in the range [0..4] (4 is computer keyboard)gamepad;// Merge states of all gamepads and return a global gamepad state.
Gamepad state works like keyboard controls: You get the state of each button, button presses and button releases, plus the values of analog controls.
varstate=gamepads[0];// get state of gamepad id 0// buttons:state.btn.A;// state of A buttonstate.btn.B;// state of B buttonstate.btn.X;// state of X buttonstate.btn.Y;// state of Y buttonstate.btn.start;// state of 'start' buttonstate.btn.back;// state of 'back' buttonstate.btn.up;// directionnal pad's up buttonstate.btn.down;// directionnal pad's down buttonstate.btn.left;// directionnal pad's left buttonstate.btn.right;// directionnal pad's right buttonstate.btn.lb;// left bumper buttonstate.btn.rb;// right bumper buttonstate.btn.lt;// left trigger buttonstate.btn.rt;// right trigger button// button press and release.// the structure is the same as state.btn but the values are true only// on button press or release.state.btnp;// button pressstate.btnr;// button release// analog valuesstate.x// x axe value (first stick horizontal)state.y// y axe value (first stick vertical)state.z// z axe value (second stick horizontal)state.w// w axe value (second stick vertical)state.lt// left trigger analog valuestate.rt// right trigger analog value
Pixelbox editor is bundled with a music tracker calledPataTracker.The tracker player must be enabled in the project settings. Player allows to directly plays the songs in thejson formatted tracker files.
PataTracker player is exposed as apatatracker global variable.
patatracker.playSong(songNumber);patatracker.stop();
PataTracker automatically loads the project's album data (assets/patatracker.json). If you need to load a different album, you can do it with the following API:
patatracker.loadData(data);
Bleeper is the sound effect editor of Pixelbox. Like for PataTracker, it must be enabled in the project settings.Note that Bleeper depends on theAudioManager component.
There are several ways to play Bleeper sounds:
If the sound is named, it is accessible on theassets global, and automatically added to AudioManager.
// from assets globalassets.bleeper.mySound.play(volume,panoramic,pitch);// all parameters optional// using audioManagersfx('mySound',volume,panoramic,pitch);// using default channelaudioManager.playSound('sfx','mySound',volume,panoramic,pitch);
The Bleeper module exposes an array of all sounds defined in the program.
varbleeper=require('pixelbox/bleeper');bleeper.sounds[3].play(volume,panoramic,pitch);
About
A sandbox framework to fast-prototype tile-based games in HTML5 and JavaScript
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors6
Uh oh!
There was an error while loading.Please reload this page.
