⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nokiagameeffects.java

📁 J2ME MIDP_Example_Applications
💻 JAVA
字号:
// Copyright 2003 Nokia Corporation.
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Corporation
// retains the right to make changes to this source code at
// any time, without notice. This source code is provided for informational
// purposes only.
//
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation.
// Java and all Java-based marks are trademarks or registered trademarks of
// Sun Microsystems, Inc.
// Other product and company names mentioned herein may be trademarks or
// trade names of their respective owners.
//
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.


import com.nokia.mid.sound.Sound;
import com.nokia.mid.ui.DeviceControl;


// Nokia GameEffects provides a 'Nokia device specific' implementation
// of game effects such as vibration, flashing lights and
// playing game sounds.

class NokiaGameEffects
    extends GameEffects
{
    private volatile boolean isPaused = true;
    private Sound sound = null;
    private volatile int currentPriority = 0;

    // The sound byte arrays are in 'Smart Messaging' ringing tone format.

    // Bullet 'ringing tone' sound effect
    private final static byte[] BULLET_SOUND_BYTES =
    {
        (byte)0x02, (byte)0x4A, (byte)0x3A, (byte)0x80,
        (byte)0x40, (byte)0x00, (byte)0xF3, (byte)0x48,
        (byte)0x22, (byte)0xC3, (byte)0x4C, (byte)0x35,
        (byte)0x02, (byte)0xAC, (byte)0x22, (byte)0xC0,
        (byte)0x00
    };

    // Base explosion 'ringing tone' sound effect
    private final static byte[] BASE_EXPLODE_SOUND_BYTES =
    {
        (byte)0x02, (byte)0x4A, (byte)0x3A, (byte)0x80,
        (byte)0x40, (byte)0x01, (byte)0x52, (byte)0x48,
        (byte)0x2D, (byte)0x02, (byte)0x71, (byte)0x49,
        (byte)0x18, (byte)0x20, (byte)0xD4, (byte)0x12,
        (byte)0x72, (byte)0x00, (byte)0x00
    };

    // Block explosion 'ringing tone' sound effect
    private final static byte[] BLOCK_EXPLODE_SOUND_BYTES =
    {
        (byte)0x02, (byte)0x4A, (byte)0x3A, (byte)0x80,
        (byte)0x40, (byte)0x00, (byte)0xD2, (byte)0xC8,
        (byte)0x31, (byte)0x03, (byte)0x90, (byte)0x23,
        (byte)0x12, (byte)0xB0, (byte)0x00
    };

    // The 'game over' music played when the player looses.
    // It is from the old tune "Loch Lomond", starting at the
    // notes for the verse "Oh, you'll take the high road ...".
    // It is defined as a 'ringing tone' byte arrary.
    private final static byte[] PLAYER_LOST_MUSIC_BYTES =
    {
        (byte)0x02, (byte)0x4A, (byte)0x3A, (byte)0x80,
        (byte)0x40, (byte)0x06, (byte)0xB2, (byte)0x08,
        (byte)0x22, (byte)0x82, (byte)0xC8, (byte)0x2C,
        (byte)0xC3, (byte)0x0C, (byte)0x34, (byte)0x83,
        (byte)0x4C, (byte)0x30, (byte)0xC2, (byte)0xC8,
        (byte)0x2C, (byte)0xC2, (byte)0x6C, (byte)0x22,
        (byte)0x82, (byte)0x2C, (byte)0x2A, (byte)0xC2,
        (byte)0xC8, (byte)0x2C, (byte)0xC2, (byte)0xCC,
        (byte)0x2C, (byte)0x83, (byte)0x4C, (byte)0x49,
        (byte)0x16, (byte)0x13, (byte)0x21, (byte)0x14,
        (byte)0x10, (byte)0x81, (byte)0x14, (byte)0x13,
        (byte)0x41, (byte)0x36, (byte)0x11, (byte)0x62,
        (byte)0x0D, (byte)0x20, (byte)0xD3, (byte)0x12,
        (byte)0x45, (byte)0x88, (byte)0x38, (byte)0xC3,
        (byte)0x4C, (byte)0x30, (byte)0xC2, (byte)0xCC,
        (byte)0x26, (byte)0x82, (byte)0x2C, (byte)0x26,
        (byte)0xC2, (byte)0xD0, (byte)0x2C, (byte)0xD3,
        (byte)0x50, (byte)0x49, (byte)0x16, (byte)0x93,
        (byte)0x41, (byte)0x16, (byte)0x20, (byte)0xD3,
        (byte)0x0C, (byte)0x10, (byte)0xB1, (byte)0x00,
        (byte)0x00
    };

    // This 'game over' music is played when the player wins.
    // It is from the old tune "Yankee Doodle", starting at
    // the notes for the verse "Yankee Doodle keep it up, Yankee
    // Doodle dandy ...". It is defined as a 'ringing tone' byte arrary.
    private final static byte[] PLAYER_WON_MUSIC_BYTES =
    {
        (byte)0x02, (byte)0x4A, (byte)0x3A, (byte)0x80,
        (byte)0x40, (byte)0x04, (byte)0x11, (byte)0xE8,
        (byte)0x2C, (byte)0xD3, (byte)0x10, (byte)0x2C,
        (byte)0xC2, (byte)0x6C, (byte)0x2C, (byte)0xC3,
        (byte)0x0C, (byte)0x34, (byte)0xC2, (byte)0xCC,
        (byte)0x2A, (byte)0xD2, (byte)0xD0, (byte)0x2A,
        (byte)0xC2, (byte)0x6C, (byte)0x22, (byte)0xD2,
        (byte)0x70, (byte)0x2A, (byte)0xC2, (byte)0x0C,
        (byte)0x2C, (byte)0xD3, (byte)0x10, (byte)0x2C,
        (byte)0xC2, (byte)0x6C, (byte)0x2C, (byte)0xC3,
        (byte)0x0C, (byte)0x34, (byte)0xC2, (byte)0xCC,
        (byte)0x2A, (byte)0xC3, (byte)0x4C, (byte)0x30,
        (byte)0xC3, (byte)0x8C, (byte)0x34, (byte)0x83,
        (byte)0x4C, (byte)0x00
    };


    NokiaGameEffects()
    {
    }


    // Game Settings methods

    boolean hasSoundCapability()
    {
        return true;
    }


    boolean hasVibrationCapability()
    {
        return true;
    }


    // Game Effects methods

    private synchronized void setIsPaused(boolean isPaused)
    {
        this.isPaused = isPaused;
    }


    void pause()
    {
        setIsPaused(true);

        if (sound != null)
        {
            sound.stop();
        }

        DeviceControl.stopVibra();
    }


    void resume()
    {
        // Note: The 'Game Settings' may change during a pause.
        // In general, one might need to handle that during the resume.
        // But we don't need to do anything to handle that before resuming
        // because any sound or vibration that were in progress just
        // before pausing were cancelled when pause() was called,
        // and any 'Game Settings' changes will take effect after the resume.

        setIsPaused(false);
    }


    private void playSound(int priority, byte[] bytes)
    {
        if (!isPaused && hasSoundCapability() && Settings.getUseSound())
        {
            try
            {
                if (sound == null || sound.getState() != Sound.SOUND_PLAYING)
                {
                    sound = new Sound(bytes, Sound.FORMAT_TONE);
                    sound.setGain(128);
                    sound.play(1); // loop = 1
                    currentPriority = priority;
                }
                else
                {
                    // A sound exists in the SOUND_PLAYING state.

                    if (priority > currentPriority)
                    {
                        sound.stop();
                        sound.init(bytes, Sound.FORMAT_TONE);
                        sound.play(1); // loop = 1
                        currentPriority = priority;
                    }
                    // else
                    //   The last sound is still playing and has a higher
                    //   priority than this play request, so we let the
                    //   last sound continue to play and ignore this play
                    //   request. We don't queue this play request for playing
                    //   after the last one finishes, as that might sound
                    //   funny to the user. (The sounds should match visual
                    //   events occurring in the game). In a fast moving
                    //   game, new play requests are likely to occur
                    //   continuously and frequently (e.g. 'fire a bullet'
                    //   noise), so we can cautiously drop a few play
                    //   requests when it is sensible to do so.
                }
            }
            catch (Exception e)
            {
                currentPriority = 0; // allow any new request to play
            }
        }
    }


    void playBullet()
    {
        // Lowest priority game sound

        playSound(1, BULLET_SOUND_BYTES);
    }


    void playBlockExplosion()
    {
        // Higher priority game sound

        playSound(2, BLOCK_EXPLODE_SOUND_BYTES);
    }


    void playBaseExplosion()
    {
        // Highest priority game sound

        playSound(3, BASE_EXPLODE_SOUND_BYTES);
    }


    void playGameOverMusic(boolean hasPLAYER_WON)
    {
        // The 'game over' music must have a higher priority
        // than any game sound.
        if (hasPLAYER_WON)
        {
            playSound(4, PLAYER_WON_MUSIC_BYTES);
        }
        else
        {
            playSound(4, PLAYER_LOST_MUSIC_BYTES);
        }
    }


    void vibrate()
    {
        if (Settings.getUseVibration())
        {
            try
            {
                // Vibrate at 100% for 300 milli-seconds.
                DeviceControl.startVibra(100, 300);
            }
            catch (Exception e)
            {
                // An IllegalStateException is thrown if the device doesn't
                // have vibration support, or is in it's battery charging
                // station, etc. We can't do anything, so just ignore it.
            }
        }
    }
}

⌨️ 快捷键说明

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