Skip to content

Instantly share code, notes, and snippets.

View drhayes's full-sized avatar
🐱

David Hayes drhayes

🐱
View GitHub Profile
@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' };
Verifying my Blockstack ID is secured with the address 1M1QN2SWzH4ecxeDgAjFDii1F6hSrGswLj https://explorer.blockstack.org/address/1M1QN2SWzH4ecxeDgAjFDii1F6hSrGswLj
@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)
@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 / 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 / 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 / syncFacingSchemes.lua
Last active August 27, 2018 14:33
Slerp between rotations without doing the flippy thing.
local ax, ay = math.cos(fov.angle), math.sin(fov.angle)
local gx, gy = math.cos(goalAngle), math.sin(goalAngle)
ax, ay = ax + gx * 5 * dt, ay + gy * 5 * dt
fov.angle = math.atan2(ay, ax)
@drhayes
drhayes / brain.lua
Created March 22, 2019 17:11
An old coroutine-based AI
local lume = require 'lib/lume'
local Brain = {}
local currentTime = 0
local timeWaits = {}
local animWaits = {}
local function waitSeconds(secs)
local co = coroutine.running()
timeWaits[co] = currentTime + secs
@drhayes
drhayes / brain.lua
Created March 22, 2019 17:13
An old, busted version of coroutine-based AI
local Brain = {}
local currentTime = 0
local timeWaits = {}
local animWaits = {}
local yields = {}
local function yield()
local co = coroutine.running()
yields[co] = true
return coroutine.yield(co)
@drhayes
drhayes / README.md
Created April 1, 2019 15:54
SCRIPT-8