@ReceiveEvent public void onDeath(DoDestroyEvent event, EntityRef entity, CharacterSoundComponent characterSounds) { if (characterSounds.deathSounds.size() > 0) { StaticSound sound = random.nextItem(characterSounds.deathSounds); entity.send(new PlaySoundEvent(entity, sound, characterSounds.deathVolume)); } }
@ReceiveEvent public void onRespawn(OnPlayerRespawnedEvent event, EntityRef character, CharacterSoundComponent characterSounds) { if (characterSounds.respawnSounds.size() > 0) { StaticSound sound = random.nextItem(characterSounds.respawnSounds); character.send(new PlaySoundEvent(character, sound, characterSounds.respawnVolume)); } }
@ReceiveEvent public void onJump(JumpEvent event, EntityRef entity, CharacterSoundComponent characterSounds) { if (characterSounds.lastSoundTime + MIN_TIME < time.getGameTimeInMs()) { StaticSound sound = null; if (characterSounds.jumpSounds.size() > 0) { sound = random.nextItem(characterSounds.jumpSounds); } else if (characterSounds.footstepSounds.size() > 0) { sound = random.nextItem(characterSounds.footstepSounds); } if (sound != null) { entity.send(new PlaySoundEvent(entity, sound, characterSounds.jumpVolume)); characterSounds.lastSoundTime = time.getGameTimeInMs(); entity.saveComponent(characterSounds); } } }
@ReceiveEvent public void onPlayerDeath(PlayerDeathEvent event, EntityRef character) { CharacterSoundComponent characterSounds = character.getComponent(CharacterSoundComponent.class); if (characterSounds.deathSounds.size() > 0) { StaticSound sound = random.nextItem(characterSounds.deathSounds); character.send(new PlaySoundEvent(character, sound, characterSounds.deathVolume)); } }
@ReceiveEvent public void onLanded(VerticalCollisionEvent event, EntityRef entity, CharacterSoundComponent characterSounds) { Vector3f velocity = event.getVelocity(); float soundVolumeModifier = (velocity.y * -1 - LANDING_VELOCITY_THRESHOLD) * LANDING_VOLUME_MODIFIER; if (soundVolumeModifier <= 0f) { return; } if (soundVolumeModifier > LANDING_VOLUME_MAX) { soundVolumeModifier = LANDING_VOLUME_MAX; } if (characterSounds.lastSoundTime + MIN_TIME < time.getGameTimeInMs()) { StaticSound sound = null; if (characterSounds.landingSounds.size() > 0) { sound = random.nextItem(characterSounds.landingSounds); } else if (characterSounds.footstepSounds.size() > 0) { sound = random.nextItem(characterSounds.footstepSounds); } if (sound != null) { entity.send(new PlaySoundEvent(entity, sound, characterSounds.landingVolume * soundVolumeModifier)); characterSounds.lastSoundTime = time.getGameTimeInMs(); entity.saveComponent(characterSounds); } } }
@ReceiveEvent public void onSwimStroke(SwimStrokeEvent event, EntityRef entity, CharacterSoundComponent characterSounds) { if (characterSounds.swimSounds.size() > 0 && characterSounds.lastSoundTime + MIN_TIME < time.getGameTimeInMs()) { StaticSound sound = random.nextItem(characterSounds.swimSounds); entity.send(new PlaySoundEvent(entity, sound, characterSounds.swimmingVolume)); characterSounds.lastSoundTime = time.getGameTimeInMs(); entity.saveComponent(characterSounds); } }
private void onPlayBlockDamageCommon(BlockFamily family, Vector3f location, EntityRef entityRef) { createBlockParticleEffect(family, location); BlockSounds sounds = family.getArchetypeBlock().getSounds(); if (!sounds.getDigSounds().isEmpty()) { StaticSound sound = random.nextItem(sounds.getDigSounds()); entityRef.send(new PlaySoundEvent(sound, 1f)); } }
@ReceiveEvent public void onDamaged(OnDamagedEvent event, EntityRef entity, CharacterSoundComponent characterSounds) { if (characterSounds.lastSoundTime + CharacterSoundSystem.MIN_TIME < time.getGameTimeInMs()) { // play the sound of damage hitting the character for everyone DamageSoundComponent damageSounds = event.getType().getComponent(DamageSoundComponent.class); if (damageSounds != null && !damageSounds.sounds.isEmpty()) { StaticSound sound = random.nextItem(damageSounds.sounds); if (sound != null) { entity.send(new PlaySoundEvent(sound, 1f)); } } // play the sound of a client's character being damaged to the client if (!characterSounds.damageSounds.isEmpty()) { StaticSound sound = random.nextItem(characterSounds.damageSounds); if (sound != null) { entity.send(new PlaySoundForOwnerEvent(sound, characterSounds.damageVolume)); } } characterSounds.lastSoundTime = time.getGameTimeInMs(); entity.saveComponent(characterSounds); } }
@ReceiveEvent public void onEnterBlock(OnEnterBlockEvent event, EntityRef entity, CharacterSoundComponent characterSounds) { // only play this sound if the feet hit the water if (event.getCharacterRelativePosition().y == 0 && characterSounds.lastSoundTime + MIN_TIME < time.getGameTimeInMs()) { boolean oldBlockIsLiquid = event.getOldBlock().isLiquid(); boolean newBlockIsLiquid = event.getNewBlock().isLiquid(); StaticSound sound = null; if (!oldBlockIsLiquid && newBlockIsLiquid) { sound = random.nextItem(characterSounds.enterWaterSounds); } else if (oldBlockIsLiquid && !newBlockIsLiquid) { sound = random.nextItem(characterSounds.leaveWaterSounds); } if (sound != null) { entity.send(new PlaySoundEvent(entity, sound, characterSounds.diveVolume)); characterSounds.lastSoundTime = time.getGameTimeInMs(); entity.saveComponent(characterSounds); } } }
@ReceiveEvent public void onFootstep(FootstepEvent event, EntityRef entity, LocationComponent locationComponent, CharacterSoundComponent characterSounds) { List<StaticSound> footstepSounds = characterSounds.footstepSounds; // Check if the block the character is standing on has footstep sounds Vector3i blockPos = new Vector3i(locationComponent.getLocalPosition()); blockPos.y--; // The block *below* the character's feet is interesting to us Block block = worldProvider.getBlock(blockPos); if (block != null) { if (block.getSounds() == null) { logger.error("Block '{}' has no sounds", block.getURI()); } else if (!block.getSounds().getStepSounds().isEmpty()) { footstepSounds = block.getSounds().getStepSounds(); } } if (footstepSounds.size() > 0 && characterSounds.lastSoundTime + MIN_TIME < time.getGameTimeInMs()) { StaticSound sound = random.nextItem(footstepSounds); entity.send(new PlaySoundEvent(entity, sound, characterSounds.footstepVolume)); characterSounds.lastSoundTime = time.getGameTimeInMs(); entity.saveComponent(characterSounds); } }
/** * This method plays sound in prediction for preventing not playing song because of server lags * @param event contains the details for the predicted event, used here for location purposes * @param entity is source of the playsound */ @ReceiveEvent(components = {PlaySoundActionComponent.class}) public void onActivationPredicted(ActivationPredicted event, EntityRef entity) { PlaySoundActionComponent playSound = entity.getComponent(PlaySoundActionComponent.class); StaticSound sound = random.nextItem(playSound.sounds); if (sound != null) { Vector3f pos = null; switch (playSound.relativeTo) { case Target: pos = event.getTargetLocation(); break; default: pos = event.getInstigatorLocation(); break; } if (pos == null) { pos = event.getOrigin(); } audioManager.playSound(sound, pos, playSound.volume, AudioManager.PRIORITY_NORMAL); } }
@ReceiveEvent public void onCrash(HorizontalCollisionEvent event, EntityRef entity, CharacterSoundComponent characterSounds, HealthComponent healthComponent) { Vector3f horizVelocity = new Vector3f(event.getVelocity()); horizVelocity.y = 0; float velocity = horizVelocity.length(); if (velocity > healthComponent.horizontalDamageSpeedThreshold) { if (characterSounds.lastSoundTime + CharacterSoundSystem.MIN_TIME < time.getGameTimeInMs()) { StaticSound sound = random.nextItem(characterSounds.landingSounds); if (sound != null) { entity.send(new PlaySoundEvent(sound, characterSounds.landingVolume)); characterSounds.lastSoundTime = time.getGameTimeInMs(); entity.saveComponent(characterSounds); } } } }
/** * This method plays sound if it wasn't played by prediction system * @param event contains the details for the active event, used here for location purposes * @param entity is source of the playsound */ @ReceiveEvent(components = {PlaySoundActionComponent.class}) public void onActivate(ActivateEvent event, EntityRef entity) { if (event.getInstigator().equals(localPlayer.getCharacterEntity())) { return; // owner has heard sound from prediction } PlaySoundActionComponent playSound = entity.getComponent(PlaySoundActionComponent.class); StaticSound sound = random.nextItem(playSound.sounds); if (sound != null) { Vector3f pos = null; switch (playSound.relativeTo) { case Target: pos = event.getTargetLocation(); break; default: pos = event.getInstigatorLocation(); break; } if (pos == null) { pos = event.getOrigin(); } audioManager.playSound(sound, pos, playSound.volume, AudioManager.PRIORITY_NORMAL); } } }
private void commonDestroyed(DoDestroyEvent event, EntityRef entity, Block block) { entity.send(new CreateBlockDropsEvent(event.getInstigator(), event.getDirectCause(), event.getDamageType())); BlockDamageModifierComponent blockDamageModifierComponent = event.getDamageType().getComponent(BlockDamageModifierComponent.class); // TODO: Configurable via block definition if (blockDamageModifierComponent == null || !blockDamageModifierComponent.skipPerBlockEffects) { // dust particle effect if (entity.hasComponent(LocationComponent.class) && block.isDebrisOnDestroy()) { EntityBuilder dustBuilder = entityManager.newBuilder("core:dustEffect"); // TODO: particle system stuff should be split out better - this is effectively a stealth dependency on Core from the engine if (dustBuilder.hasComponent(LocationComponent.class)) { dustBuilder.getComponent(LocationComponent.class).setWorldPosition(entity.getComponent(LocationComponent.class).getWorldPosition()); dustBuilder.build(); } } // sound to play for destroyed block BlockSounds sounds = block.getSounds(); if (!sounds.getDestroySounds().isEmpty()) { StaticSound sound = random.nextItem(sounds.getDestroySounds()); entity.send(new PlaySoundEvent(sound, 0.6f)); } } }