PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/JumpJump.Runtime/Coordinate.cs

http://jumpjump.googlecode.com/
C# | 358 lines | 213 code | 32 blank | 113 comment | 17 complexity | 35e71f1f600492cd17bfebe457eaaaf2 MD5 | raw file
  1. namespace JumpJump.Runtime
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. /// <summary>
  8. /// this class use for define a absolute position
  9. /// 2 dimension X,Y
  10. /// </summary>
  11. [Serializable]
  12. public class Coordinate
  13. {
  14. #region Fields
  15. /// <summary>
  16. /// how many direction one coordinate have
  17. /// </summary>
  18. public const int DirectionCount = 6;
  19. /// <summary>
  20. /// x,y delta when go to different direction
  21. /// delta[i,0]: X
  22. /// delta[i,1]: Y
  23. /// new X = X + delta[directionID, 0]
  24. /// </summary>
  25. public static int[,] Delta = new int[6, 2]
  26. {
  27. { 0, 1 },
  28. { 1, 1 },
  29. { 1, 0 },
  30. { 0, -1 },
  31. { -1, -1 },
  32. { -1, 0 },
  33. };
  34. /// <summary>
  35. /// the margin of top for each Y
  36. /// coorhashY[i] means controls whose Y coordinate is i
  37. /// in UI have a property canvas.top = coorhashY[i]
  38. /// </summary>
  39. public static double[] CoorHashY = new double[18]
  40. {
  41. -1, 615, 580, 541, 502, 463, 424, 385.25, 346.5,
  42. 307.75, 269, 230, 191, 152, 114, 75, 36, 0
  43. };
  44. /// <summary>
  45. /// the margin of left of each X
  46. /// coorhashX[i] means controls whose X coordinate is i
  47. /// in UI have a property canvas.top = coorhashX[i]
  48. /// Careful: this X is not same as X in coordinate class
  49. /// X in coordinate is oblique (X1)
  50. /// X here is horizontal (X2)
  51. /// X2 = start(X1) + offset(X1, Y)
  52. /// </summary>
  53. public static double[] CoorHashX = new double[]
  54. {
  55. -1,
  56. 0,
  57. 21.333,
  58. 44.666,
  59. 67.333,
  60. 90.832,
  61. 114.207,
  62. 136.707,
  63. 160.082,
  64. 183.914,
  65. 206.832,
  66. 228.832,
  67. 254.332,
  68. 276.666,
  69. 298.332,
  70. 323.332,
  71. 344.832,
  72. 369.582,
  73. 391.332,
  74. 414.748,
  75. 437.998,
  76. 460.123,
  77. 485.123,
  78. 508.623,
  79. 531.498,
  80. 550
  81. };
  82. /// <summary>
  83. /// each X in coordinate have a start value for X in horizontal
  84. /// start(X1) = EachXStartWithHashX[i]
  85. /// </summary>
  86. public static int[] EachXStartWithHashX = new int[]
  87. {
  88. -1, 1, 2, 3, 4, 1, 3, 5, 7, 9, 10, 11,
  89. 12, 13, 19, 21, 23, 25
  90. };
  91. /// <summary>
  92. /// initial X value
  93. /// </summary>
  94. public int X = -1;
  95. /// <summary>
  96. /// initial Y value
  97. /// </summary>
  98. public int Y = -1;
  99. /// <summary>
  100. /// name and value for each direction
  101. /// </summary>
  102. public enum Direction
  103. {
  104. // 0 1
  105. // 5 X 2
  106. // 4 3
  107. /// <summary>
  108. /// 0 position
  109. /// </summary>
  110. UpLeft = 0,
  111. /// <summary>
  112. /// 1 position
  113. /// </summary>
  114. UpRight = 1,
  115. /// <summary>
  116. /// 2 position
  117. /// </summary>
  118. Right = 2,
  119. /// <summary>
  120. /// 3 position
  121. /// </summary>
  122. DownRight = 3,
  123. /// <summary>
  124. /// 4 position
  125. /// </summary>
  126. DownLeft = 4,
  127. /// <summary>
  128. /// 5 position
  129. /// </summary>
  130. Left = 5,
  131. }
  132. #endregion
  133. #region Constructor
  134. /// <summary>
  135. /// Initializes a new instance of the Coordinate class.
  136. /// Constructor for input x,y
  137. /// </summary>
  138. /// <param name="x">x value</param>
  139. /// <param name="y">y value</param>
  140. public Coordinate(int x, int y)
  141. {
  142. this.X = x;
  143. this.Y = y;
  144. }
  145. /// <summary>
  146. /// Initializes a new instance of the Coordinate class.
  147. /// default constructor
  148. /// </summary>
  149. public Coordinate()
  150. {
  151. }
  152. #endregion
  153. #region Public Methods
  154. /// <summary>
  155. /// for each (dx, dy), which stand for the mouse position
  156. /// calculate the (X,Y) value in chessboard
  157. /// </summary>
  158. /// <param name="dx">mouse position X</param>
  159. /// <param name="dy">mouse position Y</param>
  160. /// <param name="x">chessboard position X</param>
  161. /// <param name="y">chessboard position Y</param>
  162. public static void TransformDxToX(double dx, double dy, ref int x, ref int y)
  163. {
  164. int lineNumY = (int)((dy - 12.5) / 39);
  165. double pieceCenterY = lineNumY * 39 + 12.5;
  166. if (dy - pieceCenterY <= 12.5)
  167. {
  168. y = lineNumY;
  169. y = 17 - y;
  170. }
  171. else if (pieceCenterY + 39 - dy <= 12.5)
  172. {
  173. y = lineNumY + 1;
  174. y = 17 - y;
  175. }
  176. else
  177. {
  178. y = -1;
  179. }
  180. double k = Math.Sqrt(3.0);
  181. double b = 170;
  182. double disX = (k * (dx - 12.5) + 650 - dy - b) / 2;
  183. int lineNumX = (int)(disX / 40.1);
  184. double pieceCenterX = lineNumX * 40.1;
  185. if (Math.Abs(disX - pieceCenterX) <= 12.5)
  186. {
  187. x = lineNumX + 1;
  188. }
  189. else if (pieceCenterX + 39 - disX <= 12.5)
  190. {
  191. x = lineNumX + 2;
  192. }
  193. else
  194. {
  195. x = -1;
  196. }
  197. }
  198. /// <summary>
  199. /// for each (X, Y) in chessboard
  200. /// calculate the position of (dx, dy) in UI
  201. /// </summary>
  202. /// <param name="x">chessboard position X</param>
  203. /// <param name="y">chessboard position Y</param>
  204. /// <param name="dx">UI position X</param>
  205. /// <param name="dy">UI position Y</param>
  206. public static void TransformXToDx(int x, int y, ref double dx, ref double dy)
  207. {
  208. dy = CoorHashY[y];
  209. int tranX = EachXStartWithHashX[x];
  210. int offset = Validator.CoordinateRange[x, 1] - y;
  211. tranX = tranX + offset;
  212. dx = CoorHashX[tranX];
  213. }
  214. /// <summary>
  215. /// override tostring
  216. /// </summary>
  217. /// <returns>X = value, Y = value</returns>
  218. public override string ToString()
  219. {
  220. string str = string.Empty;
  221. str += "X = " + this.X + ", ";
  222. str += "Y = " + this.Y;
  223. return str;
  224. }
  225. /// <summary>
  226. /// override the hash code of this
  227. /// </summary>
  228. /// <returns>(11,11) hash code is 1111</returns>
  229. public override int GetHashCode()
  230. {
  231. return this.X * 100 + this.Y;
  232. }
  233. /// <summary>
  234. /// override Equals
  235. /// if two coordinate have same X and Y, equals
  236. /// </summary>
  237. /// <param name="obj">object to compare</param>
  238. /// <returns>true or false</returns>
  239. public override bool Equals(object obj)
  240. {
  241. if (obj is Coordinate)
  242. {
  243. Coordinate send = obj as Coordinate;
  244. if (this.X == send.X && this.Y == send.Y)
  245. {
  246. return true;
  247. }
  248. else
  249. {
  250. return false;
  251. }
  252. }
  253. else
  254. {
  255. return false;
  256. }
  257. }
  258. /// <summary>
  259. /// Ovverride euqals, compare x,y direcitly
  260. /// </summary>
  261. /// <param name="x">x coordinate</param>
  262. /// <param name="y">coordinate</param>
  263. /// <returns>true or false</returns>
  264. public bool IsSameCoordinate(int x, int y)
  265. {
  266. if (this.X == x && this.Y == y)
  267. {
  268. return true;
  269. }
  270. else
  271. {
  272. return false;
  273. }
  274. }
  275. /// <summary>
  276. /// for a given direction, calculate the coordinate
  277. /// </summary>
  278. /// <param name="dir">direction input</param>
  279. /// <returns>chessboard position</returns>
  280. public Coordinate GetNeighbour(Direction dir)
  281. {
  282. int x = -1;
  283. int y = -1;
  284. switch (dir)
  285. {
  286. case Direction.UpLeft:
  287. x = this.X;
  288. y = this.Y + 1;
  289. break;
  290. case Direction.UpRight:
  291. x = this.X + 1;
  292. y = this.Y + 1;
  293. break;
  294. case Direction.Right:
  295. x = this.X + 1;
  296. y = this.Y;
  297. break;
  298. case Direction.DownRight:
  299. x = this.X;
  300. y = this.Y - 1;
  301. break;
  302. case Direction.DownLeft:
  303. x = this.X - 1;
  304. y = this.Y - 1;
  305. break;
  306. case Direction.Left:
  307. x = this.X - 1;
  308. y = this.Y;
  309. break;
  310. default:
  311. break;
  312. }
  313. // if is validate position, return a new coordinate
  314. if (Validator.ValidatePosition(x, y))
  315. {
  316. Coordinate coor = new Coordinate(x, y);
  317. return coor;
  318. }
  319. else
  320. {
  321. return null;
  322. }
  323. }
  324. #endregion
  325. }
  326. }