Search in sources :

Example 1 with Recipe

use of io.anuke.mindustry.resource.Recipe in project Mindustry by Anuken.

the class Placement method validPlace.

public static boolean validPlace(int x, int y, Block type) {
    for (int i = 0; i < world.getSpawns().size; i++) {
        SpawnPoint spawn = world.getSpawns().get(i);
        if (Vector2.dst(x * tilesize, y * tilesize, spawn.start.worldx(), spawn.start.worldy()) < enemyspawnspace) {
            return false;
        }
    }
    Recipe recipe = Recipes.getByResult(type);
    if (recipe == null || !state.inventory.hasItems(recipe.requirements)) {
        return false;
    }
    rect.setSize(type.width * tilesize, type.height * tilesize);
    Vector2 offset = type.getPlaceOffset();
    rect.setCenter(offset.x + x * tilesize, offset.y + y * tilesize);
    synchronized (Entities.entityLock) {
        for (SolidEntity e : Entities.getNearby(enemyGroup, x * tilesize, y * tilesize, tilesize * 2f)) {
            // not sure why this happens?
            if (e == null)
                continue;
            Rectangle rect = e.hitbox.getRect(e.x, e.y);
            if (Placement.rect.overlaps(rect)) {
                return false;
            }
        }
    }
    if (type.solid || type.solidifes) {
        for (Player player : playerGroup.all()) {
            if (!player.isAndroid && rect.overlaps(player.hitbox.getRect(player.x, player.y))) {
                return false;
            }
        }
    }
    Tile tile = world.tile(x, y);
    if (tile == null || (isSpawnPoint(tile) && (type.solidifes || type.solid)))
        return false;
    if (type.isMultiblock()) {
        int offsetx = -(type.width - 1) / 2;
        int offsety = -(type.height - 1) / 2;
        for (int dx = 0; dx < type.width; dx++) {
            for (int dy = 0; dy < type.height; dy++) {
                Tile other = world.tile(x + dx + offsetx, y + dy + offsety);
                if (other == null || (other.block() != Blocks.air && !other.block().alwaysReplace) || isSpawnPoint(other)) {
                    return false;
                }
            }
        }
        return true;
    } else {
        return tile.block() != type && (type.canReplace(tile.block()) || tile.block().alwaysReplace) && tile.block().isMultiblock() == type.isMultiblock() || tile.block() == Blocks.air;
    }
}
Also used : Player(io.anuke.mindustry.entities.Player) SolidEntity(io.anuke.ucore.entities.SolidEntity) Recipe(io.anuke.mindustry.resource.Recipe) Vector2(com.badlogic.gdx.math.Vector2) Rectangle(com.badlogic.gdx.math.Rectangle) SpawnPoint(io.anuke.mindustry.game.SpawnPoint) SpawnPoint(io.anuke.mindustry.game.SpawnPoint)

Example 2 with Recipe

use of io.anuke.mindustry.resource.Recipe in project Mindustry by Anuken.

the class Placement method breakBlock.

/**
 *Returns block type that was broken, or null if unsuccesful.
 */
public static Block breakBlock(int x, int y, boolean effect, boolean sound) {
    Tile tile = world.tile(x, y);
    if (tile == null)
        return null;
    Block block = tile.isLinked() ? tile.getLinked().block() : tile.block();
    Recipe result = Recipes.getByResult(block);
    if (result != null) {
        for (ItemStack stack : result.requirements) {
            state.inventory.addItem(stack.item, (int) (stack.amount * breakDropAmount));
        }
    }
    if (tile.block().drops != null) {
        state.inventory.addItem(tile.block().drops.item, tile.block().drops.amount);
    }
    if (sound)
        threads.run(() -> Effects.sound("break", x * tilesize, y * tilesize));
    if (!tile.block().isMultiblock() && !tile.isLinked()) {
        tile.setBlock(Blocks.air);
        if (effect)
            Effects.effect(Fx.breakBlock, tile.worldx(), tile.worldy());
    } else {
        Tile target = tile.isLinked() ? tile.getLinked() : tile;
        Array<Tile> removals = target.getLinkedTiles();
        for (Tile toremove : removals) {
            // note that setting a new block automatically unlinks it
            toremove.setBlock(Blocks.air);
            if (effect)
                Effects.effect(Fx.breakBlock, toremove.worldx(), toremove.worldy());
        }
    }
    return block;
}
Also used : Recipe(io.anuke.mindustry.resource.Recipe) ItemStack(io.anuke.mindustry.resource.ItemStack)

Aggregations

Recipe (io.anuke.mindustry.resource.Recipe)2 Rectangle (com.badlogic.gdx.math.Rectangle)1 Vector2 (com.badlogic.gdx.math.Vector2)1 Player (io.anuke.mindustry.entities.Player)1 SpawnPoint (io.anuke.mindustry.game.SpawnPoint)1 ItemStack (io.anuke.mindustry.resource.ItemStack)1 SolidEntity (io.anuke.ucore.entities.SolidEntity)1