Search in sources :

Example 1 with ItemComponent

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);
}
Also used : ItemComponent(org.terasology.engine.logic.inventory.ItemComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Aggregations

EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)1 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)1 ItemComponent (org.terasology.engine.logic.inventory.ItemComponent)1