[SOLVED] Appropriate way to show damage numbers

Hello.

In my game I have bullets, when the bullets hits some enemies I want the show the damage on top of the enemy.

I created a logic but I don’t know if it is the best choice, since sometimes I have a lot of bullets and damage in the screen.

  • I’m Using libGDX, target is mobile.
  • Bullet: I create bullets using Pool and Poolable.
  • When the bullet hits the enemy, the enemy’s HP is decreased and I add the current position and the damage received to an Array.
  • In draw() method I just draw the numbers and increase the Y position for some motion and if the Y position is bigger than the start position + 30, I remove from the list.

Everything is working fine, but what is the best way to deal with this values (position and damage) for each damage, since I have a lot of damage created everytime? I’m going to use Pool and Poolable again to re-use objs as I did with Bullets, but I’m here first to know if I’m doing right.

public class DamageAnimation
{
	private int damage;
	private int x;
	private int y;
	private int max;
	
	public DamageAnimation(int damage, int x, int y)
	{
		this.damage = damage;
		this.x = x;
		this.y = y;
		max = y+30;
	}
	
	public int getX()
	{
		return x;
	}
	
	public int getY()
	{
		return y++;
	}
	
	public int getDamage()
	{
		return damage;
	}
	
	public int getMax()
	{
		return max;
	}
}

n = listDamage.size;
for (int i=0; i<n; i++)
{
	DamageAnimation damageDraw = listDamage.get(i);
	fontTest.draw(batch, String.valueOf(damageDraw.getDamage()), damageDraw.getX(),
			damageDraw.getY());
	if (damageDraw.getY() > damageDraw.getMax())
		listDamage.removeValue(damageDraw, true);
}

If a player takes damage more frequent than it takes a single “damage animation” to play, one idea is to reset the position and the transparency (if any) of the damage animation, whenever it was not fully played and the player took damage again, by accumulating the total damage throughout the animation.
So, if the player takes 5 “damage” for the first time, a “5” would appear on top of him and starts lifting in the air, then half a second later the same player takes another 5 of “damage”, then the damage animation from the initial hit, which is still playing, would immediately be reset to the first animation frame, but now showing a “10” instead of a “5” (or instead of two successive “5”'s).
So, for this you must know which DamageAnimation belongs to which player.

If the upward motion is of a constant speed then the sprites will be expiring in FIFO order, aka use a queue/deque:


while queue.peek().shouldExpire() // y > max
    queue.pop(); // constant time

foreach sprite in queue
    draw_and_incrementY(sprite); // assorted processing

If you use an Array, then iterate in reverse to have correct removals, currently you’re skipping sprites.
Also, don’t removeValue, that’s a linear search. Just remove the current index.
There’s also the double buffer technique, iterate over array placing non-expired entries into 2nd array, then process 2nd array and clear old one, and swap.

Hello,

Thank you so much, the first and second tip solved my “ghost” issue, I was skipping sprites.

I’m trying to understand the “double buffer technique”, so I create a variable to store a conditional value to know when I can swap arrays. Pseudo-code below.

//Max
int maxArraySize = 10;


//-------


//When HIT
array1.add(new Damage(damage, x, y))


//-------


//Swap 1 --> 2
if (array1.size > maxArraySize)
{
	changeToArray2(array1);
}

//Draw
if (array1.size > 0)
{
	n = array1.size;
	for(int i=n-1; i>-1; i--)
	{
		Damage damage = Array1.get(i);
		//Draw and Increment y code...
		if (damage.y > damage.max)
			array1.removeIndex(i);
	}
}
if (array2.size > 0)
{
	n = array2.size;
	for(int i=n-1; i>-1; i--)
	{
		Damage damage = array2.get(i);
		//Draw and Increment y code...
		if (damage.y > damage.max)
			array2.removeIndex(i);
	}
}


//--------


//Methods
public void changeToArray2(Array<Damage> array)
{
	array2 = array;
	array1.clear();
}

Here is a post describing the double buffered technique: http://www.java-gaming.org/topics/concurrentmodificationexception-when-removing-an-item-from-an-arraylist/32939/msg/308953/view.html#msg308953

Now I understand, instead of use .remove(i) for each “dead” entity, I just leave the “dead” entities in the first Array and remove with a single .clear(), then I swap to return all the entities to the first array again.

Thank you.

Yep, it’s pretty much the best way if a more specialized data structure isn’t applicable.

Tangential, but this is also the basis for how many garbage collectors are implemented, look through all the objects and copy live ones, the dead objects are just overwritten on the next sweep.