Skip to content

Commit

Permalink
New effect: CJ101
Browse files Browse the repository at this point in the history
  • Loading branch information
gromchek committed Jan 28, 2024
1 parent 418e2c6 commit 30fad96
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/gtasa/effects/custom/unsorted/CJ101Effect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "util/EffectBase.h"
#include "util/GameUtil.h"
#include "util/GenericUtil.h"
#include "util/MathHelper.h"

#include <vector>

#include <CCarCtrl.h>
#include <extensions/ScriptCommands.h>

using namespace plugin;

class CJ101Effect : public EffectBase
{
private:
const int PLANE_SPAWN_TIME = 5000;
const int planesId[5] = {511, 512, 476, 593, 513};
int timer = 0;

public:
bool
CanActivate () override
{
auto *player = FindPlayerPed ();
return player && !player->m_nAreaCode && GameUtil::IsPlayerSafe ();
}

void
OnStart (EffectInstance *inst) override
{
timer = 0;
}

void
OnTick (EffectInstance *inst) override
{
if (!CanActivate ()) return;
auto *player = FindPlayerPed ();

timer += int (GenericUtil::CalculateTick ());
if (timer >= PLANE_SPAWN_TIME)
{
int id = planesId[inst->Random (0, 10000) % std::size (planesId)];
auto playerPos = player->GetPosition ();
auto [pos, angle]
= GetPositionAndHeading (playerPos, player->GetHeading (),
inst->Random (0, 1));

pos.z += inst->Random (0, 5);
auto *plane = reinterpret_cast<CPlane *> (
GameUtil::CreateVehicle (id, pos, angle, true));

float force = inst->Random (0.8f, 1.1f);
plane->m_vecMoveSpeed.x = -std::sin (angle) * force;
plane->m_vecMoveSpeed.y = std::cos (angle) * force;
plane->SetHeading (angle);

Command<eScriptCommands::COMMAND_PLANE_GOTO_COORDS> (
plane, playerPos.x, playerPos.y, playerPos.z, 0.0f, 0.0f);

if (id == 476) plane->SetGearUp ();

plane->m_fHealth = 249.0f;

timer -= PLANE_SPAWN_TIME;
}
}

std::pair<CVector, float>
GetPositionAndHeading (const CVector &playerPos, float playerAngle,
bool getBehind)
{
float dirX = 100.0f * -std::sin (playerAngle);
float dirY = 100.0f * std::cos (playerAngle);

if (getBehind)
{
float posX = playerPos.x - dirX;
float posY = playerPos.y - dirY;
float posZ = CWorld::FindGroundZForCoord (posX, posY);
return {{posX, posY, posZ}, playerAngle};
}
else
{
float posX = playerPos.x + dirX;
float posY = playerPos.y + dirY;
float posZ = CWorld::FindGroundZForCoord (posX, posY);
playerAngle -= M_PI;
return {{posX, posY, posZ}, playerAngle};
}
}
};

DEFINE_EFFECT (CJ101Effect, "effect_cj101", 0);

0 comments on commit 30fad96

Please sign in to comment.