Skip to content

Instantly share code, notes, and snippets.

@qoh
Created March 1, 2013 12:52
Show Gist options
  • Save qoh/5064458 to your computer and use it in GitHub Desktop.
Save qoh/5064458 to your computer and use it in GitHub Desktop.
function r(){exec("./main.cs");}
function swarmFighterVehicle::onAdd( %this, %obj )
{
parent::onAdd( %this, %obj );
SwarmFJetCheck( %obj );
%obj.flyState = 0;
%obj.droneTick = %obj.schedule( 50, "droneTick" );
$drone = %obj;
}
function wheeledVehicle::droneTick( %this )
{
cancel( %this.droneTick );
%transform = %this.getTransform();
%this.vel = %this.getVelocity();
%this.pos = getWords( %transform, 0, 2 );
%this.rot = getWords( %transform, 3, 6 );
%this.down_dist = %this._getVecDist( "0 0 512" );
%this.up_dist = %this._getVecDist( "0 0 -512" );
%this.resetVelocityIntent();
%this.droneUpdateVelocity();
%this.limitVelocityIntent( 10 );
%diff = vectorSub( %this.velocityIntent, %this.vel );
%targ = vectorAdd( %this.vel, vectorScale( %diff, 0.95 ) );
%this.setVelocity( vectorAdd( %this.vel, %this.velocityIntent ) );
// %this.setVelocity( %this.velocityIntent );
%this.droneTick = %this.schedule( 50, "droneTick" );
}
function wheeledVehicle::droneUpdateVelocity( %this )
{
if ( %this.flyState == 0 )
{
return;
}
if ( %this.flyState == 1 )
{
%optimal = 5;
if ( %this.down_dist < %optimal )
{
if ( %this.up_dist < %optimal )
{
%target = ( %this.down_dist + %this.up_dist ) / 2;
}
else
{
%target = %optimal;
}
}
else if ( %this.up_dist < %optimal )
{
if ( %this.down_dist < %optimal )
{
%target = ( %this.down_dist + %this.up_dist ) / 2;
}
else
{
%target = %optimal;
}
}
%diff = %target - %this.down_dist;
%abs = mAbs( %diff );
if ( %diff >= 0 )
{
%unit = 1;
}
else
{
%unit = -1;
}
%speed = mClampF( ( %abs - 0.2 ) / 1, 0, 8 );
%velocity = "0 0" SPC %unit * %speed;
bottomPrintAll( %this.down_dist SPC %speed, 1 );
%this.bufferVelocityIntent( %velocity );
}
if ( %this.flyState == 2 )
{
if ( %this.down_dist < 0.25 )
{
%this.flyState = 0;
}
%speed = mClampF( %this.down_dist / 2.5, 0, 1 );
%this.bufferVelocityIntent( "0 0" SPC %speed * -1 );
}
}
function wheeledVehicle::resetVelocityIntent( %this )
{
%this.velocityIntent = "0 0 0";
}
function wheeledVehicle::bufferVelocityIntent( %this, %velocity )
{
%this.velocityIntent = vectorAdd( %this.velocityIntent, %velocity );
}
function wheeledVehicle::limitVelocityIntent( %this, %limit )
{
%len = vectorLen( %this.velocityIntent );
%unit = vectorNormalize( %this.velocityIntent );
if ( %len > %limit )
{
%this.velocityIntent = vectorScale( %unit, %limit );
}
}
function wheeledVehicle::_getVecDist( %this, %vector )
{
%position = %this.pos;
%ray = containerRayCast(
%position,
vectorSub( %position, %vector ),
$TypeMasks::All,
%this
);
if ( !isObject( firstWord( %ray ) ) )
{
return vectorLen( %vector );
}
return vectorDist( %position, getWords( %ray, 1, 3 ) );
}
function wheeledVehicle::takeoff( %this )
{
if ( %this.flyState != 1 )
{
%this.flyState = 1;
}
}
function wheeledVehicle::land( %this )
{
if ( %this.flyState == 1 )
{
%this.flyState = 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment