01.11.2014, 18:00
Thanks.
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]);
FireShot[i] = 0;
}
}
}
new FireShot[MAX_PLAYERS];
FireShot[playerid] = 1;
SetTimerEx("ShotFire", 1000, 0, "i", playerid);
forward ShotFire(playerid);
public ShotFire(playerid)
{
FireShot[playerid] = 0;
return 1;
}
#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
}
Nice tutorial, I have learned some things myself, however I also think you could have made it better.
|
Haven't read it all yet, but this seems to be a recurrent problem among many users. Why don't you just use KEY_FIRE? I've noticed many people do the same when they need to use, for example, PLAYER_STATE_DRIVER. I reckon using the definition is a lot more clearer than 4 or 2?
|