Skip to content

Commit

Permalink
Merge branch 'main' into feature/building-games-in-maui
Browse files Browse the repository at this point in the history
  • Loading branch information
bijington authored Sep 20, 2023
2 parents 1ec05c0 + 5e92704 commit 55c301b
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 13 deletions.
8 changes: 8 additions & 0 deletions games/Orbit/Orbit/Audio/AudioItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Orbit.Audio;

public static class AudioItem
{
public const string HomeBackgroundMusic = "magic-space.mp3";

public const string ThrusterSoundEffect = "engine-heavy-loop.mp3";
}
47 changes: 47 additions & 0 deletions games/Orbit/Orbit/Audio/AudioService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using Plugin.Maui.Audio;

namespace Orbit.Audio;

public class AudioService
{
readonly IDictionary<string, IAudioPlayer> players = new Dictionary<string, IAudioPlayer>();
private readonly IAudioManager audioManager;
private readonly IFileSystem fileSystem;

public AudioService(
IAudioManager audioManager,
IFileSystem fileSystem)
{
this.audioManager = audioManager;
this.fileSystem = fileSystem;
}

public async Task Play(string audioItem, bool loop)
{
if (!players.TryGetValue(audioItem, out var audioPlayer))
{
audioPlayer = audioManager.CreatePlayer(await fileSystem.OpenAppPackageFileAsync(audioItem));

players[audioItem] = audioPlayer;

audioPlayer.Loop = loop;
}

if (!audioPlayer.IsPlaying)
{
audioPlayer.Play();
}
}

public void Stop(string audioItem)
{
if (players.TryGetValue(audioItem, out var audioPlayer))
{
if (audioPlayer.IsPlaying)
{
audioPlayer.Stop();
}
}
}
}
6 changes: 6 additions & 0 deletions games/Orbit/Orbit/Audio/SoundEffect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Orbit.Audio;

public enum SoundEffect
{
Thruster
}
15 changes: 15 additions & 0 deletions games/Orbit/Orbit/Audio/SoundEffectService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Plugin.Maui.Audio;

namespace Orbit.Audio;

public class SoundEffectService
{
static readonly IDictionary<SoundEffect, string> soundEffectToResourceMapping = new Dictionary<SoundEffect, string>()
{
[SoundEffect.Thruster] = "engine-heavy-loop.mp3"
};

public static string GetSoundEffectFile(SoundEffect soundEffect) =>
soundEffectToResourceMapping[soundEffect];

}
7 changes: 6 additions & 1 deletion games/Orbit/Orbit/GameObjects/Asteroid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ public class Asteroid : GameObject
{
private readonly IGameSceneManager gameSceneManager;
private readonly IServiceProvider serviceProvider;
private readonly IVibration vibration;
Microsoft.Maui.Graphics.IImage image;
float x;
float y;
Movement movement;

public Asteroid(
IGameSceneManager gameSceneManager,
IServiceProvider serviceProvider)
IServiceProvider serviceProvider,
IVibration vibration)
{
image = LoadImage("asteroid.png");

this.gameSceneManager = gameSceneManager;
this.serviceProvider = serviceProvider;
this.vibration = vibration;
}

public void SetMovement(Movement movement)
Expand Down Expand Up @@ -80,6 +83,8 @@ public override void Render(ICanvas canvas, RectF dimensions)
var remains = this.serviceProvider.GetRequiredService<AsteroidRemains>();
remains.SetBounds(this.Bounds);
CurrentScene.Add(remains);

this.vibration.Vibrate();
}

if (collision is Ship ship)
Expand Down
3 changes: 1 addition & 2 deletions games/Orbit/Orbit/GameObjects/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public Ship(
this.gameSceneManager = gameSceneManager;
this.thruster = thruster;
this.userInputManager = userInputManager;

Add(gun);
Add(thruster);
Add(battery);
Expand All @@ -58,8 +59,6 @@ public override void Render(ICanvas canvas, RectF dimensions)
var image = this.thruster.IsThrusting ? GetImage(userInputManager.TouchMode) : GetImage(TouchMode.None);
canvas.DrawImage(image, orbitRadius, 0, Bounds.Width, Bounds.Height);

//Bounds = WHAT???? Needs to include rotation and translation details ;(

if (MainPage.ShowBounds)
{
canvas.StrokeColor = Colors.OrangeRed;
Expand Down
19 changes: 17 additions & 2 deletions games/Orbit/Orbit/GameObjects/Thruster.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
using Orbit.Engine;
using AVFoundation;
using Orbit.Audio;
using Orbit.Engine;
using Plugin.Maui.Audio;

namespace Orbit.GameObjects;

public class Thruster : GameObject
{
private readonly Battery battery;
private readonly UserInputManager userInputManager;
private readonly AudioService audioService;
private const float batteryDrain = 0.5f;

public Thruster(
Battery battery,
UserInputManager userInputManager)
UserInputManager userInputManager,
AudioService audioService)
{
this.battery = battery;
this.userInputManager = userInputManager;
this.audioService = audioService;
}

public float Thrust { get; private set; }
Expand All @@ -31,6 +37,15 @@ public override void Update(double millisecondsSinceLastUpdate)
Thrust = -0.25f;
}

if (this.IsThrusting)
{
_ = this.audioService.Play(AudioItem.ThrusterSoundEffect, false);
}
else
{
this.audioService.Stop(AudioItem.ThrusterSoundEffect);
}

base.Update(millisecondsSinceLastUpdate);
}

Expand Down
10 changes: 8 additions & 2 deletions games/Orbit/Orbit/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Orbit.Engine;
using Orbit.Audio;
using Orbit.Engine;
using Orbit.Scenes;

namespace Orbit;
Expand All @@ -7,17 +8,20 @@ public partial class MainPage : ContentPage
{
private readonly IGameSceneManager gameSceneManager;
private readonly UserInputManager userInputManager;
private readonly AudioService audioService;

public static bool ShowBounds { get; private set; } = false;

public MainPage(
IGameSceneManager gameSceneManager,
UserInputManager userInputManager)
UserInputManager userInputManager,
AudioService audioService)
{
InitializeComponent();

this.gameSceneManager = gameSceneManager;
this.userInputManager = userInputManager;
this.audioService = audioService;
gameSceneManager.StateChanged += OnGameSceneManagerStateChanged;
gameSceneManager.LoadScene<HomeScene>(GameView);
}
Expand All @@ -31,6 +35,8 @@ private async void OnGameSceneManagerStateChanged(object sender, GameStateChange
PauseMenu.IsVisible = false;
Play.IsVisible = true;
TitleLabel.IsVisible = true;

await this.audioService.Play(AudioItem.HomeBackgroundMusic, true);
break;

case GameState.Started:
Expand Down
17 changes: 15 additions & 2 deletions games/Orbit/Orbit/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Orbit.Engine;
using Orbit.Audio;
using Orbit.Engine;
using Orbit.GameObjects;
using Orbit.Scenes;
using Plugin.Maui.Audio;

namespace Orbit;

Expand All @@ -11,18 +13,29 @@ public static MauiApp CreateMauiApp()
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseOrbitEngine()
.UseOrbitEngine() // Register the engine
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
fonts.AddFont("SpaceMono-Bold.ttf", "SpaceMonoBold");
})
.Services
// Pages
.AddTransient<MainPage>()

// Essentials
.AddSingleton(HapticFeedback.Default)
.AddSingleton(Vibration.Default)
.AddSingleton(FileSystem.Current)

// Audio
.AddSingleton(AudioManager.Current)

// Internals
.AddSingleton<UserInputManager>()
.AddSingleton<AudioService>()

.RegisterGameObjects()
.RegisterScenes();

Expand Down
6 changes: 2 additions & 4 deletions games/Orbit/Orbit/Orbit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@
<ItemGroup>
<ProjectReference Include="..\..\..\engine\Orbit.Engine\Orbit.Engine.csproj" />
</ItemGroup>

<ItemGroup>
<None Remove="Resources\EmbeddedResources\pulse.png" />
</ItemGroup>
<ItemGroup>
<BundleResource Include="Resources\EmbeddedResources\pulse.png" />
<PackageReference Include="Plugin.Maui.Audio" Version="1.0.0" />
</ItemGroup>
</Project>
Binary file not shown.
Binary file added games/Orbit/Orbit/Resources/Raw/magic-space.mp3
Binary file not shown.

0 comments on commit 55c301b

Please sign in to comment.