Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add damage events #2

Merged
merged 7 commits into from May 24, 2019
Merged

Add damage events #2

merged 7 commits into from May 24, 2019

Conversation

e-aakash
Copy link
Member

Add damage events and update HealthAuthoritySystem.

Add author in module.txt. Add javadoc for public methods in
HealthAuthoritySystem. Renamed FullHealthEvent as OnFullyHealedEvent.
Copy link
Member

@syntaxi syntaxi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to leave overall comments onto the code regardless of if you have written it.
Only minor things like simplifying and splitting code (reducing duplicates)

import org.terasology.logic.characters.events.AttackEvent;
import org.terasology.logic.characters.events.HorizontalCollisionEvent;
import org.terasology.logic.characters.events.VerticalCollisionEvent;
import org.terasology.logic.health.event.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid star imports.
If IntelliJ is auto collapsing them then you can change the settings to increase the number of classes before it collapses to some high number like 999

damageEntity(event, targetEntity);
}

static void damageEntity(AttackEvent event, EntityRef targetEntity) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a separate static method to the above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any other methods using damageEntity method. So will move it into the onAttackEntity method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not check in the original Core/Health directory for usage. Turns out BlockDamageAuthoritySystem uses this method. See here
So I will have to either revert the commit or add it back in the next commit.

ghost = (characterMovementComponent.mode == MovementMode.GHOSTING);
}
if ((health != null) && !ghost) {
int damagedAmount = health.currentHealth - Math.max(health.currentHealth - damageAmount, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is overly complicated and the name is ambiguous.
Can't you replace this line with

int damagedAmount = Math.min(health.currentHealth, damageAmount)

Further more based on that it makes sense to use maxDamage or cappedDamage as the name, eliminating the confusion between damageAmount and damagedAmount

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also applies to a similar section of the doHeal method

*/
@ReceiveEvent(components = {HealthComponent.class})
public void onLand(VerticalCollisionEvent event, EntityRef entity) {
HealthComponent health = entity.getComponent(HealthComponent.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are going to be using the component, just place it into the constructor to simplify things

ie,

@ReceiveEvent
public void onLand(VerticalCollisionEvent event, EntityRef entity, HealthComponent health) {
    /* ... */
}

vel.y = 0;
float speed = vel.length();

if (speed > health.horizontalDamageSpeedThreshold) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parts of this method and the onLand method could likely be extracted into a single highSpeedDamage method


if (velocity > healthComponent.horizontalDamageSpeedThreshold) {
if (characterSounds.lastSoundTime + CharacterSoundSystem.MIN_TIME < time.getGameTimeInMs()) {
StaticSound sound = random.nextItem(characterSounds.landingSounds);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, there are a few segments of code where sounds are played, which could be extracted into a common private method

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though this can be extracted, the onCrash only plays sound on the client (by sending PlaySoundEvent), but damage plays sound on both client and server.
So should the client sound way be extracted to a private method?

Copy link
Member

@Cervator Cervator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff! Throwing in another round of fairly minor comments 👍

import org.terasology.logic.characters.events.AttackEvent;
import org.terasology.logic.characters.events.HorizontalCollisionEvent;
import org.terasology.logic.characters.events.VerticalCollisionEvent;
import org.terasology.logic.health.event.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid wildcard import - configure IntelliJ (or whatever) to no collapse into wildcard, or at least not until 9999 imports or something crazy :-)

@@ -45,7 +58,18 @@
* - BeforeHealEvent
* - (HealthComponent saved)
* - OnHealedEvent
* - FullHealthEvent (if at full health)
* - OnFullyHealedEvent (if at full health)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe

Suggested change
* - OnFullyHealedEvent (if at full health)
* - OnFullyHealedEvent (if healed to full)

@@ -140,16 +165,199 @@ private int calculateTotal(int base, TFloatList multipliers, TIntList modifiers)

}

/** Handles DoHeal event to increase health of entity */
/**
* Handles DoHeal event to increase health of entity.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe tweak this with a code tag for DoHealEvent or even just avoid using the class name, instead referring to it generally "Event handler for increasing health ..."

damageEntity(event, targetEntity);
}

static void damageEntity(AttackEvent event, EntityRef targetEntity) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing access level? Also it might be worth throwing some bits of javadoc on some of these utility methods, even if not public :)

* @param event VerticalCollisionEvent sent when falling speed threshold is crossed.
* @param entity The entity which is damaged due to falling.
*/
@ReceiveEvent(components = {HealthComponent.class})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the HealthComponent is used inside this event handler you should be able to move it from the @ReceiveEvent filter into a method parameter. That way you don't need to separately retrieve it 👍

}

/**
* Inflicts damage to entity horizontalDamageSpeedThreshold is breached.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think there's an "if" missing here somewhere


import java.util.List;

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank javadoc - leftovers from when we did a bulk remove of @author tags ages ago. Feel free to remove them (or better yet fill them in!) wherever you see them

* Method to get the amount by which health has changed.
*
* @return The amount by which health changed. This is capped (by the implementing method),
* so if the entity received 9999 damage and only had 10 health, it will be -10.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be -10 or it will have 10 subtracted from it?

import org.slf4j.LoggerFactory;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.logic.health.event.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More wildcard :-)

event.add(5);
event.multiply(2);
});
// Expected damage value is ( initial:10 + 5 ) * 2 = 30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to keep an eye out for = vs ==

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line is just calculation, so = should work.
Will keep an eye out for them

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just math, true - I tend to always use the code style setup when it is in code even if it would be legit otherwise ... 😁

Copy link
Member

@Cervator Cervator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty solid 👍

@@ -268,7 +276,7 @@ private void checkDamage(EntityRef entity, int amount, Prefab damageType, Entity
* @param characterSounds Component having sound settings.
*/
@ReceiveEvent
public void onDamaged(OnDamagedEvent event, EntityRef entity, CharacterSoundComponent characterSounds) {
public void onDamaged(OnDamagedEvent event, EntityRef entity, CharacterSoundComponent characterSounds) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super trivial but this ended up with two spaces instead of one for some reason

highSpeedDamage(speed, entity, health.horizontalDamageSpeedThreshold, health.excessSpeedDamageMultiplier);
}

private void highSpeedDamage(float speed, EntityRef entity, float threshold, float damageMultiplier) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice utility method get! 👍

Copy link
Member

@Cervator Cervator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few more comments

import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.logic.health.event.BeforeHealEvent;
import org.terasology.logic.health.event.DoDamageEvent;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is unused now

@@ -38,7 +39,6 @@
public class HealEventTest extends ModuleTestingEnvironment {

private EntityManager entityManager;
private Time time;

private static final Logger logger = LoggerFactory.getLogger(HealEventTest.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this line could be moved above the entityManager to resolve a variable order issue

player.send(new DoHealEvent(10));
assertEquals(30, player.getComponent(HealthComponent.class).currentHealth);
}

@Test
public void healNegativeTest() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh oh! This test actually fails for me! Expected 40, got 50.

Not worried about it though, merging anyway :-)

@Cervator Cervator merged commit 63d6475 into Terasology:master May 24, 2019
@e-aakash e-aakash deleted the damage branch June 1, 2019 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants