Skip to content

Instantly share code, notes, and snippets.

View bfollington's full-sized avatar
😈
scheming

Ben Follington bfollington

😈
scheming
View GitHub Profile
@bfollington
bfollington / main.fs
Created September 19, 2018 05:21
[F# Read Input List]
open System
let read _ = Console.ReadLine()
let isValid = function null -> false | _ -> true
let readList t =
Seq.initInfinite read
|> Seq.takeWhile isValid
|> Seq.toList
|> List.map t
@bfollington
bfollington / redux.ts
Created April 11, 2019 01:25
Concise & Typesafe Redux Actions
export type Action = ReturnType<
| typeof loginRequested
| typeof logoutRequested
>
type Dispatch = React.Dispatch<Action>
export const loginRequested = (userId: string) => (<const>{
type: 'loginRequested',
payload: {
@bfollington
bfollington / EventManager.cs
Last active August 30, 2023 14:21
EventManager messaging layer designed for Unity
using System.ComponentModel.Design;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using System.Linq;
namespace Events
{
@bfollington
bfollington / FaceCamera.cs
Created May 19, 2019 22:27
A tiny MonoBehaviour for Unity3d that makes a sprite look at the camera
using UnityEngine;
using System.Collections;
public class FaceCamera : MonoBehaviour {
public bool LockX = false;
public bool LockY = false;
void Start () {
@bfollington
bfollington / RoomData.cs
Created June 21, 2019 23:37
Room data serialization
using UnityEngine;
namespace GridBased {
[CreateAssetMenu]
public class RoomData : ScriptableObject {
public GameObject ModelPrefab;
public Vector2 Dimensions;
public Vector2 Origin;
public TextAsset Layout;
}
@bfollington
bfollington / MouseCursor.cs
Created June 23, 2019 00:40
Isometric mouse cursor snapping
void Update()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100, FloorLayer)) {
Transform objectHit = hit.transform;
if (SnapToGrid) {
var newPos = new Vector3(Round(hit.point.x, GridSize), _yOffset, Round(hit.point.z, GridSize));
@bfollington
bfollington / lens.ts
Created July 16, 2019 00:12
A concise, typesafe lens implementation in TypeScript
interface INamed<T> {
name: T
}
export const Name = {
get<T>(named: INamed<T>) {
return named.name
},
set<T>(named: INamed<T>, value: T) {
return {
@bfollington
bfollington / lens.ts
Created August 20, 2019 09:04
A much more complicated, generic, typesafe lens implementation for Typescript
function lens<K extends string>(key: K) {
return <const>{
get: function<R extends any>(record: R): K extends keyof R ? R[K] : unknown {
const k: string = key
return record[k]
},
set: function<R extends any>(record: R, value: R[K]) {
return record.hasOwnProperty(key) ?
{
...record,
@bfollington
bfollington / lens.ts
Last active August 20, 2019 10:02
A moderately generic, moderately concise + typesafe lens implementation...
type Lens<R, K extends keyof R, O> = {
get(action: R):O,
set(action: R, value:O): R
}
function lens<R, K extends keyof R, O>(field: K): Lens<R, K, O> {
return {
get(action: R): O {
return (action[field] as unknown) as O
},
@bfollington
bfollington / writeUnityVersion.js
Created November 17, 2019 04:25
Write package.json version to ProjectSettings.asset
const fs = require('fs')
const package = require('./package.json')
const projectSettingsPath = 'ProjectSettings/ProjectSettings.asset'
const re = /bundleVersion:\s(\d\.\d\.\d)$/gm
const fileStr = fs.readFileSync(projectSettingsPath, 'utf8')
const updated = fileStr.replace(re, `bundleVersion: ${package.version}`)
console.log(`bundleVersion: ${package.version}`)