Skip to content

Instantly share code, notes, and snippets.

View wilbefast's full-sized avatar

William Dyce wilbefast

View GitHub Profile
@wilbefast
wilbefast / Folderise.cs
Created February 10, 2015 13:11
Useful class for automatically sorting game objects in Unity
using UnityEngine;
public class Folderise : MonoBehaviour
{
public string folderName = "";
void Start ()
{
// add to folder
if(folderName == "")
@wilbefast
wilbefast / fudge.lua
Created January 11, 2015 21:31
Example use of Bradshaw's 'fudge' sprite-packing library with love 2D
-- https://github.com/Bradshaw/Fudge
function love.load(arg)
-- set save directory
love.filesystem.setIdentity("fudge-test")
-- initialise fudge texture packer
fudge.set({ monkey = true })
-- prepare sprite packs
if FORCE_FUDGE_REPACK
or not love.filesystem.exists("background.png")
@wilbefast
wilbefast / TrophySet.cpp
Last active August 29, 2015 14:11
Syntax for declaring a constant method which returns an array of constant booleans
#include "TrophySet.h"
// Wow, you mean it actually gets worse?!?
const bool (&TrophySet::GetTrophies() const)[N_TROPHIES]
{
return trophies;
}
@wilbefast
wilbefast / Achievements.cpp
Last active August 29, 2015 14:11
Using a minimal anonymous class to implement a Listener and link it to a static object (C-style module)
#include "Achievements.h"
#include "GameplayEvents.h"
namespace Achievements
{
namespace // prefer anonymous namespaces of top-level 'static' keyword
{
void _unlock(size_t achievementId)
{
// code to implement achievement goes here
@wilbefast
wilbefast / GameplayEvents.cpp
Created December 8, 2014 16:05
Fun with C++11 variadic macros
// call lambda on all listeners
static void _dispatch(std::function<void(GameplayEvents::Listener*)> listenerEvent)
{
for(size_t i = 0; i < _n_listeners; ++i)
listenerEvent(_listeners[i]);
}
// a little syntactic sugar
#define _DISPATCH(CALL, ...) { _dispatch([__VA_ARGS__](GameplayEvents::Listener* l){ l->CALL##(##__VA_ARGS__##); }); }
@wilbefast
wilbefast / postObject.js
Created November 26, 2014 13:04
How not to trigger a post using Javascript
var postObject = function(url, object)
{
var form_html = '<form action="' + url + '" method="post">';
for(var key in object)
form_html += '<input type="text" name="' + key + '" value="' + object[key] + '" />';
form_html += '</form>';
var form = $(form_html);
$('body').append(form);
form.submit();
@wilbefast
wilbefast / index.html
Created November 4, 2014 23:11
Minimal code for WebSpeech using Chrome
<!DOCTYPE html>
<html>
<button id="start_button" onclick="startButton(event)">
<img alt="Start" id="start_img" src="mic.gif">
</button>
<div id="results">
<span class="final" id="final_span"></span>
@wilbefast
wilbefast / script_selector.html
Created November 4, 2014 10:14
Script which attaches another script to an HTML page depending on run-time logic
<html>
<script type="text/javascript">
(function(){
var script = document.createElement('script');
script.type = "text/javascript";
// in real-life situations you'll want to check the query string or something to choose the script ;)
script.src = (math.random() > 0.5) ? "a_script.js" : "other_script.js";
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
@wilbefast
wilbefast / LambdaParty.cpp
Last active August 29, 2015 14:07
Fun with C++11 lambdas in Cocos2D-X
bool Grid::IsBusy()
{
return ForEach([](int col, int row, Cell *cell) { return cell->IsBusy(); });
}
bool Grid::ForEach(std::function<bool(int col, int row, Cell*)> f)
{
for (int col = 0; col < m_Width; ++col)
for (int row = 0; row < m_Height; ++row)
if(!f(col, row, m_Cells[col][row]))
@wilbefast
wilbefast / shuffle.lua
Last active August 29, 2015 14:06
Shuffling tables and iterating them in a random order
--[[
What if you want to pick a random option but you're not sure if that random option will be valid?
Instead of doing a while loop and hoping that you'll eventually land on a valid option
try shuffling the list of options and trying each one in this random order ;)
W.
--]]
local shuffle = function(table) -- standard Fisher-Yates implementation
for i = #table, 2, -1 do