Skip to content

Instantly share code, notes, and snippets.

View drhayes's full-sized avatar
🐱

David Hayes drhayes

🐱
View GitHub Profile
@drhayes
drhayes / Gruntfile.js
Last active December 16, 2015 06:39
Custom sprite multitask
var NUM_SPRITE_COLS = 6;
var gm = require('gm');
grunt.registerMultiTask('sprite', 'Sprite images', function() {
var images = this.data;
var numRows = Math.ceil(images.length / NUM_SPRITE_COLS);
var sprite = gm('');
sprite.tile(NUM_SPRITE_COLS + 'x' + numRows);
sprite.quality('100');
sprite.background('transparent');
@drhayes
drhayes / Gruntfile.js
Created April 16, 2013 04:39
sprite multitask config
sprite: {
'media/player.png': [
'build/player-idle001.png',
'build/player-kneel001.png',
'build/player-kneel002.png',
'build/player-kneel003.png',
'build/player-kneel004.png',
'build/player-kneel005.png',
'build/player-walk001.png',
'build/player-walk002.png',
@drhayes
drhayes / README.md
Last active April 16, 2016 02:07
Entity Factory in Phaser with Webpack

entityFactory Overview

I have a lot of classes extending Phaser.Sprite. Building each one by hand via some if statement when loading a tilemap is dumb. Thankfully, webpack is here to rescue me.

Explain

The full code of entityFactory.js is below.

Say you've got a directory full of sprites, each one a subclass of Phaser.Sprite:

@drhayes
drhayes / README.md
Last active July 7, 2016 18:04
Scaled, pixel-perfect drawing in Phaser using a second canvas
var idableLayers = ['entities', 'switches']
module.exports = function(content) {
this.cacheable && this.cacheable();
var map = JSON.parse(content);
var pathBits = this.resourcePath.split('/');
var mapFilename = pathBits[pathBits.length - 1];
if (map.layers) {
map.layers.forEach(function(layer) {
if (idableLayers.indexOf(layer.name) === -1 ) {
@drhayes
drhayes / gist:d71b2c4a3d07eb0776e5db7783df0802
Created October 27, 2016 19:28
SHA-1 a string in the browser
function sha1(str) {
function hex(buffer) {
const hexCodes = [];
const view = new DataView(buffer);
for (let i = 0; i < view.byteLength; i += 4) {
// Using getUint32 reduces the number of iterations needed (we process 4 bytes each time)
const value = view.getUint32(i)
// toString(16) will give the hex representation of the number without padding
const stringValue = value.toString(16)
// We use concatenation and slice for padding.
Here they are!
@drhayes
drhayes / shockwave.js
Created December 15, 2016 05:23
Phaser plugin that draws a shockwave as an expanding line
const size = 48;
const maxFrames = 6;
const blastRadius = 23;
export default class Shockwave extends Phaser.Plugin {
constructor(game, parent) {
super(game, parent);
this.firing = false;
this.x = this.y = 0;
import forsaken from '../forsaken';
export const FACTORY = (game, mapEntity) => {
const { x, y, width, height, properties: { id, reversible = true } } = mapEntity;
return new StoneSmasher(game, id, x, y, width, height, reversible);
}
export const GROUP_NAME = 'obstacles';
const frame = i => `stoneSmasher${i}`;