Skip to content

Commit

Permalink
feat: added custom buttons entity that delivers different ammo type o…
Browse files Browse the repository at this point in the history
…n impulse use
  • Loading branch information
FeiRoN23 committed Jan 10, 2024
1 parent 7844bb6 commit df85e34
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 15 deletions.
66 changes: 53 additions & 13 deletions dlls/ammodispenser.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,82 @@
#include "ammodispenser.h"

LINK_ENTITY_TO_CLASS(ammo_dispenser, CAmmoDispenser)
const char* CButtonDispenser::ammoTypesNames[] =
{
"ammo_9mmclip",
"ammo_9mmAR",
"ammo_buckshot",
};

void CAmmoDispenser::Spawn()
void CObjectDispenser::Spawn()
{
pev->solid = SOLID_NOT;
pev->movetype = MOVETYPE_NONE;
ALERT(at_console, "Ammo dispenser spawned\n");
//UTIL_SetSize(pev, Vector(0, 0, 0), Vector(0, 0, 0));

}

void CAmmoDispenser::dispense_ammo() {
ALERT(at_console, "dispensing 9mm ammo\n");
//Create("item_sodacan", pev->origin, pev->angles, edict());
void CObjectDispenser::dispense_ammo(string_t ammoType) {
ALERT(at_console, "dispensing %s\n", STRING(ammoType));
edict_t* pent;
pent = CREATE_NAMED_ENTITY(MAKE_STRING("ammo_9mmclip"));
pent = CREATE_NAMED_ENTITY(ammoType);

if (FNullEnt(pent))
{
ALERT(at_console, "NULL Ent in Create!\n");
return;
}

CBaseEntity* ammoEntity = Instance(pent);

ammoEntity->pev->origin = pev->origin;
ammoEntity->pev->origin = Vector(pev->origin.x, pev->origin.y, pev->origin.z - 2.5f);
ammoEntity->pev->angles = pev->angles;
ammoEntity->Spawn();
ammoEntity->pev->movetype = MOVETYPE_NONE;
ammoEntity->pev->solid = SOLID_TRIGGER;

}

LINK_ENTITY_TO_CLASS(ammo_dispenser, CObjectDispenser)

void CButtonDispenser::Spawn()
{
pev->movetype = MOVETYPE_PUSH;
pev->solid = SOLID_BSP;
SET_MODEL(ENT(pev), STRING(pev->model));
SetUse(&CButtonDispenser::Use);
}

//Old method to spawn
//Create("ammo_9mmclip", pev->origin, pev->angles, edict());

void CButtonDispenser::Use(CBaseEntity* pActivator, CBaseEntity* pCaller, USE_TYPE useType, float value)
{
if(pActivator->IsPlayer())
{

CBaseEntity* dispBaseEnt = UTIL_FindEntityByTargetname(nullptr, STRING(pev->target));

if (dispBaseEnt)
{
auto dispenserEnt = dynamic_cast<CObjectDispenser*>(dispBaseEnt);
dispenserEnt->dispense_ammo(m_spawnAmmoName);
}

}

}

void CAmmoDispenser::Use(CBaseEntity* pActivator, CBaseEntity* pCaller, USE_TYPE useType, float value)
bool CButtonDispenser::KeyValue(KeyValueData* pkvd)
{
ALERT(at_console, "using dispenser targeting %s\n", STRING(pev->target));
dispense_ammo();
if (FStrEq(pkvd->szKeyName, "ammoSpawn"))
{
int object = atoi(pkvd->szValue);
if (object >= 0 && object < ARRAYSIZE(ammoTypesNames))
m_spawnAmmoName = MAKE_STRING(ammoTypesNames[object]);
return true;
}
}


LINK_ENTITY_TO_CLASS(func_button_dispenser, CButtonDispenser);



14 changes: 12 additions & 2 deletions dlls/ammodispenser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@
#include "cbase.h"
#include "weapons.h"

class CAmmoDispenser:public CBaseEntity
class CObjectDispenser:public CBaseEntity
{
public:
void Spawn() override;
void dispense_ammo(string_t ammoType);
};

class CButtonDispenser: public CBaseToggle
{
public:
void Spawn() override;
void dispense_ammo();
void Use(CBaseEntity* pActivator, CBaseEntity* pCaller, USE_TYPE useType, float value) override;
bool KeyValue(KeyValueData* pkvd) override;
int m_spawnAmmoName;
static const char* ammoTypesNames[];
int ObjectCaps() override { return (CBaseToggle::ObjectCaps() & ~FCAP_ACROSS_TRANSITION) | (pev->takedamage ? 0 : FCAP_IMPULSE_USE); }
};
12 changes: 12 additions & 0 deletions fgd/npc-talker-mod.fgd
Original file line number Diff line number Diff line change
Expand Up @@ -2717,3 +2717,15 @@
[
targetname(target_source) : "Name" : ""
]
@SolidClass base(Target,Targetname) = func_button_dispenser
[
targetname(target_source) : "Name" : ""
target(target_destination) : "Dispenser name"
ammoSpawn(choices) : "Ammo to spawn" : 0 =
[
0: "9mm Clip"
1: "Machine Gun Clip"
2: "Shotgun Shells"
]

]
Binary file modified mod-map/npc1.jmf
Binary file not shown.
Binary file modified mod-map/npc1.jmx
Binary file not shown.

0 comments on commit df85e34

Please sign in to comment.