Jump to content

Weapon Script


DarkEmperor13
 Share

You are about to reply to a thread that has been inactive for 1489 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

I'm trying to make a gun that can do regular shots and charged shots. 

float   gVelocity   = 75.0;
float   gReloadTime = 0.10;
string  gShootSound = "Energy Charge Shot";
string  gHoldSound = "Energy Charge";
string  gShootAnimation = "hold_R_rifle";
string  gBullet = "PB Bullet";
string  gBullet2 = "PBC Bullet";
integer gPermFlags;
 // Particle Script 0.5
// Created by Ama Omega
// 3-26-2004
 
// Mask Flags - set to TRUE to enable
integer glow = FALSE;            // Make the particles glow
integer bounce = FALSE;          // Make particles bounce on Z plane of object
integer interpColor = TRUE;     // Go from start to end color
integer interpSize = TRUE;      // Go from start to end size
integer wind = FALSE;           // Particles affected by wind
integer followSource = TRUE;    // Particles follow the source
integer followVel = TRUE;       // Particles turn to velocity direction
 
// Choose a pattern from the following:
// PSYS_SRC_PATTERN_EXPLODE
// PSYS_SRC_PATTERN_DROP
// PSYS_SRC_PATTERN_ANGLE_CONE_EMPTY
// PSYS_SRC_PATTERN_ANGLE_CONE
// PSYS_SRC_PATTERN_ANGLE
integer pattern = PSYS_SRC_PATTERN_ANGLE_CONE;

// Select a target for particles to go towards
// "" for no target, "owner" will follow object owner 
//    and "self" will target this object
//    or put the key of an object for particles to go to
key target = "";
 
// Particle paramaters
float age = 1;                  // Life of each particle
float maxSpeed = .05;            // Max speed each particle is spit out at
float minSpeed = 0.05;            // Min speed each particle is spit out at
string texture = "";                 // Texture used for particles, default used if blank
float startAlpha = 0.5;           // Start alpha (transparency) value
float endAlpha = 0.0;           // End alpha (transparency) value
vector startColor = <0.937, 0.863, 0.361>;    // Start color of particles <R,G,B>
vector endColor = <0.937, 0.863, 0.361>;      // End color of particles <R,G,B> (if interpColor == TRUE)
vector startSize = <.5, .5, .5>;     // Start size of particles 
vector endSize = <1,1,1>;        // End size of particles (if interpSize == TRUE)
vector push = <0.0,0.0,0>;          // Force pushed on particles
 
// System paramaters
float rate = 0.01;            // How fast (rate) to emit particles
float radius = 0;          // Radius to emit particles for BURST pattern
integer count = 5;        // How many particles to emit per BURST 
float outerAngle = 0;    // Outer angle for all ANGLE patterns
float innerAngle = 0.1;    // Inner angle for all ANGLE patterns
vector omega = <0,0,0>;    // Rotation of ANGLE patterns around the source
float life = 0;             // Life in seconds for the system to make particles
 
// Script variables
integer pre = 2;          //Adjust the precision of the generated list.
 
integer listen_handle;
integer flags;
list sys;
integer type;
vector tempVector;
rotation tempRot;
string tempString;
integer i;
 
string float2String(float in)
{
    return llGetSubString((string)in,0,pre - 7);
}
 
updateParticles()
{
    flags = 0;
    if (target == "owner") target = llGetOwner();
    if (target == "self") target = llGetKey();
    if (glow) flags = flags | PSYS_PART_EMISSIVE_MASK;
    if (bounce) flags = flags | PSYS_PART_BOUNCE_MASK;
    if (interpColor) flags = flags | PSYS_PART_INTERP_COLOR_MASK;
    if (interpSize) flags = flags | PSYS_PART_INTERP_SCALE_MASK;
    if (wind) flags = flags | PSYS_PART_WIND_MASK;
    if (followSource) flags = flags | PSYS_PART_FOLLOW_SRC_MASK;
    if (followVel) flags = flags | PSYS_PART_FOLLOW_VELOCITY_MASK;
    if (target != "") flags = flags | PSYS_PART_TARGET_POS_MASK;
    
    llParticleSystem([  PSYS_PART_MAX_AGE,age,
                        PSYS_PART_FLAGS,flags,
                        PSYS_PART_START_COLOR, startColor,
                        PSYS_PART_END_COLOR, endColor,
                        PSYS_PART_START_SCALE,startSize,
                        PSYS_PART_END_SCALE,endSize, 
                        PSYS_SRC_PATTERN, pattern,
                        PSYS_SRC_BURST_RATE,rate,
                        PSYS_SRC_ACCEL, push,
                        PSYS_SRC_BURST_PART_COUNT,count,
                        PSYS_SRC_BURST_RADIUS,radius,
                        PSYS_SRC_BURST_SPEED_MIN,minSpeed,
                        PSYS_SRC_BURST_SPEED_MAX,maxSpeed,
                        PSYS_SRC_TARGET_KEY,target,
                        PSYS_SRC_INNERANGLE,innerAngle, 
                        PSYS_SRC_OUTERANGLE,outerAngle,
                        PSYS_SRC_OMEGA, omega,
                        PSYS_SRC_MAX_AGE, life,
                        PSYS_SRC_TEXTURE, texture,
                        PSYS_PART_START_ALPHA, startAlpha,
                        PSYS_PART_END_ALPHA, endAlpha
                            ]);
}
default
{
    state_entry()
    {
        //  sanity check
        if (llGetInventoryType(gBullet) != INVENTORY_OBJECT) {
            
            return;
        }
        gPermFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS | PERMISSION_TRACK_CAMERA;
 
        if ( llGetAttached() )
            llRequestPermissions(llGetOwner(), gPermFlags);
    }
 
    attach(key id)
    {
        if (id)
            llRequestPermissions(id, gPermFlags);
        else
        {
            llStopAnimation(gShootAnimation);
            llReleaseControls();
        }
    }
 
    changed(integer change)
    {
        if (change & (CHANGED_OWNER | CHANGED_INVENTORY) )
            llResetScript();
    }
 
    run_time_permissions(integer perm)
    {
        //  ensure ALL required permissions have been granted
        if ( (perm & gPermFlags) == gPermFlags)
        {
            llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
            if (llGetAgentInfo(llDetectedKey(0)) & AGENT_MOUSELOOK)
            {
                llStartAnimation(gShootAnimation);
            }
            else
            {
                llStopAnimation(gShootAnimation);
                llParticleSystem([]);
                llStopSound();
            }
        }
    }
 
    control(key id, integer level, integer edge)
    {
        rotation Rot = llGetCameraRot();
        if ( level & CONTROL_ML_LBUTTON)
        {
            llSleep(.5);
            updateParticles();
           llLoopSound(gHoldSound, 1.0);
           state Charged;
        }
        else if (~level & edge & CONTROL_ML_LBUTTON)
        {
            llParticleSystem([]);
            llPlaySound(gShootSound, 1.0);
            llRezAtRoot(gBullet, llGetCameraPos() + <1.5, 0.0, 0.0>*Rot, gVelocity*llRot2Fwd(Rot), Rot, 10);
        }
    }
}

state Charged
{
    run_time_permissions(integer perm)
    {
        integer perm = llGetPermissions();
        //  ensure ALL required permissions have been granted
        if (perm & PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_TRACK_CAMERA)
        {
            llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
            if (llGetAgentInfo(llDetectedKey(0)) & AGENT_MOUSELOOK)
            {
                llStartAnimation(gShootAnimation);
            }
            else
            {
                llStopAnimation(gShootAnimation);
                llParticleSystem([]);
                llStopSound();
                state default;
            }
        }
    }
 
    control(key id, integer level, integer edge)
    {
        rotation Rot = llGetCameraRot();
        if ( level & CONTROL_ML_LBUTTON)
        {
            updateParticles();
        }
        else if ( edge & CONTROL_ML_LBUTTON)
        {
            llParticleSystem([]);
            llStopSound();
            llSleep(.01);
            llPlaySound(gShootSound, 1.0);            
            llRezAtRoot(gBullet2, llGetCameraPos() + <1.5, 0.0, 0.0>*Rot, gVelocity*llRot2Fwd(Rot), Rot, 10);
            llSleep(gReloadTime);
            state default;
            
        }
    }
}

the problem is when i click the left button(without holding) it still acts like I'm holding it. How do i fix this?

Link to comment
Share on other sites

in control event then the script needs finer conditions for when the mouse button is pressed

for example in state Charged

(level) fires on mouse_down and mouse_press
(edge) fires on mouse_down and mouse_up

look at the wiki code example in llTakeControls and rename the variables to:

integer mouse_down = level & edge;
integer mouse_up = ~level & edge;
integer mouse_press = level & ~edge;

then write our dev code like:

if (mouse_down) ...
if (mouse_up) ...

if (mouse_press) ...

when we have this firmly in our minds then we can inline: if (level & edge) .. etc

  • Like 1
Link to comment
Share on other sites

More specifically to what Mollymews said, in your code:

else if (~level & edge & CONTROL_ML_LBUTTON)

This triggers every time ML_LBUTTON is released. Doesn't matter how long you (don't) hold it down.

You basically need a little float to keep track of how long the button was held, and check that value on release.

Or, you can use llResetTime on press and llGetTime on release.

Edited by Wulfie Reanimator
  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

Maybe familiarising yourself with the lsl wiki might be a good idea so you know how to look up functions to see how they work, what they do etc.

http://wiki.secondlife.com/wiki/LSL_Portal

It might also be an idea to go over the basics a little so you understand how assign llGetTime to a variable and compare its saved time to its current time etc etc

My recommendation would be the college of scripting in world, they have a ton of tutorial books out on the wall and Rez is enabled so you can code and learn at the same time.

http://maps.secondlife.com/secondlife/Horsa/177/244/2165

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1489 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...