Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[added] TDM shop can be destroyed #2177

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions Entities/Industry/Trading/Trader.as
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

#include "RunnerCommon.as"
#include "Help.as";

#include "Hitters.as";

#include "TraderWantedList.as";

//trader methods
Expand All @@ -25,25 +23,6 @@ void onInit(CBlob@ this)
//EnsureWantedList();
}

void onReload(CSprite@ this)
{
this.getConsts().filename = this.getBlob().getSexNum() == 0 ?
"Entities/Special/WAR/Trading/TraderMale.png" :
"Entities/Special/WAR/Trading/TraderFemale.png";
}

void onGib(CSprite@ this)
{
CBlob@ blob = this.getBlob();
Vec2f pos = blob.getPosition();
Vec2f vel = blob.getVelocity();
vel.y -= 3.0f;
f32 hp = Maths::Min(Maths::Abs(blob.getHealth()), 2.0f) + 1.0;
CParticle@ Gib1 = makeGibParticle("Entities/Special/WAR/Trading/TraderGibs.png", pos, vel + getRandomVelocity(90, hp , 80), 0, 0, Vec2f(16, 16), 2.0f, 20, "/BodyGibFall");
CParticle@ Gib2 = makeGibParticle("Entities/Special/WAR/Trading/TraderGibs.png", pos, vel + getRandomVelocity(90, hp - 0.2 , 80), 1, 0, Vec2f(16, 16), 2.0f, 20, "/BodyGibFall");
CParticle@ Gib3 = makeGibParticle("Entities/Special/WAR/Trading/TraderGibs.png", pos, vel + getRandomVelocity(90, hp , 80), 2, 0, Vec2f(16, 16), 2.0f, 0, "/BodyGibFall");
}

void onHealthChange(CBlob@ this, f32 oldHealth)
{
if (this.getHealth() < 1.0f && !this.hasTag("dead"))
Expand Down Expand Up @@ -95,8 +74,18 @@ f32 onHit(CBlob@ this, Vec2f worldPoint, Vec2f velocity, f32 damage, CBlob@ hitt
return damage;
}


//sprite/anim update
void onInit(CSprite@ this)
{
//gender
CBlob@ blob = this.getBlob();
if (blob !is null && blob.exists("sex num"))
{
blob.setSexNum(blob.get_u8("sex num"));
SetFilename(this);
this.ReloadSprite(this.getConsts().filename);
}
}

void onTick(CSprite@ this)
{
Expand Down Expand Up @@ -143,3 +132,27 @@ void onTick(CSprite@ this)
this.SetAnimation("default");
}
}

void onReload(CSprite@ this)
{
SetFilename(this);
}

void onGib(CSprite@ this)
{
CBlob@ blob = this.getBlob();
Vec2f pos = blob.getPosition();
Vec2f vel = blob.getVelocity();
vel.y -= 3.0f;
f32 hp = Maths::Min(Maths::Abs(blob.getHealth()), 2.0f) + 1.0;
CParticle@ Gib1 = makeGibParticle("Entities/Special/WAR/Trading/TraderGibs.png", pos, vel + getRandomVelocity(90, hp , 80), 0, 0, Vec2f(16, 16), 2.0f, 20, "/BodyGibFall");
CParticle@ Gib2 = makeGibParticle("Entities/Special/WAR/Trading/TraderGibs.png", pos, vel + getRandomVelocity(90, hp - 0.2 , 80), 1, 0, Vec2f(16, 16), 2.0f, 20, "/BodyGibFall");
CParticle@ Gib3 = makeGibParticle("Entities/Special/WAR/Trading/TraderGibs.png", pos, vel + getRandomVelocity(90, hp , 80), 2, 0, Vec2f(16, 16), 2.0f, 0, "/BodyGibFall");
}

void SetFilename(CSprite@ this)
{
this.getConsts().filename = this.getBlob().getSexNum() == 0 ?
"Entities/Special/WAR/Trading/TraderMale.png" :
"Entities/Special/WAR/Trading/TraderFemale.png";
}
55 changes: 39 additions & 16 deletions Entities/Industry/Trading/TradingPost.as
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ void onInit(CBlob@ this)
if (sprite !is null)
{
sprite.SetZ(-50.0f); // push to background
string sex = traderRandom.NextRanged(2) == 0 ? "TraderMale.png" : "TraderFemale.png";
u8 trader_sex_num = this.getNetworkID() % 2;
string sex = (trader_sex_num == 0) ? "TraderMale.png" : "TraderFemale.png";
this.set_u8("trader sex num", trader_sex_num);
CSpriteLayer@ trader = sprite.addSpriteLayer("trader", sex, 16, 16, 0, 0);
trader.SetRelativeZ(20);
Animation@ stop = trader.addAnimation("stop", 1, false);
Expand All @@ -30,25 +32,25 @@ void onInit(CBlob@ this)
this.set_bool("moving left", false);
this.set_u32("move timer", getGameTime() + (traderRandom.NextRanged(5) + 5)*getTicksASecond());
this.set_u32("next offset", traderRandom.NextRanged(16));

}

//TODO: set shop type and spawn trader based on some property
}



//Sprite updates

void onTick(CSprite@ this)
{
//TODO: empty? show it.
CBlob@ blob = this.getBlob();
if (blob is null) return;

CSpriteLayer@ trader = this.getSpriteLayer("trader");
bool trader_moving = blob.get_bool("trader moving");
bool moving_left = blob.get_bool("moving left");
u32 move_timer = blob.get_u32("move timer");
u32 next_offset = blob.get_u32("next offset");

if (!trader_moving)
{
if (move_timer <= getGameTime())
Expand All @@ -71,7 +73,6 @@ void onTick(CSprite@ this)
{
offset.x -= 0.5f;
trader.SetOffset(offset);

}
else if (moving_left && offset.x <= -next_offset)
{
Expand All @@ -80,13 +81,11 @@ void onTick(CSprite@ this)
blob.set_u32("move timer", getGameTime() + (traderRandom.NextRanged(5) + 5)*getTicksASecond());
blob.set_u32("next offset", traderRandom.NextRanged(16));
trader.SetAnimation("stop");

}
else if (!moving_left && offset.x > -next_offset)
{
offset.x -= 0.5f;
trader.SetOffset(offset);

}
else if (!moving_left && offset.x <= -next_offset)
{
Expand All @@ -95,11 +94,8 @@ void onTick(CSprite@ this)
blob.set_u32("move timer", getGameTime() + (traderRandom.NextRanged(5) + 5)*getTicksASecond());
blob.set_u32("next offset", traderRandom.NextRanged(16));
trader.SetAnimation("stop");

}

}

}

f32 onHit(CBlob@ this, Vec2f worldPoint, Vec2f velocity, f32 damage, CBlob@ hitterBlob, u8 customData)
Expand All @@ -110,18 +106,45 @@ f32 onHit(CBlob@ this, Vec2f worldPoint, Vec2f velocity, f32 damage, CBlob@ hitt
} //no griffing

this.Damage(damage, hitterBlob);

return 0.0f;
}


void onHealthChange(CBlob@ this, f32 oldHealth)
{
CSprite @sprite = this.getSprite();

if (oldHealth > 0.0f && this.getHealth() < 0.0f)
// destructible in TDM and Sandbox only
CRules@ rules = getRules();
bool TDM = rules.gamemode_name == "Team Deathmatch";
bool SBX = rules.gamemode_name == "Sandbox";

if (!isServer() || !(TDM || SBX))
return;

if (oldHealth > 0.0f && this.getHealth() <= 0.0f)
{
MakeDustParticle(this.getPosition(), "Smoke.png");
this.getSprite().PlaySound("/BuildingExplosion");
// spawn trader that can be killed
if (this.exists("trader sex num"))
{
CBlob@ trader = server_CreateBlobNoInit("trader");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be calling trader.Init() + doing a null check

Clients can run this code and end up hitting a null error.

if (trader !is null)
{
trader.set_u8("sex num", this.get_u8("trader sex num"));
trader.setPosition(this.getPosition());
trader.Init();
}
}

// destroy building
this.server_Die();
}
}

void onDie(CBlob@ this)
{
MakeDustParticle(this.getPosition(), "Smoke.png");

CSprite@ sprite = this.getSprite();
sprite.PlaySound("/BuildingExplosion");
sprite.Gib();
}