11package ;
22
33import haxe .Json ;
4+ import openfl .utils .Assets ;
45
56using StringTools ;
67
78// metadatas for icons
8- // so I don't have to deal with stupid annoying icons cutting off
9+ // allows for animated icons and such
910typedef IconMeta = {
1011 ? noAntialiasing : Bool ,
1112 ? fps : Int ,
12- ? frameOrder : Array <String > // ["normal", "losing", "winning"]
13+ // ?frameOrder:Array<String> // ["normal", "losing", "winning"]
14+ // ?isAnimated:Bool,
15+ ? hasWinIcon : Bool ,
1316}
1417class HealthIcon extends FlxSprite
1518{
@@ -62,39 +65,106 @@ class HealthIcon extends FlxSprite
6265 // throw "Don't delete the placeholder icon";
6366 trace (" Warning: could not find the placeholder icon, expect crashes!" );
6467 }
65- if (iconMeta ?. frameOrder != null ){
66- final framesCount : Int = Math .floor (file .width / file .height );
67- final frameWidth : Int = Math .floor (file .width / framesCount );
68- loadGraphic (file , true , frameWidth , Math .floor (file .height ));
68+ final iSize : Float = Math .round (file .width / file .height );
69+ /*
70+ loadGraphic(file, true, Math.floor(file.width / iSize), Math.floor(file.height));
71+ initialWidth = width;
72+ initialHeight = height;
73+ iconOffsets[0] = (width - 150) / iSize;
74+ iconOffsets[1] = (height - 150) / iSize;
75+
76+ updateHitbox();
77+ */
78+ if (file .width == 300 ) {
79+ loadGraphic (file , true , Math .floor (file .width / 2 ), Math .floor (file .height ));
80+ iconOffsets [0 ] = (width - 150 ) / iSize ;
81+ iconOffsets [1 ] = (height - 150 ) / iSize ;
6982 initialWidth = width ;
7083 initialHeight = height ;
71-
72- iconOffsets [0 ] = (width - 150 ) / framesCount ;
73- iconOffsets [1 ] = (height - 150 ) / framesCount ;
74- }
75- else
76- {
77- final iSize : Float = Math .round (file .width / file .height );
78- loadGraphic (file , true , Math .floor (file .width / iSize ), Math .floor (file .height ));
84+ updateHitbox ();
85+ animation .add (char , [0 , 1 ], 0 , false , isPlayer );
86+ } else if (file .width == 450 ) {
87+ loadGraphic (file , true , Math .floor (file .width / 3 ), Math .floor (file .height ));
88+ iconOffsets [0 ] = (width - 150 ) / iSize ;
89+ iconOffsets [1 ] = (height - 150 ) / iSize ;
7990 initialWidth = width ;
8091 initialHeight = height ;
92+ updateHitbox ();
93+ animation .add (char , [0 , 1 , 2 ], 0 , false , isPlayer );
94+ } else if (Paths .fileExists (' images/ $name .xml' , TEXT )) {
95+ frames = Paths .getSparrowAtlas (name );
96+ final iconPrefixes = checkAvailablePrefixes (Paths .getPath (' images/ $name .xml' , TEXT ));
97+ final hasWinning = iconPrefixes .get (' winning' );
98+ final hasLosing = iconPrefixes .get (' losing' );
99+ final fps : Float = iconMeta .fps ?? = 24 ;
100+ final loop = fps > 0 ;
101+
102+ // Always add "normal"
103+ animation .addByPrefix (' normal' , ' normal' , fps , loop , isPlayer );
104+
105+ // Add "losing", fallback to "normal"
106+ animation .addByPrefix (' losing' , hasLosing ? ' losing' : ' normal' , fps , loop , isPlayer );
107+
108+ // Add "winning", fallback to "normal"
109+ animation .addByPrefix (' winning' , hasWinning ? ' winning' : ' normal' , fps , loop , isPlayer );
110+ playAnim (' normal' );
111+ } else { // This is just an attempt for other icon support, will detect is less than 300 or more than 300. If 300 or less, only 2 icons, if more, 3 icons.
112+ var num : Int = Std .int (Math .round (file .width / file .height ));
113+ if (file .width % file .height != 0 || num >= 4 ) {
114+ // weird icon, maybe has padding?
115+ num = 3 ; // fallback
116+ }
117+ if (file .width < 300 ) {
118+ num = 2 ;
119+ } else if (file .width >= 300 ) {
120+ num = 3 ;
121+ }
122+
123+ loadGraphic (file , true , Math .floor (file .width / num ), Math .floor (file .height ));
81124 iconOffsets [0 ] = (width - 150 ) / iSize ;
82125 iconOffsets [1 ] = (height - 150 ) / iSize ;
83- }
126+ initialWidth = width ;
127+ initialHeight = height ;
128+ updateHitbox ();
84129
85- updateHitbox ();
130+ function getWinIcon (): Array <Int >
131+ {
132+ return (iconMeta ?. hasWinIcon || num == 3 ) ? [0 , 1 , 2 ] : [0 , 1 ];
133+ }
86134
87- animation .add (char , [for (i in 0 ... frames .frames .length ) i ], 0 , false , isPlayer );
135+ final winShit : Array <Int > = (num == 2 ) ? [0 , 1 ] : getWinIcon ();
136+ animation .add (char , winShit , 0 , false , isPlayer );
137+ }
138+
139+ // animation.add(char, [for(i in 0...frames.frames.length) i], 0, false, isPlayer);
88140 animation .play (char );
89141 this .char = char ;
90142
91- antialiasing = ClientPrefs .globalAntialiasing ;
143+ antialiasing = ( ClientPrefs .globalAntialiasing || iconMeta ?. noAntialiasing ) ;
92144 if (char .endsWith (' -pixel' )) {
93145 antialiasing = false ;
94146 }
95147 }
96148 }
97149
150+ function checkAvailablePrefixes (xmlPath : String ): Map <String , Bool > {
151+ final result = new Map <String , Bool >();
152+ result .set (" normal" , false );
153+ result .set (" losing" , false );
154+ result .set (" winning" , false );
155+
156+ final xml : Xml = Xml .parse (Assets .getText (xmlPath ));
157+ final root : Xml = xml .firstElement ();
158+ for (node in root .elements ()) {
159+ final name = node .get (" name" );
160+ for (prefix in result .keys ()) {
161+ if (name .startsWith (prefix )) result .set (prefix , true );
162+ }
163+ }
164+
165+ return result ;
166+ }
167+
98168 public function bounce () {
99169 if (canBounce ) {
100170 var mult : Float = 1.2 ;
@@ -103,6 +173,11 @@ class HealthIcon extends FlxSprite
103173 }
104174 }
105175
176+ public function playAnim (anim : String ) {
177+ if (animation .exists (anim ))
178+ animation .play (anim );
179+ }
180+
106181 public static function getFile (name : String ): IconMeta {
107182 var characterPath : String = ' images/icons/ $name .json' ;
108183 var path : String = Paths .getPath (characterPath );
@@ -119,6 +194,7 @@ class HealthIcon extends FlxSprite
119194 var json : IconMeta = cast Json .parse (rawJson );
120195 if (json .noAntialiasing == null ) json .noAntialiasing = false ;
121196 if (json .fps == null ) json .fps = 24 ;
197+ if (json .hasWinIcon == null ) json .hasWinIcon = true ;
122198 // if (json.frameOrder == null) json.frameOrder = ['normal', 'losing', 'winning'];
123199 return json ;
124200 }
0 commit comments