Skip to content

Instantly share code, notes, and snippets.

View sketchpunk's full-sized avatar
🏠
Working from home

Pedro sketchpunk

🏠
Working from home
View GitHub Profile
@sketchpunk
sketchpunk / RunTillTrue.js
Created April 2, 2019 14:22
Run function on interval till it returns true
class RunTillTrue{
constructor( s, func = null ){
this.sec = s;
this.thread = null;
this.task = func;
this.active = false;
this.run = ()=>{
if( !this.active || this.task() == true ) this.stop();
};
@sketchpunk
sketchpunk / ResizeObserver.js
Created April 2, 2019 16:48
MutationObserver
/** Using Mutation Observer, it checks to see if a Widget has been resized. */
class ResizeObserver{
constructor( elm, cb, timeOut=300 ){
this.callBack = cb;
this.timeOut = timeOut;
this.obs = new MutationObserver( this.onCallback.bind(this) );
this.obs.observe( elm, { attributes:true, childList:false, characterData:false, attributeOldValue:true } );
}
onCallback( mList, ob ){
@sketchpunk
sketchpunk / ECS_example.js
Created April 2, 2019 20:38
Entity - Component - System ( ECS )
let ecs = new Ecs()
.addSystem( new TestSys(), 50, true );
let e = ecs.newEntity( "hero", ["Test"] );
ecs.updateSystems();
// COMPONENT
class Test{
constructor(){
this.string = "test";
@sketchpunk
sketchpunk / GerstnerWave.js
Created April 2, 2019 20:48
Gerstner Wave
//#################################################################################################
class Water{
constructor( gridSize=500, gridCell=51, style=0, defaultColor=0x676789 ){
this.mat = new GerstnerWaveShader( gridCell );
this.grid = new LineDotGrid( gridSize, gridSize, gridCell, gridCell, 0x676789, 0, 0, 2, this.mat, false );
this._waveCount = 0;
//Center the Grid in World Space
this.grid.obj3D.position.x = gridSize * -0.5;
this.grid.obj3D.position.z = gridSize * -0.5;
//http://codeflow.org/entries/2010/aug/28/integration-by-example-euler-vs-verlet-vs-runge-kutta/
//https://jsfiddle.net/bkanber/pDngH/
//https://github.com/pqml/spring
//http://lolengine.net/blog/2015/05/03/damping-with-delta-time
//https://github.com/keijiro/SmoothingTest
//https://gafferongames.com/post/spring_physics/
//https://stackoverflow.com/questions/5100811/algorithm-to-control-acceleration-until-a-position-is-reached
//
//http://lolengine.net/blog/2015/05/03/damping-with-delta-time
const SCROLL_TOP_OFFSET = -19;
class Scroll{
static next(){
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if( ! this.ary ){
let elm, root = document.getElementById("ContentArea");
this.ary = new Array();
for( let i=0; i < root.childNodes.length; i++ ){
if( root.childNodes[ i ].nodeType != 1 ) continue;
use std::any::Any;
use std::marker::PhantomData;
//####################################################
struct Test{ data: Box<dyn Any> }
#[derive(Default, Debug)]
struct Pos{ x: f32 }
#[derive(Default, Debug)]
@sketchpunk
sketchpunk / bitset.rs
Created May 22, 2019 17:08
Simple Bitset in Rust
const BITBLOCK_SIZE : usize = 16;
const BITBLOCK_SIZEF : f32 = 16.0;
type BitBlock = u16;
fn calc_block_size( n: f32 ) -> usize { ( n / BITBLOCK_SIZEF ).ceil() as usize }
fn calc_block( n: f32 ) -> usize { ( n / BITBLOCK_SIZEF ).floor() as usize }
#[derive(Debug)]
struct BitSet{
#[derive(Debug)]
pub struct DenseVec<T>{
data : Vec<T>,
e2d : Vec<usize>,
d2e : Vec<usize>,
}
impl<T> IStorage<T> for DenseVec<T>{
fn new() -> Self{ DenseVec{ data: Vec::new(), e2d: Vec::new(), d2e: Vec::new() } }
@sketchpunk
sketchpunk / multi_storage_ecs.rs
Created June 3, 2019 18:19
Prototype for Handling Multiple Storage Types for ECS
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_imports)]
#![allow(unused_mut)]
use std::fmt::Debug;
use std::cell::{ RefCell, RefMut, Ref };
use std::collections::HashMap;
use std::any::{ TypeId, Any };