I've been manually putting shadows under my sideview enemy sprites. Most of the time it's not a problem, except when a skill is used on them like Piledriver which flips them upside-down, or Uppercut which send them soaring into the air. Then the shadow looks silly.
Any recommended workarounds? I imagine there are plugins to add shadows (in have the VisuStella basic plugins, don't see anything for enemies).
UPDATE: I couldn't find a plugin which did the job of adding shadows to side-view static enemy sprites so I've been working on this. It's not the best code, but it seems to do what I want. It even shrinks the shadows when I cast Mini on an enemy.
Any recommended workarounds? I imagine there are plugins to add shadows (in have the VisuStella basic plugins, don't see anything for enemies).
UPDATE: I couldn't find a plugin which did the job of adding shadows to side-view static enemy sprites so I've been working on this. It's not the best code, but it seems to do what I want. It even shrinks the shadows when I cast Mini on an enemy.
Code:
(() => {
const _Sprite_Enemy_initialize = Sprite_Enemy.prototype.initialize;
Sprite_Enemy.prototype.initialize = function(battler) {
_Sprite_Enemy_initialize.call(this, battler);
// Initialize shadow sprite
this._shadowSprite = new Sprite();
this._shadowSprite.anchor.set(0.5, 0.5);
this._shadowSprite.visible = false; // Start hidden
this.addChild(this._shadowSprite);
// Initialize shadow scaling variable
this._shadowScaleX = 1;
this._shadowScaleY = 1;
// Wait for all bitmaps to initialize
this.waitForBitmapReady();
};
Sprite_Enemy.prototype.waitForBitmapReady = function() {
// Check if enemy and shadow bitmaps are ready
if (
this.bitmap &&
this.bitmap.isReady() &&
this._shadowSprite &&
(!this._shadowSprite.bitmap || this._shadowSprite.bitmap.isReady())
) {
// All bitmaps are ready
this.updateShadowScale();
this._shadowSprite.visible = true;
} else {
console.debug("Bitmap not ready, retrying...");
setTimeout(() => this.waitForBitmapReady(), 10); // Retry every 10ms
}
};
Sprite_Enemy.prototype.updateShadowScale = function() {
try {
const enemyWidth = this.bitmap ? this.bitmap.width : 0;
if (enemyWidth > 1) {
const enemyScaleX = this.scale.x; // Horizontal scaling factor of the enemy
const enemyScaleY = this.scale.y; // Vertical scaling factor of the enemy
const shadowWidth = this.bitmap.width * enemyScaleX; // Scale shadow width
const shadowHeight = shadowWidth / 8; // Maintain 8:1 ratio
// Create a new bitmap for the shadow (ellipse)
const shadowBitmap = new Bitmap(shadowWidth, shadowHeight);
const centerX = shadowWidth / 2;
const centerY = shadowHeight / 2;
const radiusX = shadowWidth / 2;
const radiusY = shadowHeight / 2;
// Loops through all points the shadow rectangle area
// Draw pixels in the rectangle which are also in the ellipse area
for (let y = 0; y < shadowHeight; y++) {
for (let x = 0; x < shadowWidth; x++) {
const dx = (x - centerX) / (shadowWidth / 2);
const dy = (y - centerY) / (shadowHeight / 2);
if (dx * dx + dy * dy <= 1) {
shadowBitmap.fillRect(x, y, 1, 1, "#000000");
}
}
}
this._shadowSprite.bitmap = shadowBitmap;
this._shadowSprite.opacity = 64; // Opaque is 255
// Position the shadow below the enemy
this._shadowSprite.x = 0;
this._shadowSprite.y = this.y + this.height;
// Scale the shadow to match the enemy
this._shadowSprite.scale.x = enemyScaleX;
this._shadowSprite.scale.y = enemyScaleY;
} else {
console.debug("Enemy bitmap width invalid; retrying...");
setTimeout(() => this.updateShadowScale(), 5);
}
} catch (error) {
console.error("Error in updateShadowScale:", error);
}
};
const _Sprite_Enemy_update = Sprite_Enemy.prototype.update;
Sprite_Enemy.prototype.update = function() {
try {
_Sprite_Enemy_update.call(this);
if (this._shadowSprite) {
this._shadowSprite.visible = this.visible;
}
} catch (error) {
//console.error("Error in update:", error);
}
};
const _Sprite_Enemy_destroy = Sprite_Enemy.prototype.destroy;
Sprite_Enemy.prototype.destroy = function() {
try {
if (this._shadowSprite) {
this.removeChild(this._shadowSprite);
this._shadowSprite.bitmap = null;
this._shadowSprite = null;
}
_Sprite_Enemy_destroy.call(this);
} catch (error) {
console.error("Error in destroy:", error);
}
};
})();