More mod content support

This commit is contained in:
Anuken
2019-10-04 19:14:45 -04:00
parent df3f23731b
commit edb0ece03b
18 changed files with 190 additions and 41 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 721 KiB

After

Width:  |  Height:  |  Size: 892 KiB

View File

@@ -20,6 +20,10 @@ public class ArtilleryBulletType extends BasicBulletType{
hitSound = Sounds.explosion; hitSound = Sounds.explosion;
} }
public ArtilleryBulletType(){
this(1f, 1f, "shell");
}
@Override @Override
public void update(io.anuke.mindustry.entities.type.Bullet b){ public void update(io.anuke.mindustry.entities.type.Bullet b){
super.update(b); super.update(b);

View File

@@ -22,6 +22,11 @@ public class BasicBulletType extends BulletType{
this.bulletSprite = bulletSprite; this.bulletSprite = bulletSprite;
} }
/** For mods. */
public BasicBulletType(){
this(1f, 1f, "bullet");
}
@Override @Override
public void load(){ public void load(){
backRegion = Core.atlas.find(bulletSprite + "-back"); backRegion = Core.atlas.find(bulletSprite + "-back");

View File

@@ -17,4 +17,8 @@ public class BombBulletType extends BasicBulletType{
collidesAir = false; collidesAir = false;
hitSound = Sounds.explosion; hitSound = Sounds.explosion;
} }
public BombBulletType(){
this(1f, 1f, "shell");
}
} }

View File

@@ -19,6 +19,10 @@ public abstract class FlakBulletType extends BasicBulletType{
bulletHeight = 10f; bulletHeight = 10f;
} }
public FlakBulletType(){
this(1f, 1f);
}
@Override @Override
public void update(Bullet b){ public void update(Bullet b){
super.update(b); super.update(b);

View File

@@ -22,6 +22,10 @@ public class HealBulletType extends BulletType{
collidesTeam = true; collidesTeam = true;
} }
public HealBulletType(){
this(1f, 1f);
}
@Override @Override
public boolean collides(Bullet b, Tile tile){ public boolean collides(Bullet b, Tile tile){
return tile.getTeam() != b.getTeam() || tile.entity.healthf() < 1f; return tile.getTeam() != b.getTeam() || tile.entity.healthf() < 1f;

View File

@@ -3,6 +3,7 @@ package io.anuke.mindustry.entities.bullet;
import io.anuke.arc.graphics.*; import io.anuke.arc.graphics.*;
import io.anuke.arc.graphics.g2d.*; import io.anuke.arc.graphics.g2d.*;
import io.anuke.arc.math.geom.*; import io.anuke.arc.math.geom.*;
import io.anuke.arc.util.ArcAnnotate.*;
import io.anuke.mindustry.content.*; import io.anuke.mindustry.content.*;
import io.anuke.mindustry.entities.*; import io.anuke.mindustry.entities.*;
import io.anuke.mindustry.entities.effect.*; import io.anuke.mindustry.entities.effect.*;
@@ -13,14 +14,17 @@ import io.anuke.mindustry.world.*;
import static io.anuke.mindustry.Vars.*; import static io.anuke.mindustry.Vars.*;
public class LiquidBulletType extends BulletType{ public class LiquidBulletType extends BulletType{
Liquid liquid; @NonNull Liquid liquid;
public LiquidBulletType(Liquid liquid){ public LiquidBulletType(@Nullable Liquid liquid){
super(3.5f, 0); super(3.5f, 0);
this.liquid = liquid;
if(liquid != null){
this.liquid = liquid;
this.status = liquid.effect;
}
lifetime = 74f; lifetime = 74f;
status = liquid.effect;
statusDuration = 90f; statusDuration = 90f;
despawnEffect = Fx.none; despawnEffect = Fx.none;
hitEffect = Fx.hitLiquid; hitEffect = Fx.hitLiquid;
@@ -30,13 +34,17 @@ public class LiquidBulletType extends BulletType{
knockback = 0.55f; knockback = 0.55f;
} }
public LiquidBulletType(){
this(null);
}
@Override @Override
public float range(){ public float range(){
return speed * lifetime / 2f; return speed * lifetime / 2f;
} }
@Override @Override
public void update(io.anuke.mindustry.entities.type.Bullet b){ public void update(Bullet b){
super.update(b); super.update(b);
if(liquid.canExtinguish()){ if(liquid.canExtinguish()){
@@ -50,7 +58,7 @@ public class LiquidBulletType extends BulletType{
} }
@Override @Override
public void draw(io.anuke.mindustry.entities.type.Bullet b){ public void draw(Bullet b){
Draw.color(liquid.color, Color.white, b.fout() / 100f); Draw.color(liquid.color, Color.white, b.fout() / 100f);
Fill.circle(b.x, b.y, 0.5f + b.fout() * 2.5f); Fill.circle(b.x, b.y, 0.5f + b.fout() * 2.5f);

View File

@@ -23,6 +23,10 @@ public class MissileBulletType extends BasicBulletType{
hitSound = Sounds.explosion; hitSound = Sounds.explosion;
} }
public MissileBulletType(){
this(1f, 1f, "missile");
}
@Override @Override
public void update(Bullet b){ public void update(Bullet b){
super.update(b); super.update(b);

View File

@@ -22,6 +22,7 @@ import io.anuke.mindustry.gen.*;
import io.anuke.mindustry.mod.Mods.*; import io.anuke.mindustry.mod.Mods.*;
import io.anuke.mindustry.type.*; import io.anuke.mindustry.type.*;
import io.anuke.mindustry.world.*; import io.anuke.mindustry.world.*;
import io.anuke.mindustry.world.consumers.*;
import java.lang.reflect.*; import java.lang.reflect.*;
@@ -35,6 +36,13 @@ public class ContentParser{
put(StatusEffect.class, (type, data) -> field(StatusEffects.class, data)); put(StatusEffect.class, (type, data) -> field(StatusEffects.class, data));
put(Loadout.class, (type, data) -> field(Loadouts.class, data)); put(Loadout.class, (type, data) -> field(Loadouts.class, data));
put(Color.class, (type, data) -> Color.valueOf(data.asString())); put(Color.class, (type, data) -> Color.valueOf(data.asString()));
put(BulletType.class, (type, data) -> {
Class<? extends BulletType> bc = data.has("type") ? resolve(data.getString("type"), "io.anuke.mindustry.entities.bullets") : BasicBulletType.class;
data.remove("type");
BulletType result = bc.getDeclaredConstructor().newInstance();
readFields(result, data);
return result;
});
put(Music.class, (type, data) -> { put(Music.class, (type, data) -> {
if(fieldOpt(Musics.class, data) != null) return fieldOpt(Musics.class, data); if(fieldOpt(Musics.class, data) != null) return fieldOpt(Musics.class, data);
@@ -57,12 +65,18 @@ public class ContentParser{
* This is done to accomodate binding of content names first.*/ * This is done to accomodate binding of content names first.*/
private Array<Runnable> reads = new Array<>(); private Array<Runnable> reads = new Array<>();
private LoadedMod currentMod; private LoadedMod currentMod;
private Content currentContent;
private Json parser = new Json(){ private Json parser = new Json(){
public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData){ @Override
public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData, Class keyType){
if(type != null){ if(type != null){
if(classParsers.containsKey(type)){ if(classParsers.containsKey(type)){
return (T)classParsers.get(type).parse(type, jsonData); try{
return (T)classParsers.get(type).parse(type, jsonData);
}catch(Exception e){
throw new RuntimeException(e);
}
} }
if(Content.class.isAssignableFrom(type)){ if(Content.class.isAssignableFrom(type)){
@@ -70,16 +84,21 @@ public class ContentParser{
String prefix = currentMod != null ? currentMod.name + "-" : ""; String prefix = currentMod != null ? currentMod.name + "-" : "";
T one = (T)Vars.content.getByName(ctype, prefix + jsonData.asString()); T one = (T)Vars.content.getByName(ctype, prefix + jsonData.asString());
if(one != null) return one; if(one != null) return one;
return (T)Vars.content.getByName(ctype, jsonData.asString()); T two = (T)Vars.content.getByName(ctype, jsonData.asString());
if(two != null) return two;
throw new IllegalArgumentException("No " + ctype + " found with name: '" + jsonData.asString() + "'");
} }
} }
return super.readValue(type, elementType, jsonData); return super.readValue(type, elementType, jsonData, keyType);
} }
}; };
private ObjectMap<ContentType, TypeParser<?>> parsers = ObjectMap.of( private ObjectMap<ContentType, TypeParser<?>> parsers = ObjectMap.of(
ContentType.block, (TypeParser<Block>)(mod, name, value) -> { ContentType.block, (TypeParser<Block>)(mod, name, value) -> {
readBundle(ContentType.block, name, value);
//TODO generate dynamically instead of doing.. this //TODO generate dynamically instead of doing.. this
Class<? extends Block> type = resolve(value.getString("type"), Class<? extends Block> type = resolve(value.getString("type"),
"io.anuke.mindustry.world", "io.anuke.mindustry.world",
@@ -100,19 +119,17 @@ public class ContentParser{
if(value.has("consumes")){ if(value.has("consumes")){
for(JsonValue child : value.get("consumes")){ for(JsonValue child : value.get("consumes")){
if(child.name.equals("item")){ if(child.name.equals("item")){
if(child.isString()){ block.consumes.item(Vars.content.getByName(ContentType.item, child.asString()));
block.consumes.item(Vars.content.getByName(ContentType.item, child.asString()));
}else{
ItemStack stack = parser.readValue(ItemStack.class, child);
block.consumes.item(stack.item, stack.amount);
}
}else if(child.name.equals("items")){ }else if(child.name.equals("items")){
block.consumes.items(parser.readValue(ItemStack[].class, child)); block.consumes.add((Consume)parser.readValue(ConsumeItems.class, child));
}else if(child.name.equals("liquid")){ }else if(child.name.equals("liquid")){
LiquidStack stack = parser.readValue(LiquidStack.class, child); block.consumes.add((Consume)parser.readValue(ConsumeLiquid.class, child));
block.consumes.liquid(stack.liquid, stack.amount);
}else if(child.name.equals("power")){ }else if(child.name.equals("power")){
block.consumes.power(child.asFloat()); if(child.isDouble()){
block.consumes.power(child.asFloat());
}else{
block.consumes.add((Consume)parser.readValue(ConsumePower.class, child));
}
}else if(child.name.equals("powerBuffered")){ }else if(child.name.equals("powerBuffered")){
block.consumes.powerBuffered(child.asFloat()); block.consumes.powerBuffered(child.asFloat());
}else{ }else{
@@ -138,6 +155,8 @@ public class ContentParser{
return block; return block;
}, },
ContentType.unit, (TypeParser<UnitType>)(mod, name, value) -> { ContentType.unit, (TypeParser<UnitType>)(mod, name, value) -> {
readBundle(ContentType.unit, name, value);
Class<BaseUnit> type = resolve(value.getString("type"), "io.anuke.mindustry.entities.type.base"); Class<BaseUnit> type = resolve(value.getString("type"), "io.anuke.mindustry.entities.type.base");
UnitType unit = new UnitType(mod + "-" + name, supply(type)); UnitType unit = new UnitType(mod + "-" + name, supply(type));
read(() -> readFields(unit, value, true)); read(() -> readFields(unit, value, true));
@@ -156,6 +175,8 @@ public class ContentParser{
if(Vars.content.getByName(type, name) != null){ if(Vars.content.getByName(type, name) != null){
item = (T)Vars.content.getByName(type, name); item = (T)Vars.content.getByName(type, name);
}else{ }else{
readBundle(type, name, value);
item = constructor.get(mod + "-" + name); item = constructor.get(mod + "-" + name);
} }
read(() -> readFields(item, value)); read(() -> readFields(item, value));
@@ -163,6 +184,22 @@ public class ContentParser{
}; };
} }
private void readBundle(ContentType type, String name, JsonValue value){
String entryName = type + "." + currentMod.name + "-" + name + ".";
I18NBundle bundle = Core.bundle;
while(bundle.getParent() != null) bundle = bundle.getParent();
if(value.has("name")){
bundle.getProperties().put(entryName + "name", value.getString("name"));
value.remove("name");
}
if(value.has("description")){
bundle.getProperties().put(entryName + "description", value.getString("description"));
value.remove("description");
}
}
/** Call to read a content's extra info later.*/ /** Call to read a content's extra info later.*/
private void read(Runnable run){ private void read(Runnable run){
LoadedMod mod = currentMod; LoadedMod mod = currentMod;
@@ -308,7 +345,7 @@ public class ContentParser{
} }
Field field = metadata.field; Field field = metadata.field;
try{ try{
field.set(object, parser.readValue(field.getType(), metadata.elementType, child)); field.set(object, parser.readValue(field.getType(), metadata.elementType, child, metadata.keyType));
}catch(ReflectionException ex){ }catch(ReflectionException ex){
throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex); throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
}catch(SerializationException ex){ }catch(SerializationException ex){
@@ -324,7 +361,7 @@ public class ContentParser{
} }
/** Tries to resolve a class from a list of potential class names. */ /** Tries to resolve a class from a list of potential class names. */
private <T> Class<T> resolve(String base, String... potentials) throws Exception{ private <T> Class<T> resolve(String base, String... potentials){
for(String type : potentials){ for(String type : potentials){
try{ try{
return (Class<T>)Class.forName(type + '.' + base); return (Class<T>)Class.forName(type + '.' + base);
@@ -335,7 +372,7 @@ public class ContentParser{
} }
private interface FieldParser{ private interface FieldParser{
Object parse(Class<?> type, JsonValue value); Object parse(Class<?> type, JsonValue value) throws Exception;
} }
private interface TypeParser<T extends Content>{ private interface TypeParser<T extends Content>{

View File

@@ -121,7 +121,7 @@ public class Mods implements Loadable{
//get textures packed //get textures packed
if(totalSprites > 0){ if(totalSprites > 0){
TextureFilter filter = Core.settings.getBool("linear") ? TextureFilter.Linear : TextureFilter.Nearest; TextureFilter filter = TextureFilter.Nearest;
packer.updateTextureAtlas(Core.atlas, filter, filter, false); packer.updateTextureAtlas(Core.atlas, filter, filter, false);
//generate new icons //generate new icons
@@ -175,7 +175,7 @@ public class Mods implements Loadable{
} }
} }
//load mods now //load workshop mods now
for(FileHandle file : platform.getExternalMods()){ for(FileHandle file : platform.getExternalMods()){
try{ try{
LoadedMod mod = loadMod(file, true); LoadedMod mod = loadMod(file, true);
@@ -185,7 +185,7 @@ public class Mods implements Loadable{
disabled.add(mod); disabled.add(mod);
} }
}catch(Exception e){ }catch(Exception e){
Log.err("Failed to load mod file {0}. Skipping.", file); Log.err("Failed to load mod workshop file {0}. Skipping.", file);
Log.err(e); Log.err(e);
} }
} }

View File

@@ -96,6 +96,8 @@ public class Block extends BlockStorage{
public boolean targetable = true; public boolean targetable = true;
/** Whether the overdrive core has any effect on this block. */ /** Whether the overdrive core has any effect on this block. */
public boolean canOverdrive = true; public boolean canOverdrive = true;
/** Outlined icon color.*/
public Color outlineColor = Color.valueOf("404049");
/** Whether the icon region has an outline added. */ /** Whether the icon region has an outline added. */
public boolean outlineIcon = false; public boolean outlineIcon = false;
/** Whether this block has a shadow under it. */ /** Whether this block has a shadow under it. */
@@ -385,6 +387,10 @@ public class Block extends BlockStorage{
buildCost += stack.amount * stack.item.cost; buildCost += stack.amount * stack.item.cost;
} }
if(consumes.has(ConsumeType.power)) hasPower = true;
if(consumes.has(ConsumeType.item)) hasItems = true;
if(consumes.has(ConsumeType.liquid)) hasLiquids = true;
setStats(); setStats();
setBars(); setBars();
@@ -668,8 +674,8 @@ public class Block extends BlockStorage{
} }
@Override @Override
public void createIcons(PixmapPacker out, PixmapPacker editor){ public void createIcons(PixmapPacker packer, PixmapPacker editor){
super.createIcons(out, editor); super.createIcons(packer, editor);
editor.pack(name + "-icon-editor", Core.atlas.getPixmap((AtlasRegion)icon(Cicon.full)).crop()); editor.pack(name + "-icon-editor", Core.atlas.getPixmap((AtlasRegion)icon(Cicon.full)).crop());
@@ -677,6 +683,56 @@ public class Block extends BlockStorage{
PixmapRegion image = Core.atlas.getPixmap((AtlasRegion)icon(Cicon.full)); PixmapRegion image = Core.atlas.getPixmap((AtlasRegion)icon(Cicon.full));
color.set(image.getPixel(image.width/2, image.height/2)); color.set(image.getPixel(image.width/2, image.height/2));
} }
getGeneratedIcons();
Pixmap last = null;
if(outlineIcon){
final int radius = 4;
PixmapRegion region = Core.atlas.getPixmap(getGeneratedIcons()[getGeneratedIcons().length-1]);
Pixmap out = new Pixmap(region.width, region.height);
Color color = new Color();
for(int x = 0; x < region.width; x++){
for(int y = 0; y < region.height; y++){
region.getPixel(x, y, color);
out.draw(x, y, color);
if(color.a < 1f){
boolean found = false;
outer:
for(int rx = -radius; rx <= radius; rx++){
for(int ry = -radius; ry <= radius; ry++){
if(Structs.inBounds(rx + x, ry + y, region.width, region.height) && Mathf.dst2(rx, ry) <= radius*radius && color.set(region.getPixel(rx + x, ry + y)).a > 0.01f){
found = true;
break outer;
}
}
}
if(found){
out.draw(x, y, outlineColor);
}
}
}
}
last = out;
packer.pack(name, out);
}
if(generatedIcons.length > 1){
Pixmap base = Core.atlas.getPixmap(generatedIcons[0]).crop();
for(int i = 1; i < generatedIcons.length; i++){
if(i == generatedIcons.length - 1 && last != null){
base.drawPixmap(last);
}else{
base.draw(Core.atlas.getPixmap(generatedIcons[i]));
}
}
packer.pack("block-" + name + "-full", base);
generatedIcons = null;
Arrays.fill(cicons, null);
}
} }
/** Never use outside of the editor! */ /** Never use outside of the editor! */

View File

@@ -2,6 +2,7 @@ package io.anuke.mindustry.world.consumers;
import io.anuke.arc.collection.*; import io.anuke.arc.collection.*;
import io.anuke.arc.scene.ui.layout.*; import io.anuke.arc.scene.ui.layout.*;
import io.anuke.arc.util.ArcAnnotate.*;
import io.anuke.mindustry.entities.type.*; import io.anuke.mindustry.entities.type.*;
import io.anuke.mindustry.game.*; import io.anuke.mindustry.game.*;
import io.anuke.mindustry.type.*; import io.anuke.mindustry.type.*;
@@ -11,12 +12,17 @@ import io.anuke.mindustry.world.meta.*;
import io.anuke.mindustry.world.meta.values.*; import io.anuke.mindustry.world.meta.values.*;
public class ConsumeItems extends Consume{ public class ConsumeItems extends Consume{
public final ItemStack[] items; public final @NonNull ItemStack[] items;
public ConsumeItems(ItemStack[] items){ public ConsumeItems(ItemStack[] items){
this.items = items; this.items = items;
} }
/** Mods.*/
protected ConsumeItems(){
this(new ItemStack[]{});
}
@Override @Override
public void applyItemFilter(Bits filter){ public void applyItemFilter(Bits filter){
for(ItemStack stack : items){ for(ItemStack stack : items){

View File

@@ -1,14 +1,13 @@
package io.anuke.mindustry.world.consumers; package io.anuke.mindustry.world.consumers;
import io.anuke.arc.collection.*; import io.anuke.arc.collection.*;
import io.anuke.arc.scene.ui.layout.Table; import io.anuke.arc.scene.ui.layout.*;
import io.anuke.mindustry.entities.type.TileEntity; import io.anuke.mindustry.entities.type.*;
import io.anuke.mindustry.game.*; import io.anuke.mindustry.game.*;
import io.anuke.mindustry.type.Liquid; import io.anuke.mindustry.type.*;
import io.anuke.mindustry.ui.ReqImage; import io.anuke.mindustry.ui.*;
import io.anuke.mindustry.world.Tile; import io.anuke.mindustry.world.*;
import io.anuke.mindustry.world.meta.BlockStat; import io.anuke.mindustry.world.meta.*;
import io.anuke.mindustry.world.meta.BlockStats;
public class ConsumeLiquid extends ConsumeLiquidBase{ public class ConsumeLiquid extends ConsumeLiquidBase{
public final Liquid liquid; public final Liquid liquid;
@@ -18,6 +17,10 @@ public class ConsumeLiquid extends ConsumeLiquidBase{
this.liquid = liquid; this.liquid = liquid;
} }
protected ConsumeLiquid(){
this(null, 0f);
}
@Override @Override
public void applyLiquidFilter(Bits filter){ public void applyLiquidFilter(Bits filter){
filter.set(liquid.id); filter.set(liquid.id);

View File

@@ -20,6 +20,10 @@ public class ConsumePower extends Consume{
this.buffered = buffered; this.buffered = buffered;
} }
protected ConsumePower(){
this(0f, 0f, false);
}
@Override @Override
public ConsumeType type(){ public ConsumeType type(){
return ConsumeType.power; return ConsumeType.power;

View File

@@ -149,6 +149,7 @@ public class DesktopLauncher extends ClientLauncher{
SVars.stats = new SStats(); SVars.stats = new SStats();
SVars.workshop = new SWorkshop(); SVars.workshop = new SWorkshop();
SVars.user = new SUser(); SVars.user = new SUser();
boolean[] isShutdown = {false};
Events.on(ClientLoadEvent.class, event -> { Events.on(ClientLoadEvent.class, event -> {
player.name = SVars.net.friends.getPersonaName(); player.name = SVars.net.friends.getPersonaName();
@@ -177,8 +178,18 @@ public class DesktopLauncher extends ClientLauncher{
} }
}); });
}); });
Events.on(DisposeEvent.class, event -> {
SteamAPI.shutdown();
isShutdown[0] = true;
});
//steam shutdown hook //steam shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(SteamAPI::shutdown)); Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if(!isShutdown[0]){
SteamAPI.shutdown();
}
}));
} }
static void handleCrash(Throwable e){ static void handleCrash(Throwable e){

View File

@@ -1,3 +1,3 @@
org.gradle.daemon=true org.gradle.daemon=true
org.gradle.jvmargs=-Xms256m -Xmx1024m org.gradle.jvmargs=-Xms256m -Xmx1024m
archash=e17b152d4f597837640fe4d659ca5a820e8a2b15 archash=9f567f04a005ea8b4132b31d64f325ec105def5e

View File

@@ -320,7 +320,7 @@ task pack(dependsOn: classes){
//antialias everything except UI elements //antialias everything except UI elements
fileTree(dir: '../core/assets-raw/sprites_out/', include: "**/*.png").visit{ file -> fileTree(dir: '../core/assets-raw/sprites_out/', include: "**/*.png").visit{ file ->
if(file.isDirectory() || file.toString().replace("\\", "/").contains("zones/") || (file.toString().replace("\\", "/").contains("/ui/") && !file.toString().contains("icon-small") && !file.toString().contains("icon-medium") && !file.toString().contains("icon-large"))) return if(file.isDirectory() || file.toString().replace("\\", "/").contains("zones/") || (file.toString().replace("\\", "/").contains("/ui/") && file.toString().startsWith("icon-"))) return
antialias(file.file) antialias(file.file)
} }

View File

@@ -68,7 +68,6 @@ public class Generators{
ImagePacker.generate("block-icons", () -> { ImagePacker.generate("block-icons", () -> {
Image colors = new Image(content.blocks().size, 1); Image colors = new Image(content.blocks().size, 1);
Color outlineColor = Color.valueOf("404049");
for(Block block : content.blocks()){ for(Block block : content.blocks()){
TextureRegion[] regions = block.getGeneratedIcons(); TextureRegion[] regions = block.getGeneratedIcons();
@@ -114,7 +113,7 @@ public class Generators{
} }
} }
if(found){ if(found){
out.draw(x, y, outlineColor); out.draw(x, y, block.outlineColor);
} }
} }
} }