CRIWARE Unity Plugin Manual  Last Updated: 2024-07-12
Game: Pinball

Description of the sample

Overview

Sound implementation sample for a pinball game using ADX.

cri4u_samples_criatom_script_game_pinball_main.png

Operations

When the scene is executed, the pinball game is started automatically.
  • Stopper button
    Click this to display the stopper at the bottom of the playfield to prevent the ball from being lost.

  • Pause] / Resume button
    Pause button, only available while the stopper is displayed.
    Pauses or resumes the movement of the ball.

  • Multi Ball button
    This button adds a ball to the game. It is available only while the stopper is displayed.
    Every time you click it, a new ball is added.

  • Click the playfield
    Moves the ball to the position you clicked.

Scene information


Middleware CRI ADX (CRI Atom)
Sample Script sample: Game: Pinball
Location /CRIWARE/SDK/unity/samples/UnityProject/Assets/Scenes/criatom/script/GameSample_Pinball
Scene file GameSample_Pinball.unity
Original ADX data Data: Pinball


Description of the program

This is an automatically-played pinball game.
The sound changes when the ball hits the wall, bumpers, or a target.
The music also changes every time the ball is lost.

Game control and sound control scripts

This sample consists of two scripts, one for game control and the other for sound control.

Script file for game control: GameMain/GameMain.cs
Script file for sound control: SoundManager/SoundManager.cs
GameMain.cs stores the game object in SoundManager when the game is started, and triggers sound playback as required.
The simple implementation of SoundManager.cs and the variety of sounds it can produce shows the advantages of using ADX.

The following section describes some of the processes used to control the sound.

Change in sound when the ball hits the wall

The data is configured so that the sound when the ball hits the wall varies according to the collision speed.
An AISAC function is used to change the sound, so the script uses the collision speed as the AISAC's parameter.
For more information about using AISAC, also see the " [CriAtom] AISAC " sample.
public void PlaybackBall(int index,float velocity)
{
if(lastPlaybackBallTime+0.25 < Time.timeSinceLevelLoad){
velocity = Mathf.Min(velocity,1.0f);
atomSourceBall.SetAisac(0,velocity);
atomSourceBall.Play(index);
lastPlaybackBallTime = Time.timeSinceLevelLoad;
lastVelocity = velocity;
}
}

This sample code also determines whether to play the collision sound based on the time since the last playback to prevent too many collision sounds.

Random voice playback when the ball is lost

The data is configured so that voices such as "Wow!" and "I lost it!" are randomly played when the ball is lost.
Randomization is completely configured on the data side. The script only triggers the playback.

public void PlayGameOver()
{
//atomSourceSe.Play(4); // by Cue ID
atomSourceSe.Play("GameOver"); // by Cue Name
}

An effect is also applied to the sounds when the ball hits the bumper and the target. This is also entirely configured data-side and no parameters are specified in the script.

Change in the music volume when the ball is lost

When a voice line such as "Wow!" is played after the ball is lost, the volume of the music is turned down temporarily.
This emphasizes the voice line.
The change in the volume of the music is also completely set within the tool by using the REACT feature. The script only needs to trigger the playback.
For more information about the REACT feature, also see the " [CriAtom] Category " sample.

Change of music

The data is configured so that a different music is played based on the number of lost balls.
The block playback feature is used to change the music. The script checks the block index that is played when the ball is lost, and specifies the next block index to play.
For more information about using block playback, also see the " [CriAtom] Block playback " sample.
public void PlayBGM()
{
bool startFlag = false;
CriAtomSource.Status status = atomSourceBgm.status;
if ((status == CriAtomSource.Status.Stop) || (status == CriAtomSource.Status.PlayEnd)) {
this.playbackBGM = atomSourceBgm.Play(100);
startFlag = true;
}
/* Perform block playback except for the first time */
if(startFlag == false){
int cur = this.playbackBGM.GetCurrentBlockIndex();
CriAtomExAcb acb = CriAtom.GetAcb("PinballMain");
if(acb != null){
acb.GetCueInfo("BGM",out this.cueInfo);
cur++;
if(this.cueInfo.numBlocks > 0){
this.playbackBGM.SetNextBlockIndex(cur % this.cueInfo.numBlocks);
}
}
}
}

Music restart control

SoundManager.cs contains a process to resume any stopped music.

public void ResumeBGM()
{
/* Play if the status is in the PlayEnd or the Stop. (automatically restart when ACB is updated) */
CriAtomSource.Status status = atomSourceBgm.status;
if ((status == CriAtomSource.Status.Stop) || (status == CriAtomSource.Status.PlayEnd)) {
/* Play */
PlayBGM();
}
}
Although this is not required for the game, it is provided in case you use the in-game preview feature in ADX.
The in-game preview feature connects from CRI Atom Craft to the game that is being played (pinball in this case) and allows to control the sound while playing the game. For example, you can adjust the echo effect and the volume of each hit sound while playing the game.
When you connect from CRI Atom Craft to the game program for an in-game preview, all sound playback is temporarily stopped.
Stopping sound effects causes no problem, because they are played by the game as required. However, music is not automatically resumed after it is stopped.
Since this clearly conflicts with the goal of an in-game preview, the SoundManager restarts music.
For more information about the in-game preview feature, also see the " [CriAtom] In-game preview (pinball demo) " sample.
This sample supports in-game preview. By opening the " Data: Pinball " sample in CRI Atom Craft, you can use in-game preview with this sample.