Skip to content

Instantly share code, notes, and snippets.

@pixelrevision
pixelrevision / PixelPerfectCam.cs
Last active January 26, 2024 05:23
Script for unity to create a pixel locked orthogonal camera
using UnityEngine;
/**
* A camera to help with Orthagonal mode when you need it to lock to pixels. Desiged to be used on android and retina devices.
*/
public class PixelPerfectCam : MonoBehaviour {
/**
* The target size of the view port.
*/
public Vector2 targetViewportSizeInPixels = new Vector2(480.0f, 320.0f);
/**
@pixelrevision
pixelrevision / PascalJSONCodingTests.swift
Last active May 17, 2022 16:18
Pascal to Camel json coding
import XCTest
struct SampleCodable: Codable {
let sampleVariable1: String
let sampleVariable2: String
let cAPPEDVariable3: String
let sample: String
}
class PascalJSONCoding: XCTestCase {
class DependencyContainer {
static let shared = DependencyContainer()
private var types: [String: Any] = [:]
func registerType<T: Any>(_ type: T.Type, named name: String? = nil, creator: @escaping (String) -> T) {
let key = name ?? String(describing: type)
types[key] = creator
}
@pixelrevision
pixelrevision / BitField.js
Created November 1, 2012 20:50
Bitmask javascript
var BitField = function(){};
BitField.isSet = function(i, flag){
return (i & flag) === flag;
}
BitField.setFlag = function(i, flag){
return i | flag;
}
BitField.removeFlag = function(i, flag){
return i & ~flag;
}
@pixelrevision
pixelrevision / Observable.swift
Last active December 27, 2018 17:17
Swift observable protocol
import Foundation
public protocol Observable: class {
associatedtype ObservableTarget = AnyObject
func add(observer: ObservableTarget)
func remove(observer: ObservableTarget)
func dispatch(_ handler: (ObservableTarget) -> ())
@pixelrevision
pixelrevision / MiddlewareProcessor.swift
Created December 19, 2018 16:19
Protocol for common ReSwift middleware usecase
import ReSwift
public protocol MiddlewareProcessor {
typealias GetStateFunction = () -> Any?
func preprocess(dispatch: DispatchFunction, action: Action, getState: GetStateFunction)
func postprocess(dispatch: DispatchFunction, action: Action, getState: GetStateFunction)
}
public extension MiddlewareProcessor {
@pixelrevision
pixelrevision / PixelLevelCollision.js
Last active January 23, 2018 19:28
PixelLevelCollision.js
/*
Works with the alpha channel of an image to determine if there is collision with alpha
@parameter image - the image to check coordinate with.
@parameter sampling - (0.1-1.0) The percentage to downsample the image. Helps with performance and memory. Default 0.5
@parameter threshhold - (0.0-1.0) the threshhold to set the alpha for a hit. default 0.2
*/
var PixelLevelCollision = function(image, sampling, threshhold){
var self = this;
if(sampling === undefined){
extension Sequence {
func walk(_ childSelector: (Element) -> [Element], handler: (Element) -> Void) {
for item in self {
handler(item)
let children = childSelector(item)
if children.count > 0 {
children.walk(childSelector, handler: handler)
}
}
@pixelrevision
pixelrevision / Sequence+RecursiveFlatten.swift
Created December 5, 2017 21:56
Sequence+RecursiveFlatten
extension Sequence {
func recursiveFlatten(_ childSelector: (Element) -> [Element]) -> [Element] {
var output: [Element] = []
for item in self {
output.append(item)
let newItems = childSelector(item)
if newItems.count > 0 {
output += newItems.recursiveFlatten(childSelector)
}
import UIKit
import PlaygroundSupport
class SampleController: UIViewController {
init() {
super.init(nibName: nil, bundle: nil)
title = "Sample View"
view = UIView()
view.backgroundColor = UIColor.green