Java Code Examples for com.badlogic.gdx.math.MathUtils#random()

The following examples show how to use com.badlogic.gdx.math.MathUtils#random() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FallingObject.java    From FruitCatcher with Apache License 2.0 6 votes vote down vote up
public FallingObject(ImageProvider imageProvider, TextureRegion [] textureRegions,
					 FallingObjectState state) {
    rect = new Rectangle();
    rect.width = width;
    rect.height = height;
    
    this.textureRegions = textureRegions;
    this.state = state;
    
    if(state.getPosX() < 0 || state.getPosY() < 0) {
    	rect.x = MathUtils.random(0, imageProvider.getScreenWidth()-width);
    	rect.y = imageProvider.getScreenHeight();
    }
    else {
    	rect.x = state.getPosX();
    	rect.y = state.getPosY();
    }
    
    state.setPosX((int) rect.x);
    state.setPosY((int) rect.y);
}
 
Example 2
Source File: Box2dLightTest.java    From box2dlights with Apache License 2.0 6 votes vote down vote up
void initConeLights() {
	clearLights();
	for (int i = 0; i < BALLSNUM; i++) {
		ConeLight light = new ConeLight(
				rayHandler, RAYS_PER_BALL, null, LIGHT_DISTANCE,
				0, 0, 0f, MathUtils.random(15f, 40f));
		light.attachToBody(
				balls.get(i),
				RADIUS / 2f, RADIUS / 2f, MathUtils.random(0f, 360f));
		light.setColor(
				MathUtils.random(),
				MathUtils.random(),
				MathUtils.random(),
				1f);
		lights.add(light);
	}
}
 
Example 3
Source File: Icicles.java    From ud405 with MIT License 6 votes vote down vote up
public void update(float delta) {
    if (MathUtils.random() < delta * Constants.ICICLE_SPAWNS_PER_SECOND) {
        Vector2 newIciclePosition = new Vector2(
                MathUtils.random() * viewport.getWorldWidth(),
                viewport.getWorldHeight()
        );
        Icicle newIcicle = new Icicle(newIciclePosition);
        icicleList.add(newIcicle);
    }

    for (Icicle icicle : icicleList) {
        icicle.update(delta);
    }

    // TODO: begin a removal session
    icicleList.begin();

    // TODO: Remove any icicle completely off the bottom of the screen
    for (int i = 0; i < icicleList.size; i++) {
        if (icicleList.get(i).position.y < -Constants.ICICLES_HEIGHT) {
            icicleList.removeIndex(i);
        }
    }
    // TODO: End removal session
    icicleList.end();
}
 
Example 4
Source File: CameraShaker.java    From uracer-kotd with Apache License 2.0 6 votes vote down vote up
public Vector2 compute (float factor) {
	float px = Pixels;
	px *= ScaleUtils.Scale;

	float radiusX = MathUtils.random(-px, px);
	float radiusY = MathUtils.random(-px, px);
	float noiseAlpha = 0.1f;// * factor;
	noiseX.set(radiusX, noiseAlpha);
	noiseY.set(radiusY, noiseAlpha);
	result.set(noiseX.get() * factor, noiseY.get() * factor);

	// int limit = 50;
	// result.x = MathUtils.clamp(result.x, -limit, limit);
	// result.y = MathUtils.clamp(result.y, -limit, limit);

	// Gdx.app.log("", result.toString() + " / " + factor);
	// Gdx.app.log("", "pixels=" + px);
	return result;
}
 
Example 5
Source File: Debris.java    From libgdx-demo-pax-britannica with MIT License 6 votes vote down vote up
public Debris(Vector2 position) {
	super();
	this.position = position;
	this.setPosition(position.x, position.y);

	this.facing.rotate(random_direction);
	this.setScale(random_scale, random_scale);

	switch (MathUtils.random(0, 2)) {
	case 0:
		this.set(Resources.getInstance().debrisSmall);
		break;
	case 1:
		this.set(Resources.getInstance().debrisMed);
		break;
	default:
		this.set(Resources.getInstance().debrisLarge);
		break;
	}
}
 
Example 6
Source File: EntityFactory.java    From xibalba with MIT License 6 votes vote down vote up
/**
 * Create exit entity.
 *
 * @param mapIndex Map to place it on
 * @return The exit entity
 */
public Entity createExit(int mapIndex) {
  Map map = WorldManager.world.getMap(mapIndex);

  int cellX;
  int cellY;

  do {
    cellX = MathUtils.random(0, map.width - 1);
    cellY = MathUtils.random(0, map.height - 1);
  } while (WorldManager.mapHelpers.isBlocked(mapIndex, new Vector2(cellX, cellY))
      && WorldManager.mapHelpers.getWallNeighbours(mapIndex, cellX, cellY) >= 4);

  Vector2 position = new Vector2(cellX, cellY);
  Entity entity = new Entity();
  entity.add(new ExitComponent());
  entity.add(new PositionComponent(position));

  entity.add(new VisualComponent(
      Main.asciiAtlas.createSprite("1403"), position
  ));

  return entity;
}
 
Example 7
Source File: Normal.java    From Unlucky with MIT License 5 votes vote down vote up
@Override
public void setStats() {
    // if the enemy is an elite then its stats are multiplied by an elite multiplier
    float eliteMultiplier = MathUtils.random(Util.MIN_ELITE_MULTIPLIER, Util.MAX_ELITE_MULTIPLIER);

    // hp is scaled polynomially with curve MHP = level ^ 2.0 + 25 as a seed then a value is chosen from deviation
    int mhpSeed = (int) (Math.pow(level, 2) + 25);
    int mhp = Util.getDeviatedRandomValue(mhpSeed, 4);

    int minDmg = MathUtils.random(Util.ENEMY_INIT_MIN_MINDMG, Util.ENEMY_INIT_MAX_MINDMG);
    int maxDmg = MathUtils.random(Util.ENEMY_INIT_MIN_MAXDMG, Util.ENEMY_INIT_MAX_MAXDMG);

    for (int i = 0; i < this.level - 1; i++) {
        int dmgMean = MathUtils.random(Util.ENEMY_MIN_DMG_INCREASE, Util.ENEMY_MAX_DMG_INCREASE);
        int minDmgIncrease = (dmgMean - MathUtils.random(2));
        int maxDmgIncrease = (dmgMean + MathUtils.random(2));

        minDmg += minDmgIncrease;
        maxDmg += maxDmgIncrease;
    }

    // sets a random accuracy initially
    this.setAccuracy(MathUtils.random(Util.ENEMY_MIN_ACCURACY, Util.ENEMY_MAX_ACCURACY));

    // finalize stats
    this.setMaxHp(isElite ? (int) (eliteMultiplier * mhp) : mhp);
    this.setMinDamage(isElite ? (int) (eliteMultiplier * minDmg) : minDmg);
    this.setMaxDamage(isElite ? (int) (eliteMultiplier * maxDmg) : maxDmg);
}
 
Example 8
Source File: Array.java    From collider with Apache License 2.0 5 votes vote down vote up
public void shuffle () {
	T[] items = this.items;
	for (int i = size - 1; i >= 0; i--) {
		int ii = MathUtils.random(i);
		T temp = items[i];
		items[i] = items[ii];
		items[ii] = temp;
	}
}
 
Example 9
Source File: Background.java    From JavaExercises with GNU General Public License v2.0 5 votes vote down vote up
void update() {
    position.x -= speed;
    if (position.x < 0) {
        position.set(Gdx.graphics.getWidth(),
                MathUtils.random(0, Gdx.graphics.getHeight()));
        speed = MathUtils.random(0.5f, 2.0f);
    }
}
 
Example 10
Source File: Enemy.java    From ud406 with MIT License 5 votes vote down vote up
public Enemy(Platform platform) {
    this.platform = platform;
    direction = Direction.RIGHT;
    position = new Vector2(platform.left, platform.top + Constants.ENEMY_CENTER.y);
    startTime = TimeUtils.nanoTime();
    health = Constants.ENEMY_HEALTH;

    // TODO: Initialized it
    randomPhase = MathUtils.random();
}
 
Example 11
Source File: Fish.java    From libgdx-demo-pax-britannica with MIT License 5 votes vote down vote up
public Fish(Vector2 position) {
	super();
	this.position = position;
	this.setPosition(position.x, position.y);
	this.setRotation(random_direction);
	this.setScale(random_scale, random_scale);

	switch (MathUtils.random(0, 7)) {
	case 0:
		this.set(Resources.getInstance().fish1);
		break;
	case 1:
		this.set(Resources.getInstance().fish2);
		break;
	case 2:
		this.set(Resources.getInstance().fish3);
		break;
	case 3:
		this.set(Resources.getInstance().fish4);
		break;
	case 4:
		this.set(Resources.getInstance().fish5);
		break;
	case 5:
		this.set(Resources.getInstance().fish6);
		break;
	case 6:
		this.set(Resources.getInstance().fish7);
		break;
	default:
		this.set(Resources.getInstance().fish8);
		break;
	}
	
	if(random_direction==-1) {
		flip(true, false);
	}
}
 
Example 12
Source File: WordCloud.java    From ud405 with MIT License 5 votes vote down vote up
static Word randomWord(float minScale, float maxScale) {
    float x = MathUtils.random(-.25f, .75f);
    float y = MathUtils.random();
    float scale = minScale + (maxScale - minScale) * MathUtils.random();
    Color color = new Color(MathUtils.random(), MathUtils.random(), MathUtils.random(), 1);
    String letters = WORDS[MathUtils.random(WORDS.length - 1)];
    return new Word(x, y, scale, color, letters);
}
 
Example 13
Source File: Enemy.java    From ud406 with MIT License 5 votes vote down vote up
public Enemy(Platform platform) {
    this.platform = platform;
    direction = Direction.RIGHT;
    position = new Vector2(platform.left, platform.top + Constants.ENEMY_CENTER.y);
    startTime = TimeUtils.nanoTime();
    health = Constants.ENEMY_HEALTH;
    bobOffset = MathUtils.random();
}
 
Example 14
Source File: Enemy.java    From ud406 with MIT License 5 votes vote down vote up
public Enemy(Platform platform) {
    this.platform = platform;
    direction = Direction.RIGHT;
    position = new Vector2(platform.left, platform.top + Constants.ENEMY_CENTER.y);
    startTime = TimeUtils.nanoTime();
    health = Constants.ENEMY_HEALTH;
    bobOffset = MathUtils.random();
}
 
Example 15
Source File: Enemy.java    From ud406 with MIT License 5 votes vote down vote up
public Enemy(Platform platform) {
    this.platform = platform;
    direction = Direction.RIGHT;
    position = new Vector2(platform.left, platform.top + Constants.ENEMY_CENTER.y);
    startTime = TimeUtils.nanoTime();
    health = Constants.ENEMY_HEALTH;
    bobOffset = MathUtils.random();
}
 
Example 16
Source File: GameplayScreen.java    From ud406 with MIT License 5 votes vote down vote up
private void startNewLevel() {

//        level = Level.debugLevel();

        String levelName = Constants.LEVELS[MathUtils.random(Constants.LEVELS.length - 1)];
        level = LevelLoader.load(levelName);

        chaseCam.camera = level.viewport.getCamera();
        chaseCam.target = level.getGigaGal();
        onscreenControls.gigaGal = level.getGigaGal();
        resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    }
 
Example 17
Source File: RandomOperation.java    From artemis-odb-orion with Apache License 2.0 5 votes vote down vote up
@Override
protected void begin(RandomOperation op, OperationTree node) {
	int index = 0;
	float rnd = MathUtils.random(op.accumulated);
	for (int i = 0, s = op.cumulativeSums.size; s > i; i++) {
		if (rnd < op.cumulativeSums.items[i])
			break;

		index++;
	}

	op.operationIndex = index;
}
 
Example 18
Source File: Ssao.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
/** Computes random field for the ssao shader */
private void createRandomField (int width, int height, Format format) {
	randomField = new Texture(width, height, format);
	randomField.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
	randomField.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);

	Pixmap pixels = new Pixmap(width, height, format);
	ByteBuffer bytes = pixels.getPixels();
	int wrote = 0;

	while (wrote < width * height * 4) {
		float x = (MathUtils.random() - 0.5f) * 2.0f;
		float y = (MathUtils.random() - 0.5f) * 2.0f;
		float z = (MathUtils.random() - 0.5f) * 2.0f;
		float l = (float)Math.sqrt(x * x + y * y + z * z);
		if (l <= 1.0f && l > 0.1f) {
			x = (x + 1.0f) * 0.5f;
			y = (y + 1.0f) * 0.5f;
			z = (z + 1.0f) * 0.5f;
			bytes.put((byte)(x * 255f));
			bytes.put((byte)(y * 255f));
			bytes.put((byte)(z * 255f));
			bytes.put((byte)255);
			wrote += 4;
		}
	}

	bytes.flip();
	randomField.draw(pixels, 0, 0);
	pixels.dispose();
}
 
Example 19
Source File: EffectUtils.java    From jorbs-spire-mod with MIT License 4 votes vote down vote up
public static AbstractGameEffect getWrathEffect(AbstractCard card) {
    AbstractGameEffect effect = new FireBurstParticleEffect(card.current_x + (WRATH_TEXT_OFFSET_X + MathUtils.random(-10.0F, 10.0F)) * card.drawScale * Settings.scale,
            card.current_y + (WRATH_TEXT_OFFSET_Y + MathUtils.random(-10.0F, 10.0F)) * card.drawScale * Settings.scale);
    ReflectionUtils.setPrivateField(effect, AbstractGameEffect.class, "color", Color.ORANGE.cpy());
    return effect;
}
 
Example 20
Source File: CombatHelpers.java    From xibalba with MIT License 2 votes vote down vote up
private int rollHit(Entity starter, Entity target, int skillLevel, String bodyPart) {
  // Roll relevant skill and a 6, highest result is used as your hit roll

  int skillRoll = skillLevel == 0 ? 0 : MathUtils.random(1, skillLevel);
  int otherRoll = MathUtils.random(1, 6);
  int hitRoll = skillRoll > otherRoll ? skillRoll : otherRoll;

  // Add accuracy

  AttributesComponent starterAttributes = ComponentMappers.attributes.get(starter);

  hitRoll += starterAttributes.agility == 0 ? 0 : MathUtils.random(1, starterAttributes.agility);

  // Roll their dodge

  AttributesComponent targetAttributes = ComponentMappers.attributes.get(target);
  int dodgeRoll = MathUtils.random(1, targetAttributes.agility);

  // Miss if under

  if (hitRoll < dodgeRoll) {
    return 0;
  }

  // Roll target body part

  BodyComponent targetBody = ComponentMappers.body.get(target);
  int bodyPartRoll = MathUtils.random(1, targetBody.bodyParts.get(bodyPart));

  // Miss if under

  if (hitRoll < bodyPartRoll) {
    return 0;
  }

  // Tween some opacity on the entity hit
  VisualComponent targetVisual = ComponentMappers.visual.get(target);

  WorldManager.tweens.add(
      Tween.to(targetVisual.sprite, SpriteAccessor.ALPHA, .05f)
          .target(.25f).repeatYoyo(1, 0f)
  );

  // Ya didn't miss!
  return hitRoll;
}