Animated item support

This commit is contained in:
Anuken
2021-12-06 00:52:30 -05:00
parent 7d6428fcf2
commit c3dbe9b09d
3 changed files with 73 additions and 2 deletions

View File

@@ -1,8 +1,14 @@
package mindustry.type;
import arc.*;
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.struct.*;
import arc.util.*;
import mindustry.ctype.*;
import mindustry.game.EventType.*;
import mindustry.graphics.*;
import mindustry.graphics.MultiPacker.*;
import mindustry.world.blocks.environment.*;
import mindustry.world.meta.*;
@@ -29,6 +35,13 @@ public class Item extends UnlockableContent{
/** if true, this item is of lowest priority to drills. */
public boolean lowPriority;
/** If >0, this item is animated. */
public int frames = 0;
/** Number of generated transition frames between each frame */
public int transitionFrames = 0;
/** Ticks in-between animation frames. */
public float frameTime = 5f;
public Item(String name, Color color){
super(name);
this.color = color;
@@ -38,6 +51,40 @@ public class Item extends UnlockableContent{
this(name, new Color(Color.black));
}
@Override
public void loadIcon(){
super.loadIcon();
//animation code ""borrowed"" from Project Unity - original implementation by GlennFolker and sk7725
if(frames > 0){
TextureRegion[] regions = new TextureRegion[frames * (transitionFrames + 1)];
if(transitionFrames <= 0){
for(int i = 1; i <= frames; i++){
regions[i - 1] = Core.atlas.find(name + i);
}
}else{
for(int i = 0; i < frames; i++){
regions[i * (transitionFrames + 1)] = Core.atlas.find(name + (i + 1));
for(int j = 1; j <= transitionFrames; j++){
int index = i * (transitionFrames + 1) + j;
regions[index] = Core.atlas.find(name + "-t" + index);
}
}
}
fullIcon = new TextureRegion(fullIcon);
uiIcon = new TextureRegion(uiIcon);
Events.run(Trigger.update, () -> {
int frame = (int)(Time.globalTime / frameTime) % regions.length;
fullIcon.set(regions[frame]);
uiIcon.set(regions[frame]);
});
}
}
@Override
public void setStats(){
stats.addPercent(Stat.explosiveness, explosiveness);
@@ -56,6 +103,30 @@ public class Item extends UnlockableContent{
return ContentType.item;
}
@Override
public void createIcons(MultiPacker packer){
super.createIcons(packer);
//create transitions
if(frames > 0 && transitionFrames > 0){
var pixmaps = new PixmapRegion[frames];
for(int i = 0; i < frames; i++){
pixmaps[i] = Core.atlas.getPixmap(name + (i + 1));
}
for(int i = 0; i < frames; i++){
for(int j = 1; j <= transitionFrames; j++){
float f = (float)j / (transitionFrames + 1);
int index = i * (transitionFrames + 1) + j;
Pixmap res = Pixmaps.blend(pixmaps[i], pixmaps[(i + 1) % frames], f);
packer.add(PageType.main, name + "-t" + index, res);
}
}
}
}
/** Allocates a new array containing all items that generate ores. */
public static Seq<Item> getAllOres(){
return content.blocks().select(b -> b instanceof OreBlock).map(b -> b.itemDrop);