TheKeeper81

Untitled

Oct 16th, 2025
1,466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  Popgun
  3. //
  4. //  This script is a basic gun- it waits for a mouseclick in mouselook and
  5. //  then fires a bullet in the direction the user is facing.
  6. //  It also animates the avatar holding it, to create a convining hold and
  7. //  firing look for the gun.
  8. //
  9. //  This script can be used as a good basis for other weapons.
  10. //  
  11.  
  12. float SPEED         = 20.0;         //  Speed of arrow in meters/sec
  13. integer LIFETIME    = 7;            //  How many seconds will bullets live
  14.                                     //  before deleting themselves
  15. float DELAY         = 0.2;          //  Delay between shots to impose
  16.  
  17. vector vel;                         //  Used to store velocity of arrow to be shot
  18. vector pos;                         //  Used to store position of arrow to be shot
  19. rotation rot;                       //  Used to store rotation of arrow to be shot
  20.  
  21. integer have_permissions = FALSE;   //  Indicates whether wearer has yet given permission
  22.                                     //  to take over their controls and animation.
  23.                                    
  24. integer armed = TRUE;               //  Used to impose a short delay between firings
  25.  
  26. string instruction_held_1 = "Use Mouselook (press 'M') to shoot me.";
  27. string instruction_held_2 = "Choose 'Detach' from my menu to take me off.";
  28.                                     //  Echoed to wearer when they are holding the bow
  29. string instruction_not_held = "Right-click (apple-click) me, and choose More > Wear' from the menu to use me.";
  30.                                     //  Echoed to toucher if not worn
  31.                                    
  32.  
  33. fire()
  34. {
  35.     //
  36.     //  This subroutine creates and fires an arrow
  37.     //
  38.     if (armed)
  39.     {
  40.         //  
  41.         //  Actually fires the arrow
  42.         //  
  43.         armed = FALSE;
  44.         rot = llGetRot();               //  Get current avatar mouselook direction
  45.         vel = llRot2Fwd(rot);           //  Convert rotation to a direction vector
  46.         pos = llGetPos();               //  Get position of avatar to create arrow
  47.         pos = pos + vel;                //  Create arrow slightly in direction of travel
  48.         pos.z += 0.75;                  //  Correct creation point upward to eye point
  49.                                         //  from hips,  so that in mouselook we see arrow
  50.                                         //  travelling away from the camera.
  51.         vel = vel * SPEED;              //  Multiply normalized vector by speed
  52.        
  53.         //llStartAnimation("shoot_R_handgun");    //  Trigger the bow release animation
  54.         llTriggerSound("shoot", 1.0); //  Make the sound of the arrow being shot
  55.         llRezObject("bullet", pos, vel, rot, LIFETIME);
  56.                                             //  Create the actual arrow from object
  57.                                             //  inventory, and set its position, velocity,
  58.                                             //  and rotation.  Pass a parameter to it to
  59.                                             //  tell it how long to live.
  60.                                            
  61.         llSetTimerEvent(DELAY);         //  Wait until can fire again
  62.     }
  63. }
  64.  
  65. default
  66. {
  67.     state_entry()
  68.     //  
  69.     //  This routine is called whenever the script is edited and restarted.  So if you
  70.     //  are editing the bow while wearing it, this code will re-request permissions
  71.     //  to animate and capture controls.
  72.     //
  73.     {
  74.         if (!have_permissions)
  75.         {
  76.             llRequestPermissions(llGetOwner(),  
  77.                 PERMISSION_TRIGGER_ANIMATION| PERMISSION_TAKE_CONTROLS);  
  78.         }
  79.     }
  80.     on_rez(integer param)
  81.     {
  82.         //
  83.         //  Called when the gun is created from inventory.
  84.         //
  85.         llPreloadSound("shoot");        //  Preload shooting sound so you hear it
  86.     }
  87.  
  88.      run_time_permissions(integer permissions)
  89.     {
  90.         //
  91.         //  This routine is called when the user accepts the permissions request
  92.         //  (sometimes this is automatic)
  93.         //  so on receiving permissions, start animation and take controls.
  94.         //
  95.         if (permissions == PERMISSION_TRIGGER_ANIMATION| PERMISSION_TAKE_CONTROLS)
  96.         {
  97.             if (!have_permissions)
  98.             {
  99.                 llWhisper(0, instruction_held_1);
  100.                 llWhisper(0, instruction_held_2);
  101.             }
  102.             llTakeControls((CONTROL_ML_LBUTTON | CONTROL_FWD), TRUE, FALSE);
  103.             llStartAnimation("hold_R_handgun");
  104.             have_permissions = TRUE;
  105.         }
  106.     }
  107.  
  108.     attach(key attachedAgent)
  109.     {
  110.         //
  111.         //  If attached/detached from agent, change behavior
  112.         //  
  113.         if (attachedAgent != NULL_KEY)
  114.         {
  115.             //  Bow has been attached or rezzed from inventory, so
  116.             //  ask for needed permissions.
  117.             llRequestPermissions(llGetOwner(),  
  118.                 PERMISSION_TRIGGER_ANIMATION| PERMISSION_TAKE_CONTROLS);  
  119.         }
  120.         else
  121.         {
  122.             //  Bow has been detached from avatar, so stop animation and release controls
  123.             if (have_permissions)
  124.             {
  125.                 llStopAnimation("hold_R_handgun");
  126.                 llStopAnimation("aim_R_handgun");
  127.                 llReleaseControls();
  128.                 llSetRot(<0,0,0,1>);
  129.                 have_permissions = FALSE;
  130.             }
  131.         }
  132.     }
  133.  
  134.     control(key name, integer levels, integer edges)
  135.     {
  136.         //  This function is called when the mouse button or other controls
  137.         //  are pressed, and the controls are being captured.
  138.         //  
  139.         //  Note the logical AND (single &) used - the levels and edges
  140.         //  variables passed in are bitmasks, and must be checked with
  141.         //  logical compare.
  142.         //  
  143.         //  Checking for both edge and level means that the button has just
  144.         //  been pushed down, which is when we want to fire the arrow!
  145.         //
  146.         if (  ((edges & (CONTROL_ML_LBUTTON | CONTROL_FWD)))
  147.             &&((levels & (CONTROL_ML_LBUTTON | CONTROL_FWD))) )
  148.         {
  149.             //  If left mousebutton is pressed, fire arrow
  150.             fire();
  151.         }
  152.     }
  153.    
  154.     touch_start(integer num)
  155.     {
  156.         //  If touched, remind user how to enter mouselook and shoot
  157.         if (have_permissions)
  158.         {
  159.             llWhisper(0, instruction_held_1);
  160.             llWhisper(0, instruction_held_2);
  161.         }
  162.         else
  163.         {  
  164.             llWhisper(0, instruction_not_held);
  165.         }
  166.     }
  167.    
  168.     timer()
  169.     {
  170.         //  After timer expires, allow user to shoot bow again
  171.         llSetTimerEvent(0.0);
  172.         armed = TRUE;
  173.     }
  174.  
  175. }
  176.  
Advertisement
Add Comment
Please, Sign In to add comment