Skip to content

Instantly share code, notes, and snippets.

View townofdon's full-sized avatar

Don Juan Javier townofdon

View GitHub Profile
@townofdon
townofdon / AISightCone.cs
Created September 21, 2022 03:36
Enemy Sight Cone
using System.Collections;
using UnityEngine;
public class AIController : MonoBehaviour {
[SerializeField][Range(0f, 20f)] float sightRange = 5f;
[SerializeField][Range(0f, 1f)] float sightAngle = 0.3f;
GameObject player;
Vector3 headingToPlayer;
Vector3 sightHeading;
@townofdon
townofdon / ULayer.cs
Created January 30, 2022 00:08
Unity Layer Utility
using System.Collections.Generic;
using UnityEngine;
/**
* LAYER UTIL - ONE SCRIPT TO HOUSE EVERY LAYER MASK, ETC.
*
* USAGE:
*
* - Add all of your layers to the ULayerType enum
* - Update static getters to include all of your layers
@townofdon
townofdon / executePromisesInSequence.js
Created October 11, 2021 15:58
Execute Promises in Sequence
/**
* executePromisesInSequence
*
* Given an array of functions, each of which return a Promise,
* fire off each promise in sequence, one after the other.
*
* @param {func[]} promises
* @returns {Promise}
*/

React Native Hooks

useKeyboardHeight

Calculate the height of the keyboard when it is open.

useRefreshQuery

An apollo useQuery hook with built-in support for ScrollView's RefreshControl

@townofdon
townofdon / react-native-nuke.sh
Created November 16, 2020 15:51
react-native-nuke.sh
#!/bin/bash
# ReactNative script to nuke all the things
# usage:
# - add each item below as a separate script in package.json
# - add one final script:
# - "nuke": "yarn clean-node-modules && yarn clean-pods && yarn clean-ios && yarn clean-android && yarn clean-rn-cache"
# - alternatively, copy this shell script and add the following cmd to package.json:
# - "nuke": "./react-native-nuke-sh"
# - you may need to run `sudo chmod 777 ./react-native-nuke-sh before this script can run`
@townofdon
townofdon / reset_auto_increment_rails.rb
Created October 5, 2020 18:40
Rails Postgresql - reset auto increment to latest ids
# stolen from: http://blog.joncairns.com/2013/01/reset-postgresql-auto-increment-value-in-rails/
TABLES_TO_IGNORE = []
ActiveRecord::Base.connection.tables.each do |table|
next if TABLES_TO_IGNORE.include?(table)
result = ActiveRecord::Base.connection.execute("SELECT id FROM #{table} ORDER BY id DESC LIMIT 1")
if result.any?
ai_val = result.first['id'].to_i + 1
puts "Resetting auto increment ID for #{table} to #{ai_val}"
ActiveRecord::Base.connection.execute("ALTER SEQUENCE #{table}_id_seq RESTART WITH #{ai_val}")
end
@townofdon
townofdon / filter-bad-words.js
Created May 26, 2020 13:16
filter-bad-words.js
const badwords = [
'frick',
'fark',
'dern',
'shiz'
].join('|');
const badEmojis = [
'\\u{1F4A9}', // pile of poo
'\\u{1F595}' // middle finger
].join('|');
@townofdon
townofdon / deck_of_cards.rb
Created January 3, 2020 05:57
ruby deck of cards
#
# Simulates a shuffled deck of cards.
# This demonstrates *one* way to simulate drawing
# 5 cards at a time from a deck of cards.
# This logic could easily be applied to games such
# as Blackjack or Texas Hold'em.
#
# Note - I included a card.val method specifically for
# checking a card's value for comparison in Poker (e.g.
# a straight is 5 consecutive card vals of the same suite).
@townofdon
townofdon / delay.js
Last active December 18, 2019 15:01
Simple delayed Promise
const delay = (callback, time = 5000) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, time);
})
.then(callback);
}
// USAGE
@townofdon
townofdon / ComponentAutoBind.js
Last active March 3, 2019 23:55
Autobind React Class Methods
import { Component } from 'react';
/**
* Auto-bind all methods of a React class, excluding lifecycle methods
*
* USAGE:
*
* ```
* class MyClass extends ComponentAutoBind { ... }
* ```