Java Code Examples for com.watabou.utils.GameMath#gate()

The following examples show how to use com.watabou.utils.GameMath#gate() . 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: ElixirOfAquaticRejuvenation.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean act() {
	
	if (Dungeon.level.water[target.pos] && target.HP < target.HT){
		float healAmt = GameMath.gate( 1, target.HT/50f, left );
		healAmt = Math.min(healAmt, target.HT - target.HP);
		if (Random.Float() < (healAmt % 1)){
			healAmt = (float)Math.ceil(healAmt);
		} else {
			healAmt = (float)Math.floor(healAmt);
		}
		target.HP += healAmt;
		left -= healAmt;
		target.sprite.emitter().burst( Speck.factory( Speck.HEALING ), 1 );
	}
	
	if (left <= 0){
		detach();
	} else {
		spend(TICK);
	}
	return true;
}
 
Example 2
Source File: HeroSelectScene.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void update() {
	super.update();
	//do not fade when a window is open
	for (Object v : members){
		if (v instanceof Window) resetFade();
	}
	if (GamesInProgress.selectedClass != null) {
		if (uiAlpha > 0f){
			uiAlpha -= Game.elapsed/4f;
		}
		float alpha = GameMath.gate(0f, uiAlpha, 1f);
		for (StyledButton b : heroBtns){
			b.alpha(alpha);
		}
		startBtn.alpha(alpha);
		btnExit.icon().alpha(alpha);
		challengeButton.icon().alpha(alpha);
		infoButton.icon().alpha(alpha);
	}
}
 
Example 3
Source File: DungeonTilemap.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
public int screenToTile(int x, int y, boolean wallAssist ) {
	PointF p = camera().screenToCamera( x, y ).
		offset( this.point().negate() ).
		invScale( SIZE );
	
	//snap to the edges of the tilemap
	p.x = GameMath.gate(0, p.x, Dungeon.level.width()-0.001f);
	p.y = GameMath.gate(0, p.y, Dungeon.level.height()-0.001f);

	int cell = (int)p.x + (int)p.y * Dungeon.level.width();

	if (wallAssist
			&& map != null
			&& DungeonTileSheet.wallStitcheable(map[cell])){

		if (cell + mapWidth < size
				&& p.y % 1 >= 0.75f
				&& !DungeonTileSheet.wallStitcheable(map[cell + mapWidth])){
			cell += mapWidth;
		}

	}

	return cell;
}
 
Example 4
Source File: HallwayRoom.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 6 votes vote down vote up
protected final Point getDoorCenter(){
	PointF doorCenter = new PointF(0, 0);

	for (Door door : connected.values()) {
		doorCenter.x += door.x;
		doorCenter.y += door.y;
	}

	Point c = new Point((int)doorCenter.x / connected.size(), (int)doorCenter.y / connected.size());
	if (Random.Float() < doorCenter.x % 1) c.x++;
	if (Random.Float() < doorCenter.y % 1) c.y++;
	c.x = (int) GameMath.gate(left+2, c.x, right-2);
	c.y = (int)GameMath.gate(top+2, c.y, bottom-2);

	return c;
}
 
Example 5
Source File: ElixirOfAquaticRejuvenation.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean act() {
	
	if (Dungeon.level.water[target.pos] && target.HP < target.HT){
		float healAmt = GameMath.gate( 1, target.HT/50f, left );
		healAmt = Math.min(healAmt, target.HT - target.HP);
		if (Random.Float() < (healAmt % 1)){
			healAmt = (float)Math.ceil(healAmt);
		} else {
			healAmt = (float)Math.floor(healAmt);
		}
		target.HP += healAmt;
		left -= healAmt;
		target.sprite.emitter().burst( Speck.factory( Speck.HEALING ), 1 );
	}
	
	if (left <= 0){
		detach();
	} else {
		spend(TICK);
		if (left <= target.HT/4f){
			BuffIndicator.refreshHero();
		}
	}
	return true;
}
 
Example 6
Source File: CellSelector.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onMouseScroll(int scroll) {
	mouseZoom -= scroll / 3f;
	if (PDInputProcessor.modifier) {
		mouseZoom = zoom( mouseZoom );
	} else {
		zoom( Math.round( mouseZoom ) );
		mouseZoom = GameMath.gate( PixelScene.minZoom, mouseZoom, PixelScene.maxZoom );
	}
	return true;
}
 
Example 7
Source File: Berserk.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean act() {
	if (berserking()){
		ShieldBuff buff = target.buff(WarriorShield.class);
		if (target.HP <= 0) {
			if (buff != null && buff.shielding() > 0) {
				buff.absorbDamage(1 + (int)Math.ceil(target.shielding() * 0.1f));
			} else {
				//if there is no shield buff, or it is empty, then try to remove from other shielding buffs
				buff = target.buff(ShieldBuff.class);
				if (buff != null) buff.absorbDamage(1 + (int)Math.ceil(target.shielding() * 0.1f));
			}
			if (target.shielding() <= 0) {
				target.die(this);
				if (!target.isAlive()) Dungeon.fail(this.getClass());
			}
		} else {
			state = State.RECOVERING;
			levelRecovery = LEVEL_RECOVER_START;
			BuffIndicator.refreshHero();
			if (buff != null) buff.absorbDamage(buff.shielding());
			power = 0f;
		}
	} else if (state == State.NORMAL) {
		power -= GameMath.gate(0.1f, power, 1f) * 0.067f * Math.pow((target.HP/(float)target.HT), 2);
		
		if (power <= 0){
			detach();
		}
		BuffIndicator.refreshHero();
	}
	spend(TICK);
	return true;
}
 
Example 8
Source File: Generator.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public static Armor randomArmor(int floorSet) {

		floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1);
		
		Armor a = (Armor)Reflection.newInstance(Category.ARMOR.classes[Random.chances(floorSetTierProbs[floorSet])]);
		a.random();
		return a;
	}
 
Example 9
Source File: AlchemistsToolkit.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public void gainCharge(float levelPortion) {
	alchemyReady = true;
	
	if (cursed) return;
	
	if (charge < chargeCap) {
		
		//generates 2 energy every hero level, +0.1 energy per toolkit level
		//to a max of 12 energy per hero level
		//This means that energy absorbed into the kit is recovered in 6.67 hero levels (as 33% of input energy is kept)
		//exp towards toolkit levels is included here
		float effectiveLevel = GameMath.gate(0, level() + exp/10f, 10);
		partialCharge += (2 + (1f * effectiveLevel)) * levelPortion;
		
		//charge is in increments of 1/10 max hunger value.
		while (partialCharge >= 1) {
			charge++;
			partialCharge -= 1;
			
			if (charge == chargeCap){
				GLog.p( Messages.get(AlchemistsToolkit.class, "full") );
				partialCharge = 0;
			}
			
			updateQuickslot();
		}
	} else
		partialCharge = 0;
}
 
Example 10
Source File: Generator.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public static MeleeWeapon randomWeapon(int floorSet) {

		floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1);
		
		Category c = wepTiers[Random.chances(floorSetTierProbs[floorSet])];
		MeleeWeapon w = (MeleeWeapon)Reflection.newInstance(c.classes[Random.chances(c.probs)]);
		w.random();
		return w;
	}
 
Example 11
Source File: Generator.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 5 votes vote down vote up
public static Armor randomArmor(int floorSet) {

		floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1);
		
		Armor a = (Armor)Reflection.newInstance(Category.ARMOR.classes[Random.chances(floorSetTierProbs[floorSet])]);
		a.random();
		return a;
	}
 
Example 12
Source File: Generator.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 5 votes vote down vote up
public static MissileWeapon randomMissile(int floorSet) {
	
	floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1);
	
	Category c = misTiers[Random.chances(floorSetTierProbs[floorSet])];
	MissileWeapon w = (MissileWeapon)Reflection.newInstance(c.classes[Random.chances(c.probs)]);
	w.random();
	return w;
}
 
Example 13
Source File: Affection.java    From pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int proc( Armor armor, Char attacker, Char defender, int damage) {

	int level = (int)GameMath.gate( 0, armor.effectiveLevel(), 6 );
	
	if (Level.adjacent( attacker.pos, defender.pos ) && Random.Int( level / 2 + 5 ) >= 4) {
		
		int duration = Random.IntRange( 3, 7 );
		
		Buff.affect( attacker, Charm.class, Charm.durationFactor( attacker ) * duration ).object = defender.id();
		attacker.sprite.centerEmitter().start( Speck.factory( Speck.HEART ), 0.2f, 5 );
		
		duration *= Random.Float( 0.5f, 1 );
		
		Buff.affect( defender, Charm.class, Charm.durationFactor( defender ) * duration ).object = attacker.id();
		defender.sprite.centerEmitter().start( Speck.factory( Speck.HEART ), 0.2f, 5 );
	}
	
	return damage;
}
 
Example 14
Source File: Affection.java    From remixed-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int defenceProc(Armor armor, Char attacker, Char defender, int damage) {

	int level = (int) GameMath.gate(0, armor.level(), 6);

	if (Dungeon.level.adjacent(attacker.getPos(), defender.getPos()) && Random.Int(level / 2 + 5) >= 4) {

		int duration = Random.IntRange(2, 5);

		Buff.affect(attacker, Charm.class, Charm.durationFactor(attacker) * duration);
		attacker.getSprite().centerEmitter().start(Speck.factory(Speck.HEART), 0.2f, 5);

		Buff.affect(defender, Charm.class, Random.Float(Charm.durationFactor(defender) * duration / 2, duration));
		defender.getSprite().centerEmitter().start(Speck.factory(Speck.HEART), 0.2f, 5);
	}

	return damage;
}
 
Example 15
Source File: Healing.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
private int healingThisTick(){
	return (int)GameMath.gate(1,
			Math.round(healingLeft * percentHealPerTick) + flatHealPerTick,
			healingLeft);
}
 
Example 16
Source File: RankingsScene.java    From unleashed-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void create() {
	
	super.create();
	
	Music.INSTANCE.play( Assets.THEME, true );
	Music.INSTANCE.volume( 1f );
	
	uiCamera.visible = false;
	
	int w = Camera.main.width;
	int h = Camera.main.height;
	
	archs = new Archs();
	archs.setSize( w, h );
	add( archs );
	
	Rankings.INSTANCE.load();

	BitmapText title = PixelScene.createText(TXT_TITLE, 9);
	title.hardlight(Window.SHPX_COLOR);
	title.measure();
	title.x = align((w - title.width()) / 2);
	title.y = align( GAP );
	add(title);
	
	if (Rankings.INSTANCE.records.size() > 0) {

		//attempts to give each record as much space as possible, ideally as much space as portrait mode
		float rowHeight = GameMath.gate(ROW_HEIGHT_MIN, (uiCamera.height - 26)/Rankings.INSTANCE.records.size(), ROW_HEIGHT_MAX);

		float left = (w - Math.min( MAX_ROW_WIDTH, w )) / 2 + GAP;
		float top = align( (h - rowHeight  * Rankings.INSTANCE.records.size()) / 2 );
		
		int pos = 0;
		
		for (Rankings.Record rec : Rankings.INSTANCE.records) {
			Record row = new Record( pos, pos == Rankings.INSTANCE.lastRecord, rec );
			float offset = rowHeight <= 14 ?
							pos %2 == 1? 5 : -5
							: 0;
			row.setRect( left+offset, top + pos * rowHeight, w - left * 2, rowHeight );
			try {
				add(row);
				pos++;
			} catch (Exception e) {
				//
			}
		}
		
		if (Rankings.INSTANCE.totalNumber >= Rankings.TABLE_SIZE) {
			BitmapText label = PixelScene.createText( TXT_TOTAL, 8 );
			label.hardlight( 0xCCCCCC );
			label.measure();
			add( label );

			BitmapText won = PixelScene.createText( Integer.toString( Rankings.INSTANCE.wonNumber ), 8 );
			won.hardlight( Window.SHPX_COLOR );
			won.measure();
			add( won );

			BitmapText total = PixelScene.createText( "/" + Rankings.INSTANCE.totalNumber, 8 );
			total.hardlight( 0xCCCCCC );
			total.measure();
			total.x = align( (w - total.width()) / 2 );
			total.y = align( top + pos * rowHeight + GAP );
			add( total );

			float tw = label.width() + won.width() + total.width();
			label.x = align( (w - tw) / 2 );
			won.x = label.x + label.width();
			total.x = won.x + won.width();
			label.y = won.y = total.y = align( h - label.height() - GAP );

		}
		
	} else {

		BitmapText noRec = PixelScene.createText(TXT_NO_GAMES, 8);
		noRec.hardlight( 0xCCCCCC );
		noRec.measure();
		noRec.x = align((w - noRec.width()) / 2);
		noRec.y = align((h - noRec.height()) / 2);
		add(noRec);
		
	}

	ExitButton btnExit = new ExitButton();
	btnExit.setPos( Camera.main.width - btnExit.width(), 0 );
	add( btnExit );

	fadeIn();
}
 
Example 17
Source File: OptionSlider.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void createChildren() {
	super.createChildren();

	add( BG = Chrome.get(Chrome.Type.RED_BUTTON));
	BG.alpha(0.5f);

	add(title = PixelScene.renderTextBlock(9));
	add(this.minTxt = PixelScene.renderTextBlock(6));
	add(this.maxTxt = PixelScene.renderTextBlock(6));

	add(sliderBG = new ColorBlock(1, 1, 0xFF222222));
	sliderNode = Chrome.get(Chrome.Type.RED_BUTTON);
	sliderNode.size(5, 9);

	pointerArea = new PointerArea(0, 0, 0, 0){
		boolean pressed = false;

		@Override
		protected void onPointerDown( PointerEvent event ) {
			pressed = true;
			PointF p = camera().screenToCamera((int) event.current.x, (int) event.current.y);
			sliderNode.x = GameMath.gate(sliderBG.x-2, p.x - sliderNode.width()/2, sliderBG.x+sliderBG.width()-2);
			sliderNode.brightness(1.5f);
		}

		@Override
		protected void onPointerUp( PointerEvent event ) {
			if (pressed) {
				PointF p = camera().screenToCamera((int) event.current.x, (int) event.current.y);
				sliderNode.x = GameMath.gate(sliderBG.x - 2, p.x - sliderNode.width()/2, sliderBG.x + sliderBG.width() - 2);
				sliderNode.resetColor();
				
				//sets the selected value
				selectedVal = minVal + Math.round(sliderNode.x / tickDist);
				sliderNode.x = x + tickDist * (selectedVal - minVal);
				PixelScene.align(sliderNode);
				onChange();
				pressed = false;
			}
		}

		@Override
		protected void onDrag( PointerEvent event ) {
			if (pressed) {
				PointF p = camera().screenToCamera((int) event.current.x, (int) event.current.y);
				sliderNode.x = GameMath.gate(sliderBG.x - 2, p.x - sliderNode.width()/2, sliderBG.x + sliderBG.width() - 2);
			}
		}
	};
	add(pointerArea);

}
 
Example 18
Source File: Hunger.java    From remixed-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean act() {
	if (target.isAlive()) {

		int difficulty = Game.getDifficulty();

		if (!target.level().isSafe() && isStarving()) {

			if (Random.Float() < 0.3f && (target.hp() > 1 || !target.paralysed)) {

				if(target==Dungeon.hero) {
					GLog.n(Game.getVars(R.array.Hunger_Starving)[target.getGender()]);
				}

				if(difficulty >= 3) {
					target.damage(Math.max(target.effectiveSTR() - 10, 1), this);
				} else {
					target.damage( 1, this );
				}
			}
			
			if(difficulty >= 3) {
				if(Random.Float() < 0.01) {
					Buff.prolong(target, Weakness.class, Weakness.duration(target));
				}
				
				if(Random.Float() < 0.01) {
					Buff.prolong(target, Vertigo.class, Vertigo.duration(target));
				}
			}
			
		} else {	
			
			int bonus = target.buffLevel(RingOfSatiety.Satiety.class);

			float delta = Math.max(STEP - bonus, 1);

			delta *= RemixedDungeon.getDifficultyFactor() / 1.5f;

			if(target.level().isSafe()){
				delta = 0;
			}
			
			float newLevel = hungerLevel + delta;

			if(target==Dungeon.hero) {
				boolean statusUpdated = false;
				if (newLevel >= STARVING) {

					GLog.n(Game.getVars(R.array.Hunger_Starving)[target.getGender()]);
					statusUpdated = true;

				} else if (newLevel >= HUNGRY && hungerLevel < HUNGRY) {
					GLog.w(Game.getVars(R.array.Hunger_Hungry)[target.getGender()]);
					statusUpdated = true;

				}

				hungerLevel = GameMath.gate(0, newLevel, STARVING);

				if (statusUpdated) {
					BuffIndicator.refreshHero();
				}
			}
			
		}
		
		float step = target.getHeroClass() == HeroClass.ROGUE ? STEP * 1.2f : STEP;
		step *= target.hasBuff(Shadows.class) ? 1.5f : 1;
		step *= Dungeon.realtime() ? 10f : 1;

		spend( step );
	} else {
		deactivate();
	}
	
	return true;
}
 
Example 19
Source File: Hero.java    From YetAnotherPixelDungeon with GNU General Public License v3.0 3 votes vote down vote up
public int viewDistance(){

        if( restoreHealth )
            return 0;

        int distance = buff( Light.class ) != null ? super.viewDistance() : super.viewDistance() / 2 ;

        return GameMath.gate( 1, distance, 8 );
    }
 
Example 20
Source File: DewVial.java    From shattered-pixel-dungeon with GNU General Public License v3.0 2 votes vote down vote up
@Override
public void execute( final Hero hero, String action ) {

	super.execute( hero, action );

	if (action.equals( AC_DRINK )) {

		if (volume > 0) {
			
			float missingHealthPercent = 1f - (hero.HP / (float)hero.HT);
			
			//trimming off 0.01 drops helps with floating point errors
			int dropsNeeded = (int)Math.ceil((missingHealthPercent / 0.05f) - 0.01f);
			dropsNeeded = (int)GameMath.gate(1, dropsNeeded, volume);
			
			//20 drops for a full heal normally
			int heal = Math.round( hero.HT * 0.05f * dropsNeeded );
			
			int effect = Math.min( hero.HT - hero.HP, heal );
			if (effect > 0) {
				hero.HP += effect;
				hero.sprite.emitter().burst( Speck.factory( Speck.HEALING ), 1 + dropsNeeded/5 );
				hero.sprite.showStatus( CharSprite.POSITIVE, Messages.get(this, "value", effect) );
			}

			volume -= dropsNeeded;

			hero.spend( TIME_TO_DRINK );
			hero.busy();

			Sample.INSTANCE.play( Assets.Sounds.DRINK );
			hero.sprite.operate( hero.pos );

			updateQuickslot();


		} else {
			GLog.w( Messages.get(this, "empty") );
		}

	}
}