Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Simple melee attacks, looking for advice  (Read 1529 times)
0 Members and 1 Guest are viewing this topic.
Offline 355333532

Junior Newbie





« Posted 2012-10-18 15:11:13 »

I have run into a problem calculating attacks.
If the player is attacking and an object intersects his attack hit box, I call the damage() function of the object.
Problem is, this happens every loop, and the object is damaged several times during a single attack animation.

I searched the forums for a solution, but I didn't find anything. My ideas:
  • I thought I might end the attack after causing damage once, but that makes cleaving attacks impossible.
  • I thought of "tagging" the enemy as damaged, and testing for this before applying more damage, but I would have to reset each object between attack animations.
  • My best idea is to use a timer, allowing damage() to be called only so often, so there is a recovery period after being attacked, during which the object cannot take damage. Problem is, the recovery period cannot be shorter than the animation. If it is too short, the attack will still hit multiple times. If it is too long, the next attack might not cause damage. I don't want to use a set animation length. Maybe I can pass the animation length to the object when it is damaged...

Any thoughts? How do you handle this kind of attack?
Offline Varkas
« Reply #1 - Posted 2012-10-18 15:16:41 »

The idea of a cooldown time looks appealing, this allows to have attacks which repeat faster or slwer.

But I'd just clear the player attack hit frame once every intersecting object has been damaged. Unless it is a cleaving attachh for which you seem to want to have just the effect of repeated attacks?

Myself, I handle attacks in the player input code, and if an attack is triggered once it will only execute once. That seemed simple enough and worked fairly well for melee attacks. If you have a flamethrower which does time-based damage it will be something different ...

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline 355333532

Junior Newbie





« Reply #2 - Posted 2012-10-18 15:44:23 »

I'll elaborate with an example scenario.

My designer decides to give the player a slashing sword attack that can definitely hit multiple enemies. He sets the hit box of this attack so that it will hit anything in front of the player, where the sword will swing. It takes one full second for the sword to go from the initial position to the end of the swing.

The player is testing this, and two enemies are in front of him. One of the enemies is already within the hit box, and the other is fast approaching. So the player hits the attack button, and the sword swings. The second enemy steps into the hit box while the sword is swinging, exactly .5 sec after the attack is initiated. The pixels of the sword make clear contact with both enemies.

Currently, here is what happens: The game logic is updated at 60 frames per second. The first enemy, who was within the hit box for the entire length of the attack, is damaged 60 times. The second enemy, who is only present for half of the attack, is damaged 30 times. These numbers might be off, but that is essentially what happens.

Ideally, I would like to damage each enemy only once.

Quote
But I'd just clear the player attack hit frame once every intersecting object has been damaged

This is attractive to me, but in my example scenario the second enemy would not be damaged. Perhaps that is correct, and an enemy should not be damaged if they were not in range at the exact moment the attack begins. But visually, the player still sees the sword make contact.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #3 - Posted 2012-10-18 17:58:47 »

You can save a list of damaged entities in your attack instance. If enemy is already in list, do not damage it again.

Offline theagentd
« Reply #4 - Posted 2012-10-19 00:48:02 »

You can save a list of damaged entities in your attack instance. If enemy is already in list, do not damage it again.
This. A HashSet can be used for it.

Myomyomyo.
Offline StumpyStrust
« Reply #5 - Posted 2012-10-19 01:51:07 »

....I think it would be much easier to just have a boolean in enemies that means they were hit or have the attack have a list (use your favorite data type) of enemies hit in this cycle and just check if an enemies is in the list, if so, don't damage.

So for every swing/animation cycle, reset , check for collision, if collision, damage() and set so we wont check this guy for the remainder of this animation, and at the end reset the list. You could easily add a int to cap the maximum enemies the weapon can hit at one time. That way a cleaver/greatsword would be much more useful for big mobs where a fast short sword or dagger would be great for a single target.

Offline theagentd
« Reply #6 - Posted 2012-10-19 01:55:29 »

But what if two people attack at the same time? Surely monsters and allies will be able to cleave too. With a boolean per enemy that's impossible. You also have the performance problem of resetting the boolean on every single enemy after each attack.

Myomyomyo.
Offline 355333532

Junior Newbie





« Reply #7 - Posted 2012-10-19 07:28:51 »

You can save a list of damaged entities in your attack instance. If enemy is already in list, do not damage it again.
You can save a list of damaged entities in your attack instance. If enemy is already in list, do not damage it again.
This. A HashSet can be used for it.

That's great! Thank you guys so much. I didn't think to create an instance of the attack. Rather, my attack consisted of a box that followed the player around and a player state that determined whether to check for objects within the box. Now I feel silly.
Offline StumpyStrust
« Reply #8 - Posted 2012-10-19 07:33:21 »

I was leaning more towards the list as it will not have to reset all enemies after each attack but w/e.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (46 views)
2014-10-17 03:59:02

Norakomi (37 views)
2014-10-16 15:22:06

Norakomi (28 views)
2014-10-16 15:20:20

lcass (32 views)
2014-10-15 16:18:58

TehJavaDev (62 views)
2014-10-14 00:39:48

TehJavaDev (62 views)
2014-10-14 00:35:47

TehJavaDev (52 views)
2014-10-14 00:32:37

BurntPizza (70 views)
2014-10-11 23:24:42

BurntPizza (40 views)
2014-10-11 23:10:45

BurntPizza (82 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!