PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/WindowsGame1/WindowsGame1/WindowsGame1/Klassid/Menu.cs

#
C# | 311 lines | 261 code | 10 blank | 40 comment | 49 complexity | a4cdffb629bfc5db1e739894c7f2a1f4 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Audio;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.GamerServices;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using Microsoft.Xna.Framework.Input;
  11. using Microsoft.Xna.Framework.Media;
  12. namespace TnaguTetris
  13. {
  14. /// <summary>
  15. /// Siia tuleb kõik mis on seotud menüüga.
  16. /// </summary>
  17. class Menu
  18. {
  19. Mootor mootor;
  20. public int rgb = 117; // nupu v2rv alguses
  21. public float alpha = 1; // nupu l2bipaistvus alguses
  22. public bool fadingIn = false; // kas fadeb sisse v6i v2lja
  23. public int buttonTime = 0; // nupu vilkumise aeg
  24. public float menuOffset = 1;
  25. public enum State
  26. {
  27. Mainmenu = 1,
  28. Pause = 2,
  29. Gameover = 3,
  30. Playing = 4,
  31. Options = 5,
  32. Credits = 6,
  33. Multiplayer = 7,
  34. MultiplayerWaitPlayer = 8,
  35. MultiplayerJoin = 9,
  36. MultiplayerReady = 10,
  37. MultiplayerPlaying = 11,
  38. Help = 12,
  39. MultiplayerGameOver = 13
  40. }
  41. public static volatile State newMenuState;
  42. public static volatile State menuState;
  43. public const int MainmenuIntValue = 1;
  44. public const int PauseIntValue = 2;
  45. public const int GameoverIntValue = 3;
  46. public const int PlayingIntValue = 4;
  47. public const int OptionsIntValue = 5;
  48. public const int CreditsIntValue = 6;
  49. public const int MultiplayerIntValue = 7;
  50. public const int MultiplayerWaitPlayerIntValue = 8;
  51. public const int MultiplayerJoinIntValue = 9;
  52. public const int MultiplayerReadyIntValue = 10;
  53. public const int MultiplayerPlayingIntValue = 11;
  54. public const int HelpIntValue = 12;
  55. public const int MultiplayerGameOverIntValue = 13;
  56. public bool run = false;
  57. public String[] menuArray; // menyy nuppude array
  58. public int menuArraySelected = 0; // milline menyy nupp valitud on
  59. public int tempMenuArraySelected = 0; // milline menyy nupp valitakse peale animatsiooni
  60. public Color color; // valitud menyynupu v2rvi deklareerimine
  61. public int tempSelect = 0; // kas m6ni menyynupp on ajutiselt valitud (optionsis kui saad nooltega vahetada)
  62. /// <summary>
  63. /// Klassi konstruktor
  64. /// </summary>
  65. public Menu(Mootor mootorO)
  66. {
  67. this.mootor = mootorO;
  68. }
  69. /// <summary>
  70. /// Muudab aktiivseks eelmise või järgmise menüü nupu
  71. /// </summary>
  72. public void MenuMove(string direction)
  73. {
  74. if (menuArray.GetLength(0) != 0)
  75. {
  76. if (direction == "down")
  77. {
  78. if (menuArraySelected == menuArray.GetLength(0) - 1)
  79. {
  80. menuArraySelected = 0;
  81. }
  82. else
  83. {
  84. menuArraySelected += 1;
  85. }
  86. if (menuArray[menuArraySelected] == "")
  87. {
  88. MenuMove("down");
  89. }
  90. }
  91. if (direction == "up")
  92. {
  93. if (menuArraySelected == 0)
  94. {
  95. menuArraySelected = menuArray.GetLength(0) - 1;
  96. }
  97. else
  98. {
  99. menuArraySelected -= 1;
  100. }
  101. if (menuArray[menuArraySelected] == "")
  102. {
  103. MenuMove("up");
  104. }
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// Muudab optionsis muudetud väärtust
  110. /// </summary>
  111. public void ChangeOption(string direction)
  112. {
  113. switch (tempSelect)
  114. {
  115. // alustamise level
  116. case 1:
  117. if (direction == "up")
  118. {
  119. if (mootor.GetLevel() < 20)
  120. {
  121. int lvl = mootor.GetLevel();
  122. lvl++;
  123. mootor.SetLevel(lvl);
  124. }
  125. else
  126. {
  127. mootor.SetLevel(0);
  128. }
  129. }
  130. if (direction == "down")
  131. {
  132. if (mootor.GetLevel() > 0)
  133. {
  134. int lvl = mootor.GetLevel();
  135. lvl--;
  136. mootor.SetLevel(lvl);
  137. }
  138. else
  139. {
  140. mootor.SetLevel(20);
  141. }
  142. }
  143. break;
  144. // leveli t6stmise skoor
  145. case 2:
  146. if (direction == "up")
  147. {
  148. if (mootor.GetScoreLimit() < 50000)
  149. {
  150. int score = mootor.GetScoreLimit();
  151. score += 100;
  152. mootor.SetScoreLimit(score);
  153. }
  154. else
  155. {
  156. mootor.SetScoreLimit(0);
  157. }
  158. }
  159. if (direction == "down")
  160. {
  161. if (mootor.GetScoreLimit() > 0)
  162. {
  163. int score = mootor.GetScoreLimit();
  164. score -= 100;
  165. mootor.SetScoreLimit(score);
  166. }
  167. else
  168. {
  169. mootor.SetScoreLimit(50000);
  170. }
  171. }
  172. break;
  173. // kas n2idatakse cheatLine
  174. case 3:
  175. if (mootor.GetCheatLine() == 0)
  176. {
  177. mootor.SetCheatLine(1);
  178. }
  179. else
  180. {
  181. mootor.SetCheatLine(0);
  182. }
  183. break;
  184. // music
  185. case 4:
  186. if (mootor.GetMusic() == 0)
  187. {
  188. mootor.SetMusic(1);
  189. }
  190. else
  191. {
  192. mootor.SetMusic(0);
  193. }
  194. break;
  195. // sfx
  196. case 5:
  197. if (mootor.GetSfx() == 0)
  198. {
  199. mootor.SetSfx(1);
  200. }
  201. else
  202. {
  203. mootor.SetSfx(0);
  204. }
  205. break;
  206. }
  207. }
  208. /// <summary>
  209. /// Kuvab mängu menüü. Valitud nupp vilgub.
  210. /// </summary>
  211. /// <param name="spriteBatch">Peaklassi joonistaja</param>
  212. /// <param name="font">Jonistuse font</param>
  213. public void Draw(SpriteBatch spriteBatch, SpriteFont font)
  214. {
  215. if (run)
  216. {
  217. if (menuOffset == 0f)
  218. {
  219. menuOffset = 1f;
  220. }
  221. menuOffset += menuOffset * 0.18f;
  222. alpha -= 0.025f;
  223. if(alpha < 0)
  224. alpha = 0f;
  225. if (menuOffset > Game1.graphics.PreferredBackBufferWidth + 10000)
  226. {
  227. run = false;
  228. alpha = 1f;
  229. SetState((int)newMenuState, tempMenuArraySelected);
  230. }
  231. }
  232. else
  233. {
  234. menuOffset = 0;
  235. }
  236. if (menuArray.GetLength(0) != 0)
  237. {
  238. for (int i = 0; i < menuArray.GetLength(0); i++)
  239. {
  240. if (menuArray[i] != "")
  241. {
  242. color = new Color(255, 255, 255)*alpha;
  243. if (menuArraySelected == i)
  244. {
  245. color = new Color(rgb, rgb, 0)*alpha;
  246. }
  247. spriteBatch.DrawString(font, menuArray[i], new Vector2((int)(2 * menuOffset + 22f), (int)(182 + (i * 25))), new Color(0, 0, 0)*alpha);
  248. // kui pole ykski asi options menyys valitud siis on ainult praegune hover
  249. if (tempSelect == 0)
  250. {
  251. spriteBatch.DrawString(font, menuArray[i], new Vector2((int)(2 * menuOffset + 20f), (int)(180 + (i * 25))), color);
  252. }
  253. // kui on valitud midagi options menyys
  254. else
  255. {
  256. // ja valitud on sama mis i, siis on see heledam kui teised
  257. if (tempSelect == i + 1)
  258. {
  259. spriteBatch.DrawString(font, menuArray[i], new Vector2((int)(2 * menuOffset + 20f), (int)(180 + (i * 25))), color);
  260. }
  261. // teised on tumedamad
  262. else
  263. {
  264. spriteBatch.DrawString(font, menuArray[i], new Vector2((int)(2 * menuOffset + 20f), (int)(180 + (i * 25))), Color.DarkKhaki*alpha);
  265. }
  266. }
  267. }
  268. }
  269. }
  270. }
  271. /// <summary>
  272. /// Muutes siin mängu state'i siis toimub ka animatsioon
  273. /// </summary>
  274. /// <param name="state">Millisesse state'i liigutakse</param>
  275. /// <param name="select">Milline menüü nupp on valitud järgmises menüüs</param>
  276. public void SetNewState(int state, int select)
  277. {
  278. newMenuState = (State)state;
  279. tempMenuArraySelected = select;
  280. run = true;
  281. }
  282. /// <summary>
  283. /// Tagastab hetke mängu state
  284. /// </summary>
  285. /// <returns>hetke state</returns>
  286. public int GetState()
  287. {
  288. return (int)menuState;
  289. }
  290. /// <summary>
  291. /// Muutes siin mängu state'i siis animatsiooni ei toimu ning state ilmub koheselt
  292. /// </summary>
  293. /// <param name="state">Millisesse state'i liigutakse</param>
  294. /// <param name="select">Milline menüü nupp on valitud järgmises menüüs</param>
  295. public void SetState(int state, int select)
  296. {
  297. menuArraySelected = select;
  298. menuState = (State)state;
  299. }
  300. }
  301. }