Example usage for com.badlogic.gdx.utils Array random

List of usage examples for com.badlogic.gdx.utils Array random

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils Array random.

Prototype

public T random() 

Source Link

Document

Returns a random item from the array, or null if the array is empty.

Usage

From source file:com.ray3k.libraryinvaders.entities.BulletEntity.java

License:Open Source License

private TextureRegion getBullet() {
    Array<String> names = getCore().getImagePacks().get(Core.DATA_PATH + "/lasers");

    return getCore().getAtlas().findRegion(names.random());
}

From source file:com.ray3k.libraryinvaders.entities.EnemyEntity.java

License:Open Source License

private TextureRegion getEnemy() {
    Array<String> names = getCore().getImagePacks().get(Core.DATA_PATH + "/enemies");

    return getCore().getAtlas().findRegion(names.random());
}

From source file:com.ray3k.libraryinvaders.entities.UfoEntity.java

License:Open Source License

private TextureRegion getUfo() {
    Array<String> names = getCore().getImagePacks().get(Core.DATA_PATH + "/ufos");

    return getCore().getAtlas().findRegion(names.random());
}

From source file:com.ray3k.libraryinvaders.states.GameState.java

License:Open Source License

@Override
public void start() {
    EnemyEntity.speedMultiplier = 1.0f;/*from w  w w  .  j  av a 2 s  .co  m*/
    respawnTimer = -1;
    score = 0;

    ufoTimer = UFO_MAX_TIME;

    inputManager = new InputManager();

    camera = new OrthographicCamera();
    viewport = new ScreenViewport(camera);
    viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    viewport.apply();

    camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);

    skin = getCore().getAssetManager().get(Core.DATA_PATH + "/skin/skin.json", Skin.class);
    stage = new Stage(new ScreenViewport());

    InputMultiplexer inputMultiplexer = new InputMultiplexer();
    inputMultiplexer.addProcessor(inputManager);
    inputMultiplexer.addProcessor(stage);
    Gdx.input.setInputProcessor(inputMultiplexer);

    table = new Table();
    table.setFillParent(true);
    stage.addActor(table);

    entityManager = new EntityManager();
    PlayerEntity player = new PlayerEntity(this);

    spawnEntities();

    Array<BarricadeEntity> barricades = new Array<BarricadeEntity>();
    float barricadesWidth = 0.0f;
    final int BARRICADE_COUNT = 4;
    for (int i = 0; i < BARRICADE_COUNT; i++) {
        BarricadeEntity barricade = new BarricadeEntity(this);
        Array<String> names = getCore().getImagePacks().get(Core.DATA_PATH + "/barricades");
        barricade.setTextureRegion(getCore().getAtlas().findRegion(names.random()));

        barricade.setX(barricadesWidth);
        barricade.setY(30.0f + player.getTextureRegion().getRegionHeight() + 35.0f);

        barricadesWidth += barricade.getTextureRegion().getRegionWidth();

        barricades.add(barricade);
    }

    final float GAP = (Gdx.graphics.getWidth() - barricadesWidth - 100.0f) / (BARRICADE_COUNT - 1);

    for (int i = 1; i < barricades.size; i++) {
        BarricadeEntity barricade = barricades.get(i);
        barricade.addX(GAP * i);
        barricadesWidth += GAP;
    }

    final float addX = (Gdx.graphics.getWidth() - barricadesWidth) / 2.0f;
    for (BarricadeEntity barricade : barricades) {
        barricade.addX(addX);
        subdivideBarricade(barricade);
    }

    createStageElements();
}

From source file:com.ray3k.libraryinvaders.states.MenuState.java

License:Open Source License

private Drawable getEnemy() {
    Array<String> names = getCore().getImagePacks().get(Core.DATA_PATH + "/enemies");

    Drawable drawable = new TextureRegionDrawable(getCore().getAtlas().findRegion(names.random()));
    return drawable;
}

From source file:com.turbogerm.helljump.game.generator.RiseSectionGenerator.java

License:Open Source License

private static int getRandomAvailableOffset(Array<Integer> takenOffsets) {
    Array<Integer> availableOffsets = GameUtils.getRange(PlatformData.MAX_PLATFORM_OFFSET + 1);

    for (Integer takenOffset : takenOffsets) {
        int firstInvalidatedOffset = takenOffset - PlatformData.PLATFORM_WIDTH_OFFSETS + 1;
        int numInvalidatedOffsets = PlatformData.PLATFORM_WIDTH_OFFSETS * 2 - 1;
        int lastInvalidatedOffset = firstInvalidatedOffset + numInvalidatedOffsets - 1;
        for (int i = firstInvalidatedOffset; i <= lastInvalidatedOffset; i++) {
            availableOffsets.removeValue(i, false);
        }//from  w  ww  .j av a2 s . c om
    }

    return availableOffsets.random();
}

From source file:com.vlaaad.dice.game.world.controllers.PveLoadLevelController.java

License:Open Source License

@Override
protected void start() {
    LevelDescription description = world.level;
    for (Map.Entry<Grid2D.Coordinate, String> entry : description.getElements(LevelElementType.tile)) {
        world.dispatcher.dispatch(LOAD_TILE,
                new TileInfo(entry.getKey().x(), entry.getKey().y(), entry.getValue()));
    }//from  w  w  w. jav a  2  s .  c  om
    ObjectMap<Grid2D.Coordinate, Creature> creatures = new ObjectMap<Grid2D.Coordinate, Creature>();
    Array<Creature> dropCreatures = new Array<Creature>();
    ObjectMap<Creature, ObjectIntMap<Item>> drop = new ObjectMap<Creature, ObjectIntMap<Item>>();
    Player antagonist = world.players.get(PlayerHelper.antagonist);
    for (Map.Entry<Grid2D.Coordinate, Die> entry : description.getElements(LevelElementType.enemy)) {
        Creature creature = antagonist.addCreature(entry.getValue());
        creatures.put(entry.getKey(), creature);
    }
    for (Creature creature : creatures.values()) {
        dropCreatures.add(creature);
    }
    final ObjectIntMap<Creature> weights = new ObjectIntMap<Creature>();
    int total = 0;
    for (Creature creature : dropCreatures) {
        int cost = ExpHelper.getTotalCost(creature);
        weights.put(creature, cost);
        total += cost;
    }
    dropCreatures.sort(new Comparator<Creature>() {
        @Override
        public int compare(Creature o1, Creature o2) {
            return weights.get(o2, 0) - weights.get(o1, 0);
        }
    });
    if (dropCreatures.size > 1) {
        dropCreatures.truncate(Math.max(1, dropCreatures.size * 3 / 5));
    }

    for (Creature creature : dropCreatures) {
        drop.put(creature, new ObjectIntMap<Item>());
    }
    //        Logger.debug("drop: " + world.level.drop);
    ObjectIntMap<Item> droppedItems = world.level.drop.roll();
    //        Logger.debug("rolled: " + droppedItems);
    for (Item item : droppedItems.keys()) {
        int count = droppedItems.get(item, 1);
        float factor = count / (float) total;
        int distributedCount = 0;
        for (Creature creature : dropCreatures) {
            int creatureItemCount = (int) (weights.get(creature, 0) * factor);
            drop.get(creature).put(item, creatureItemCount);
            distributedCount += creatureItemCount;
        }
        if (distributedCount > count)
            throw new IllegalStateException("OMG! distributed " + item + " more than should! drop: " + drop
                    + ", to distribute: " + droppedItems);
        if (distributedCount < count) {
            while (distributedCount < count) {
                Creature random = dropCreatures.random();
                drop.get(random).getAndIncrement(item, 0, 1);
                //                    Logger.debug("added lost 1 " + item + " to " + random + "!");
                distributedCount++;
            }
        }
    }
    for (Creature creature : drop.keys()) {
        creature.setDrop(drop.get(creature));
        //            Logger.debug("set drop: " + creature + " => " + creature.drop);
    }
    for (Grid2D.Coordinate coordinate : creatures.keys()) {
        world.add(coordinate.x(), coordinate.y(), creatures.get(coordinate));
    }
    for (Map.Entry<Grid2D.Coordinate, Obstacle> entry : description.getElements(LevelElementType.obstacle)) {
        world.add(entry.getKey().x(), entry.getKey().y(), entry.getValue());
    }
    for (Map.Entry<Grid2D.Coordinate, StepDetector> entry : description
            .getElements(LevelElementType.stepDetector)) {
        world.add(entry.getKey().x(), entry.getKey().y(), entry.getValue());
    }
    for (Map.Entry<Grid2D.Coordinate, Fraction> entry : description.getElements(LevelElementType.spawn)) {
        world.dispatcher.dispatch(ADD_SPAWN_POINT,
                new SpawnPoint(entry.getKey().x(), entry.getKey().y(), entry.getValue()));
    }
    world.dispatcher.dispatch(LEVEL_LOADED, null);
}

From source file:com.vlaaad.dice.pvp.ServerMessageListener.java

License:Open Source License

private void applyDrop(ObjectIntMap<Item> droppedItems, Array<PlacedCreature> dice) {
    for (PlacedCreature c : dice) {
        c.drop.clear();/*from w  w w  .j av  a  2  s. co  m*/
    }
    final ObjectIntMap<PlacedCreature> weights = new ObjectIntMap<PlacedCreature>();
    int total = 0;
    for (PlacedCreature creature : dice) {
        int cost = creature.exp;
        weights.put(creature, cost);
        total += cost;
    }
    dice.sort(new Comparator<PlacedCreature>() {
        @Override
        public int compare(PlacedCreature o1, PlacedCreature o2) {
            return weights.get(o2, 0) - weights.get(o1, 0);
        }
    });

    ObjectMap<PlacedCreature, ObjectIntMap<Item>> drop = new ObjectMap<PlacedCreature, ObjectIntMap<Item>>();
    for (PlacedCreature creature : dice) {
        drop.put(creature, new ObjectIntMap<Item>());
    }

    for (Item item : droppedItems.keys()) {
        int count = droppedItems.get(item, 1);
        float factor = count / (float) total;
        int distributedCount = 0;
        for (PlacedCreature creature : dice) {
            int creatureItemCount = (int) (weights.get(creature, 0) * factor);
            drop.get(creature).put(item, creatureItemCount);
            distributedCount += creatureItemCount;
        }
        if (distributedCount > count)
            throw new IllegalStateException("OMG! distributed " + item + " more than should! drop: " + drop
                    + ", to distribute: " + droppedItems);
        if (distributedCount < count) {
            while (distributedCount < count) {
                PlacedCreature random = dice.random();
                drop.get(random).getAndIncrement(item, 0, 1);
                distributedCount++;
            }
        }
    }
    for (PlacedCreature creature : drop.keys()) {
        for (ObjectIntMap.Entry<Item> e : drop.get(creature).entries()) {
            creature.drop.put(e.key.id, e.value);
        }
    }
}

From source file:com.vlaaad.dice.ui.windows.DieSettingsWindow.java

License:Open Source License

@Override
protected void doShow(final Params params) {
    Table content = new Table(Config.skin);
    content.setBackground("ui-store-window-background");
    content.defaults().pad(4);//from   w  ww  . j av a  2 s.  com
    content.setTouchable(Touchable.enabled);

    final LocLabel infoLabel = new LocLabel(
            "ui-renames-left", Thesaurus.params().with("count", String.valueOf(params.die.renames))
                    .with("free-renames", "free-renames." + Thesaurus.Util.countForm(params.die.renames)),
            StoreWindow.INACTIVE);
    infoLabel.setWrap(true);
    infoLabel.setAlignment(Align.center);

    final StringSpin nameSpin = new StringSpin(4, 10, Config.thesaurus.localize(params.die.nameLocKey()));
    Container inner = new Container(nameSpin);
    inner.padTop(3);
    final Container nameContainer = new Container(inner);
    nameContainer.setBackground(Config.skin.getDrawable("ui/button/name-change-background"));
    final Button changeNameButton = new Button(Config.skin);
    final Thesaurus thesaurus = Config.assetManager.get("names.yml", Thesaurus.class);
    final Array<ThesaurusData> values = thesaurus.values();
    final Item coin = Config.items.get("coin");
    updateChangeNameButton(changeNameButton, params);
    changeNameButton.addListener(new ChangeListener() {
        @SuppressWarnings("unchecked")
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            ObjectSet<ThesaurusData> existingNames = Pools.obtain(ObjectSet.class);
            for (Die die : params.userData.dice()) {
                existingNames.add(thesaurus.getData(die.name.toLowerCase()));
            }
            ThesaurusData newName = values.random();
            while (existingNames.contains(newName)) {
                newName = values.random();
            }
            existingNames.clear();
            Pools.free(existingNames);
            final ThesaurusData chosenName = newName;
            changeNameButton.setDisabled(true);
            nameSpin.setText(Config.thesaurus.localize(newName.key), new Runnable() {
                @Override
                public void run() {
                    params.die.name = chosenName.key;
                    if (params.die.renames > 0) {
                        params.die.renames -= 1;
                        infoLabel.setParams(Thesaurus.params().with("count", String.valueOf(params.die.renames))
                                .with("free-renames",
                                        "free-renames." + Thesaurus.Util.countForm(params.die.renames)));
                    } else {
                        params.userData.incrementItemCount(coin, -1);
                    }
                    updateChangeNameButton(changeNameButton, params);
                    fire(RefreshEvent.INSTANCE);
                }
            });
        }
    });

    content.add(new LocLabel("ui-select-die-name")).row();
    content.add(new Tile("ui-creature-info-line")).width(80).padTop(0).row();
    content.add(nameContainer).size(70, 21).row();
    content.add(changeNameButton).size(100, 21).padBottom(0).row();
    content.add(infoLabel).width(110).row();

    table.add(content);
}

From source file:kyle.game.besiege.party.Soldier.java

License:Open Source License

public Soldier(Array<Weapon> weapons, Party party) {
    this.weapon = weapons.random();
    this.party = party;

    if (Math.random() > 0.5)
        tier = weapon.tier;//from   w  w  w. ja  va2  s .  com
    else
        tier = weapon.tier + 1;
    this.nextUpgrade = LEVEL_TIER[tier + 1];

    if (tier % 2 == 0)
        this.name = weapon.troopName;
    else
        this.name = VETERAN + weapon.troopName;

    this.level = LEVEL_TIER[tier] + (int) (Math.random() * (LEVEL_TIER[tier + 1] - LEVEL_TIER[tier]));
    this.exp = 0;
    this.next = (int) (Math.pow(LEVEL_FACTOR, level) * INITIAL_NEXT);

    baseAtk = 0;
    baseDef = 0;
    baseSpd = 0;
    for (int i = 0; i <= tier; i++) {
        if (ATK_TIER[i])
            baseAtk++;
        if (DEF_TIER[i])
            baseDef++;
        if (SPD_TIER[i])
            baseSpd++;
    }

    wounded = false;
    timeWounded = 0;

    //      this.equipment = new Array<Equipment>();
    //      while (equipment.size > 0)
    //         this.equipment.add(equipment.pop());

    calcBonus();
    calcStats();
}