Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (798)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (865)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED] Appropriate way to show damage numbers  (Read 3188 times)
0 Members and 1 Guest are viewing this topic.
Offline craftm

JGO Coder


Medals: 15
Projects: 1


_Keep Trying


« Posted 2015-08-17 22:23:58 »

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<DamageAnimation>.
- 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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
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;
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
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);
}

_ Wink
Offline KaiHH

JGO Kernel


Medals: 708



« Reply #1 - Posted 2015-08-17 22:33:30 »

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.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #2 - Posted 2015-08-17 22:45:21 »

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

1  
2  
3  
4  
5  
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline craftm

JGO Coder


Medals: 15
Projects: 1


_Keep Trying


« Reply #3 - Posted 2015-08-18 00:26:33 »

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

1  
2  
3  
4  
5  
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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
//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();
}

_ Wink
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #4 - Posted 2015-08-18 00:31:02 »

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
Offline craftm

JGO Coder


Medals: 15
Projects: 1


_Keep Trying


« Reply #5 - Posted 2015-08-18 00:42:25 »

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.

_ Wink
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #6 - Posted 2015-08-18 00:49:49 »

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.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (34 views)
2019-09-04 15:33:17

hadezbladez (3979 views)
2018-11-16 13:46:03

hadezbladez (1444 views)
2018-11-16 13:41:33

hadezbladez (3986 views)
2018-11-16 13:35:35

hadezbladez (769 views)
2018-11-16 13:32:03

EgonOlsen (4085 views)
2018-06-10 19:43:48

EgonOlsen (4664 views)
2018-06-10 19:43:44

EgonOlsen (2753 views)
2018-06-10 19:43:20

DesertCoockie (3647 views)
2018-05-13 18:23:11

nelsongames (3849 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!