-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ECS wrapper for the sound system #590
base: develop
Are you sure you want to change the base?
Changes from all commits
ca02e42
dd523ca
55ffcc6
b92f0e0
163bf1b
c90790f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import org.destinationsol.game.DmgType; | ||
import org.destinationsol.game.SolGame; | ||
import org.destinationsol.game.SolObject; | ||
import org.destinationsol.material.MaterialType; | ||
|
||
import java.util.Arrays; | ||
|
||
|
@@ -106,4 +107,48 @@ public void playColl(SolGame game, float absImpulse, SolObject o, Vector2 positi | |
} | ||
game.getSoundManager().play(game, metal ? metalColl : rockColl, position, o, absImpulse * Const.IMPULSE_TO_COLL_VOL); | ||
} | ||
|
||
/** | ||
* Gets the damage sound associated with the given {@link MaterialType} and {@link DmgType}. If no sound is defined, | ||
* null is returned. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe return an Optional instead then for these two methods? |
||
* | ||
* @param materialType the material type of the damaged entity | ||
* @param damageType the type of damage done | ||
* @return the sound of the damage | ||
*/ | ||
public PlayableSound getHitSound(MaterialType materialType, DmgType damageType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this deserve to exist if |
||
if (damageType == DmgType.ENERGY) { | ||
if (materialType == MaterialType.METAL) { | ||
return metalEnergyHit; | ||
} | ||
if (materialType == MaterialType.ROCK) { | ||
return rockEnergyHit; | ||
} | ||
} | ||
if (damageType == DmgType.BULLET) { | ||
if (materialType == MaterialType.METAL) { | ||
return metalBulletHit; | ||
} | ||
if (materialType == MaterialType.ROCK) { | ||
return rockBulletHit; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Gets the collision sound associated with the given {@link MaterialType}. If no sound is defined, null is returned. | ||
* | ||
* @param materialType the material type of the entity | ||
* @return the sound of the collision | ||
*/ | ||
public PlayableSound getCollisionSound(MaterialType materialType) { | ||
if (materialType == MaterialType.METAL) { | ||
return metalColl; | ||
} | ||
if (materialType == MaterialType.ROCK) { | ||
return rockColl; | ||
} | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2020 The Terasology Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.destinationsol.asteroids.systems; | ||
|
||
import org.destinationsol.assets.sound.SpecialSounds; | ||
import org.destinationsol.asteroids.components.AsteroidMesh; | ||
import org.destinationsol.common.In; | ||
import org.destinationsol.common.SolMath; | ||
import org.destinationsol.entitysystem.EntitySystemManager; | ||
import org.destinationsol.entitysystem.EventReceiver; | ||
import org.destinationsol.location.components.Position; | ||
import org.destinationsol.removal.events.DeletionEvent; | ||
import org.destinationsol.removal.systems.DestructionSystem; | ||
import org.destinationsol.size.components.Size; | ||
import org.destinationsol.sound.events.SoundEvent; | ||
import org.terasology.gestalt.entitysystem.entity.EntityRef; | ||
import org.terasology.gestalt.entitysystem.event.Before; | ||
import org.terasology.gestalt.entitysystem.event.EventResult; | ||
import org.terasology.gestalt.entitysystem.event.ReceiveEvent; | ||
|
||
/** | ||
* This system plays asteroid-specific sounds. | ||
*/ | ||
public class AsteroidSoundSystem implements EventReceiver { | ||
|
||
@In | ||
private EntitySystemManager entitySystemManager; | ||
|
||
@In | ||
private SpecialSounds specialSounds; | ||
|
||
/** | ||
* When an asteroid is destroyed, this plays the asteroid destruction sound. | ||
*/ | ||
@ReceiveEvent(components = {AsteroidMesh.class, Position.class}) | ||
@Before(DestructionSystem.class) | ||
public EventResult playDeathSound(DeletionEvent event, EntityRef entity) { | ||
float volumeMultiplier = 1; | ||
if (entity.hasComponent(Size.class)) { | ||
float size = entity.getComponent(Size.class).get().size; | ||
volumeMultiplier = SolMath.clamp(size / .5f); | ||
} | ||
entitySystemManager.sendEvent(new SoundEvent(specialSounds.asteroidCrack, volumeMultiplier), entity); | ||
return EventResult.CONTINUE; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ | |
import org.destinationsol.game.SolCam; | ||
import org.destinationsol.game.SolGame; | ||
import org.destinationsol.game.SolObject; | ||
import org.destinationsol.location.components.Position; | ||
import org.terasology.gestalt.entitysystem.entity.EntityRef; | ||
|
||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
|
@@ -35,8 +37,11 @@ public class DebugHint { | |
private SolObject myOwner; | ||
private String myMsg; | ||
|
||
public DebugHint(SolObject owner, Vector2 position) { | ||
private EntityRef entity; | ||
|
||
public DebugHint(SolObject owner, EntityRef entity, Vector2 position) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this would be appropriate as an overload so you don't have to send through arbitrary null values |
||
myOwner = owner; | ||
this.entity = entity; | ||
this.position = new Vector2(position); | ||
myMsgs = new HashMap<>(); | ||
} | ||
|
@@ -66,6 +71,16 @@ public void update(SolGame game) { | |
} | ||
} | ||
|
||
if (entity != null) { | ||
if (!entity.exists()) { | ||
entity = null; | ||
} else { | ||
entity.getComponent(Position.class).ifPresent(entityPosition -> { | ||
position.set(entityPosition.position); | ||
}); | ||
} | ||
} | ||
|
||
long now = TimeUtils.millis(); | ||
boolean needsRebuild = false; | ||
Iterator<Map.Entry<String, Long>> it = myMsgs.entrySet().iterator(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is all duplicated code. Can you really not unify this?