Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View 1bardesign's full-sized avatar

Max Cahill 1bardesign

View GitHub Profile
@1bardesign
1bardesign / ecs_writeup.md
Last active June 2, 2020 13:42
Hopefully straightforward writeup on the design of my lua-side "ecs", which I should find a better name for.

Components:

  • Managed by systems, self-contained as much as possible
  • May have references to components in other systems that they're dependent on (eg a sprite might want a position, though it may accept any vector (shared or unshared) for that position)
  • Generally take those references in their constructor
  • Generally designed to have a nice oop-y method interface, rather than be "just" data - eg sprite:set_frame(fx, fy), animation:reset() etc

Systems:

  • Do stuff with their set of components
--baseline vs churn "try it yourself" love demonstration
--byo jprof
PROF_CAPTURE = true
local prof = require("jprof")
--starting parameters
local churn = 1e3
local baseline = 1e5
local baseline_change_per_frame = 1e3
@1bardesign
1bardesign / loop_benchmark.lua
Last active April 10, 2022 04:27
a microbenchmark comparing different loop types, and different work size per-iteration
--config
local replicas = 3
local test_duration = 0.5
local single_test_size = 1e3
--per-iteration operation
function test_operation(a, b, work_size)
for _ = 1, work_size do
local s = tostring(a + b)
@1bardesign
1bardesign / dialogue.lua
Created June 12, 2019 02:32
example dialogue system for love2d based on coroutines - runnable love file prepared here: https://www.dropbox.com/s/0n6vs634jzlk266/dialogue_co.love
--dialogue type
local dialogue = {}
dialogue._mt = {
__index = dialogue,
}
function dialogue:new(f)
return setmetatable({
@1bardesign
1bardesign / main.lua
Created May 27, 2019 23:49
state machine example code - to help understand how to define states, and how to use them to keep related logic all in one place
--require the state machine module
local state_machine = require("state_machine")
--variable for the example state machine - set up in love.load
local example_machine
--(simple "last 10" history of what states we've been to)
local state_history = {}
function add_history(s)
table.insert(state_history, s)
@1bardesign
1bardesign / stable_sort.lua
Last active July 7, 2022 00:03
stable sorting routines for lua
-- stable sorting routines for lua
--
-- modifies the global table namespace so you don't have
-- to re-require it everywhere.
--
-- table.stable_sort
-- a fast stable sort
-- table.unstable_sort
-- alias for the builtin unstable table.sort
-- table.insertion_sort
@1bardesign
1bardesign / oo.lua
Last active February 6, 2019 04:41
state management code - fsm and a push/pop "game state manager" - see state_machine.lua for the main interesting bit
--[[
oop basics
]]
function class(inherits)
local c = {}
c.__mt = {__index = c}
--handle single inheritence
if type(inherits) == "table" and inherits.__mt then
setmetatable(c, inherits.__mt)
@1bardesign
1bardesign / audio.lua
Last active November 14, 2018 02:01
will not work out of the box, as it relies on my class() impl and table.foreach, but should be fairly readable as a jumping off point!
--[[
audio manager
]]
--sound source wrapper
local sound = class()
--
function sound:new(source, volume, set)
return self:init({
@1bardesign
1bardesign / main.lua
Created November 13, 2018 00:10
recolour.lua
--[[
example use
]]
local recolour = require("recolour")
--we want to recolour this asset image
local to_recolour = love.image.newImageData("path/to/image.png")
--using this palette image
local palette = love.image.newImageData("path/to/palette.png")
@1bardesign
1bardesign / typewriter_demo.lua
Created October 5, 2018 07:03
a simple line by line "typewriter" text demo for love2d
--[[
typewriter class
]]
local typewriter = {}
--(metatable used for instances, don't worry about it too much
-- but do read up on metatables at some point)
typewriter._mt = {__index = typewriter}
--create a new typewriter