class module_Sprite
{
VOXEL = {};
DIRTY = {};
Dispose()
{
for( var $name in this.VOXEL )
{ this.VOXEL[$name].Dispose();
}
}
IsReady( $name )
{
if( this.Is($name) )
{
if( this.VOXEL[$name].dirty )
{ this.VOXEL[$name].Build();
delete( µB.$Sprite.DIRTY[$name] );
}
return true;
}
else
{
return false;
}
}
Is( name )
{
if( ! this.VOXEL[name] )
{ µB.RuntimeError( "Voxel " + name + " doesn't exist" ) ;
return false ;
}
else
{ return true ;
}
}
UpdateDirty()
{
for( let id in this.DIRTY )
{
if( this.VOXEL[id] )
{ this.VOXEL[id].Build();
}
}
this.DIRTY = {} ;
}
VoxelCopy( $from, $name, clone )
{
{
var $size = this.VOXEL[$from].size ;
this.create_voxel($size, $name);
this.VOXEL[$name].$Mesh.material = this.VOXEL[$from].$Mesh.material ;
if( clone )
{ this.VOXEL[$name].$Mesh.geometry = this.VOXEL[$from].$Geometry.clone() ;
this.VOXEL[$name].dirty = false ;
}
else
{
this.VOXEL[$name].DEF = Object.assign({},this.VOXEL[$from].DEF) ;
this.VOXEL[$name].Obb_min = this.VOXEL[$from].Obb_min ;
this.VOXEL[$name].Obb_max = this.VOXEL[$from].Obb_max ;
}
let $x,$y,$z;
$x = this.VOXEL[$from].$Mesh.scale.x ;
$y = this.VOXEL[$from].$Mesh.scale.y ;
$z = this.VOXEL[$from].$Mesh.scale.z ;
this.zoom( $name, $x*100, $y*100, $z*100 );
$x = this.VOXEL[$from].$Mesh.position.x ;
$y = this.VOXEL[$from].$Mesh.position.y ;
$z = this.VOXEL[$from].$Mesh.position.z*ZA ;
this.move( $name, $x, $y, $z );
$x = this.VOXEL[$from].euler.x / µB.$Config.deg ;
$y = this.VOXEL[$from].euler.y / µB.$Config.deg ;
$z = this.VOXEL[$from].euler.z / µB.$Config.deg ;
this.turn( $name, $x, $y, $z );
}
}
create_voxel(size, name)
{
this.delete(name) ;
this.VOXEL[name] = new Voxel(name,size) ;
}
create_text( $input, $name )
{
let $string = String($input) ;
let $size = $string.length*7 + 1 ;
this.create_voxel($size, $name) ;
µB.$Craft.AddText( this.VOXEL[$name], $string ) ;
}
delete( name )
{
if( this.VOXEL[name] )
{ this.VOXEL[name].Dispose();
delete( this.VOXEL[name] );
}
}
is(name)
{
return this.VOXEL[name] ? 1 : 0 ;
}
copy(from, name)
{
if( this.Is(from) )
{ this.VoxelCopy(from, name, false);
this.DIRTY[name] = true ;
}
}
clone(from, name)
{
if( this.IsReady(from) )
{ this.VoxelCopy(from, name, true);
}
}
hide( name )
{ if( this.Is(name) )
{ this.VOXEL[name].$Mesh.visible = false ;
}
}
show( name )
{ if( this.Is(name) )
{ this.VOXEL[name].$Mesh.visible = true ;
}
}
show_paint(name, type)
{
if( this.Is(name) )
{ type = µB.$Config.GetEnum(type);
if( ! µB.$D3.MATERIAL[type] )
{ µB.RuntimeError( "Paint " + type + " not defined." ) ;
}
else
{ this.VOXEL[name].$Mesh.visible = true ;
this.VOXEL[name].$Mesh.material = µB.$D3.MATERIAL[type];
this.VOXEL[name].order = 100 ;
}
}
}
center( name )
{
if( this.Is(name) )
{ this.VOXEL[name].SetOrigin(ORIGIN_CENTER) ;
}
}
center_auto( name )
{
if( this.Is(name) )
{ this.VOXEL[name].SetOrigin(ORIGIN_AUTO) ;
}
}
center_to(name, x, y, z)
{
if( this.Is(name) )
{ this.VOXEL[name].SetOrigin(ORIGIN_TO, x,y,z) ;
}
}
set_position( $name, $xyz, $value )
{ if( $xyz === "z" ) $value = $value *ZA ;
if( this.Is($name) )
{ this.VOXEL[$name].$Mesh.position[$xyz] = $value ;
}
}
set_angle( $name, $angle, $value )
{ if( this.Is($name) )
{
this.VOXEL[$name].euler[$angle] = $value * µB.$Config.deg ;
this.VOXEL[$name].$Mesh.setRotationFromEuler( this.VOXEL[$name].euler ) ;
}
}
set_voxel_zoom( $name, $xyz, $value )
{
if( this.Is($name) )
{ this.VOXEL[$name].$Mesh.scale[$xyz] = $value/100 ;
}
}
move_all( $name, $x )
{ this.move($name, $x,$x,$x);
}
move( $name, $x, $y, $z )
{ if( this.Is($name) )
{ this.VOXEL[$name].$Mesh.position.set($x*1,$y*1,$z*ZA);
}
}
move_by( $name, $x, $y, $z )
{ if( this.Is($name) )
{ this.VOXEL[$name].$Mesh.position.x += $x ;
this.VOXEL[$name].$Mesh.position.y += $y ;
this.VOXEL[$name].$Mesh.position.z += $z*ZA ;
}
}
turn_all( $name, $all )
{ this.turn( $name, $all, $all, $all ) ;
}
turn( $name, $x, $y, $z )
{ if( this.Is($name) )
{ this.VOXEL[$name].euler.x = $x * µB.$Config.deg ;
this.VOXEL[$name].euler.y = $y * µB.$Config.deg ;
this.VOXEL[$name].euler.z = $z * µB.$Config.deg ;
this.VOXEL[$name].$Mesh.setRotationFromEuler( this.VOXEL[$name].euler ) ;
}
}
turn_by_all( $name, $x )
{ this.turn_by($name,$x,$x,$x);
}
turn_by( $name, $x, $y, $z )
{
if( this.Is($name) )
{
this.VOXEL[$name].euler.x += $x * µB.$Config.deg ;
this.VOXEL[$name].euler.y += $y * µB.$Config.deg ;
this.VOXEL[$name].euler.z += $z * µB.$Config.deg ;
this.VOXEL[$name].$Mesh.setRotationFromEuler( this.VOXEL[$name].euler ) ;
}
}
zoom_all($name, $x)
{
this.zoom($name,$x,$x,$x);
}
zoom($name, $x, $y, $z)
{ if( this.Is($name) )
{ this.VOXEL[$name].$Mesh.scale.set($x/100,$y/100,$z/100);
}
}
zoom_by_all($name, $x)
{ let $f = $x/100 ;
this.zoom_by($name,$f,$f,$f);
}
zoom_by($name, $x, $y, $z)
{ if( this.Is($name) )
{ this.VOXEL[$name].$Mesh.scale.x += $x/100 ;
this.VOXEL[$name].$Mesh.scale.y += $y/100 ;
this.VOXEL[$name].$Mesh.scale.z += $z/100 ;
}
}
distance( name, target )
{
if( this.Is(name) && this.Is(target) )
{ return this.VOXEL[name].$Mesh.position.distanceTo(this.VOXEL[target].$Mesh.position) ;
}
return -1 ;
}
};