added basic setting persistence system

This commit is contained in:
Harrison Deng 2018-10-28 22:42:47 -05:00
parent 6a28aeb852
commit 8d762c7841
7 changed files with 102 additions and 31 deletions

View File

@ -44,6 +44,7 @@
<ApplicationManifest>app.manifest</ApplicationManifest> <ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Zer01HD\Utilities\Persistence\IPreferences.cs" />
<Compile Include="Zer01HD\Utilities\UI\LoadingScreen.cs" /> <Compile Include="Zer01HD\Utilities\UI\LoadingScreen.cs" />
<Compile Include="Zer01HD\Game\MainScreen\MainScreen.cs" /> <Compile Include="Zer01HD\Game\MainScreen\MainScreen.cs" />
<Compile Include="Zer01HD\Utilities\UI\Modular\Module.cs" /> <Compile Include="Zer01HD\Utilities\UI\Modular\Module.cs" />
@ -63,7 +64,7 @@
<Compile Include="Zer01HD\Utilities\ParticleSystem\Particle.cs" /> <Compile Include="Zer01HD\Utilities\ParticleSystem\Particle.cs" />
<Compile Include="Zer01HD\Game\Preferences\Controls.cs" /> <Compile Include="Zer01HD\Game\Preferences\Controls.cs" />
<Compile Include="Zer01HD\Game\Preferences\General.cs" /> <Compile Include="Zer01HD\Game\Preferences\General.cs" />
<Compile Include="Zer01HD\Utilities\Preferences.cs" /> <Compile Include="Zer01HD\Utilities\Persistence\PreferencesManager.cs" />
<Compile Include="Zer01HD\Utilities\Resolution.cs" /> <Compile Include="Zer01HD\Utilities\Resolution.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -76,9 +77,6 @@
<Reference Include="MonoGame.Framework"> <Reference Include="MonoGame.Framework">
<HintPath>$(MonoGameInstallDirectory)\MonoGame\v3.0\Assemblies\DesktopGL\MonoGame.Framework.dll</HintPath> <HintPath>$(MonoGameInstallDirectory)\MonoGame\v3.0\Assemblies\DesktopGL\MonoGame.Framework.dll</HintPath>
</Reference> </Reference>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>

View File

@ -3,9 +3,12 @@ using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using RhythmBullet.Zer01HD.Game; using RhythmBullet.Zer01HD.Game;
using RhythmBullet.Zer01HD.Game.ContentResolvers; using RhythmBullet.Zer01HD.Game.ContentResolvers;
using RhythmBullet.Zer01HD.Game.Preferences;
using RhythmBullet.Zer01HD.UI; using RhythmBullet.Zer01HD.UI;
using RhythmBullet.Zer01HD.Utilities;
using RhythmBullet.Zer01HD.Utilities.ContentSystem; using RhythmBullet.Zer01HD.Utilities.ContentSystem;
using RhythmBullet.Zer01HD.Utilities.UI; using RhythmBullet.Zer01HD.Utilities.UI;
using System;
namespace RhythmBullet namespace RhythmBullet
{ {
@ -17,24 +20,9 @@ namespace RhythmBullet
public GraphicsDeviceManager Graphics; public GraphicsDeviceManager Graphics;
SpriteBatch spriteBatch; SpriteBatch spriteBatch;
public readonly ContentSystem Assets; public readonly ContentSystem Assets;
private readonly ResolutionContentResolver resolutionContentResolver; readonly ResolutionContentResolver resolutionContentResolver;
public PreferencesManager preferencesManager;
private Screen currentScreen; private Screen currentScreen;
Screen CurrentScreen
{
get
{
return currentScreen;
}
set
{
if (currentScreen != null)
{
CurrentScreen.Hide();
}
currentScreen = value;
CurrentScreen.Show();
}
}
public RhythmBulletGame() public RhythmBulletGame()
{ {
@ -48,6 +36,27 @@ namespace RhythmBullet
FontContentResolver fcr = new FontContentResolver(resolutionContentResolver); FontContentResolver fcr = new FontContentResolver(resolutionContentResolver);
Assets.contentResolver.Add(typeof(Texture2D), resolutionContentResolver); Assets.contentResolver.Add(typeof(Texture2D), resolutionContentResolver);
Assets.contentResolver.Add(typeof(SpriteFont), fcr); Assets.contentResolver.Add(typeof(SpriteFont), fcr);
preferencesManager = new PreferencesManager(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/RhythmBullet",
new Controls(),
new General());
}
public Screen Screen
{
get
{
return currentScreen;
}
set
{
if (currentScreen != null)
{
Screen.Hide();
}
currentScreen = value;
Screen.Show();
}
} }
/// <summary> /// <summary>
@ -70,7 +79,7 @@ namespace RhythmBullet
{ {
// Create a new SpriteBatch, which can be used to draw textures. // Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice); spriteBatch = new SpriteBatch(GraphicsDevice);
CurrentScreen = new LoadingScreen(Content.Load<Texture2D>("recrown")); Screen = new LoadingScreen(Content.Load<Texture2D>("recrown"));
// TODO: use this.Content to load your game content here // TODO: use this.Content to load your game content here
} }
@ -91,7 +100,7 @@ namespace RhythmBullet
/// <param name="gameTime">Provides a snapshot of timing values.</param> /// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime) protected override void Update(GameTime gameTime)
{ {
CurrentScreen.Update(gameTime); Screen.Update(gameTime);
base.Update(gameTime); base.Update(gameTime);
} }
@ -103,17 +112,23 @@ namespace RhythmBullet
{ {
GraphicsDevice.Clear(Color.White); GraphicsDevice.Clear(Color.White);
spriteBatch.Begin(); spriteBatch.Begin();
CurrentScreen.Draw(spriteBatch); Screen.Draw(spriteBatch);
spriteBatch.End(); spriteBatch.End();
base.Draw(gameTime); base.Draw(gameTime);
} }
public void Reload() public void PreAssetLoad()
{ {
Assets.UnloadAll(); Assets.UnloadAll();
resolutionContentResolver.Width = Graphics.PreferredBackBufferWidth; resolutionContentResolver.Width = Graphics.PreferredBackBufferWidth;
resolutionContentResolver.Height = Graphics.PreferredBackBufferHeight; resolutionContentResolver.Height = Graphics.PreferredBackBufferHeight;
QueueContent();
}
public void PostAssetLoad()
{
} }
void QueueContent() void QueueContent()

View File

@ -1,4 +1,5 @@
using System; using RhythmBullet.Zer01HD.Utilities.Persistence;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -6,11 +7,12 @@ using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Game.Preferences namespace RhythmBullet.Zer01HD.Game.Preferences
{ {
class Controls public class Controls : IPreferences
{ {
public int Forward; public int Forward;
public int Backward; public int Backward;
public int Left; public int Left;
public int Right; public int Right;
public int Shoot;
} }
} }

View File

@ -3,14 +3,18 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using RhythmBullet.Zer01HD.Utilities;
using RhythmBullet.Zer01HD.Utilities.Persistence;
namespace RhythmBullet.Zer01HD.Game.Preferences namespace RhythmBullet.Zer01HD.Game.Preferences
{ {
class General public class General : IPreferences
{ {
public Resolution Resolution; public Resolution Resolution;
public bool Fullscreen; public bool Fullscreen;
public float MusicVolume; public float MusicVolume;
public float FXVolume; public float FXVolume;
public string MusicDirectory;
public string GameDirectory;
} }
} }

View File

@ -4,9 +4,9 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities namespace RhythmBullet.Zer01HD.Utilities.Persistence
{ {
class Preferences public interface IPreferences
{ {
} }
} }

View File

@ -0,0 +1,53 @@
using RhythmBullet.Zer01HD.Utilities.Persistence;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace RhythmBullet.Zer01HD.Utilities
{
public class PreferencesManager
{
private readonly Dictionary<Type, IPreferences> preferenceList;
public string SavePath;
XmlSerializer xmlSerializer;
public PreferencesManager(string savePath, params IPreferences[] preferences)
{
this.SavePath = savePath;
preferenceList = new Dictionary<Type, IPreferences>();
foreach (IPreferences prefs in preferences)
{
preferenceList.Add(prefs.GetType(), prefs);
}
}
public T GetPreferences<T>() where T : IPreferences
{
return (T) preferenceList[typeof(T)];
}
public void Load()
{
foreach (KeyValuePair<Type, IPreferences> prefs in preferenceList)
{
Stream stream = new FileStream(SavePath + nameof(prefs.Key), FileMode.Open);
preferenceList[prefs.Key] = (IPreferences) xmlSerializer.Deserialize(stream);
stream.Close();
}
}
public void Save()
{
foreach (KeyValuePair<Type, IPreferences> prefs in preferenceList)
{
Stream stream = new FileStream(SavePath + nameof(prefs.Key), FileMode.Create);
xmlSerializer.Serialize(stream, prefs.Value);
stream.Close();
}
}
}
}

View File

@ -2,5 +2,4 @@
<packages> <packages>
<package id="BulletSharp" version="0.11.1" targetFramework="net45" /> <package id="BulletSharp" version="0.11.1" targetFramework="net45" />
<package id="DSP" version="1.0.0" targetFramework="net45" /> <package id="DSP" version="1.0.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net45" />
</packages> </packages>