Squat,
My heart script has pickup and throw as well in it.
If you open the script, the following is from the rock.gsl file within:
//debug attributes
bool DEBUG_ENABLED = true;
//attributes
bool CanBePickedUp = false;
bool CanBeSmashed = false;
bool CanBeExploded = false;
//player event states
bool PlayerInProximity = false;
//states
bool PickedUp = false;
bool Thrown = false;
bool Destroyed = false;
bool Exploded = false;
bool Smashed = false;
//timer for input
float inputTimer = 0.2f;
float lastInput = 0;
float orientTimer = 1;
float lastOrient = 0;
bool orientEnabled = false;
bool inputEnabled = true;
//position and direction if carried
Vector direction;
Vector startingPosition;
Vector startingRotation;
Object@ thisObject;
Object@ playerObject;
ObjectCollide@ thisObjectCollision;
void Initialize( Object@ object)
{
CanBePickedUp = object.GetControllerSettingBool("CanBePickedUp");
CanBeSmashed = object.GetControllerSettingBool("CanBeSmashed");
CanBeExploded = object.GetControllerSettingBool("CanBeExploded");
startingPosition = object.GetPosition();
startingRotation = object.GetRotation();
@playerObject = GetGameManager().GetWorld().GetObject("Player");
@thisObject = object;
@thisObjectCollision = thisObject.GetCollideObject("Base");
thisObject.SetCollisionEnabled(true);
}
void DynamicsAdvance( Object@ object, float seconds)
{
//input code
if(!inputEnabled)
{
lastInput+=seconds;
if(lastInput >= inputTimer)
{
inputEnabled = true;
lastInput = 0.0f;
if(DEBUG_ENABLED)
Print("Input Timer Stopped");
}
}
if(orientEnabled)
{
lastOrient+=seconds;
}
if(PickedUp && !Thrown)
{
Vector pos = playerObject.GetPosition();
pos.y+=2.4;//above players head
thisObject.SetPosition(pos);
thisObject.SetRotation(playerObject.GetRotation());
thisObject.UpdateCollisionObjectPositions();
return;
}
else if(Thrown) // pickedup is implied
{
//object has to fly
Vector pos = playerObject.GetPosition();
pos.y+=2.4;//above players head
thisObject.SetPosition(pos);
thisObject.SetRotation(playerObject.GetRotation());
//thisObject.UpdateCollisionObjectPositions();
//
direction = playerObject.GetControllerVariableVector("shotDir");
direction.Normalize();
if(DEBUG_ENABLED)
Print(direction.x + "," + direction.y + "," + direction.z);
//direction.x=0;
//direction.z = 0;
direction.y=1.2;
direction*=4000;
Vector throwVelocity = playerObject.GetCollideObject("base").GetLinearVelocity();
//if(throwVelocity.y < 0)
throwVelocity.y=1;
thisObjectCollision.AddForce(direction);
thisObjectCollision.SetLinearVelocity(throwVelocity);
thisObjectCollision.SetAngularVelocity(Vector(0,0,0));
//thisObjectCollision.SetBouncyness(0);
thisObject.UpdateCollisionObjectPositions();
//thisObject.SetCollisionEnabled(false);
Thrown = false;
PickedUp = false;
Destroyed = true;
inputEnabled = false;
return;
}
else if(Exploded)
{
//todo - add some explosive properties
return;
}
else if(Smashed)
{
//todo perhaps we wish to hammer this thing?
return;
}
else
{
if(!Destroyed)
{
thisObject.SetPosition(startingPosition);
thisObject.SetRotation(startingRotation);
thisObjectCollision.SetAngularVelocity(Vector(0,0,0));
thisObjectCollision.SetLinearVelocity(Vector(0,0,0));
thisObject.UpdateCollisionObjectPositions();
}
else
{
//experimental
/*
Matrix mtx = thisObjectCollision.GetMatrix();
mtx.YAxis.Set( 0, 1, 0);
mtx.ZAxis = thisObject.GetCurMatrix().ZAxis;
mtx.XAxis = thisObject.GetCurMatrix().XAxis;
mtx.Orthogonalize( 1, 2);
thisObjectCollision.SetMatrix( mtx);
thisObject.GetCurMatrix().XAxis = mtx.XAxis;
thisObject.GetCurMatrix().YAxis = mtx.YAxis;
thisObject.GetCurMatrix().ZAxis = mtx.ZAxis;
//thisObjectCollision.SetAngularVelocity(Vector(0,0,0));
//thisObjectCollision.SetLinearVelocity(Vector(0,0,0));
thisObject.UpdateCollisionObjectPositions();*/
}
}
//experimental, quite buggy
//thisObjectCollision.SetAngularVelocity(Vector(0,0,0));
//thisObject.UpdateCollisionObjectPositions();
}
bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
{
//player interaction only
if (params.player == null)
return false;
//This code seems pointless so comment it out for now
/*
if(event == "EnterProximity") //we are in proximity, set the value
{
PlayerInProximity = true;
return true;
}
if(event == "ExitProximity") //we are in proximity, now we leave
{
PlayerInProximity = false;
return true;
}
*/
if(event == "Input")
{
if(params.stringValue == "Pickup")
{
if(params.floatValue > 0 && inputEnabled)
{
inputEnabled = false;
if(DEBUG_ENABLED)
Print("Input Timer Started");
if(!PickedUp)//we havent picked it up yet
{
PickedUp = true;
if(DEBUG_ENABLED)
Print("Player picked up " + thisObject.GetName());
return true;
}
else if(PickedUp && !Thrown) //we have picked it up and have not thrown it, so throw it already
{
//we have picked it up, lets see if we are throwing it or not
Thrown = true;
//BeingDestroyed = true; //since this is going to be thrown its going to be destroyed
if(DEBUG_ENABLED)
Print("Player is throwing " + thisObject.GetName());
return true;
}
}
}
}
return false;
}
void CollisionCallback( Object@ object, Object@ otherObject, ObjectCollide@ collide, ObjectCollide@ otherCollide, const Vector& in worldPos)
{
if(otherObject.GetName() == "Basic_Terrain")
{
//collision with terrain
if(Destroyed)
{
if(!orientEnabled)
orientEnabled = true;
if(lastOrient >= orientTimer)
{
orientEnabled = false;
lastOrient = 0;
float distance = -1;
Vector intersect;
Vector normal;
object.Hide();
Object@ hitObject = object.GetWorld().RayCast(object.GetPosition(), Vector(0,-1,0), distance, intersect, normal, false);
object.Show();
normal.Normalize();
object.SetRotation(normal);
Matrix mtx = collide.GetMatrix();
mtx.YAxis = normal;
object.GetCurMatrix() = mtx;
startingPosition = object.GetPosition();
startingRotation = object.GetRotation();
Destroyed = false;
}
}
}
}
void SetControllerVariable( Object@ object, const String& in name, const String& in value)
{
}
void SetControllerVariable( Object@ object, const String& in name, float value)
{
}
void SetControllerVariable( Object@ object, const String& in name, int value)
{
}
void SetControllerVariable( Object@ object, const String& in name, bool value)
{
if(name == "PickedUp")
PickedUp = value;
if(name == "Exploded")
Exploded = value;
if(name == "Smashed")
Smashed == value;
}
void SetControllerVariable( Object@ object, const String& in name, const Vector& in value)
{
if(name == "Direction")
{
direction = value;
direction.Normalize();
}
}
int GetControllerVariableInt( Object@ object, const String& in name)
{
return 0;
}
bool GetControllerVariableBool( Object@ object, const String& in name)
{
if(name == "CanBePickedUp")
return CanBePickedUp;
if(name == "CanBeSmashed")
return CanBeSmashed;
if(name == "CanBeExploded")
return CanBeExploded;
if(name == "InProximity")
return PlayerInProximity;
return false;
}
float GetControllerVariableFloat( Object@ object, const String& in name)
{
return 0.0f;
}
Vector GetControllerVariableVector( Object@ object, const String& in name)
{
return null;
}
String GetControllerVariableString( Object@ object, const String& in name)
{
return null;
}
Object@ GetControllerVariableObject( Object@ object, const String& in name)
{
return null;
}