-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnterCombatComponent.java
54 lines (43 loc) · 1.76 KB
/
EnterCombatComponent.java
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
package com.csse3200.game.components;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.csse3200.game.GdxGame;
import com.csse3200.game.physics.BodyUserData;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.physics.components.HitboxComponent;
/**
* When this entity touches a valid enemy's hitbox, deal damage to them and apply a knockback.
*
* <p>Requires CombatStatsComponent, HitboxComponent on this entity.
*
* <p>Damage is only applied if target entity has a CombatStatsComponent. Knockback is only applied
* if target entity has a PhysicsComponent.
*/
public class EnterCombatComponent extends Component {
private final short targetLayer;
private HitboxComponent hitboxComponent;
private final GdxGame game;
/**
* Create a component which attacks entities on collision, without knockback.
* @param targetLayer The physics layer of the target's collider.
*/
public EnterCombatComponent(short targetLayer, GdxGame game) {
this.targetLayer = targetLayer;
this.game = game;
}
@Override
public void create() {
entity.getEvents().addListener("collisionStart", this::onCollisionStart);
hitboxComponent = entity.getComponent(HitboxComponent.class);
}
private void onCollisionStart(Fixture me, Fixture other) {
if (hitboxComponent.getFixture() != me) {
// Not triggered by hitbox, ignore
return;
}
if (!PhysicsLayer.contains(targetLayer, other.getFilterData().categoryBits)) {
// Doesn't match our target layer, ignore
return;
}
game.enterCombatScreen(entity, ((BodyUserData) other.getBody().getUserData()).entity);
}
}