2015-05-10 21:26:12 +02:00
|
|
|
package com.saltosion.gladiator.listeners;
|
2015-05-10 20:07:37 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import com.badlogic.ashley.core.ComponentMapper;
|
|
|
|
import com.badlogic.ashley.core.Entity;
|
|
|
|
import com.saltosion.gladiator.components.CCombat;
|
2015-05-10 21:26:12 +02:00
|
|
|
import com.saltosion.gladiator.util.AppUtil;
|
2015-05-10 20:07:37 +02:00
|
|
|
import com.saltosion.gladiator.util.Direction;
|
|
|
|
|
|
|
|
public class SwingHitboxListener implements CollisionListener {
|
|
|
|
|
|
|
|
private ArrayList<Entity> hitEntities = new ArrayList<Entity>();
|
|
|
|
private ComponentMapper<CCombat> cm = ComponentMapper.getFor(CCombat.class);
|
|
|
|
private Entity source;
|
|
|
|
|
|
|
|
public SwingHitboxListener(Entity source) {
|
|
|
|
this.source = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void collision(Direction side, Entity host, Entity other) {
|
|
|
|
if (other.equals(source) || hitEntities.contains(other)) {
|
|
|
|
return; // These entities don't need to take damage
|
|
|
|
}
|
|
|
|
hitEntities.add(other);
|
|
|
|
|
|
|
|
CCombat otherCombat = cm.get(other);
|
|
|
|
if (otherCombat == null) {
|
|
|
|
System.out.println("This entity doesn't have combat!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int damage = cm.get(source).getDamage();
|
|
|
|
otherCombat.health -= damage;
|
2015-05-10 21:26:12 +02:00
|
|
|
System.out.println(String.format("Entity was hit for %d damage, it now has only %d health left!",
|
|
|
|
damage, otherCombat.health));
|
|
|
|
if (otherCombat.health <= 0) {
|
|
|
|
AppUtil.engine.removeEntity(other);
|
|
|
|
}
|
2015-05-10 20:07:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|