Skip to content

Instantly share code, notes, and snippets.

View bartwttewaall's full-sized avatar

Bart Wttewaall bartwttewaall

View GitHub Profile
@bartwttewaall
bartwttewaall / 2DArraySingleForLoop.js
Last active July 22, 2019 09:32
Traverse a 2d array in single for-loop
function traverse (values, numCols) {
var numRows = values.length / numCols;
for (var i = 0, length = values.length; i < length; i++) {
var row = (i % length / numCols) << 0; // = Math.floor
var col = i % numCols;
console.log(i, row, col);
}
}
@bartwttewaall
bartwttewaall / twig-extentions_meta.ts
Last active July 22, 2019 07:11
Generate HTML meta tags from JSON data (Twig)
import { extendFunction } from "twig";
/**
* Method for building meta tags. Insprited by:
* @link https://gist.github.com/rquast/a9cbc0551a48d10e83b2ad899b293c77
*
* @example js preperation
const meta = {
page: {
description:
@bartwttewaall
bartwttewaall / Unity3D_ScrollRect.md
Last active July 22, 2019 07:11
Unity3D ScrollRect flexible layout recipe

ScrollRect Layout recipe

Used for creating a scrollable list of items, such as a chat panel.

  • Create a new ScrollRect component
  • Navigate to its Content child
  • Add a VerticalLayout component
    • set Child Controls Sizewidth: true, height: true
    • set Child Force Expandwidth: false, height: false
    • set the padding / spacing to whatever you like
  • Add a ContentSizeFitter
@bartwttewaall
bartwttewaall / Unity3D_MinimapComponent.cs
Created July 22, 2019 07:22
A clickable minimap that places a pointer sprite at the position where you clicked and returns a Vector2 with a range of -1 to 1 in both dimensions
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Wirelab.Pages;
public class MinimapComponent : BaseComponent, IPointerClickHandler {
public GameObject pointerPrefab;
public DataEvent<Vector2> onClick;
@bartwttewaall
bartwttewaall / Unity3D_AimReticle.cs
Created July 22, 2019 07:30
Position and rotate a reticle on the floor, at the feet of a player in the 3D direction we're aiming at a range from the center
Transform aimTransform;
float spriteOffsetY = 0.01f;
float aimRange = 1.5f;
// use incoming player.state.aimDirection to calculate the rotation and position of the reticle's aimTransform
Vector3 aim = player.state.aimDirection.normalized;
aimTransform.localPosition = new Vector3 (aim.x * aimRange, spriteOffsetY, aim.z * aimRange);
float angle = Mathf.Atan2 (aim.x, aim.z) * (180 / Mathf.PI);
aimTransform.rotation = Quaternion.Euler (90, angle, 0);
@bartwttewaall
bartwttewaall / Unity3D_UrlEncoding.md
Created July 22, 2019 08:01
When you need to send a string to a backend but some characters are missing, chance is you need to URLEncode it

UrlEncoding

Sending a string over to the colyseus server through a Dictionary<string, object> can result in missing characters.

var options = new Dictionary<string, object> () {
  { "sessionTicket", WebUtility.UrlEncode(sessionTicket) }
};
room = client.Join<SK.Schemas.PvpGameState> (roomId, options);
@bartwttewaall
bartwttewaall / Unity3D_CullingMask-example.cs
Created July 22, 2019 08:05
Combining layers to define a cullingmask
LayerMask everything = -1;
void Start () {
LayerMask uiMask = 1 << LayerMask.NameToLayer ("UI");
cam.cullingMask = everything;
cam.cullingMask &= ~uiMask; // exclude UI layer
}
@bartwttewaall
bartwttewaall / TriggeredInputTicker.cs
Last active July 22, 2019 09:30
A ticker / stopwatch class that keeps track of the activation and cooldown of a player's ability, like a shield.
public abstract class AbstractTicker {
public delegate void TickerEvent ();
public delegate void TickerProgressEvent (float progress);
public abstract void Tick (float time, bool input);
}
// Example
// TriggeredInputTicker shieldTicker = new TriggeredInputTicker () { duration = 4, cooldown = 8 };
@bartwttewaall
bartwttewaall / PlayerInputState.cs
Last active July 22, 2019 09:25
Compress multiple boolean states into a single integer and unpack it on the server or vice versa
using System;
using UnityEngine;
// https://dev.to/somedood/bitmasks-a-very-esoteric-and-impractical-way-of-managing-booleans-1hlf
[Flags]
public enum KeyFlags {
UP = 1 << 0,
DOWN = 1 << 1,
LEFT = 1 << 2,
@bartwttewaall
bartwttewaall / Unity3D_UpdateFixedDeltaTime.cs
Created July 22, 2019 08:31
Update as many times in steps of fixedDeltaTime until we have caught up to the elapsed time
float timer;
void Update () {
timer += Time.deltaTime;
while (timer >= Time.fixedDeltaTime) {
timer -= Time.fixedDeltaTime;
// calculate single step
// ..