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>
</PropertyGroup>
<ItemGroup>
<Compile Include="Zer01HD\Utilities\Persistence\IPreferences.cs" />
<Compile Include="Zer01HD\Utilities\UI\LoadingScreen.cs" />
<Compile Include="Zer01HD\Game\MainScreen\MainScreen.cs" />
<Compile Include="Zer01HD\Utilities\UI\Modular\Module.cs" />
@ -63,7 +64,7 @@
<Compile Include="Zer01HD\Utilities\ParticleSystem\Particle.cs" />
<Compile Include="Zer01HD\Game\Preferences\Controls.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" />
</ItemGroup>
<ItemGroup>
@ -76,9 +77,6 @@
<Reference Include="MonoGame.Framework">
<HintPath>$(MonoGameInstallDirectory)\MonoGame\v3.0\Assemblies\DesktopGL\MonoGame.Framework.dll</HintPath>
</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.Xml" />
</ItemGroup>

View File

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

View File

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

View File

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

View File

@ -4,9 +4,9 @@ using System.Linq;
using System.Text;
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>
<package id="BulletSharp" version="0.11.1" targetFramework="net45" />
<package id="DSP" version="1.0.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net45" />
</packages>