/Assets/Grid Framework/Examples/Lights Off/Scripts/Extension Methods/GFGridLightsExampleExtensions.cs

https://bitbucket.org/henchmen/hexagon · C# · 60 lines · 35 code · 6 blank · 19 comment · 19 complexity · 673765bb2360a42e9a765b1a03dc0a12 MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. /*
  4. ABOUT THIS SCRIPT
  5. This script creates an extension method for the GFGrid class, which means it adds a new
  6. method to the class without the need to alter the source code of the class. Once declared
  7. you can use the method just like any normal class method.
  8. One of the design principles behind Grid Framework says that the GFGrid class and all its
  9. methods are abstract and that the exact implementation lies in the sublasses. However, since
  10. extensions methods are static they cannot be abstract, it's simply part of the language
  11. design of C#, so I had to do a workaround. The method performs some preperation and then
  12. it picks the implementation based on the exact type of grid. Note that those functions are
  13. not extension methods, they are just regular methods of this static class.
  14. */
  15. public static class GFGridLightsExampleExtensions{
  16. public static bool IsAdjacent(this GFGrid theGrid, Vector3 position, Vector3 reference){
  17. //convert to Grid Space first
  18. Vector3 gridPosition = theGrid.WorldToGrid(position);//the light we want to test
  19. Vector3 gridReference = theGrid.WorldToGrid(reference);//the light that was pressed
  20. //pick the implentation based on the type of grid
  21. if(theGrid.GetType() == typeof(GFRectGrid)){
  22. return RectIsAdjacent((GFRectGrid)theGrid, gridPosition, gridReference);
  23. } else if(theGrid.GetType() == typeof(GFHexGrid)){
  24. return HexIsAdjacent((GFHexGrid)theGrid, gridPosition, gridReference);
  25. } else{
  26. return false;
  27. }
  28. }
  29. private static bool RectIsAdjacent(GFRectGrid theGrid, Vector3 position, Vector3 reference){
  30. bool isAdjacent = Mathf.Abs(position.x-reference.x) <= 1.25f && Mathf.Abs(position.y-reference.y) <= 1.25f;
  31. bool isDiagonal = 0.25f <= Mathf.Abs(position.x-reference.x) && 0.25f <= Mathf.Abs(position.y-reference.y) && isAdjacent;
  32. return isAdjacent && !isDiagonal;
  33. }
  34. private static bool HexIsAdjacent(GFHexGrid theGrid, Vector3 position, Vector3 reference){
  35. bool isNeighbour = false;
  36. if(Mathf.Abs(reference.y - position.y) < 1.1f && Mathf.Abs(reference.x - position.x) < 0.1f){//straight above or below
  37. isNeighbour = true;
  38. } else{
  39. if(Mathf.RoundToInt(reference.x) % 2 == 0){//two cases, depending on whether the x-coordinate is even or odd
  40. //neighbours are either strictly left or right of the switch or right/left and one unit below
  41. if(Mathf.Abs(reference.x - position.x) < 1.1f && position.y - reference.y < 0.1f && position.y - reference.y > -1.1f)
  42. isNeighbour = true;
  43. } else{//x-coordinate odd
  44. //neighbours are either strictly left or right of the switch or right/left and one unit above
  45. if(Mathf.Abs(reference.x - position.x) < 1.1f && reference.y - position.y < 0.1f && position.y - reference.y < 1.1f)
  46. isNeighbour = true;
  47. }
  48. }
  49. return isNeighbour;
  50. }
  51. }