com.watabou.utils.GameMath Java Examples

The following examples show how to use com.watabou.utils.GameMath. 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: 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 #2
Source File: CellSelector.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
private float zoom( float value ) {

		value = GameMath.gate( PixelScene.minZoom, value, PixelScene.maxZoom );
		SPDSettings.zoom((int) (value - PixelScene.defaultZoom));
		camera.zoom( value );

		//Resets character sprite positions with the new camera zoom
		//This is important as characters are centered on a 16x16 tile, but may have any sprite size
		//This can lead to none-whole coordinate, which need to be aligned with the zoom
		for (Char c : Actor.chars()){
			if (c.sprite != null && !c.sprite.isMoving){
				c.sprite.point(c.sprite.worldToCamera(c.pos));
			}
		}

		return value;
	}
 
Example #3
Source File: CellSelector.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onDrag( PointerEvent event ) {

	if (pinching) {

		float curSpan = PointF.distance( curEvent.current, another.current );
		float zoom = (startZoom * curSpan / startSpan);
		camera.zoom( GameMath.gate(
			PixelScene.minZoom,
				zoom - (zoom % 0.1f),
			PixelScene.maxZoom ) );

	} else {
	
		if (!dragging && PointF.distance( event.current, event.start ) > dragThreshold) {
			
			dragging = true;
			lastPos.set( event.current );
			
		} else if (dragging) {
			camera.shift( PointF.diff( lastPos, event.current ).invScale( camera.zoom ) );
			lastPos.set( event.current );
		}
	}
	
}
 
Example #4
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 #5
Source File: SewerPipeRoom.java    From shattered-pixel-dungeon 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 #6
Source File: HallwayRoom.java    From shattered-pixel-dungeon 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 #7
Source File: TunnelRoom.java    From shattered-pixel-dungeon 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+1, c.x, right-1);
	c.y = (int)GameMath.gate(top+1, c.y, bottom-1);

	return c;
}
 
Example #8
Source File: Stone.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int proc(Armor armor, Char attacker, Char defender, int damage) {
	
	testing = true;
	float evasion = defender.defenseSkill(attacker);
	float accuracy = attacker.attackSkill(defender);
	testing = false;
	
	float hitChance;
	if (evasion >= accuracy){
		hitChance = (accuracy/evasion)/2f;
	} else {
		hitChance = 1f - (evasion/accuracy)/2f;
	}
	
	//75% of dodge chance is applied as damage reduction
	// we clamp in case accuracy or evasion were negative
	hitChance = GameMath.gate(0.25f, (1f + 3f*hitChance)/4f, 1f);
	
	damage = (int)Math.ceil(damage * hitChance);
	
	return damage;
}
 
Example #9
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 #10
Source File: DungeonTilemap.java    From shattered-pixel-dungeon-gdx 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 #11
Source File: Visual.java    From remixed-dungeon with GNU General Public License v3.0 6 votes vote down vote up
protected void updateMotion() {
	
	float elapsed = Game.elapsed;
	
	float d = (GameMath.speed( speed.x, acc.x ) - speed.x) / 2;
	speed.x += d;
	x += speed.x * elapsed;
	speed.x += d;
	
	d = (GameMath.speed( speed.y, acc.y ) - speed.y) / 2;
	speed.y += d;
	y += speed.y * elapsed;
	speed.y += d;
	
	angle += angularSpeed * elapsed;
}
 
Example #12
Source File: Visual.java    From PD-classes with GNU General Public License v3.0 6 votes vote down vote up
protected void updateMotion() {
	
	float elapsed = Game.elapsed;
	
	float d = (GameMath.speed( speed.x, acc.x ) - speed.x) / 2;
	speed.x += d;
	x += speed.x * elapsed;
	speed.x += d;
	
	d = (GameMath.speed( speed.y, acc.y ) - speed.y) / 2;
	speed.y += d;
	y += speed.y * elapsed;
	speed.y += d;
	
	angle += angularSpeed * elapsed;
}
 
Example #13
Source File: CellSelector.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 6 votes vote down vote up
private float zoom( float value ) {

		value = GameMath.gate( PixelScene.minZoom, value, PixelScene.maxZoom );
		SPDSettings.zoom((int) (value - PixelScene.defaultZoom));
		camera.zoom( value );

		//Resets character sprite positions with the new camera zoom
		//This is important as characters are centered on a 16x16 tile, but may have any sprite size
		//This can lead to none-whole coordinate, which need to be aligned with the zoom
		for (Char c : Actor.chars()){
			if (c.sprite != null && !c.sprite.isMoving){
				c.sprite.point(c.sprite.worldToCamera(c.pos));
			}
		}

		return value;
	}
 
Example #14
Source File: CellSelector.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onDrag( NoosaInputProcessor.Touch t ) {

	if (pinching) {

		float curSpan = PointF.distance( touch.current, another.current );
		float zoom = (startZoom * curSpan / startSpan);
		camera.zoom( GameMath.gate(
				PixelScene.minZoom,
				zoom - (zoom % 0.1f),
				PixelScene.maxZoom ) );

	} else {
	
		if (!dragging && PointF.distance( t.current, t.start ) > dragThreshold) {
			
			dragging = true;
			lastPos.set( t.current );
			
		} else if (dragging) {
			camera.shift( PointF.diff( lastPos, t.current ).invScale( camera.zoom ) );
			lastPos.set( t.current );
		}
	}
	
}
 
Example #15
Source File: SewerPipeRoom.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 #16
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 #17
Source File: TunnelRoom.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+1, c.x, right-1);
	c.y = (int)GameMath.gate(top+1, c.y, bottom-1);

	return c;
}
 
Example #18
Source File: Visual.java    From YetAnotherPixelDungeon with GNU General Public License v3.0 6 votes vote down vote up
protected void updateMotion() {
	
	float elapsed = Game.elapsed;
	
	float d = (GameMath.speed( speed.x, acc.x ) - speed.x) / 2;
	speed.x += d;
	x += speed.x * elapsed;
	speed.x += d;
	
	d = (GameMath.speed( speed.y, acc.y ) - speed.y) / 2;
	speed.y += d;
	y += speed.y * elapsed;
	speed.y += d;
	
	angle += angularSpeed * elapsed;
}
 
Example #19
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 #20
Source File: AlchemistsToolkit.java    From shattered-pixel-dungeon-gdx 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 #21
Source File: CellSelector.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onDrag( Touch t ) {
	 
	camera.target = null;

	if (pinching) {

		float curSpan = PointF.distance( touch.current, another.current );
		camera.zoom( GameMath.gate( 
			PixelScene.minZoom, 
			startZoom * curSpan / startSpan, 
			PixelScene.maxZoom ) );

	} else {
	
		if (!dragging && PointF.distance( t.current, t.start ) > dragThreshold) {
			
			dragging = true;
			lastPos.set( t.current );
			
		} else if (dragging) {
			camera.scroll.offset( PointF.diff( lastPos, t.current ).invScale( camera.zoom ) );
			lastPos.set( t.current );	
		}	
	}
	
}
 
Example #22
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 #23
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 #24
Source File: Berserk.java    From shattered-pixel-dungeon 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) {
			int dmg = 1 + (int)Math.ceil(target.shielding() * 0.1f);
			if (buff != null && buff.shielding() > 0) {
				buff.absorbDamage(dmg);
			} else {
				//if there is no shield buff, or it is empty, then try to remove from other shielding buffs
				for (ShieldBuff s : target.buffs(ShieldBuff.class)){
					dmg = s.absorbDamage(dmg);
					if (dmg == 0) break;
				}
			}
			if (target.shielding() <= 0) {
				target.die(this);
				if (!target.isAlive()) Dungeon.fail(this.getClass());
			}
		} else {
			state = State.RECOVERING;
			levelRecovery = LEVEL_RECOVER_START;
			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();
		}
	}
	spend(TICK);
	return true;
}
 
Example #25
Source File: Generator.java    From shattered-pixel-dungeon 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 #26
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 #27
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 #28
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 #29
Source File: Generator.java    From shattered-pixel-dungeon-gdx 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 #30
Source File: CellSelector.java    From unleashed-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onDrag( Touch t ) {
	 
	camera.target = null;

	if (pinching) {

		float curSpan = PointF.distance( touch.current, another.current );
		camera.zoom( GameMath.gate(
			PixelScene.minZoom,
			startZoom * curSpan / startSpan,
			PixelScene.maxZoom ) );

	} else {
	
		if (!dragging && PointF.distance( t.current, t.start ) > dragThreshold) {
			
			dragging = true;
			lastPos.set( t.current );
			
		} else if (dragging) {
			camera.scroll.offset( PointF.diff( lastPos, t.current ).invScale( camera.zoom ) );
			lastPos.set( t.current );
		}
	}
	
}