soundmanager.java.svn-base

来自「一个JAVA程序员的游戏」· SVN-BASE 代码 · 共 117 行

SVN-BASE
117
字号
/*
 * SoundManager.java
 *
 * Created on 4. Februar 2007, 21:01
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package kanjitori.util;

import com.jme.system.DisplaySystem;
import com.jmex.sound.openAL.SoundSystem;
import kanjitori.Player;
import kanjitori.event.BotKilledEventManager;
import kanjitori.event.ItemCollectedEventManager;
import kanjitori.event.Listener;
import kanjitori.event.PlayerRelocateEventManager;
import kanjitori.graphics.Thing;
import kanjitori.graphics.bot.Bot;
import kanjitori.graphics.item.Item;

/**
 *
 * @author Pirx
 */
public class SoundManager {
    
    private final static SoundManager INSTANCE = new SoundManager();
    
    private static final String BOT_KILLED_SAMPLE = "kanjitori/res/sfx/gasgrenade.ogg";
    private static final int BOT_KILLED = 1;
    private int botKilledSound;
    
    private static final String ITEM_COLLECTED_SAMPLE = "kanjitori/res/sfx/am_pkup.wav";
    private static final int ITEM_COLLECTED = 2;
    private int itemCollectedSound;

    private static final String PLAYER_RELOCATE_SAMPLE = "kanjitori/res/sfx/respawn1.wav";
    private static final int PLAYER_RELOCATE = 3;
    private int playerRelocateSound;
    
    private boolean on = true;
    private int snode;
    
    
    /** Creates a new instance of SoundManager */
    private SoundManager() {
        initSound();
        BotKilledEventManager.getManager().register(new Listener<Bot>(){
            public void notify(Bot bot) {
                playSound(bot, botKilledSound, BOT_KILLED);
            }
        });
        ItemCollectedEventManager.getManager().register(new Listener<Item>(){
            public void notify(Item item) {
                playSound(item, itemCollectedSound, ITEM_COLLECTED);
            }
        });
        PlayerRelocateEventManager.getManager().register(new Listener<Player>(){
            public void notify(Player player) {
                playSound(player, playerRelocateSound, PLAYER_RELOCATE);
            }
        });
    }
    
    public boolean isOn() {
        return on;
    }
    
    public void setOn(boolean on) {
        this.on = on;
    }
    
    
    public static SoundManager getManager() {
        return INSTANCE;
    }
    
    public void draw() {
        SoundSystem.draw(snode);
    }
    
    private void initSound() {
        SoundSystem.init(DisplaySystem.getDisplaySystem().getRenderer().getCamera(),
                SoundSystem.OUTPUT_DEFAULT);
        snode = SoundSystem.createSoundNode();
        
        botKilledSound = createSample(BOT_KILLED_SAMPLE, BOT_KILLED);
        itemCollectedSound = createSample(ITEM_COLLECTED_SAMPLE, ITEM_COLLECTED);
        playerRelocateSound = createSample(PLAYER_RELOCATE_SAMPLE, PLAYER_RELOCATE);
    }
    
    private int createSample(String sample, int eventID) {
        int sampleID = SoundSystem.create3DSample(SoundManager.class
                .getClassLoader().getResource(sample));
        SoundSystem.bindEventToSample(sampleID, eventID);
        SoundSystem.addSampleToNode(sampleID, snode);
        return sampleID;
    }
    
    private void playSound(Thing thing, int sampleID, int eventID) {
        if (on) {
           SoundSystem.setSamplePosition(sampleID,
                thing.getNode().getLocalTranslation().x,
                thing.getNode().getLocalTranslation().y,
                thing.getNode().getLocalTranslation().z);
           SoundSystem.onEvent(eventID);
        }
    }
    
    public void update(float tpf) {
       SoundSystem.update(tpf); 
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?