private void doDamage(
     EntityRef entity,
     int damageAmount,
     Prefab damageType,
     EntityRef instigator,
     EntityRef directCause) {
   HealthComponent health = entity.getComponent(HealthComponent.class);
   CharacterMovementComponent characterMovementComponent =
       entity.getComponent(CharacterMovementComponent.class);
   boolean ghost = false;
   if (characterMovementComponent != null) {
     ghost = (characterMovementComponent.mode == MovementMode.GHOSTING);
   }
   if ((health != null) && !ghost) {
     int damagedAmount = health.currentHealth - Math.max(health.currentHealth - damageAmount, 0);
     health.currentHealth -= damagedAmount;
     health.nextRegenTick =
         time.getGameTimeInMs() + TeraMath.floorToInt(health.waitBeforeRegen * 1000);
     entity.saveComponent(health);
     entity.send(new OnDamagedEvent(damageAmount, damagedAmount, damageType, instigator));
     if (health.currentHealth == 0 && health.destroyEntityOnNoHealth) {
       entity.send(new DestroyEvent(instigator, directCause, damageType));
     }
   }
 }
 @Command(
     shortDescription = "Set health regen rate",
     runOnServer = true,
     requiredPermission = PermissionManager.CHEAT_PERMISSION)
 public String setRegenRate(@Sender EntityRef client, @CommandParam("rate") float rate) {
   ClientComponent clientComp = client.getComponent(ClientComponent.class);
   HealthComponent health = clientComp.character.getComponent(HealthComponent.class);
   if (health != null) {
     health.regenRate = rate;
     clientComp.character.saveComponent(health);
   }
   return "Set health regeneration rate to " + rate;
 }
 private void doHeal(EntityRef entity, int healAmount, EntityRef instigator) {
   HealthComponent health = entity.getComponent(HealthComponent.class);
   if (health != null) {
     int healedAmount =
         Math.min(health.currentHealth + healAmount, health.maxHealth) - health.currentHealth;
     health.currentHealth += healedAmount;
     entity.saveComponent(health);
     entity.send(new OnHealedEvent(healAmount, healedAmount, instigator));
     if (health.currentHealth == health.maxHealth) {
       entity.send(new FullHealthEvent(instigator));
     }
   }
 }
  @Command(
      shortDescription = "Land without breaking a leg",
      runOnServer = true,
      requiredPermission = PermissionManager.CHEAT_PERMISSION)
  public String softLanding(@Sender EntityRef client) {
    ClientComponent clientComp = client.getComponent(ClientComponent.class);
    HealthComponent health = clientComp.character.getComponent(HealthComponent.class);
    if (health != null) {
      health.fallingDamageSpeedThreshold = 85f;
      health.excessSpeedDamageMultiplier = 2f;
      clientComp.character.saveComponent(health);

      return "Soft landing mode activated";
    }

    return "";
  }
 private int regenerateHealth(HealthComponent health, int healAmount) {
   int newHeal = healAmount;
   while (time.getGameTimeInMs() >= health.nextRegenTick) {
     newHeal++;
     health.nextRegenTick = health.nextRegenTick + (long) (1000 / health.regenRate);
   }
   return newHeal;
 }
  @Command(
      shortDescription = "Restore default collision damage values",
      runOnServer = true,
      requiredPermission = PermissionManager.CHEAT_PERMISSION)
  public String restoreCollisionDamage(@Sender EntityRef client) {
    ClientComponent clientComp = client.getComponent(ClientComponent.class);

    Optional<Prefab> prefab = Assets.get(new ResourceUrn("engine:player"), Prefab.class);
    HealthComponent healthDefault = prefab.get().getComponent(HealthComponent.class);
    HealthComponent health = clientComp.character.getComponent(HealthComponent.class);
    if (health != null && healthDefault != null) {
      health.fallingDamageSpeedThreshold = healthDefault.fallingDamageSpeedThreshold;
      health.horizontalDamageSpeedThreshold = healthDefault.horizontalDamageSpeedThreshold;
      health.excessSpeedDamageMultiplier = healthDefault.excessSpeedDamageMultiplier;
      clientComp.character.saveComponent(health);
    }

    return "Normal collision damage values restored";
  }
Esempio n. 7
0
  @SuppressWarnings("incomplete-switch")
  @Override
  public void onTick(float dt) {
    switch (Spout.getPlatform()) {
      case PROXY:
      case SERVER:
        World world = head.getPosition().getWorld();
        if (!(world.getBlock(head.getPosition()).getMaterial() instanceof Water)) {
          setAir(MAX_AIR);
          return;
        }

        if (getOwner() instanceof Player
            && !getOwner().get(Human.class).getGameMode().equals(GameMode.SURVIVAL)) {
          return;
        }

        setAir(getAir() - dt);
        if (getAir() < 0) {
          // out of air; damage one heart every second
          if (damageTimer-- < 0) {
            health.damage(
                2, new BlockDamageCause(world.getBlock(head.getPosition()), DamageType.DROWN));
            damageTimer = 20;
          }
        }
        break;
      case CLIENT:
        if (!(getOwner() instanceof Player)) {
          return;
        }
        // Animate air meter
        final float maxSecsBubbles = VanillaData.AIR_SECS.getDefaultValue();
        final float secsBubbles = getData().get(VanillaData.AIR_SECS);
        if (secsBubbles == maxSecsBubbles) {
          hideBubbles();
        } else {
          HUDComponent hud = getOwner().get(HUDComponent.class);
          if (hud != null) {
            hud.getAirMeter().update();
          }
        }
        break;
    }
  }
Esempio n. 8
0
 @Override
 public boolean canTick() {
   return !health.isDead();
 }