/** * Set the AI for this entity. * * @param ai */ public final void setAI(Class<? extends EntityAI2D> ai) { try { EntityAI2D aiv = ai.getConstructor(Entity2D.class).newInstance(this); this.ai = aiv; this.health = aiv.getMaxHealth(); } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { CrimsonLog.warning( "Unable to apply AI %s to entity %s because %s.", ai, this, e.getMessage()); CrimsonLog.severe(e); } }
/** * Hurt this entity. * * @param amount */ public final void damageEntity(float amount) { Entity2DHurtEvent eventHurt = new Entity2DHurtEvent(this, amount); if (!Scarlet.eventReactor.post(eventHurt)) { return; } float newAmount = eventHurt.amt; if (ai != null) { if (ai.canHostTakeDamage()) { ai.onHostDamaged(newAmount); if (ai.getCurrentHealth() <= 0) { setDead(); } } return; } health -= newAmount; if (health < 0) { if (!Scarlet.eventReactor.post(new Entity2DKilledEvent(this))) { return; } setDead(); } }
/** Stops the AI if it has been registered. */ public final void killAIIfApplicable() { if (ai == null) { return; } ai.kill(); }
/** Starts the AI if it has been registered. */ public final void initAIIfApplicable() { if (ai == null) { return; } ai.spawn(); }