From b2176c244c5a1f39917d923c5608f6795e0fa9ef Mon Sep 17 00:00:00 2001 From: Saturn Harrison <115556838+saturn-volv@users.noreply.github.com> Date: Sun, 28 Apr 2024 21:40:12 +1000 Subject: [PATCH 1/3] Better soft coding on icons --- source/objects/HealthIcon.hx | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/source/objects/HealthIcon.hx b/source/objects/HealthIcon.hx index 1039721c818..15b05745ae4 100644 --- a/source/objects/HealthIcon.hx +++ b/source/objects/HealthIcon.hx @@ -1,3 +1,12 @@ +/* + The reason for all my changes here is so that icons can be more configurable out of the box, without having to change a thing from base source. + Yes, users can already make winning icons, but this supports a built in override, which I think is neat. + + This also just seems like a good practice for icons anyway. As it provides a way to create really wacky icons by abusing the height value. + + -- saturn-volv +*/ + package objects; class HealthIcon extends FlxSprite @@ -32,12 +41,15 @@ class HealthIcon extends FlxSprite if(!Paths.fileExists('images/' + name + '.png', IMAGE)) name = 'icons/icon-face'; //Prevents crash from missing icon var graphic = Paths.image(name, allowGPU); - loadGraphic(graphic, true, Math.floor(graphic.width / 2), Math.floor(graphic.height)); - iconOffsets[0] = (width - 150) / 2; - iconOffsets[1] = (height - 150) / 2; + var iconSize:Int = Math.floor(graphic.width / graphic.height); // Creates a stepper for how many icons based from the height. + loadGraphic(graphic, true, Math.floor(graphic.width / iconSize), Math.floor(graphic.height)); + iconOffsets[0] = (width - 150) / iconSize; + iconOffsets[1] = (height - 150) / iconSize; updateHitbox(); - animation.add(char, [0, 1], 0, false, isPlayer); + animation.add(char, + [for(i in 0...iconSize-1) i], // Creates an array from 0 of iconSize in length; + 0, false, isPlayer); animation.play(char); this.char = char; From 7de18c9d736f62704c16374c79cd5efe0458c0ec Mon Sep 17 00:00:00 2001 From: volv <115556838+saturn-volv@users.noreply.github.com> Date: Mon, 29 Apr 2024 16:04:55 +1000 Subject: [PATCH 2/3] Fixed the animation length being too short. --- source/objects/HealthIcon.hx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/objects/HealthIcon.hx b/source/objects/HealthIcon.hx index 15b05745ae4..fedbd384a2c 100644 --- a/source/objects/HealthIcon.hx +++ b/source/objects/HealthIcon.hx @@ -33,6 +33,7 @@ class HealthIcon extends FlxSprite setPosition(sprTracker.x + sprTracker.width + 12, sprTracker.y - 30); } + private var iconSize:Int = 0; private var iconOffsets:Array = [0, 0]; public function changeIcon(char:String, ?allowGPU:Bool = true) { if(this.char != char) { @@ -41,14 +42,14 @@ class HealthIcon extends FlxSprite if(!Paths.fileExists('images/' + name + '.png', IMAGE)) name = 'icons/icon-face'; //Prevents crash from missing icon var graphic = Paths.image(name, allowGPU); - var iconSize:Int = Math.floor(graphic.width / graphic.height); // Creates a stepper for how many icons based from the height. + iconSize = Math.floor(graphic.width / graphic.height); loadGraphic(graphic, true, Math.floor(graphic.width / iconSize), Math.floor(graphic.height)); iconOffsets[0] = (width - 150) / iconSize; iconOffsets[1] = (height - 150) / iconSize; updateHitbox(); animation.add(char, - [for(i in 0...iconSize-1) i], // Creates an array from 0 of iconSize in length; + [for(i in 0...iconSize) i], // Creates an array from 0 of iconSize in length; 0, false, isPlayer); animation.play(char); this.char = char; From d94aac9d0bf6f5463212a94d7dbf5786bbefcf19 Mon Sep 17 00:00:00 2001 From: volv <115556838+saturn-volv@users.noreply.github.com> Date: Mon, 29 Apr 2024 16:06:23 +1000 Subject: [PATCH 3/3] Added util for changing frame indexes safely --- source/objects/HealthIcon.hx | 11 +++++++++++ source/states/PlayState.hx | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/source/objects/HealthIcon.hx b/source/objects/HealthIcon.hx index fedbd384a2c..a0f33ce5360 100644 --- a/source/objects/HealthIcon.hx +++ b/source/objects/HealthIcon.hx @@ -34,6 +34,17 @@ class HealthIcon extends FlxSprite } private var iconSize:Int = 0; + /** + * An index which decides which frame of the icon to use. + */ + @:isVar public var iconIndex(get, set):Int; + public function get_iconIndex() { + return iconIndex = animation?.curAnim.curFrame ?? 0; + } + public function set_iconIndex(i:Int):Int { + return iconIndex = animation.curAnim.curFrame = (i % iconSize); + } + private var iconOffsets:Array = [0, 0]; public function changeIcon(char:String, ?allowGPU:Bool = true) { if(this.char != char) { diff --git a/source/states/PlayState.hx b/source/states/PlayState.hx index 9c5903c2690..ca8a203143e 100644 --- a/source/states/PlayState.hx +++ b/source/states/PlayState.hx @@ -1843,8 +1843,8 @@ class PlayState extends MusicBeatState var newPercent:Null = FlxMath.remapToRange(FlxMath.bound(healthBar.valueFunction(), healthBar.bounds.min, healthBar.bounds.max), healthBar.bounds.min, healthBar.bounds.max, 0, 100); healthBar.percent = (newPercent != null ? newPercent : 0); - iconP1.animation.curAnim.curFrame = (healthBar.percent < 20) ? 1 : 0; //If health is under 20%, change player icon to frame 1 (losing icon), otherwise, frame 0 (normal) - iconP2.animation.curAnim.curFrame = (healthBar.percent > 80) ? 1 : 0; //If health is over 80%, change opponent icon to frame 1 (losing icon), otherwise, frame 0 (normal) + iconP1.iconIndex = (healthBar.percent < 20) ? 1 : 0; //If health is under 20%, change player icon to frame 1 (losing icon), otherwise, frame 0 (normal) + iconP2.iconIndex = (healthBar.percent > 80) ? 1 : 0; //If health is over 80%, change opponent icon to frame 1 (losing icon), otherwise, frame 0 (normal) return health; }