[Tutorial] [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry]
#28

Nice tutorial, I have learned some things myself, however I also think you could have made it better:
  • Although it's not necessary, you could tag FireShot[MAX_PLAYERS] as a bool so it only accepts two values.
  • I'm not sure but I think IsPlayerInAnyVehicle(playerid) works for passengers too, and I guess you don't want passengers to shoot. I would use if GetPlayerState(playerid) == PLAYER_STATE_DRIVER instead.
  • Instead of newkeys & 4 I would use newkeys & KEY_FIRE for better readability and because you never know if sa-mp might change its defines someday for some reason.
  • Why are you using SetPlayerTime(playerid,0,0) when the laser is fired?
  • When you do Float:dist = 50.0 and if(IsPlayerInRangeOfPoint(i, 50.0, x, y, z)) I think it's better that 50.0 be a define at the top of your script, so you can easily change that value if you want.
  • Instead of SetTimerEx("ShotFire", 1000, 0, "i", playerid) I think it's better to use GetTickCount() and a variable to store the time. I think with this you wouldn't even need FireShot[MAX_PLAYERS] anymore.
  • In your two loops you should use break; after your code is executed so the loop will not continue checking for players.
  • EDIT: VERY IMPORTANT!!! If you write a filterscript, you must return a value in your callbacks because otherwise those callbacks won't be called in your gamemode.
So I would script it like this. I have commented those lines of yours that I would remove or replace for others:

pawn Code:
#include <a_samp>

#define LASER_DISTANCE 50 // Added this

//new FireShot[MAX_PLAYERS];
new TimeOfLastShot[MAX_PLAYERS]; // This instead
new gRocketObj[MAX_PLAYERS];

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    //if(IsPlayerInAnyVehicle(playerid) && FireShot[playerid] == 0 && newkeys & 4 && !IsValidObject(gRocketObj[playerid]))   // Only run the code if the object doesn't already exist, otherwise more objects will take up gRocketObj and the previous ones won't be deleted
    if( (GetPlayerState(playerid) == PLAYER_STATE_DRIVER) && ( (GetTickCount() - TimeOfLastShot[playerid]) >= 1000 ) && (newkeys & KEY_FIRE) && !IsValidObject(gRocketObj[playerid]) ) // This instead
    {
        SetPlayerTime(playerid,0,0);
        new
            vehicleid = GetPlayerVehicleID(playerid),
            Float:x,
            Float:y,
            Float:z,
            Float:r,
            //Float:dist = 50.0,
            Float:dist = LASER_DISTANCE, // This instead
            Float:tmpang,
            Float:tmpx,
            Float:tmpy,
            Float:tmpz;

        //FireShot[playerid] = 1;
        //SetTimerEx("ShotFire", 1000, 0, "i", playerid);
        TimeOfLastShot[playerid] = GetTickCount(); // This instead
        GetVehiclePos(vehicleid, x, y, z);
        GetVehicleZAngle(vehicleid, r);
        new rand = random(12);
        switch(rand)
        {
            case 0: gRocketObj[playerid] = CreateObject(18647, x, y, z, 0, 0, r);
            case 1: gRocketObj[playerid] = CreateObject(18648, x, y, z, 0, 0, r);
            case 2: gRocketObj[playerid] = CreateObject(18649, x, y, z, 0, 0, r);
            case 3: gRocketObj[playerid] = CreateObject(18650, x, y, z, 0, 0, r);
            case 4: gRocketObj[playerid] = CreateObject(18651, x, y, z, 0, 0, r);
            case 5: gRocketObj[playerid] = CreateObject(18652, x, y, z, 0, 0, r);
            case 6: gRocketObj[playerid] = CreateObject(18647, x, y, z, 0, 0, r+90);
            case 7: gRocketObj[playerid] = CreateObject(18648, x, y, z, 0, 0, r+90);
            case 8: gRocketObj[playerid] = CreateObject(18649, x, y, z, 0, 0, r+90);
            case 9: gRocketObj[playerid] = CreateObject(18650, x, y, z, 0, 0, r+90);
            case 10: gRocketObj[playerid] = CreateObject(18651, x, y, z, 0, 0, r+90);
            case 11: gRocketObj[playerid] = CreateObject(18652, x, y, z, 0, 0, r+90);
        }
        for(new i;i<MAX_PLAYERS;i++)
        {
            if(IsPlayerConnected(i))
            if(i == playerid)continue;
            //if(IsPlayerInRangeOfPoint(i, 50.0, x, y, z))
            if(IsPlayerInRangeOfPoint(i, LASER_DISTANCE, x, y, z)) // This instead
            {
                GetPlayerPos(i, tmpx, tmpy, tmpz);
                tmpang = (90-atan2(tmpy-y, tmpx-x));
                if(tmpang < 0)tmpang = 360.0+tmpang;
                tmpang = 360.0 - tmpang;
                if(floatabs(tmpang-r) < 5.0)
                {
                    dist = GetPlayerDistanceFromPoint(i, x, y, z);
                    break; // Added this
                }
            }
        }
        MoveObject(gRocketObj[playerid],x + (dist * floatsin(-r, degrees)),y + (dist * floatcos(-r, degrees)),z,100.0);                             // Nice and fast!
    }
    return 1; // Added this
}



/*forward ShotFire(playerid);
public ShotFire(playerid)
{
        FireShot[playerid] = 0;
        return 1;
}*/
 //Not needed

public OnObjectMoved(objectid)
{
        for(new i;i<MAX_PLAYERS;i++)
        {
                if(objectid == gRocketObj[i])
                {
                    new
                        Float:x,
                        Float:y,
                        Float:z;

                    GetObjectPos(gRocketObj[i], x, y, z);
                    CreateExplosion(x, y, z, 11, 3.0);
                    DestroyObject(gRocketObj[i]);
                    break; // Added this
                }
        }
        return 1; // Added this
}
Reply


Messages In This Thread
[TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 23.10.2014, 14:52
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Onfroi - 23.10.2014, 16:26
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 23.10.2014, 16:30
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by IDarkness - 23.10.2014, 18:42
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by jamjamnewbie - 24.10.2014, 00:13
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by JaKe Elite - 24.10.2014, 00:18
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 24.10.2014, 09:47
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by GamingPro - 25.10.2014, 13:16
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by ExtremeHostOwner - 25.10.2014, 13:21
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Glossy42O - 25.10.2014, 14:35
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 25.10.2014, 14:58
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by ExtremeHostOwner - 25.10.2014, 15:46
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Slaughters - 25.10.2014, 16:13
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by serdar189 - 25.10.2014, 16:34
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 25.10.2014, 16:44
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Quickie - 26.10.2014, 08:49
AW: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Flori - 26.10.2014, 09:02
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 26.10.2014, 16:48
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Alex Magaсa - 30.10.2014, 14:14
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by iPrivate - 01.11.2014, 17:49
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 01.11.2014, 18:00
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by BornHuman - 02.11.2014, 17:44
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Michael@Belgium - 02.11.2014, 20:50
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 03.11.2014, 05:53
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Arastair - 03.11.2014, 07:37
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Arxalan - 02.01.2015, 03:45
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 02.01.2015, 13:33
Respuesta: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Gryphus One - 17.01.2015, 21:25
Re: Respuesta: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 29.01.2015, 13:50
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Arastair - 29.01.2015, 13:58
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 29.01.2015, 14:34
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Vince - 29.01.2015, 14:50
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 30.01.2015, 14:55
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by lanix - 30.01.2015, 16:51
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by ZcvDev - 31.01.2015, 11:09
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 01.02.2015, 15:54
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by shadowstorm - 05.04.2015, 02:42
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by Rudy_ - 10.04.2015, 08:17
Re: [TuT]How to create a Vehicle Shooting (laser) + Some [Trigonometry] - by ItzRbj - 03.07.2015, 09:45

Forum Jump:


Users browsing this thread: 7 Guest(s)