Skip to content

Instantly share code, notes, and snippets.

@DarrenTsung
DarrenTsung / gist:5219869
Created March 22, 2013 08:52
Prints out a histogram showing number of entries per bucket for a Hashtable using DLists as buckets. Also calculates the estimated Collision number and compares it to the actual collision number.
public void printHist() {
235 int collisions = 0;
236 for(int i=0; i<defTable.length; i++) {
237 list.DList temp = defTable[i];
238 list.DListNode tempy = (list.DListNode) temp.front();
239 System.out.print("Entry " + i + ": [ ");
240 for(int j=0; j<temp.size(); j++) {
241 try {
242 System.out.print("*");
243 tempy = (list.DListNode) tempy.next();
@DarrenTsung
DarrenTsung / server.js
Created July 11, 2014 20:51
Simple node.js server which has two routes: '/write' which writes a string to a file & '/read' which returns the file data in the response.
// Load the http module to create an http server.
var http = require('http');
var fs = require('fs');
var path = require("path");
var url = require("url");
var sys = require("sys");
var qs = require('querystring');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function(request,response){
@DarrenTsung
DarrenTsung / FloatSpring.cs
Created July 21, 2016 17:31
Unity spring for floats
using System;
using System.Collections;
using UnityEngine;
namespace DT {
/// <summary>
/// uses the semi-implicit euler method. faster, but not always stable.
/// see http://allenchou.net/2015/04/game-math-more-on-numeric-springing/
/// </summary>
public class FloatSpring : MonoBehaviour {
@DarrenTsung
DarrenTsung / AngryPufferfishExample1.cs
Created August 29, 2016 17:29
Example for my StateMachineBehaviour article
class RotateFacingDirection : LogicalStateMachineBehaviour {
[SerializeField] private float _rotationSpeed = 10.0f;
...
}
class AimInFacingDirection : LogicalStateMachineBehaviour {
[SerializeField] private LayerMask _targetLayerMask;
void OnStateUpdate() {
@DarrenTsung
DarrenTsung / AngryPufferfishExample2.cs
Last active April 14, 2018 08:20
Second example in my StateMachineBehaviour article
class TriggerContinueAfterDelay : LogicalStateMachineBehaviour {
[SerializeField] private float _delay = 1.0f;
void OnStateEntered() {
this.DoAfterDelay(this._delay, () => {
this.Animator.SetTrigger("Continue");
}
}
}
@DarrenTsung
DarrenTsung / AngryPufferfishExample3.cs
Last active August 29, 2016 17:31
Third example in my StateMachineBehaviour article
class MoveInFacingDirection : LogicalStateMachineBehaviour {
[SerializeField] private float _speed = 5.0f;
void OnStateUpdate() {
this.MoveInFacingDirection();
}
...
}
@DarrenTsung
DarrenTsung / LogicalStateMachineBehaviour.cs
Created August 30, 2016 17:24
Encapsulated class I use instead of StateMachineBehaviour
using System;
using System.Collections;
using UnityEngine;
namespace DT {
public class LogicalStateMachineBehaviour : StateMachineBehaviour {
// PRAGMA MARK - StateMachineBehaviour Lifecycle
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
this.Animator = animator;
@DarrenTsung
DarrenTsung / StateBehaviourExtensions.cs
Last active May 15, 2017 17:53
Example on how I configure my StateMachineBehaviours
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace DTAnimatorStateMachine {
public static class StateBehaviourExtensions {
public static IEnumerable<IStateBehaviour<TStateMachine>> GetStateBehaviours<TStateMachine>(this TStateMachine stateMachine, Animator animator) {
StateMachineBehaviour[] behaviours = animator.GetBehaviours<StateMachineBehaviour>();
foreach (StateMachineBehaviour behaviour in behaviours) {
@DarrenTsung
DarrenTsung / ValidationTests.cs
Created June 4, 2017 21:57
Validation tests that you can copy into your project - see DTValidator (https://github.com/DarrenTsung/DTValidator)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using UnityEngine.Events;
using NUnit.Framework;
void BeOnTheLookout(float radius) {
Collider[] hitColliders = Physics.OverlapSphere(transform.position, radius);
int highestBounty = 0;
Collider mostWantedCriminal = null;
foreach (Collider criminal in hitColliders) {
var bounty = criminal.GetComponent<Bounty>();
if (bounty == null) {
continue;
}