Java Code Examples for com.watabou.utils.GameMath

The following examples show how to use com.watabou.utils.GameMath. These examples are extracted from open source projects. 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
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 2
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
@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
@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
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
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
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
@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
@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
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 Project: remixed-dungeon   Source File: Visual.java    License: 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 Project: PD-classes   Source File: Visual.java    License: 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
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
@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
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
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
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
@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 19
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 20
@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 21
@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 Project: pixel-dungeon   Source File: CellSelector.java    License: 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 23
Source Project: shattered-pixel-dungeon   Source File: Hero.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void hitSound(float pitch) {
	if ( belongings.weapon != null ){
		belongings.weapon.hitSound(pitch);
	} else if (RingOfForce.getBuffedBonus(this, RingOfForce.Force.class) > 0) {
		//pitch deepens by 2.5% (additive) per point of strength, down to 75%
		super.hitSound( pitch * GameMath.gate( 0.75f, 1.25f - 0.025f*STR(), 1f) );
	} else {
		super.hitSound(pitch * 1.1f);
	}
}
 
Example 24
@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
@Override
protected void onScroll( ScrollEvent event ) {
	float diff = event.amount/10f;
	
	//scale zoom difference so zooming is consistent
	diff /= ((camera.zoom+1)/camera.zoom)-1;
	diff = Math.min(1, diff);
	mouseZoom = GameMath.gate( PixelScene.minZoom, mouseZoom - diff, PixelScene.maxZoom );
	
	zoom( Math.round(mouseZoom) );
}
 
Example 26
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 27
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
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 29
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 30
Source Project: remixed-dungeon   Source File: CellSelector.java    License: 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 );	
		}	
	}
	
}