Advertisement
fosterboy123

Helper.cs

Aug 6th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Storage;
  8. using System.IO;
  9. using System.Xml.Serialization;
  10. using Microsoft.Xna.Framework.Content;
  11.  
  12. namespace Ze_shooter.Objects
  13. {
  14.     public struct SaveData
  15.     {
  16.         public int Score;
  17.     }
  18.  
  19.     public enum SaveState
  20.     {
  21.         NotSaving,
  22.         ReadyToSelectStorageDevice,
  23.         SelectingStorageDevice,
  24.  
  25.         ReadyToOpenStorageContainer, // Once we have a storage device, start here
  26.         OpeningStorageContainer,
  27.         ReadyToSave
  28.     }
  29.  
  30.     class Helper
  31.     {
  32.         public Helper()
  33.         {
  34.            
  35.         }
  36.  
  37.         /// <summary>
  38.         /// Performs a transformed per pixel collision check on two sprites.
  39.         /// Ensure that transformation matricies are correctly configured or this will fail
  40.         /// </summary>
  41.         /// <param name="matrixA">Transformation matrix for sprite A</param>
  42.         /// <param name="colorsA">1 Dimensional color array of sprite A's texture data</param>
  43.         /// <param name="widthA">Sprite A's width</param>
  44.         /// <param name="heightA">Sprite A's height</param>
  45.         /// <param name="matrixB">Transformation matrix for sprite B</param>
  46.         /// <param name="colorsB">1 Dimensional color array of sprite B's texture data</param>
  47.         /// <param name="widthB">Sprite B's width</param>
  48.         /// <param name="heightB">Sprite B's height</param>
  49.         /// <returns></returns>
  50.         public bool TransformedPPC(Matrix matrixA, Color[] colorsA,
  51.                                   int widthA, int heightA,
  52.                                   Matrix matrixB, Color[] colorsB,
  53.                                   int widthB, int heightB)
  54.         {
  55.             // Matrix containing data to switch A space to B space
  56.             Matrix AtoB = matrixA * Matrix.Invert(matrixB);
  57.  
  58.             // for every pixel on the X axis of A
  59.             for (int xA = 0; xA < widthA; xA++)
  60.             {
  61.                 // for every pixel on the Y axis of A
  62.                 for (int yA = 0; yA < heightA; yA++)
  63.                 {
  64.                     // transform this pixel (xA, yA) by AtoB, so it corresponds to the pixel in B's local space
  65.                     Vector2 positionInB = Vector2.Transform(new Vector2(xA, yA), AtoB);
  66.  
  67.                     // round the pixel for accuracy, down into an int
  68.                     int xB = (int)Math.Round(positionInB.X);
  69.                     int yB = (int)Math.Round(positionInB.Y);
  70.  
  71.                     // if the pixel is within B's space
  72.                     if (xB >= 0 && xB < widthB &&
  73.                         yB >= 0 && yB < heightB)
  74.                     {
  75.                         // fetch colors corresponding to A space and B space
  76.                         Color colorA = colorsA[xA + yA * widthA];
  77.                         Color colorB = colorsB[xB + yB * widthB];
  78.  
  79.                         // if both colors aren't transparent, return true
  80.                         if (colorA.A != 0 && colorB.A != 0)
  81.                             return true;
  82.                     }
  83.                 }
  84.             }
  85.             // no collision found
  86.             return false;
  87.         }
  88.  
  89.  
  90.         public static SaveData SaveData;
  91.         private SaveState _saveState = SaveState.NotSaving;
  92.         private IAsyncResult asyncResult;
  93.         private PlayerIndex _playerIndex = PlayerIndex.One;
  94.         private StorageContainer _storageContainer;
  95.         private StorageDevice _storageDevice;
  96.         private string fileName = "score.omg";
  97.         public EventHandler FinishedSaving;
  98.         private string _displayName = "ZeShooterStorageContainer";
  99.  
  100.         private void SetSaveData(Player player)
  101.         {
  102.             SaveData = new SaveData()
  103.                        {
  104.                            Score = player.Score,
  105.                        };
  106.         }
  107.  
  108.         public void RequestSave(Player player)
  109.         {
  110.             if (_saveState == SaveState.NotSaving && player.Score > LoadData.Score)
  111.             {
  112.                 SetSaveData(player);
  113.                 _saveState = SaveState.ReadyToSelectStorageDevice;
  114.             }
  115.             else
  116.             {
  117.                 FinishedSaving.Invoke(this, new EventArgs());
  118.             }
  119.         }
  120.  
  121.         public void UpdateSaving()
  122.         {
  123.             switch (_saveState)
  124.             {
  125.                 case SaveState.ReadyToSelectStorageDevice:
  126.                     {
  127.                         asyncResult = StorageDevice.BeginShowSelector(_playerIndex, null, null);
  128.                         _saveState = SaveState.SelectingStorageDevice;
  129.                     }
  130.                     break;
  131.  
  132.                 case SaveState.SelectingStorageDevice:
  133.                     if (asyncResult.IsCompleted)
  134.                     {
  135.                         _storageDevice = StorageDevice.EndShowSelector(asyncResult);
  136.                         _saveState = SaveState.ReadyToOpenStorageContainer;
  137.                     }
  138.                     break;
  139.  
  140.                 case SaveState.ReadyToOpenStorageContainer:
  141.                     if (_storageDevice == null || !_storageDevice.IsConnected)
  142.                     {
  143.                         _saveState = SaveState.ReadyToSelectStorageDevice;
  144.                     }
  145.                     else
  146.                     {
  147.                         asyncResult = _storageDevice.BeginOpenContainer(_displayName, null, null);
  148.                         _saveState = SaveState.OpeningStorageContainer;
  149.                     }
  150.                     break;
  151.  
  152.                 case SaveState.OpeningStorageContainer:
  153.                     if (asyncResult.IsCompleted)
  154.                     {
  155.                         _storageContainer = _storageDevice.EndOpenContainer(asyncResult);
  156.                         _saveState = SaveState.ReadyToSave;
  157.                     }
  158.                     break;
  159.  
  160.                 case SaveState.ReadyToSave:
  161.                     if (_storageContainer == null)
  162.                     {
  163.                         _saveState = SaveState.ReadyToOpenStorageContainer;
  164.                     }
  165.                     else
  166.                     {
  167.                         try
  168.                         {
  169.                             DeleteExisting();
  170.                             Save();
  171.                         }
  172.                         catch (IOException e)
  173.                         {
  174.                             //replace with in game dialogue notifying user of error
  175.                             Debug.WriteLine(e.Message);
  176.                         }
  177.                         finally
  178.                         {
  179.                             _storageContainer.Dispose();
  180.                             _storageContainer = null;
  181.                             _saveState = SaveState.NotSaving;
  182.                             FinishedSaving.Invoke(this, new EventArgs());
  183.                         }
  184.                     }
  185.                     break;
  186.             }
  187.         }
  188.  
  189.         public enum LoadState
  190.         {
  191.             NotLoading,
  192.             ReadyToSelectStorageDevice,
  193.             SelectingStorageDevice,
  194.  
  195.             ReadyToOpenStorageContainer, // Once we have a storage device, start here
  196.             OpeningStorageContainer,
  197.             ReadyToLoad
  198.         }
  199.  
  200.         private LoadState _loadState;
  201.         public EventHandler FinishedLoading;
  202.  
  203.         public void LoadUpdate()
  204.         {
  205.             switch (_loadState)
  206.             {
  207.                 case LoadState.ReadyToSelectStorageDevice:
  208.                     {
  209.                         asyncResult = StorageDevice.BeginShowSelector(_playerIndex, null, null);
  210.                         _loadState = LoadState.SelectingStorageDevice;
  211.                     }
  212.                     break;
  213.  
  214.                 case LoadState.SelectingStorageDevice:
  215.                     if (asyncResult.IsCompleted)
  216.                     {
  217.                         _storageDevice = StorageDevice.EndShowSelector(asyncResult);
  218.                         _loadState = LoadState.ReadyToOpenStorageContainer;
  219.                     }
  220.                     break;
  221.  
  222.                 case LoadState.ReadyToOpenStorageContainer:
  223.                     if (_storageDevice == null || !_storageDevice.IsConnected)
  224.                         _loadState = LoadState.ReadyToSelectStorageDevice;
  225.                     else
  226.                     {
  227.                         asyncResult = _storageDevice.BeginOpenContainer(_displayName, null, null);
  228.                         _loadState = LoadState.OpeningStorageContainer;
  229.                     }
  230.                     break;
  231.  
  232.                 case LoadState.OpeningStorageContainer:
  233.                     if (asyncResult.IsCompleted)
  234.                     {
  235.                         _storageContainer = _storageDevice.EndOpenContainer(asyncResult);
  236.                         _loadState = LoadState.ReadyToLoad;
  237.                     }
  238.                     break;
  239.  
  240.                 case LoadState.ReadyToLoad:
  241.                     if (_storageContainer == null)
  242.                         _loadState = LoadState.ReadyToOpenStorageContainer;
  243.                     else
  244.                     {
  245.                         try
  246.                         {
  247.                             Load();
  248.                         }
  249.                         catch (Exception e)
  250.                         {
  251.                             //Replace with error loading message
  252.                             Debug.Write(e.Message);
  253.                         }
  254.                         finally
  255.                         {
  256.                             _storageContainer.Dispose();
  257.                             _storageContainer = null;
  258.                             _loadState = LoadState.NotLoading;
  259.                             FinishedLoading.Invoke(this, new EventArgs());
  260.                         }
  261.                     }
  262.                     break;
  263.  
  264.             }
  265.         }
  266.  
  267.         private void DeleteExisting()
  268.         {
  269.             if (_storageContainer.FileExists(fileName))
  270.             {
  271.                 _storageContainer.DeleteFile(fileName);
  272.             }
  273.         }
  274.  
  275.         private void Save()
  276.         {
  277.             using (Stream stream = _storageContainer.CreateFile(fileName))
  278.             {
  279.                 XmlSerializer serializer = new XmlSerializer(typeof(SaveData));
  280.                 serializer.Serialize(stream, SaveData);
  281.             }
  282.         }
  283.  
  284.         public void RequestLoad()
  285.         {
  286.             _loadState = LoadState.ReadyToSelectStorageDevice;
  287.         }
  288.  
  289.         public static SaveData LoadData;
  290.  
  291.         private void Load()
  292.         {
  293.             using (StreamReader reader = new StreamReader(_storageContainer.OpenFile(fileName, FileMode.Open)))
  294.             {
  295.                 XmlSerializer deserializer = new XmlSerializer(typeof(SaveData));
  296.                 LoadData = (SaveData) deserializer.Deserialize(reader);
  297.             }
  298.         }
  299.     }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement