Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>SimpleAudio</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Stride.Core" Version="4.2.0.1" PrivateAssets="contentfiles;analyzers" />
Expand Down
129 changes: 63 additions & 66 deletions samples/Audio/SimpleAudio/SimpleAudio.Game/SoundScript.cs
Original file line number Diff line number Diff line change
@@ -1,95 +1,92 @@
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System.Linq;
using System.Threading.Tasks;

using Stride.Audio;
using Stride.Core;
using Stride.Core.Mathematics;
using Stride.Audio;
using Stride.Engine;
using Stride.Input;
using Stride.UI;
using Stride.UI.Controls;
using Stride.UI.Panels;

namespace SimpleAudio
namespace SimpleAudio;

/// <summary>
/// The main script in charge of the sound.
/// </summary>
public class SoundScript : AsyncScript
{
/// <summary>
/// The main script in charge of the sound.
/// The page containing the UI elements
/// </summary>
public class SoundScript : AsyncScript
public UIPage Page {get; set; }

public Sound SoundMusic;
private SoundInstance music;
public Sound SoundEffect;
private SoundInstance effect;

[DataMember(Mask = LiveScriptingMask)] // keep the value when reloading the script (live-scripting)
private float originalPositionX;

[DataMember(Mask = LiveScriptingMask)] // keep the value when reloading the script (live-scripting)
private float fontColor;

public override async Task Execute()
{
/// <summary>
/// The page containing the UI elements
/// </summary>
public UIPage Page {get; set; }
var imgLeft = Page?.RootElement.FindVisualChildOfType<ImageElement>("LeftWave");
var imgRight = Page?.RootElement.FindVisualChildOfType<ImageElement>("RightWave");

public Sound SoundMusic;
private SoundInstance music;
public Sound SoundEffect;
private SoundInstance effect;
music = SoundMusic.CreateInstance();
effect = SoundEffect.CreateInstance();

[DataMember(Mask = LiveScriptingMask)] // keep the value when reloading the script (live-scripting)
private float originalPositionX;
if (!IsLiveReloading)
{
// start ambient music
music.IsLooping = true;
music.Play();

[DataMember(Mask = LiveScriptingMask)] // keep the value when reloading the script (live-scripting)
private float fontColor;
fontColor = 0;
originalPositionX = (imgRight != null) ? imgRight.GetCanvasRelativePosition().X : 0.65f;
}

public override async Task Execute()
while (Game.IsRunning)
{
var imgLeft = Page?.RootElement.FindVisualChildOfType<ImageElement>("LeftWave");
var imgRight = Page?.RootElement.FindVisualChildOfType<ImageElement>("RightWave");

music = SoundMusic.CreateInstance();
effect = SoundEffect.CreateInstance();

if (!IsLiveReloading)
if (Input.PointerEvents.Any(item => item.EventType == PointerEventType.Pressed)) // New click
{
// start ambient music
music.IsLooping = true;
music.Play();
if (imgLeft != null && imgRight != null)
{
// reset wave position
imgLeft.SetCanvasRelativePosition(new Vector3(1 - originalPositionX, 0.5f, 0));
imgLeft.Opacity = 0;

imgRight.SetCanvasRelativePosition(new Vector3(originalPositionX, 0.5f, 0));
imgRight.Opacity = 0;
}

// reset transparency
fontColor = 1;

fontColor = 0;
originalPositionX = (imgRight != null) ? imgRight.GetCanvasRelativePosition().X : 0.65f;
// play the sound effect on each touch on the screen
effect.Stop();
effect.Play();
}

while (Game.IsRunning)
else
{
if (Input.PointerEvents.Any(item => item.EventType == PointerEventType.Pressed)) // New click
if (imgLeft != null && imgRight != null)
{
if (imgLeft != null && imgRight != null)
{
// reset wave position
imgLeft.SetCanvasRelativePosition(new Vector3(1 - originalPositionX, 0.5f, 0));
imgLeft.Opacity = 0;
imgLeft.SetCanvasRelativePosition(imgLeft.GetCanvasRelativePosition() - new Vector3(0.0025f, 0, 0));
imgRight.SetCanvasRelativePosition(imgRight.GetCanvasRelativePosition() + new Vector3(0.0025f, 0, 0));

imgRight.SetCanvasRelativePosition(new Vector3(originalPositionX, 0.5f, 0));
imgRight.Opacity = 0;
}

// reset transparency
fontColor = 1;

// play the sound effect on each touch on the screen
effect.Stop();
effect.Play();
// changing font transparency
fontColor = 0.93f * fontColor;
imgLeft.Opacity = fontColor;
imgRight.Opacity = fontColor;
}
else
{
if (imgLeft != null && imgRight != null)
{
imgLeft.SetCanvasRelativePosition(imgLeft.GetCanvasRelativePosition() - new Vector3(0.0025f, 0, 0));
imgRight.SetCanvasRelativePosition(imgRight.GetCanvasRelativePosition() + new Vector3(0.0025f, 0, 0));

// changing font transparency
fontColor = 0.93f * fontColor;
imgLeft.Opacity = fontColor;
imgRight.Opacity = fontColor;
}
}

// wait for next frame
await Script.NextFrame();
}

// wait for next frame
await Script.NextFrame();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<OutputPath>..\Bin\Windows\$(Configuration)\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<DefineConstants>STRIDE_PLATFORM_DESKTOP</DefineConstants>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup>
<StrideCurrentPackagePath>$(MSBuildThisFileDirectory)..\SimpleAudio.sdpkg</StrideCurrentPackagePath>
Expand Down
30 changes: 14 additions & 16 deletions samples/Games/JumpyJet/JumpyJet.Game/BackgroundScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,27 @@
using System.Threading.Tasks;
using Stride.Engine;
using Stride.Engine.Events;
using Stride.Physics;
using Stride.Rendering.Compositing;

namespace JumpyJet
namespace JumpyJet;

public class BackgroundScript : AsyncScript
{
public class BackgroundScript : AsyncScript
private readonly EventReceiver gameOverListener = new(GameGlobals.GameOverEventKey);
private readonly EventReceiver gameResetListener = new(GameGlobals.GameResetEventKey);

public override async Task Execute()
{
private EventReceiver gameOverListener = new EventReceiver(GameGlobals.GameOverEventKey);
private EventReceiver gameResetListener = new EventReceiver(GameGlobals.GameResetEventKey);
// Find our JumpyJetRenderer to start/stop parallax background
var renderer = (JumpyJetRenderer)((SceneCameraRenderer)SceneSystem.GraphicsCompositor.Game).Child;

public override async Task Execute()
while (Game.IsRunning)
{
// Find our JumpyJetRenderer to start/stop parallax background
var renderer = (JumpyJetRenderer)((SceneCameraRenderer)SceneSystem.GraphicsCompositor.Game).Child;

while (Game.IsRunning)
{
await gameOverListener.ReceiveAsync();
renderer.StopScrolling();
await gameOverListener.ReceiveAsync();
renderer.StopScrolling();

await gameResetListener.ReceiveAsync();
renderer.StartScrolling();
}
await gameResetListener.ReceiveAsync();
renderer.StartScrolling();
}
}
}
Loading