Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Storage;
- using System.IO;
- using System.Xml.Serialization;
- using Microsoft.Xna.Framework.Content;
- namespace Ze_shooter.Objects
- {
- public struct SaveData
- {
- public int Score;
- }
- public enum SaveState
- {
- NotSaving,
- ReadyToSelectStorageDevice,
- SelectingStorageDevice,
- ReadyToOpenStorageContainer, // Once we have a storage device, start here
- OpeningStorageContainer,
- ReadyToSave
- }
- class Helper
- {
- public Helper()
- {
- }
- /// <summary>
- /// Performs a transformed per pixel collision check on two sprites.
- /// Ensure that transformation matricies are correctly configured or this will fail
- /// </summary>
- /// <param name="matrixA">Transformation matrix for sprite A</param>
- /// <param name="colorsA">1 Dimensional color array of sprite A's texture data</param>
- /// <param name="widthA">Sprite A's width</param>
- /// <param name="heightA">Sprite A's height</param>
- /// <param name="matrixB">Transformation matrix for sprite B</param>
- /// <param name="colorsB">1 Dimensional color array of sprite B's texture data</param>
- /// <param name="widthB">Sprite B's width</param>
- /// <param name="heightB">Sprite B's height</param>
- /// <returns></returns>
- public bool TransformedPPC(Matrix matrixA, Color[] colorsA,
- int widthA, int heightA,
- Matrix matrixB, Color[] colorsB,
- int widthB, int heightB)
- {
- // Matrix containing data to switch A space to B space
- Matrix AtoB = matrixA * Matrix.Invert(matrixB);
- // for every pixel on the X axis of A
- for (int xA = 0; xA < widthA; xA++)
- {
- // for every pixel on the Y axis of A
- for (int yA = 0; yA < heightA; yA++)
- {
- // transform this pixel (xA, yA) by AtoB, so it corresponds to the pixel in B's local space
- Vector2 positionInB = Vector2.Transform(new Vector2(xA, yA), AtoB);
- // round the pixel for accuracy, down into an int
- int xB = (int)Math.Round(positionInB.X);
- int yB = (int)Math.Round(positionInB.Y);
- // if the pixel is within B's space
- if (xB >= 0 && xB < widthB &&
- yB >= 0 && yB < heightB)
- {
- // fetch colors corresponding to A space and B space
- Color colorA = colorsA[xA + yA * widthA];
- Color colorB = colorsB[xB + yB * widthB];
- // if both colors aren't transparent, return true
- if (colorA.A != 0 && colorB.A != 0)
- return true;
- }
- }
- }
- // no collision found
- return false;
- }
- public static SaveData SaveData;
- private SaveState _saveState = SaveState.NotSaving;
- private IAsyncResult asyncResult;
- private PlayerIndex _playerIndex = PlayerIndex.One;
- private StorageContainer _storageContainer;
- private StorageDevice _storageDevice;
- private string fileName = "score.omg";
- public EventHandler FinishedSaving;
- private string _displayName = "ZeShooterStorageContainer";
- private void SetSaveData(Player player)
- {
- SaveData = new SaveData()
- {
- Score = player.Score,
- };
- }
- public void RequestSave(Player player)
- {
- if (_saveState == SaveState.NotSaving && player.Score > LoadData.Score)
- {
- SetSaveData(player);
- _saveState = SaveState.ReadyToSelectStorageDevice;
- }
- else
- {
- FinishedSaving.Invoke(this, new EventArgs());
- }
- }
- public void UpdateSaving()
- {
- switch (_saveState)
- {
- case SaveState.ReadyToSelectStorageDevice:
- {
- asyncResult = StorageDevice.BeginShowSelector(_playerIndex, null, null);
- _saveState = SaveState.SelectingStorageDevice;
- }
- break;
- case SaveState.SelectingStorageDevice:
- if (asyncResult.IsCompleted)
- {
- _storageDevice = StorageDevice.EndShowSelector(asyncResult);
- _saveState = SaveState.ReadyToOpenStorageContainer;
- }
- break;
- case SaveState.ReadyToOpenStorageContainer:
- if (_storageDevice == null || !_storageDevice.IsConnected)
- {
- _saveState = SaveState.ReadyToSelectStorageDevice;
- }
- else
- {
- asyncResult = _storageDevice.BeginOpenContainer(_displayName, null, null);
- _saveState = SaveState.OpeningStorageContainer;
- }
- break;
- case SaveState.OpeningStorageContainer:
- if (asyncResult.IsCompleted)
- {
- _storageContainer = _storageDevice.EndOpenContainer(asyncResult);
- _saveState = SaveState.ReadyToSave;
- }
- break;
- case SaveState.ReadyToSave:
- if (_storageContainer == null)
- {
- _saveState = SaveState.ReadyToOpenStorageContainer;
- }
- else
- {
- try
- {
- DeleteExisting();
- Save();
- }
- catch (IOException e)
- {
- //replace with in game dialogue notifying user of error
- Debug.WriteLine(e.Message);
- }
- finally
- {
- _storageContainer.Dispose();
- _storageContainer = null;
- _saveState = SaveState.NotSaving;
- FinishedSaving.Invoke(this, new EventArgs());
- }
- }
- break;
- }
- }
- public enum LoadState
- {
- NotLoading,
- ReadyToSelectStorageDevice,
- SelectingStorageDevice,
- ReadyToOpenStorageContainer, // Once we have a storage device, start here
- OpeningStorageContainer,
- ReadyToLoad
- }
- private LoadState _loadState;
- public EventHandler FinishedLoading;
- public void LoadUpdate()
- {
- switch (_loadState)
- {
- case LoadState.ReadyToSelectStorageDevice:
- {
- asyncResult = StorageDevice.BeginShowSelector(_playerIndex, null, null);
- _loadState = LoadState.SelectingStorageDevice;
- }
- break;
- case LoadState.SelectingStorageDevice:
- if (asyncResult.IsCompleted)
- {
- _storageDevice = StorageDevice.EndShowSelector(asyncResult);
- _loadState = LoadState.ReadyToOpenStorageContainer;
- }
- break;
- case LoadState.ReadyToOpenStorageContainer:
- if (_storageDevice == null || !_storageDevice.IsConnected)
- _loadState = LoadState.ReadyToSelectStorageDevice;
- else
- {
- asyncResult = _storageDevice.BeginOpenContainer(_displayName, null, null);
- _loadState = LoadState.OpeningStorageContainer;
- }
- break;
- case LoadState.OpeningStorageContainer:
- if (asyncResult.IsCompleted)
- {
- _storageContainer = _storageDevice.EndOpenContainer(asyncResult);
- _loadState = LoadState.ReadyToLoad;
- }
- break;
- case LoadState.ReadyToLoad:
- if (_storageContainer == null)
- _loadState = LoadState.ReadyToOpenStorageContainer;
- else
- {
- try
- {
- Load();
- }
- catch (Exception e)
- {
- //Replace with error loading message
- Debug.Write(e.Message);
- }
- finally
- {
- _storageContainer.Dispose();
- _storageContainer = null;
- _loadState = LoadState.NotLoading;
- FinishedLoading.Invoke(this, new EventArgs());
- }
- }
- break;
- }
- }
- private void DeleteExisting()
- {
- if (_storageContainer.FileExists(fileName))
- {
- _storageContainer.DeleteFile(fileName);
- }
- }
- private void Save()
- {
- using (Stream stream = _storageContainer.CreateFile(fileName))
- {
- XmlSerializer serializer = new XmlSerializer(typeof(SaveData));
- serializer.Serialize(stream, SaveData);
- }
- }
- public void RequestLoad()
- {
- _loadState = LoadState.ReadyToSelectStorageDevice;
- }
- public static SaveData LoadData;
- private void Load()
- {
- using (StreamReader reader = new StreamReader(_storageContainer.OpenFile(fileName, FileMode.Open)))
- {
- XmlSerializer deserializer = new XmlSerializer(typeof(SaveData));
- LoadData = (SaveData) deserializer.Deserialize(reader);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement