Skip to content

Instantly share code, notes, and snippets.

View drhayes's full-sized avatar
🐱

David Hayes drhayes

🐱
View GitHub Profile
@drhayes
drhayes / camera.lua
Created March 9, 2018 15:37
LÖVE camera shake
self.noiseFactor = self.noiseFactor + dt
-- self.trauma = math.min(1, self.trauma) - dt * config.camera.traumaFactor
self.trauma = math.max(0, self.trauma - dt * config.camera.traumaFactor)
if self.cameraTarget then
self.x = self.x + (self.cameraTarget.pos.x - self.x) * dt * self.slideFactor
self.y = self.y + (self.cameraTarget.pos.y - self.y) * dt * self.slideFactor
end
-- Shake based on trauma
local shake = self.trauma ^ 2
local maxOffset, maxAngle = config.camera.shake.maxOffset, config.camera.shake.maxAngle
@drhayes
drhayes / camera.lua
Created March 9, 2018 15:33
LÖVE rescale
function Camera:resize(w, h)
local dw, dh = config.graphics.width, config.graphics.height
local x, y = w / dw, h / dh
local scale = math.min(x, y)
local width, height = dw * scale, dh * scale
self.camera:setScale(scale)
local wPadding, hPadding = (w - width) / 2, (h - height) / 2
self.padding = {
x = wPadding, y = hPadding
}
@drhayes
drhayes / drawSystem.lua
Created February 22, 2018 15:50
An ECS draw system I wrote.
local tiny = require 'lib.tiny'
local spriteAtlas = require 'core.spriteAtlas'
local lume = require 'lib.lume'
local physicsSystem = require 'systems.physics'
local config = require 'gameConfig'
function drawSystem(groupName)
groupName = groupName or 'default'
local drawSystem = tiny.processingSystem({ isDraw = true })
@drhayes
drhayes / drone.lua
Created February 15, 2018 19:23
The onDead method from drone enemies
local function onDead(world, drone)
Signal.emit('enemyKilled', drone)
local viralAmount = (drone.health.damage.viral or 0) / drone.health.max
if viralAmount >= 0.5 then
drone.animation.currentName = 'dead'
drone.body.fx = 50
drone.invoke = {
onInvoke = 'doLoot'
}
particles:viralSparks(drone)
Verifying my Blockstack ID is secured with the address 1M1QN2SWzH4ecxeDgAjFDii1F6hSrGswLj https://explorer.blockstack.org/address/1M1QN2SWzH4ecxeDgAjFDii1F6hSrGswLj
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}`;
@drhayes
drhayes / gist:6ea084f169a94e48fb0c40dd3974dc89
Last active January 30, 2017 19:36
In which I write about "this"

In JavaScript, this refers to the calling context of the function. Unlike other more traditional languages, in JS it is only possible to know the value of this within a function by examining, at runtime, what called the function. Let's look at some examples:

function blah() { console.log(this.cats); }

window.cats = 'dogs';

var o = { cats: 'pants' };
@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;
Here they are!
@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.