rokojori-godot/rokojori-cpp-generator/source/cpp-creation/Variations.ts

124 lines
2.0 KiB
TypeScript

import { RJLog } from "../library/RJLog";
import { VariationDefinitions } from "./VariationTemplates";
export class Variations
{
_variationDefinitions:VariationDefinitions;
_indicies:number[] = [];
_lengths:number[] = [];
_items = 1;
_counter = 0;
_map = new Map<string,string>();
constructor( vds:VariationDefinitions )
{
this._variationDefinitions = vds;
this._items = 1;
vds.forEach(
vd =>
{
this._indicies.push( 0 );
let length = -1;
for ( let it in vd )
{
if ( length !=-1 || ! vd.hasOwnProperty( it ) )
{
continue;
}
length = vd[ it ].length;
}
this._lengths.push( length );
this._items *= length;
}
);
console.log( "Found items:", this._items );
this._grabCurrentMap();
}
_grabCurrentMap()
{
this._map.clear();
for ( let i = 0; i < this._indicies.length; i++ )
{
this._grabItems( i );
}
}
get hasItems()
{
return this._counter <= this._items;
}
get items()
{
return this._items;
}
get counter()
{
return this._counter;
}
nextItem()
{
this._counter ++;
this._incrementCounter( 0 );
this._grabCurrentMap();
// RJLog.log( this._counter, this._indicies, this.currentMap );
}
_incrementCounter( position:number )
{
let value = this._indicies[ position ];
if ( value == ( this._lengths[ position ] - 1 ) )
{
this._indicies[ position ] = 0;
this._incrementCounter( position + 1 );
}
else
{
this._indicies[ position ] ++;
}
}
_grabItems( index:number )
{
let counter = this._indicies[ index ];
let vd = this._variationDefinitions[ index ];
let map = this._map;
for ( let it in vd )
{
if ( ! vd.hasOwnProperty( it ) )
{
continue;
}
let key = it;
let values = vd[ it ];
map.set( key, values[ counter ] );
}
}
get currentMap():Map<string, string>
{
return this._map;
}
}