Skip to content

Instantly share code, notes, and snippets.

View HDZD's full-sized avatar

HDZD HDZD

View GitHub Profile
@HDZD
HDZD / HttpRequests.cs
Last active April 19, 2024 20:49
Static class to allow easy HTTP Get / Post requests, also supports fetching images.
using System;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Networking;
namespace HDZD {
public static class HttpRequests {
private static CoroutineRunner _routineRunner = null;
@HDZD
HDZD / StateMachine.js
Created January 16, 2024 16:15
A simple state machine for JavaScript projects, pass in a logger to log events, you can remove the logging entirely if you want.
class StateMachine {
constructor(logger) {
this.currentState = null;
this.states = {};
this.Logger = logger;
}
AddState(context, stateName, onEnter = null, tick = null, onExit = null, isEntry = false) {
this.states[stateName] =
{
@HDZD
HDZD / DfsMazeGenerator.cs
Created December 11, 2023 22:15
Maze generation using DFS with an iterative implementation to avoid stack overflow.
using System.Collections.Generic;
using UnityEngine;
namespace HDZD
{
//Maze generation using a DFS approach to carve a path in a completely closed Maze
public class DfsMazeGenerator
{
private static int[] _dx = { -1, 0, 1, 0 };
private static int[] _dy = { 0, -1, 0, 1 };
@HDZD
HDZD / SpatialHashGrid.cs
Last active December 11, 2023 12:57
A slightly optimized spatial hash grid implementation, more optimization can be done in certain use cases (dont lazy initialize hash sets if you know world size). Feel free to use it in your own games. Vector3I is simply a Vector 3 with integer xyz values.
using System;
using System.Collections.Generic;
namespace HDZD
{
class SpatialHashGrid <T>
{
private Vector3 m_cellSizeUnit;
private Dictionary<Vector3I, HashSet<ItemProxy>> m_objectGrid = new Dictionary<Vector3I, HashSet<ItemProxy>>();
@HDZD
HDZD / SaveManager.cs
Created March 9, 2022 07:41
A simple class that manages multiple save files
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SaveManager : MonoBehaviour {
//Structure your save file here
[Serializable]
public class SaveFile {
@HDZD
HDZD / SafeDictionary.cs
Last active October 15, 2023 20:54
C# dictionary that behaves similarly to C++ std::unordered_map. Returns default if value of non-existing key is accessed instead of throwing an exception.
using System.Collections.Generic;
public class SafeDictionary<A,B> : Dictionary<A,B>
{
public new B this[A key] {
get {
if (TryGetValue(key,out B value))
return value;
return default;
@HDZD
HDZD / StateMachine.cs
Last active January 20, 2022 15:02
A State Machine class with an example for usage, very useful for implementing AI in games
using System.Collections.Generic;
namespace StateManagment {
public class StateMachine <T> {
private Dictionary <T,State> states;
private Dictionary <T,List <Transition>> transitions;
private State currentState = null;
public StateMachine() {
states = new Dictionary<T, State>();
@HDZD
HDZD / Pooler.cs
Last active January 19, 2022 20:35
Object Pooling class that provides a very simple way to spawn and destroy objects, Simple use Pooler.Instantiate and Pooler.Destroy for objects that you want to be pooled.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Pooler : MonoBehaviour {
private static Dictionary<string,UniqueQueue<GameObject>> _queueMap;
private static Dictionary<string,UniqueQueue<GameObject>> queueMap{
get {
if (_queueMap == null){
@HDZD
HDZD / UniqueQueue.cs
Last active November 24, 2021 14:51
A Queue DS that only stores an objects once, and disregards a an Enqueue operation if the item being Enqueued already exists in the queue. All in O(1)
using System.Collections.Generic;
public class UniqueQueue <T> {
Queue <T> queue;
HashSet <T> set;
public int Count{
get {
return queue.Count;
}
@HDZD
HDZD / RingBuffer.cs
Last active October 31, 2021 14:33
A ring buffer class written in C# originally made to be used in a unity game.
using System.Collections;
using System.Collections.Generic;
public class RingBuffer <T> {
private int maxSize;
private T[] data;
private int headIndex;
private int currentIndex;
private int currentSize;