0.0.6 • Published 4 years ago
muffin-game v0.0.6
Muffin Game Engine
A game engine for web-playable video games using PixiJS.
This is alpha software. Until v1.0.0 I do not guarantee stability of the API.
Features
- Game Scenes contain Actors or other Scenes inside of them
- Create Actors by extending base classes, using builder functions, and/or nesting them
- Actors can be moved, tinted, scaled, rotated, translucent, etc.
- Keyboard, mouse, and touchscreen support
- No sound (yet!)
Getting Started
- Fork the muffin-game-js repo for a minimal JavaScript project using NPM.
- Check out Pipe Puzzle for a full working example project (it's a clone of Pipe Dream!)
How to Create a Game
JavaScript, using as an NPM package
npm install muffin-gameimport { createGame, getTexture } from "muffin-game/core/setuptools"createGamehas 3 positional arguments:- an array of strings: filenames of all the PNGs that must be preloaded
- a callback function to receive resources (PNGs) and return an object of sprite factory functions
- a GameOptions object which uses default values for missing keys:
assetPrefix: string with URL prefix for assets- default:
assets/
- default:
gameClass: the game class which gets instantiated- default: Game
postInit: a function that takes the Game instance as an argument- default: connects HTML element with id=pause_button
- The Game object has static properties:
loadingScene: This is the prevScene (previous scene) when the game starts or resets (after a game over, for example). It is never mounted, only unmounted.entryScene: This is typically the MenuScenegameOverScene: The default GameOverScene setsgame.state.functions.tickto an empty function, uses a Pauser to stop Pixi.js's interactivity, and resets the game when clicked.pauseScene: The default PauseScene is similar, but its tick function checks if the letter "P" is pressed, and it goes back to the prevScene whengame.state.flags.pausedis false.
- To create a custom menu scene, try "
Game.entryScene = MyCustomMenuScene" before calling createGame() - See setup.js from "Pipe Puzzle" for an example
TypeScript, developing the engine itself
npm installto get the dependencies- All source code goes in
/src - Load assets by copy-pasting and/or editing
examples/setup.ts - Creates scenes by subclassing/editing things inside
/scenes - Commands:
- Development build:
npm run dev - Production build:
npm run build
- Development build:
Integration on a website
- Look at
index.htmlfor an example of how to embed the game in a webpage- Basically you need
<script src="bundle.js">and a<div id="game">somewhere /assetsmust be accessible on the web
- Basically you need
- Score-sending functionality can be adapted using functions
send_scoreandhide_send_score_button. Seecompat.rainey_arcade.jsfor an example of how I'm doing this with Rainey Arcade
How it works, slightly more in depth
Containers
- Anything visible on-screen is a Pixi.js Container. A container is any drawing surface with children: the main "stage" of the game is a Container with the dimensions of the canvas.
- The main Containers you will use are Actors. These are subclasses of Pixi.js Sprites but with extra functionality to make designing games easier.
Scenes
- Scenes are the main "rooms" or "levels" of a game. Scenes receive events from the game, such as
tick, and relay these events to their Actors. They can be used to group related Actors together so they can be handled collectively. - Scenes may mount or unmount a Container to add or remove their Actors from it. Scenes can only mount 1 Container at a time.
Game Object
- The Game object has a current
sceneand a previous SceneprevScene. Use thechangeScene()method to change the current Scene properly. - Game object is instantiated when pre-loading assets is complete.
- Use
game.startTimer(func, frames)to time a function to trigger after a certain number of frames. This method returns the index of the timer created, so you can stop the timer early by usinggame.stopTimer(index) - The current
tickfunction which occurs on every game tick is stored ingame.state.functions.tickso that you can customize the gameplay loop at a higher level if needed.
Setup
- The "loading" text is drawn by
setup.tsso it happens as soon as possible - Game starts with
LoadingSceneas theprevScene, asking it to unmount the stage (which removes the loading text and anything else on the stage blindly) - Game then mounts the
MenuScene-- customize this scene for your game.
Grids
- A Grid is any class that owns an array of arrays with helper methods for managing it
- The
GridSceneis a type of Scene which implements the grid interface for its actors (it has an array of arrays of actors) - The
GridRectangleis part of aTileActorused to store a grid of textures (it has an array of arrays of rectangular regions)
Credits
See assets/README.md for graphics attribution