Mouse Aiming & Mouse Firing

From FSG Wiki

Jump to: navigation, search

Back to Main Page

A lots of people asked for me to add weapons firing to their mod. So instead of adapting to each mod, i rewrite the first code i use in my Cannonball.

I tried to make something pretty much easy to use, and that could fit in any mod. And i've come up with something pretty good.

But for now im just putting the code here, so you can test play with it.


Here you can try a Demo


Contents

Code

// TO SHOOT A LINE PROJECTILE
REMOVETRIGGER ShootLineProjectile 
ON ShootLineProjectile SET rcvdParam0 $0
ON ShootLineProjectile SET rcvdParam1 $1
ON ShootLineProjectile SET rcvdParam2 $2
ON ShootLineProjectile SET rcvdParam3 $3
ON ShootLineProjectile SET sendParam0 (rcvdParam0 * 50)
ON ShootLineProjectile SET sendParam1 (rcvdParam1 * 50)
ON ShootLineProjectile SET sendParam2 ((tmpX * (rcvdParam2 * -50)) / tmp)
ON ShootLineProjectile SET sendParam3 ((tmpY * (rcvdParam3 * -50)) / tmp)
ON ShootLineProjectile SET sendParam4 ELEMENT1
ON ShootLineProjectile EXEC GET_ANGLE rcvdParam0 rcvdParam1
ON ShootLineProjectile EXEC MoveLineProjectile sendParam0 sendParam1 sendParam2 sendParam3 sendParam4

// MAKE THE LINE PROJECTILE MOVE
REMOVETRIGGER MoveLineProjectile
ON MoveLineProjectile SET FlyX (($0 + $2) / 50)
ON MoveLineProjectile SET FlyY (($1 + $3) / 50)
ON MoveLineProjectile SET StopMoving 0
ON MoveLineProjectile IF (($0 / 50) > WIDTH) <SET StopMoving 1>
ON MoveLineProjectile IF (($1 / 50) > HEIGHT) <SET StopMoving 1>
ON MoveLineProjectile IF (($0 / 50) < 0) <SET StopMoving 1>
ON MoveLineProjectile IF (($1 / 50) < 0) <SET StopMoving 1>
ON MoveLineProjectile DRAW $4 LINE FlyX FlyY ($2 / 50) ($3 / 50)
ON MoveLineProjectile IF (StopMoving == 0) <TIMER 1 FRAMES MoveLineProjectile ($0 + $2) ($1 + $3) $2 $3 $4>

// TO SHOOT A FILLEDCIRCLE PROJECTILE
REMOVETRIGGER ShootCircleProjectile 
ON ShootCircleProjectile EXEC GET_ANGLE $0 $1
ON ShootCircleProjectile EXEC MoveCircleProjectile ($0 * 50) ($1 * 50) ((tmpX * ($2 * -50)) / tmp) ((tmpY * ($2 * -50)) / tmp) $3 ELEMENT1

// MAKE THE FILLEDCIRCLE PROJECTILE MOVE
REMOVETRIGGER MoveCircleProjectile
ON MoveCircleProjectile SET FlyX (($0 + $2) / 50)
ON MoveCircleProjectile SET FlyY (($1 + $3) / 50)
ON MoveCircleProjectile SET StopMoving 0
ON MoveCircleProjectile IF (($0 / 50) > WIDTH) <SET StopMoving 1>
ON MoveCircleProjectile IF (($1 / 50) > HEIGHT) <SET StopMoving 1>
ON MoveCircleProjectile IF (($0 / 50) < 0) <SET StopMoving 1>
ON MoveCircleProjectile IF (($1 / 50) < 0) <SET StopMoving 1>
ON MoveCircleProjectile DRAW $5 FILLEDCIRCLE FlyX FlyY $4
ON MoveCircleProjectile IF (StopMoving == 0) <TIMER 1 FRAMES MoveCircleProjectile ($0 + $2) ($1 + $3) $2 $3 $4 $5>

// CALCULATE THE X,Y MOVEMENT BETWEEN A POINT TO THE CURSOR
REMOVETRIGGER GET_ANGLE
ON GET_ANGLE SET tmpX ($0 - X) 
ON GET_ANGLE SET tmpY ($1 - Y)
ON GET_ANGLE SET tmp (SQRT ((tmpX * tmpX) + (tmpY * tmpY)))

Explications

 // TO SHOOT A LINE PROJECTILE
 REMOVETRIGGER ShootLineProjectile 
 ON ShootLineProjectile SET rcvdParam0 $0
 ON ShootLineProjectile SET rcvdParam1 $1
 ON ShootLineProjectile SET rcvdParam2 $2
 ON ShootLineProjectile SET sendParam0 (rcvdParam0 * 50)
 ON ShootLineProjectile SET sendParam1 (rcvdParam1 * 50)
 ON ShootLineProjectile SET sendParam2 ((tmpX * (rcvdParam2 * -50)) / tmp)
 ON ShootLineProjectile SET sendParam3 ((tmpY * (rcvdParam2 * -50)) / tmp)
 ON ShootLineProjectile SET sendParam4 ELEMENT1
 ON ShootLineProjectile EXEC GET_ANGLE rcvdParam0 rcvdParam1
 ON ShootLineProjectile EXEC MoveLineProjectile sendParam0 sendParam1 sendParam2 sendParam3 sendParam4

Received Parameters (Received by ShootLineProjectile)

These are parameters that the function receive when it is called, so lets call it like this for example:

 EXEC ShootLineProjectile 210 210 10
  • [210] --> rcvdParam0 or ($0): The X value of the Shooting Point (where the bullet start)
  • [210] --> rcvdParam1 or ($1): The Y value of the Shoooting Point (where the bullet start)
  • [10] ---> rcvdParam2 or ($2): The lenght of the line to be drawn

Parameters to send (To MoveLineProjectile)

This is just for better undesrtanding, you only need the shooting function to shoot and move a bullet.
These are parameters that the function will send executing the function, lets take a look:

  • sendParam0 or ($0): The X value of the Shooting Point (where the bullet start)
  • sendParam1 or ($1): The Y value of the Shoooting Point (where the bullet start)
  • sendParam2 or ($2): The X movement (Ex: +7=going 7 pixel Right, -7=going 7 pixel Left)
  • sendParam3 or ($3): The Y movement (Ex: +7=going 7 pixel Up, -7=going 7 pixel Down)
  • sendParam4 or ($4): The ElementID of the element to draw

So all theses parameters will be sent to the function MoveLineProjectile when we call it, like this:

 ON ShootLineProjectile EXEC MoveLineProjectile sendParam0 sendParam1 sendParam2 sendParam3 sendParam4

So each sendParam will become a $ in the function MoveLineProjectile.

How to use

So to use it simply use this syntax:

 EXEC ShootLineProjectile StartingX StartingY LineLength


inferno's SUPER COMPACT Version

This is inferno's compact version, for BS2 Alpha 5 and later.

// EXEC ShootProjectile StartX StartY Speed Element Aiming ShootType Radius DrawType ReplaceElement StopType
// EXEC ShootProjectile $0     $1    $2    $3       $4     $5         $6     $7          $8           $9

TRIGGER ShootProjectile {
IF ($0 > -10) {
          IF ($4 == 0) <SET tmpX ($0 - X)>
          IF ($4 == 0) <SET tmpY ($1 - Y)>
          SET tmp (SQRT ((tmpX * tmpX) + (tmpY * tmpY)))
          }
EXEC MoveProjectile ($0 * 50) ($1 * 50) ((tmpX * ($2 * -50)) / tmp) ((tmpY * ($2 * -50)) / tmp) $3 $7 $8 $9 $6 $5
SET StopVar 0
}

TRIGGER MoveProjectile {
SET FlyX (($0 + $2) / 50)
SET FlyY (($1 + $3) / 50)
SET StopMoving 0
IF (($0 / 50) > WIDTH) <SET StopMoving 1>
IF (($1 / 50) > HEIGHT) <SET StopMoving 1>
IF (($0 / 50) < 0) <SET StopMoving 1>
IF (($1 / 50) < 0) <SET StopMoving 1>
IF ($9 == 0) {
          IF ($5 == 0) <DRAW $4 LINE FlyX FlyY ($2 / 50) ($3 / 50)>
          IF ($5 == 1) <DRAW $4 REPLACELINE FlyX FlyY ($2 / 50) ($3 / 50) 0 $6>
          IF (($7 == 0) && (StopMoving == 0)) <TIMER 1 FRAMES MoveProjectile ($0 + $2) ($1 + $3) $2 $3 $4 $5 $6 $7 $8 0>
          IF (($7 == 1) && (StopMoving == 0) && (StopVar == 0)) <TIMER 1 FRAMES MoveProjectile ($0 + $2) ($1 + $3) $2 $3 $4 $5 $6 $7 $8 0>
          }
IF ($9 == 1) {
          IF ($5 == 0) <DRAW $4 FILLEDCIRCLE FlyX FlyY $8>
          IF ($5 == 1) <DRAW $4 REPLACEFILLEDCIRCLE FlyX FlyY $8 0 $6>
          IF (($7 == 0) && (StopMoving == 0)) <TIMER 1 FRAMES MoveProjectile ($0 + $2) ($1 + $3) $2 $3 $4 $5 $6 $7 $8 1>
          IF (($7 == 1) && (StopMoving == 0) && (StopVar == 0)) <TIMER 1 FRAMES MoveProjectile ($0 + $2) ($1 + $3) $2 $3 $4 $5 $6 $7 $8 1>
IF (StopMoving == 1) <DRAW ELEMENT:Clear REPLACEFILLEDCIRCLE 0 0 (WIDTH * 2) 0 $4>
}
}

It is extremely compact, and provides lots of control. It also has a few comment lines explaining what you do to use it.

The parameters are set in this order:

StartX StartY Speed Element Aiming ShootType Radius DrawType ReplaceElement StopType

Parameter Explanations

StartX - The starting X coordinate

StartY - The starting Y coordinate

Speed - The speed, represented as the amount of pixels traveled per frame.

Element - The element of the projectile. Should be represented as ELEMENT:elementname.

Aiming - The aiming type. It can remain unused, but for automatic aiming and firing, edit

IF ($0 > -10) {
          IF ($4 == 0) <SET tmpX ($0 - X)>
          IF ($4 == 0) <SET tmpY ($1 - Y)>
          SET tmp (SQRT ((tmpX * tmpX) + (tmpY * tmpY)))
          }

with more IF statements, replacing X and Y with the coordinates to aim at, and the 0 in ($4 == 0) with the value you want Aiming to be in order for that type of aiming.

ShootType - 0 if you want it to be a line, 1 if you want it to be a circle.

Radius - The radius of a circle projectile. Only used if you choose to use a circle.

DrawType - Whether it draws over everything (0) of just a single element (1).

ReplaceElement - The element it draws over if you choose to set the DrawType to 1. Should be written as ELEMENT:elementname.

StopType - This determines whether it stops when StopVar is 1. Use INTERACTIONTRIGGERs with the projectile and the element that will stop it for full use of this.

Arcing Shots

This is a collaboration of JoeMaximum and inferno. Based off the compact aim and firing system as seen above.

// EXEC ShootProjectile StartX StartY Speed Element Aiming ShootType Radius DrawType ReplaceElement StopType
// EXEC ShootProjectile $0     $1    $2    $3       $4     $5         $6     $7          $8           $9

TRIGGER ShootProjectile {
IF ($0 > -10) {
          IF ($4 == 0) <SET tmpX ($0 - X)>
          IF ($4 == 0) <SET tmpY ($1 - Y)>
          SET tmp (SQRT ((tmpX * tmpX) + (tmpY * tmpY)))
          }
EXEC MoveProjectile ($0 * 50) ($1 * 50) ((tmpX * ($2 * -50)) / tmp) ((tmpY * ($2 * -50)) / tmp) $3 $7 $8 $9 $6 $5
SET StopVar 0
}

TRIGGER MoveProjectile {
SET FlyX (($0 + $2) / 50)
SET FlyY ((($1 + $3) / 50) + (GRAVITY / 100))
SET StopMoving 0
IF (($0 / 50) > WIDTH) <SET StopMoving 1>
IF (($1 / 50) > HEIGHT) <SET StopMoving 1>
IF (($0 / 50) < 0) <SET StopMoving 1>
IF (($1 / 50) < 0) <SET StopMoving 1>
IF ($9 == 0) {
          IF ($5 == 0) <DRAW $4 POINT FlyX FlyY>
          IF ($5 == 1) <DRAW $4 REPLACEFILLEDCIRCLE FlyX FlyY 0 0 $6>
          IF (($7 == 0) && (StopMoving == 0)) <TIMER 1 FRAMES MoveProjectile ($0 + $2) ($1 + $3) $2 ($3 + (GRAVITY / 100)) $4 $5 $6 $7 $8 0>
          IF (($7 == 1) && (StopMoving == 0) && (StopVar == 0)) <TIMER 1 FRAMES MoveProjectile ($0 + $2) ($1 + $3) $2 ($3 + (GRAVITY / 100)) $4 $5 $6 $7 $8 0>
          }
IF ($9 == 1) {
          IF ($5 == 0) <DRAW $4 FILLEDCIRCLE FlyX FlyY $8>
          IF ($5 == 1) <DRAW $4 REPLACEFILLEDCIRCLE FlyX FlyY $8 0 $6>
          IF (($7 == 0) && (StopMoving == 0)) <TIMER 1 FRAMES MoveProjectile ($0 + $2) ($1 + $3) $2 ($3 + (GRAVITY / 100)) $4 $5 $6 $7 $8 1>
          IF (($7 == 1) && (StopMoving == 0) && (StopVar == 0)) <TIMER 1 FRAMES MoveProjectile ($0 + $2) ($1 + $3) $2 ($3 + (GRAVITY / 100)) $4 $5 $6 $7 $8 1>
IF (StopMoving == 1) <DRAW ELEMENT:Clear REPLACEFILLEDCIRCLE 0 0 (WIDTH * 2) 0 $4>
}
}

The parameters are the same as inferno's compact version, but the shots arc.

Personal tools