
class Z_ShadeMe : CustomInventory
{
// reference to the shadow
Z_SpriteShadow myShadow;
int countFromLastUpdate;
int maxMonsterShadowAgeInTicks;
bool shadowCloseEnoughToPlayer;
...
void setEnableShadow(bool bEnable) {
countFromLastUpdate = 0;
if (bEnable != self.shadowCloseEnoughToPlayer) {
if (bEnable) {
if (!self.myShadow) {
let sh = Z_SpriteShadow(Spawn("Z_SpriteShadow", Owner.Pos, NO_REPLACE));
if (sh)
{
self.myShadow = sh;
self.myShadow.ownerRef = Owner;
}
}
} else {
//destroy the shadow because disabled, or too far from the player.
if (self.myShadow) {
self.myShadow.Destroy();
self.myShadow = null;
}
}
//update
self.shadowCloseEnoughToPlayer = bEnable;
}
}
override void PostBeginPlay() {
//by default is false, and is enabled in Handler WorldTick():
self.shadowCloseEnoughToPlayer = false;
//if countFromLastUpdate is bigger than 2s (35 * 2 ticks) remove the shadow unconditionally.
maxMonsterShadowAgeInTicks = 35 * 2;
Super.PostBeginPlay();
}
override void Tick() {
countFromLastUpdate++;
//Cleanup : shadow is too old, it means it has not been tested
//for player proximity for a long time, which means it is indeed too far !
if (countFromLastUpdate > maxMonsterShadowAgeInTicks) {
self.setEnableShadow(false);
}
Super.Tick();
}
...
}
class SpriteShadowHandler : EventHandler
{
int countTicks;
// spawn shadows for monsters
override void WorldThingSpawned(WorldEvent e)
{
if (e.Thing.bIsMonster && !e.Thing.CountInv("Z_ShadeMe"))
{
//Every monster got a Z_ShadeMe inventory token, but your shadow is effectively enabled or not later.
e.Thing.A_GiveInventory("Z_ShadeMe", 1);
}
}
...
override void WorldTick(void)
{
//0) Save game : do not serialize shadows.
if (gameaction == ga_savegame || gameaction == ga_autosave)
{
ThinkerIterator it = ThinkerIterator.Create("Z_ShadeMe");
Z_ShadeMe shadeMe;
while (shadeMe = Z_ShadeMe(it.Next()))
{
//VSO: Destroy all shadows, NOT the Z_ShadeMe !
// monster Shadows will be re-created at nexts WorldTick().
if (shadeMe) {
shadeMe.setEnableShadow(false);
}
}
//VSO: Trick so that the next Shadow re-creation will only occur in 35 ticks = 1s.
countTicks = 0;
return;
}
countTicks++;
// no need to update this too often
if (countTicks % 35 != 0) return;
PlayerInfo p = players[consoleplayer];
if (!p) return;
double shadowDist = Cvar.FindCvar("spriteshadowdistance").GetFloat();
bool shadows_enabled = Cvar.FindCvar("spriteshadowenabled").GetBool();
//1) update shadows enabling flag to Z_ShadeMe according to distance to player:
// look for shadow casters around you
BlockThingsIterator it = BlockThingsIterator.Create(p.mo, shadowDist);
while (it.Next())
{
Actor mo = it.thing;
//only consider monster-tagged things:
if (mo.bIsMonster)
{
let shadeMe = Z_ShadeMe(mo.FindInventory("Z_ShadeMe"));
if (shadeMe)
{
if (shadows_enabled && (mo.Distance2DSquared(p.mo) < shadowDist ** 2)) {
shadeMe.setEnableShadow(true);
} else {
shadeMe.setEnableShadow(false);
}
}
}
}//end while.
}
}
Can you make a pull request at https://github.com/nashmuhandes/SpriteShadow ? It is more convenient to review code changes that way.
@panzerfaustmna: thank you, added to Github issues. Will fix when I have more spare time.https://github.com/nashmuhandes/SpriteShadow/issues/3
vsonnier wrote:@panzerfaustmna: thank you, added to Github issues. Will fix when I have more spare time. :) https://github.com/nashmuhandes/SpriteShadow/issues/3
I've asubmitted a PR fixing this problem that I have encountered as well.
Users browsing this forum: No registered users and 2 guests