use of org.terasology.engine.logic.inventory.ItemComponent in project Terasology by MovingBlocks.
the class CharacterSystem method onItemUse.
@ReceiveEvent(components = CharacterComponent.class)
public void onItemUse(OnItemUseEvent event, EntityRef entity, CharacterHeldItemComponent characterHeldItemComponent) {
long currentTime = time.getGameTimeInMs();
if (characterHeldItemComponent.nextItemUseTime > currentTime) {
// this character is not yet ready to use another item, they are still cooling down from last use
event.consume();
return;
}
EntityRef selectedItemEntity = characterHeldItemComponent.selectedItem;
characterHeldItemComponent.lastItemUsedTime = currentTime;
characterHeldItemComponent.nextItemUseTime = currentTime;
ItemComponent itemComponent = selectedItemEntity.getComponent(ItemComponent.class);
// Add the cooldown time for the next use of this item.
if (itemComponent != null) {
// Send out this event so other systems can alter the cooldown time.
AffectItemUseCooldownTimeEvent affectItemUseCooldownTimeEvent = new AffectItemUseCooldownTimeEvent(itemComponent.cooldownTime);
entity.send(affectItemUseCooldownTimeEvent);
characterHeldItemComponent.nextItemUseTime += (long) affectItemUseCooldownTimeEvent.getResultValue();
} else {
characterHeldItemComponent.nextItemUseTime += 200;
}
entity.saveComponent(characterHeldItemComponent);
}
Aggregations