2023-11-07 09:04:22 +00:00
|
|
|
package funkin.play.components;
|
2022-03-26 02:30:37 +00:00
|
|
|
|
2023-05-26 21:10:35 +00:00
|
|
|
import funkin.play.character.CharacterData;
|
2022-03-26 02:30:37 +00:00
|
|
|
import flixel.FlxSprite;
|
2022-04-29 04:22:42 +00:00
|
|
|
import flixel.math.FlxMath;
|
2022-03-27 02:18:26 +00:00
|
|
|
import flixel.math.FlxPoint;
|
|
|
|
import funkin.play.character.CharacterData.CharacterDataParser;
|
2022-03-26 02:30:37 +00:00
|
|
|
import openfl.utils.Assets;
|
2024-02-22 23:55:24 +00:00
|
|
|
import funkin.graphics.FunkinSprite;
|
2023-11-07 09:04:22 +00:00
|
|
|
import funkin.util.MathUtil;
|
2022-03-26 02:30:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a rework of the health icon with the following changes:
|
|
|
|
* - The health icon now owns its own state logic. It queries health and updates the sprite itself,
|
|
|
|
* rather than relying on PlayState to command it.
|
|
|
|
* - The health icon now supports animations.
|
|
|
|
* - The health icon will now search for a SparrowV2 (XML) spritesheet, and use that for rendering if it can.
|
|
|
|
* - If it can't find a spritesheet, it will the old format; a two-frame 300x150 image.
|
|
|
|
* - If the spritesheet is found, the health icon will attempt to load and use the following animations as appropriate:
|
|
|
|
* - `idle`, `winning`, `losing`, `toWinning`, `fromWinning`, `toLosing`, `fromLosing`
|
|
|
|
* - The health icon is now easier to control via scripts.
|
|
|
|
* - Set `autoUpdate` to false to prevent the health icon from changing its own animations.
|
|
|
|
* - Once `autoUpdate` is false, you can manually call `playAnimation()` to play a specific animation.
|
|
|
|
* - i.e. `PlayState.instance.iconP1.playAnimation("losing")`
|
|
|
|
* - Scripts can also utilize all functionality that a normal FlxSprite would have access to, such as adding supplimental animations.
|
|
|
|
* - i.e. `PlayState.instance.iconP1.animation.addByPrefix("jumpscare", "jumpscare", 24, false);`
|
|
|
|
* @author MasterEric
|
|
|
|
*/
|
2023-10-17 06:42:52 +00:00
|
|
|
@:nullSafety
|
2024-02-22 23:55:24 +00:00
|
|
|
class HealthIcon extends FunkinSprite
|
2022-03-26 02:30:37 +00:00
|
|
|
{
|
2023-01-23 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* The character this icon is representing.
|
|
|
|
* Setting this variable will automatically update the graphic.
|
|
|
|
*/
|
2023-10-17 06:42:52 +00:00
|
|
|
public var characterId(default, set):Null<String>;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this health icon should automatically update its state based on the character's health.
|
|
|
|
* Note that turning this off means you have to manually do the following:
|
2023-05-26 21:10:35 +00:00
|
|
|
* - Boping the icon on the beat.
|
2023-01-23 03:25:45 +00:00
|
|
|
* - Switching between winning/losing/idle animations.
|
|
|
|
* - Repositioning the icon as health changes.
|
|
|
|
*/
|
|
|
|
public var autoUpdate:Bool = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Since the `scale` of the sprite dynamically changes over time,
|
|
|
|
* this value allows you to set a relative scale for the icon.
|
|
|
|
* @default 1x scale = 150px width and height.
|
|
|
|
*/
|
|
|
|
public var size:FlxPoint = new FlxPoint(1, 1);
|
|
|
|
|
|
|
|
/**
|
2023-05-26 21:10:35 +00:00
|
|
|
* Apply the "bop" animation once every X steps.
|
2023-01-23 03:25:45 +00:00
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
public var bopEvery:Int = 4;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The amount, in degrees, to rotate the icon by when boping.
|
|
|
|
* ERIC NOTE: I experimented with this a bit but ended up turning it off,
|
|
|
|
* but why not leave it in for the script kiddies?
|
|
|
|
*/
|
|
|
|
public var bopAngle:Float = 0.0;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The player the health icon is attached to.
|
|
|
|
*/
|
|
|
|
var playerId:Int = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the sprite is pixel art or not.
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
public var isPixel(default, set):Bool = false;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this is a legacy icon or not.
|
|
|
|
*/
|
|
|
|
var isLegacyStyle:Bool = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* At this amount of health, play the Winning animation instead of the idle.
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
static final WINNING_THRESHOLD:Float = 0.8 * 2;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* At this amount of health, play the Losing animation instead of the idle.
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
static final LOSING_THRESHOLD:Float = 0.2 * 2;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum health of the player.
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
static final MAXIMUM_HEALTH:Float = 2;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The size of a non-pixel icon when using the legacy format.
|
|
|
|
* Remember, modern icons can be any size.
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
public static final HEALTH_ICON_SIZE:Int = 150;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The size of a pixel icon when using the legacy format.
|
|
|
|
* Remember, modern icons can be any size.
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
static final PIXEL_ICON_SIZE:Int = 32;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The amount, in percent, to scale the icon by when bopping.
|
|
|
|
*/
|
|
|
|
static final BOP_SCALE:Float = 0.2;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* shitty hardcoded value for a specific positioning!!!
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
static final POSITION_OFFSET:Int = 26;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
public function new(char:String = 'bf', playerId:Int = 0)
|
|
|
|
{
|
|
|
|
super(0, 0);
|
|
|
|
this.playerId = playerId;
|
|
|
|
this.scrollFactor.set();
|
|
|
|
|
|
|
|
this.characterId = char;
|
|
|
|
|
|
|
|
initTargetSize();
|
|
|
|
}
|
|
|
|
|
2023-10-17 06:42:52 +00:00
|
|
|
function set_characterId(value:Null<String>):Null<String>
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
|
|
|
if (value == characterId) return value;
|
|
|
|
|
2023-10-17 06:42:52 +00:00
|
|
|
characterId = value ?? Constants.DEFAULT_HEALTH_ICON;
|
|
|
|
return characterId;
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 21:10:35 +00:00
|
|
|
function set_isPixel(value:Bool):Bool
|
|
|
|
{
|
|
|
|
if (value == isPixel) return value;
|
|
|
|
|
|
|
|
isPixel = value;
|
2023-10-17 06:42:52 +00:00
|
|
|
return isPixel;
|
2023-05-26 21:10:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Easter egg; press 9 in the PlayState to use the old player icon.
|
|
|
|
*/
|
|
|
|
public function toggleOldIcon():Void
|
|
|
|
{
|
2023-08-22 08:27:30 +00:00
|
|
|
if (characterId == 'bf-old')
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2024-02-06 02:35:58 +00:00
|
|
|
PlayState.instance.currentStage.getBoyfriend().initHealthIcon(false);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-22 08:27:30 +00:00
|
|
|
characterId = 'bf-old';
|
2024-02-06 02:35:58 +00:00
|
|
|
loadCharacter(characterId);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 06:42:52 +00:00
|
|
|
/**
|
|
|
|
* Use the provided CharacterHealthIconData to configure this health icon's appearance.
|
|
|
|
* @param data The data to use to configure this health icon.
|
|
|
|
*/
|
|
|
|
public function configure(data:Null<HealthIconData>):Void
|
|
|
|
{
|
|
|
|
if (data == null)
|
|
|
|
{
|
|
|
|
this.characterId = Constants.DEFAULT_HEALTH_ICON;
|
2023-10-18 05:02:10 +00:00
|
|
|
this.isPixel = false;
|
|
|
|
|
|
|
|
loadCharacter(characterId);
|
|
|
|
|
2023-10-17 06:42:52 +00:00
|
|
|
this.size.set(1.0, 1.0);
|
|
|
|
this.offset.x = 0.0;
|
|
|
|
this.offset.y = 0.0;
|
|
|
|
this.flipX = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.characterId = data.id;
|
2023-10-18 05:02:10 +00:00
|
|
|
this.isPixel = data.isPixel ?? false;
|
|
|
|
|
|
|
|
loadCharacter(characterId);
|
|
|
|
|
2023-10-17 06:42:52 +00:00
|
|
|
this.size.set(data.scale ?? 1.0, data.scale ?? 1.0);
|
|
|
|
this.offset.x = (data.offsets != null) ? data.offsets[0] : 0.0;
|
|
|
|
this.offset.y = (data.offsets != null) ? data.offsets[1] : 0.0;
|
|
|
|
this.flipX = data.flipX ?? false; // Face the OTHER way by default, since that is more common.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Called by Flixel every frame. Includes logic to manage the currently playing animation.
|
|
|
|
*/
|
|
|
|
override function update(elapsed:Float):Void
|
|
|
|
{
|
|
|
|
super.update(elapsed);
|
|
|
|
|
2023-05-26 21:10:35 +00:00
|
|
|
if (bopEvery != 0)
|
|
|
|
{
|
|
|
|
// Lerp the health icon back to its normal size,
|
|
|
|
// while maintaining aspect ratio.
|
|
|
|
if (this.width > this.height)
|
|
|
|
{
|
|
|
|
// Apply linear interpolation while accounting for frame rate.
|
2023-11-07 09:04:22 +00:00
|
|
|
var targetSize:Int = Std.int(MathUtil.coolLerp(this.width, HEALTH_ICON_SIZE * this.size.x, 0.15));
|
2023-05-26 21:10:35 +00:00
|
|
|
|
|
|
|
setGraphicSize(targetSize, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-07 09:04:22 +00:00
|
|
|
var targetSize:Int = Std.int(MathUtil.coolLerp(this.height, HEALTH_ICON_SIZE * this.size.y, 0.15));
|
2023-05-26 21:10:35 +00:00
|
|
|
|
|
|
|
setGraphicSize(0, targetSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lerp the health icon back to its normal angle.
|
2023-11-07 09:04:22 +00:00
|
|
|
this.angle = MathUtil.coolLerp(this.angle, 0, 0.15);
|
2023-05-26 21:10:35 +00:00
|
|
|
|
|
|
|
this.updateHitbox();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updatePosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the position (and status) of the health icon.
|
|
|
|
*/
|
|
|
|
public function updatePosition():Void
|
|
|
|
{
|
|
|
|
// Make sure autoUpdate is false if the health icon is not being used in the PlayState.
|
2023-01-23 03:25:45 +00:00
|
|
|
if (autoUpdate && PlayState.instance != null)
|
|
|
|
{
|
|
|
|
switch (playerId)
|
|
|
|
{
|
|
|
|
case 0: // Boyfriend
|
|
|
|
// Update the animation based on the current state.
|
|
|
|
updateHealthIcon(PlayState.instance.health);
|
|
|
|
// Update the position to match the health bar.
|
|
|
|
this.x = PlayState.instance.healthBar.x
|
|
|
|
+ (PlayState.instance.healthBar.width * (FlxMath.remapToRange(PlayState.instance.healthBar.value, 0, 2, 100, 0) * 0.01) - POSITION_OFFSET);
|
|
|
|
case 1: // Dad
|
|
|
|
// Update the animation based on the current state.
|
|
|
|
updateHealthIcon(MAXIMUM_HEALTH - PlayState.instance.health);
|
|
|
|
// Update the position to match the health bar.
|
|
|
|
this.x = PlayState.instance.healthBar.x
|
|
|
|
+ (PlayState.instance.healthBar.width * (FlxMath.remapToRange(PlayState.instance.healthBar.value, 0, 2, 100, 0) * 0.01))
|
|
|
|
- (this.width - POSITION_OFFSET);
|
|
|
|
}
|
|
|
|
|
2023-05-26 21:10:35 +00:00
|
|
|
// Keep the icon centered vertically on the health bar.
|
|
|
|
this.y = PlayState.instance.healthBar.y - (this.height / 2); // - (PlayState.instance.healthBar.height / 2)
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 21:10:35 +00:00
|
|
|
/**
|
|
|
|
* Called on every step.
|
|
|
|
* @param curStep The current step number.
|
|
|
|
*/
|
|
|
|
public function onStepHit(curStep:Int):Void
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2023-05-26 21:10:35 +00:00
|
|
|
// Make the icons bop.
|
|
|
|
if (bopEvery != 0 && curStep % bopEvery == 0 && isLegacyStyle)
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2023-05-26 21:10:35 +00:00
|
|
|
// Make the icon increase in size (the update function causes them to lerp back down).
|
2023-01-23 03:25:45 +00:00
|
|
|
if (this.width > this.height)
|
|
|
|
{
|
2023-05-26 21:10:35 +00:00
|
|
|
setGraphicSize(Std.int(this.width + (HEALTH_ICON_SIZE * this.size.x * BOP_SCALE)), 0);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-26 21:10:35 +00:00
|
|
|
setGraphicSize(0, Std.int(this.height + (HEALTH_ICON_SIZE * this.size.y * BOP_SCALE)));
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
2023-05-26 21:10:35 +00:00
|
|
|
// Make the icon twist (the update function causes them to lerp back to normal).
|
|
|
|
this.angle += bopAngle * (playerId == 0 ? 1 : -1);
|
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
this.updateHitbox();
|
2023-05-26 21:10:35 +00:00
|
|
|
|
|
|
|
// Ensure the icon is positioned correctly after updating the hitbox.
|
|
|
|
this.updatePosition();
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 21:10:35 +00:00
|
|
|
inline function initTargetSize():Void
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
|
|
|
setGraphicSize(HEALTH_ICON_SIZE);
|
|
|
|
updateHitbox();
|
|
|
|
}
|
|
|
|
|
2023-05-26 21:10:35 +00:00
|
|
|
function updateHealthIcon(health:Float):Void
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
|
|
|
// We want to efficiently handle animation playback
|
|
|
|
|
|
|
|
// Here, we use the current animation name to track the current state
|
|
|
|
// of a simple state machine. Neat!
|
|
|
|
|
|
|
|
switch (getCurrentAnimation())
|
|
|
|
{
|
2023-05-26 21:10:35 +00:00
|
|
|
case Idle:
|
|
|
|
if (health < LOSING_THRESHOLD)
|
|
|
|
{
|
|
|
|
playAnimation(ToLosing, Losing);
|
|
|
|
}
|
|
|
|
else if (health > WINNING_THRESHOLD)
|
|
|
|
{
|
|
|
|
playAnimation(ToWinning, Winning);
|
|
|
|
}
|
2023-01-23 03:25:45 +00:00
|
|
|
else
|
2023-05-26 21:10:35 +00:00
|
|
|
{
|
|
|
|
playAnimation(Idle);
|
|
|
|
}
|
|
|
|
case Winning:
|
|
|
|
if (health < WINNING_THRESHOLD)
|
|
|
|
{
|
|
|
|
playAnimation(FromWinning, Idle);
|
|
|
|
}
|
2023-01-23 03:25:45 +00:00
|
|
|
else
|
2023-05-26 21:10:35 +00:00
|
|
|
{
|
|
|
|
playAnimation(Winning, Idle);
|
|
|
|
}
|
|
|
|
case Losing:
|
|
|
|
if (health > LOSING_THRESHOLD) playAnimation(FromLosing, Idle);
|
2023-01-23 03:25:45 +00:00
|
|
|
else
|
2023-05-26 21:10:35 +00:00
|
|
|
{
|
|
|
|
playAnimation(Losing, Idle);
|
|
|
|
}
|
|
|
|
case ToLosing:
|
|
|
|
if (isAnimationFinished())
|
|
|
|
{
|
|
|
|
playAnimation(Losing, Idle);
|
|
|
|
}
|
|
|
|
case ToWinning:
|
|
|
|
if (isAnimationFinished())
|
|
|
|
{
|
|
|
|
playAnimation(Winning, Idle);
|
|
|
|
}
|
|
|
|
case FromLosing | FromWinning:
|
|
|
|
if (isAnimationFinished())
|
|
|
|
{
|
|
|
|
playAnimation(Idle);
|
|
|
|
}
|
|
|
|
case '':
|
|
|
|
playAnimation(Idle);
|
2023-01-23 03:25:45 +00:00
|
|
|
default:
|
2023-05-26 21:10:35 +00:00
|
|
|
playAnimation(Idle);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-26 21:10:35 +00:00
|
|
|
* Load health icon animations from a Sparrow XML file (the kind used by characters)
|
|
|
|
* Note that this is looking for SPECIFIC animation names, so you may need to modify the XML.
|
2023-06-08 20:30:45 +00:00
|
|
|
* @param charId
|
2023-01-23 03:25:45 +00:00
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
function loadAnimationNew():Void
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2023-05-26 21:10:35 +00:00
|
|
|
this.animation.addByPrefix(Idle, Idle, 24, true);
|
|
|
|
this.animation.addByPrefix(Winning, Winning, 24, true);
|
|
|
|
this.animation.addByPrefix(Losing, Losing, 24, true);
|
2024-05-02 08:09:38 +00:00
|
|
|
this.animation.addByPrefix(ToWinning, ToWinning, 24, false);
|
|
|
|
this.animation.addByPrefix(ToLosing, ToLosing, 24, false);
|
|
|
|
this.animation.addByPrefix(FromWinning, FromWinning, 24, false);
|
|
|
|
this.animation.addByPrefix(FromLosing, FromLosing, 24, false);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load health icon animations using the legacy format.
|
2023-05-26 21:10:35 +00:00
|
|
|
* Simply assumes two icons, the idle and losing icons.
|
2023-06-08 20:30:45 +00:00
|
|
|
* @param charId
|
2023-01-23 03:25:45 +00:00
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
function loadAnimationOld():Void
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2023-05-26 21:10:35 +00:00
|
|
|
// Don't flip BF's icon here! That's done later.
|
|
|
|
this.animation.add(Idle, [0], 0, false, false);
|
|
|
|
this.animation.add(Losing, [1], 0, false, false);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 06:42:52 +00:00
|
|
|
function correctCharacterId(charId:Null<String>):String
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2023-10-17 06:42:52 +00:00
|
|
|
if (charId == null)
|
|
|
|
{
|
|
|
|
return Constants.DEFAULT_HEALTH_ICON;
|
|
|
|
}
|
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
if (!Assets.exists(Paths.image('icons/icon-$charId')))
|
|
|
|
{
|
|
|
|
FlxG.log.warn('No icon for character: $charId : using default placeholder face instead!');
|
2023-10-17 06:42:52 +00:00
|
|
|
return Constants.DEFAULT_HEALTH_ICON;
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return charId;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isNewSpritesheet(charId:String):Bool
|
|
|
|
{
|
|
|
|
return Assets.exists(Paths.file('images/icons/icon-$characterId.xml'));
|
|
|
|
}
|
|
|
|
|
2023-10-17 06:42:52 +00:00
|
|
|
function loadCharacter(charId:Null<String>):Void
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2023-10-17 06:42:52 +00:00
|
|
|
if (charId == null || correctCharacterId(charId) != charId)
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2023-10-17 06:42:52 +00:00
|
|
|
// This will recursively trigger loadCharacter to be called again.
|
2023-01-23 03:25:45 +00:00
|
|
|
characterId = correctCharacterId(charId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isLegacyStyle = !isNewSpritesheet(charId);
|
|
|
|
|
|
|
|
if (!isLegacyStyle)
|
|
|
|
{
|
2024-02-22 23:55:24 +00:00
|
|
|
loadSparrow('icons/icon-$charId');
|
2023-01-23 03:25:45 +00:00
|
|
|
|
2023-05-26 21:10:35 +00:00
|
|
|
loadAnimationNew();
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
loadGraphic(Paths.image('icons/icon-$charId'), true, isPixel ? PIXEL_ICON_SIZE : HEALTH_ICON_SIZE, isPixel ? PIXEL_ICON_SIZE : HEALTH_ICON_SIZE);
|
|
|
|
|
2023-05-26 21:10:35 +00:00
|
|
|
loadAnimationOld();
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
2023-05-26 21:10:35 +00:00
|
|
|
|
|
|
|
this.antialiasing = !isPixel;
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Name of the current animation being played by this health icon.
|
|
|
|
*/
|
|
|
|
public function getCurrentAnimation():String
|
|
|
|
{
|
|
|
|
if (this.animation == null || this.animation.curAnim == null) return "";
|
|
|
|
return this.animation.curAnim.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-26 21:10:35 +00:00
|
|
|
* @param id The name of the animation to check for.
|
2023-01-23 03:25:45 +00:00
|
|
|
* @return Whether this sprite posesses the given animation.
|
|
|
|
* Only true if the animation was successfully loaded from the XML.
|
|
|
|
*/
|
|
|
|
public function hasAnimation(id:String):Bool
|
|
|
|
{
|
|
|
|
if (this.animation == null) return false;
|
|
|
|
|
|
|
|
return this.animation.getByName(id) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Whether the current animation is in the finished state.
|
|
|
|
*/
|
|
|
|
public function isAnimationFinished():Bool
|
|
|
|
{
|
|
|
|
return this.animation.finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plays the animation with the given name.
|
|
|
|
* @param name The name of the animation to play.
|
|
|
|
* @param fallback The fallback animation to play if the given animation is not found.
|
|
|
|
* @param restart Whether to forcibly restart the animation if it is already playing.
|
|
|
|
*/
|
|
|
|
public function playAnimation(name:String, fallback:String = null, restart = false):Void
|
|
|
|
{
|
|
|
|
// Attempt to play the animation
|
|
|
|
if (hasAnimation(name))
|
|
|
|
{
|
|
|
|
this.animation.play(name, restart, false, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Play the fallback animation if the requested animation was not found
|
|
|
|
if (fallback != null && hasAnimation(fallback))
|
|
|
|
{
|
|
|
|
this.animation.play(fallback, restart, false, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have an animation, we're done.
|
|
|
|
}
|
2022-03-26 02:30:37 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 21:10:35 +00:00
|
|
|
/**
|
|
|
|
* The current state of the health
|
|
|
|
*/
|
2022-03-26 02:30:37 +00:00
|
|
|
enum abstract HealthIconState(String) to String from String
|
|
|
|
{
|
2023-01-23 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Indicates the health icon is in the default animation.
|
|
|
|
* Plays as long as health is between 20% and 80%.
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
public var Idle = 'idle';
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates the health icon is playing the Winning animation.
|
|
|
|
* Plays as long as health is above 80%.
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
public var Winning = 'winning';
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates the health icon is playing the Losing animation.
|
|
|
|
* Plays as long as health is below 20%.
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
public var Losing = 'losing';
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates that the health icon is transitioning between `idle` and `winning`.
|
|
|
|
* The next animation will play once the current animation finishes.
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
public var ToWinning = 'toWinning';
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates that the health icon is transitioning between `idle` and `losing`.
|
|
|
|
* The next animation will play once the current animation finishes.
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
public var ToLosing = 'toLosing';
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates that the health icon is transitioning between `winning` and `idle`.
|
|
|
|
* The next animation will play once the current animation finishes.
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
public var FromWinning = 'fromWinning';
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates that the health icon is transitioning between `losing` and `idle`.
|
|
|
|
* The next animation will play once the current animation finishes.
|
|
|
|
*/
|
2023-05-26 21:10:35 +00:00
|
|
|
public var FromLosing = 'fromLosing';
|
2022-03-26 02:30:37 +00:00
|
|
|
}
|