XNA Game Control Xbox360 & Keyboard

    Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

    • XNA Game Control Xbox360 & Keyboard

      Gute Tag liebe XNA Freunde

      Hiermit Release ich für euch die Xbox360 & Keyboard Controll Funktion.
      Es ist schon alles perfekt eingestellt. Ihr müsst es nur noch an eurem
      XNA Game anpassen.



      "Code"

      Quellcode

      1. /*//---------------------------------------------------//
      2. * GameControl.cs *
      3. * Microsoft XNA Game Platform *
      4. * Adventure of Kanaarh *
      5. * Copyright (C) Jok3r *
      6. * Copyright (C) Kevin Kaymak. All rights reserved *
      7. *///--------------------------------------------------//
      8. using System;
      9. using System.Collections.Generic;
      10. using Microsoft.Xna.Framework;
      11. using Microsoft.Xna.Framework.Input;
      12. namespace AdventureofK
      13. {
      14. public static class GameControl
      15. {
      16. public enum Action
      17. {
      18. MainMenu,
      19. Ok,
      20. Back,
      21. CharackterManagment,
      22. Exit,
      23. TakeView,
      24. DropUnEquip,
      25. MoveUp,
      26. MoveDown,
      27. MoveLeft,
      28. MoveRight,
      29. CursorUp,
      30. CursorDown,
      31. DecreaseAmount,
      32. IncreaseAmount,
      33. PageLeft,
      34. PageRight,
      35. TargetUp,
      36. TargetDown,
      37. ActiveLeft,
      38. ActiveRight,
      39. TotalActionCount,
      40. }
      41. private static readonly string[] actionNames =
      42. {
      43. "Haupt Menü",
      44. "Ok",
      45. "Zurück",
      46. "Charackter Auswahl",
      47. "Spiel Beenden",
      48. "Nehmen/Ansehen",
      49. "Drop/Unequip",
      50. "Move - Up",
      51. "Move - Down",
      52. "Move - Right",
      53. "Move Cursor - Up",
      54. "Move Cursor - Down",
      55. "Decrease Amount",
      56. "Increase Amount",
      57. "Page Left",
      58. "Page Right",
      59. "Target Up",
      60. "Target Down",
      61. "Select Char Left",
      62. "Select Char Right",
      63. };
      64. public static string GetActionName(Action action)
      65. {
      66. int index = (int)action;
      67. if ((index < 0) || (index > actionNames.Length))
      68. {
      69. throw new ArgumentException("action");
      70. }
      71. return actionNames[index];
      72. }
      73. public enum GamePadButtons
      74. {
      75. Start,
      76. Back,
      77. A,
      78. B,
      79. X,
      80. Y,
      81. Up,
      82. Down,
      83. Left,
      84. Right,
      85. LeftSchoulder,
      86. RightSchoulder,
      87. LeftTrigger,
      88. RightTrigger,
      89. }
      90. public class ActionMap
      91. {
      92. public List<GamePadButtons> gamePadButtons = new List<GamePadButtons>();
      93. public List<Keys> keyboardKeys = new List<Keys>();
      94. }
      95. const float analogLimit = 0.5f;
      96. public static KeyboardState currentKeyboardState;
      97. public static KeyboardState CurrentKeyboardState
      98. {
      99. get { return currentKeyboardState; }
      100. }
      101. private static KeyboardState previousKeyboardState;
      102. public static bool IsKeyPressed(Keys key)
      103. {
      104. return currentKeyboardState.IsKeyDown(key);
      105. }
      106. public static bool IsKeyTriggered(Keys key)
      107. {
      108. return (currentKeyboardState.IsKeyDown(key)) &&
      109. (!previousKeyboardState.IsKeyDown(key));
      110. }
      111. //GamePad Controlls
      112. private static GamePadState currentGamePadState;
      113. private static GamePadState CurrentGamePadState
      114. {
      115. get { return currentGamePadState; }
      116. }
      117. private static GamePadState previousGamePadState;
      118. public static bool IsGamePadStartPressed()
      119. {
      120. return (currentGamePadState.Buttons.Start == ButtonState.Pressed);
      121. }
      122. public static bool IsGamePadBackPressed()
      123. {
      124. return (currentGamePadState.Buttons.Back == ButtonState.Pressed);
      125. }
      126. public static bool IsGamePadAPressed()
      127. {
      128. return (currentGamePadState.Buttons.A == ButtonState.Pressed);
      129. }
      130. public static bool IsGamePadBPressed()
      131. {
      132. return (currentGamePadState.Buttons.B == ButtonState.Pressed);
      133. }
      134. public static bool IsGamePadXPressed()
      135. {
      136. return (currentGamePadState.Buttons.X == ButtonState.Pressed);
      137. }
      138. public static bool IsGamePadYkPressed()
      139. {
      140. return (currentGamePadState.Buttons.Y == ButtonState.Pressed);
      141. }
      142. public static bool IsGamePadLeftShoulderPressed()
      143. {
      144. return (currentGamePadState.Buttons.LeftShoulder == ButtonState.Pressed);
      145. }
      146. public static bool IsGamePadRightShoulderPressed()
      147. {
      148. return (currentGamePadState.Buttons.RightShoulder == ButtonState.Pressed);
      149. }
      150. public static bool IsGamePadDPadDownPressed()
      151. {
      152. return (currentGamePadState.DPad.Down == ButtonState.Pressed);
      153. }
      154. public static bool IsGamePadDPadUpPressed()
      155. {
      156. return (currentGamePadState.DPad.Up == ButtonState.Pressed);
      157. }
      158. public static bool IsGamePadDPadLeftPressed()
      159. {
      160. return (currentGamePadState.DPad.Left == ButtonState.Pressed);
      161. }
      162. public static bool IsGamePadDPadRightPressed()
      163. {
      164. return (currentGamePadState.DPad.Right == ButtonState.Pressed);
      165. }
      166. public static bool IsGamePadLeftTriggerPressed()
      167. {
      168. return (currentGamePadState.Triggers.Left > analogLimit);
      169. }
      170. public static bool IsGamePadRightTriggerPressed()
      171. {
      172. return (currentGamePadState.Triggers.Right > analogLimit);
      173. }
      174. public static bool IsGamePadLeftStickUpPressed()
      175. {
      176. return (currentGamePadState.ThumbSticks.Left.Y > analogLimit);
      177. }
      178. public static bool IsGamePadLeftStickDownPressed()
      179. {
      180. return (-1f * currentGamePadState.ThumbSticks.Left.Y > analogLimit);
      181. }
      182. public static bool IsGamePadLeftStickLeftPressed()
      183. {
      184. return (-1f * currentGamePadState.ThumbSticks.Left.X > analogLimit);
      185. }
      186. public static bool IsGamePadLeftStickRightPressed()
      187. {
      188. return (currentGamePadState.ThumbSticks.Left.X > analogLimit);
      189. }
      190. private static bool IsGamePadButtonsPressed(GamePadButtons gamePadKey)
      191. {
      192. switch (gamePadKey)
      193. {
      194. case GamePadButtons.Start:
      195. return IsGamePadStartPressed();
      196. case GamePadButtons.Back:
      197. return IsGamePadBackPressed();
      198. case GamePadButtons.A:
      199. return IsGamePadAPressed();
      200. case GamePadButtons.B:
      201. return IsGamePadBPressed();
      202. case GamePadButtons.X:
      203. return IsGamePadXPressed();
      204. case GamePadButtons.LeftSchoulder:
      205. return IsGamePadLeftShoulderPressed();
      206. case GamePadButtons.RightSchoulder:
      207. return IsGamePadRightShoulderPressed();
      208. case GamePadButtons.LeftTrigger:
      209. return IsGamePadLeftTriggerPressed();
      210. case GamePadButtons.RightTrigger:
      211. return IsGamePadRightTriggerPressed();
      212. case GamePadButtons.Up:
      213. return IsGamePadDPadUpPressed() ||
      214. IsGamePadLeftStickUpPressed();
      215. case GamePadButtons.Down:
      216. return IsGamePadDPadDownPressed() ||
      217. IsGamePadLeftStickDownPressed();
      218. case GamePadButtons.Left:
      219. return IsGamePadDPadLeftPressed() ||
      220. IsGamePadLeftStickLeftPressed();
      221. case GamePadButtons.Right:
      222. return IsGamePadDPadRightPressed() ||
      223. IsGamePadLeftStickRightPressed();
      224. }
      225. return false;
      226. }
      227. //Gamepad Triggers
      228. public static bool IsGamePadStartTriggered()
      229. {
      230. return ((currentGamePadState.Buttons.Start == ButtonState.Pressed) &&
      231. (previousGamePadState.Buttons.Start == ButtonState.Released));
      232. }
      233. public static bool IsGamePadBackTriggered()
      234. {
      235. return ((currentGamePadState.Buttons.Back == ButtonState.Pressed) &&
      236. (previousGamePadState.Buttons.Back == ButtonState.Released));
      237. }
      238. public static bool IsGamePadATriggered()
      239. {
      240. return ((currentGamePadState.Buttons.A == ButtonState.Pressed) &&
      241. (previousGamePadState.Buttons.A == ButtonState.Released));
      242. }
      243. public static bool IsGamePadBTriggered()
      244. {
      245. return ((currentGamePadState.Buttons.B == ButtonState.Pressed) &&
      246. (previousGamePadState.Buttons.B == ButtonState.Released));
      247. }
      248. public static bool IsGamePadXTriggered()
      249. {
      250. return ((currentGamePadState.Buttons.X == ButtonState.Pressed) &&
      251. (previousGamePadState.Buttons.X == ButtonState.Released));
      252. }
      253. public static bool IsGamePadYTriggered()
      254. {
      255. return ((currentGamePadState.Buttons.Y == ButtonState.Pressed) &&
      256. (previousGamePadState.Buttons.Y == ButtonState.Released));
      257. }
      258. public static bool IsGamePadLeftShoulderTriggered()
      259. {
      260. return ((currentGamePadState.Buttons.LeftShoulder == ButtonState.Pressed) &&
      261. (previousGamePadState.Buttons.LeftShoulder == ButtonState.Released));
      262. }
      263. public static bool IsGamePadRightShoulderTriggered()
      264. {
      265. return ((currentGamePadState.Buttons.RightShoulder == ButtonState.Pressed) &&
      266. (previousGamePadState.Buttons.RightShoulder == ButtonState.Released));
      267. }
      268. public static bool IsGamePadDPatUpTriggered()
      269. {
      270. return ((currentGamePadState.DPad.Up == ButtonState.Pressed) &&
      271. (previousGamePadState.DPad.Up == ButtonState.Released));
      272. }
      273. public static bool IsGamePadDPatDownTriggered()
      274. {
      275. return ((currentGamePadState.DPad.Down == ButtonState.Pressed) &&
      276. (previousGamePadState.DPad.Down == ButtonState.Released));
      277. }
      278. public static bool IsGamePadDPatLeftTriggered()
      279. {
      280. return ((currentGamePadState.DPad.Left == ButtonState.Pressed) &&
      281. (previousGamePadState.DPad.Left == ButtonState.Released));
      282. }
      283. public static bool IsGamePadDPatRightTriggered()
      284. {
      285. return ((currentGamePadState.DPad.Right == ButtonState.Pressed) &&
      286. (previousGamePadState.DPad.Right == ButtonState.Released));
      287. }
      288. public static bool IsGamePadLeftTriggerTriggered()
      289. {
      290. return ((currentGamePadState.Triggers.Left > analogLimit) &&
      291. (previousGamePadState.Triggers.Left > analogLimit));
      292. }
      293. public static bool IsGamePadRightTriggerTriggered()
      294. {
      295. return ((currentGamePadState.Triggers.Right > analogLimit) &&
      296. (previousGamePadState.Triggers.Right > analogLimit));
      297. }
      298. public static bool IsGamePadLeftStickUpTriggered()
      299. {
      300. return ((currentGamePadState.ThumbSticks.Left.Y > analogLimit) &&
      301. (previousGamePadState.ThumbSticks.Left.Y > analogLimit));
      302. }
      303. public static bool IsGamePadLeftStickDownTriggered()
      304. {
      305. return ((-1f * currentGamePadState.ThumbSticks.Left.Y > analogLimit) &&
      306. (-1f * previousGamePadState.ThumbSticks.Left.Y > analogLimit));
      307. }
      308. public static bool IsGamePadLeftStickLeftTriggered()
      309. {
      310. return ((-1f * currentGamePadState.ThumbSticks.Left.X > analogLimit) &&
      311. (-1f * previousGamePadState.ThumbSticks.Left.X > analogLimit));
      312. }
      313. public static bool IsGamePadLeftStickRightTriggered()
      314. {
      315. return ((currentGamePadState.ThumbSticks.Left.X > analogLimit) &&
      316. (previousGamePadState.ThumbSticks.Left.X > analogLimit));
      317. }
      318. private static bool IsGamePadButtonTriggered(GamePadButtons gamePadKey)
      319. {
      320. switch (gamePadKey)
      321. {
      322. case GamePadButtons.Start:
      323. return IsGamePadStartTriggered();
      324. case GamePadButtons.Back:
      325. return IsGamePadBackTriggered();
      326. case GamePadButtons.A:
      327. return IsGamePadATriggered();
      328. case GamePadButtons.B:
      329. return IsGamePadBTriggered();
      330. case GamePadButtons.X:
      331. return IsGamePadXTriggered();
      332. case GamePadButtons.Y:
      333. return IsGamePadYTriggered();
      334. case GamePadButtons.LeftSchoulder:
      335. return IsGamePadLeftShoulderTriggered();
      336. case GamePadButtons.RightSchoulder:
      337. return IsGamePadRightShoulderTriggered();
      338. case GamePadButtons.LeftTrigger:
      339. return IsGamePadLeftTriggerTriggered();
      340. case GamePadButtons.RightTrigger:
      341. return IsGamePadRightTriggerTriggered();
      342. case GamePadButtons.Up:
      343. return IsGamePadDPatUpTriggered() ||
      344. IsGamePadLeftStickUpTriggered();
      345. case GamePadButtons.Down:
      346. return IsGamePadDPatDownTriggered() ||
      347. IsGamePadLeftStickDownTriggered();
      348. case GamePadButtons.Left:
      349. return IsGamePadDPatLeftTriggered() ||
      350. IsGamePadLeftStickLeftTriggered();
      351. case GamePadButtons.Right:
      352. return IsGamePadDPatRightTriggered() ||
      353. IsGamePadLeftStickRightTriggered();
      354. }
      355. return false;
      356. }
      357. //Action Mapping Game
      358. private static ActionMap[] actionMaps;
      359. public static ActionMap[] ActionMaps
      360. {
      361. get { return actionMaps; }
      362. }
      363. private static void ResetActionMaps()
      364. {
      365. actionMaps = new ActionMap[(int)Action.TotalActionCount];
      366. actionMaps[(int)Action.MainMenu] = new ActionMap();
      367. actionMaps[(int)Action.MainMenu].keyboardKeys.Add(Keys.Tab);
      368. actionMaps[(int)Action.MainMenu].gamePadButtons.Add(GamePadButtons.Start);
      369. actionMaps[(int)Action.Ok] = new ActionMap();
      370. actionMaps[(int)Action.Ok].keyboardKeys.Add(Keys.Enter);
      371. actionMaps[(int)Action.Ok].gamePadButtons.Add(GamePadButtons.A);
      372. actionMaps[(int)Action.Back] = new ActionMap();
      373. actionMaps[(int)Action.Back].keyboardKeys.Add(Keys.Escape);
      374. actionMaps[(int)Action.Back].gamePadButtons.Add(GamePadButtons.B);
      375. actionMaps[(int)Action.CharackterManagment] = new ActionMap();
      376. actionMaps[(int)Action.CharackterManagment].keyboardKeys.Add(Keys.Space);
      377. actionMaps[(int)Action.CharackterManagment].gamePadButtons.Add(GamePadButtons.Y);
      378. actionMaps[(int)Action.Exit] = new ActionMap();
      379. actionMaps[(int)Action.Exit].keyboardKeys.Add(Keys.Escape);
      380. actionMaps[(int)Action.Exit].gamePadButtons.Add(GamePadButtons.Back);
      381. actionMaps[(int)Action.TakeView] = new ActionMap();
      382. actionMaps[(int)Action.TakeView].keyboardKeys.Add(Keys.LeftControl);
      383. actionMaps[(int)Action.TakeView].gamePadButtons.Add(GamePadButtons.Y);
      384. actionMaps[(int)Action.DropUnEquip] = new ActionMap();
      385. actionMaps[(int)Action.DropUnEquip].keyboardKeys.Add(Keys.D);
      386. actionMaps[(int)Action.DropUnEquip].gamePadButtons.Add(GamePadButtons.X);
      387. actionMaps[(int)Action.MoveUp] = new ActionMap();
      388. actionMaps[(int)Action.MoveUp].keyboardKeys.Add(Keys.Up);
      389. actionMaps[(int)Action.MoveUp].gamePadButtons.Add(GamePadButtons.Up);
      390. actionMaps[(int)Action.MoveDown] = new ActionMap();
      391. actionMaps[(int)Action.MoveDown].keyboardKeys.Add(Keys.Down);
      392. actionMaps[(int)Action.MoveDown].gamePadButtons.Add(GamePadButtons.Down);
      393. actionMaps[(int)Action.MoveLeft] = new ActionMap();
      394. actionMaps[(int)Action.MoveLeft].keyboardKeys.Add(Keys.Left);
      395. actionMaps[(int)Action.MoveLeft].gamePadButtons.Add(GamePadButtons.Left);
      396. actionMaps[(int)Action.MoveRight] = new ActionMap();
      397. actionMaps[(int)Action.MoveRight].keyboardKeys.Add(Keys.Right);
      398. actionMaps[(int)Action.MoveRight].gamePadButtons.Add(GamePadButtons.Right);
      399. actionMaps[(int)Action.CursorUp] = new ActionMap();
      400. actionMaps[(int)Action.CursorUp].keyboardKeys.Add(Keys.Up);
      401. actionMaps[(int)Action.CursorUp].gamePadButtons.Add(GamePadButtons.Up);
      402. actionMaps[(int)Action.CursorDown] = new ActionMap();
      403. actionMaps[(int)Action.CursorDown].keyboardKeys.Add(Keys.Down);
      404. actionMaps[(int)Action.CursorDown].gamePadButtons.Add(GamePadButtons.Down);
      405. actionMaps[(int)Action.DecreaseAmount] = new ActionMap();
      406. actionMaps[(int)Action.DecreaseAmount].keyboardKeys.Add(Keys.Left);
      407. actionMaps[(int)Action.DecreaseAmount].gamePadButtons.Add(GamePadButtons.Left);
      408. actionMaps[(int)Action.IncreaseAmount] = new ActionMap();
      409. actionMaps[(int)Action.IncreaseAmount].keyboardKeys.Add(Keys.Right);
      410. actionMaps[(int)Action.IncreaseAmount].gamePadButtons.Add(GamePadButtons.Right);
      411. actionMaps[(int)Action.PageLeft] = new ActionMap();
      412. actionMaps[(int)Action.PageLeft].keyboardKeys.Add(Keys.LeftShift);
      413. actionMaps[(int)Action.PageLeft].gamePadButtons.Add(GamePadButtons.LeftTrigger);
      414. actionMaps[(int)Action.PageRight] = new ActionMap();
      415. actionMaps[(int)Action.PageRight].keyboardKeys.Add(Keys.RightShift);
      416. actionMaps[(int)Action.PageRight].gamePadButtons.Add(GamePadButtons.RightTrigger);
      417. actionMaps[(int)Action.TargetUp] = new ActionMap();
      418. actionMaps[(int)Action.TargetUp].keyboardKeys.Add(Keys.Up);
      419. actionMaps[(int)Action.TargetUp].gamePadButtons.Add(GamePadButtons.Up);
      420. actionMaps[(int)Action.TargetDown] = new ActionMap();
      421. actionMaps[(int)Action.TargetDown].keyboardKeys.Add(Keys.Down);
      422. actionMaps[(int)Action.TargetDown].gamePadButtons.Add(GamePadButtons.Down);
      423. actionMaps[(int)Action.ActiveLeft] = new ActionMap();
      424. actionMaps[(int)Action.ActiveLeft].keyboardKeys.Add(Keys.Left);
      425. actionMaps[(int)Action.ActiveLeft].gamePadButtons.Add(GamePadButtons.Left);
      426. actionMaps[(int)Action.ActiveRight] = new ActionMap();
      427. actionMaps[(int)Action.ActiveRight].keyboardKeys.Add(Keys.Right);
      428. actionMaps[(int)Action.ActiveRight].gamePadButtons.Add(GamePadButtons.Right);
      429. }
      430. public static bool IsActionPressed(Action action)
      431. {
      432. return IsActionMapPressed(actionMaps[(int)action]);
      433. }
      434. public static bool IsActionTriggered(Action action)
      435. {
      436. return IsActionMapTriggered(actionMaps[(int)action]);
      437. }
      438. private static bool IsActionMapPressed(ActionMap actionMap)
      439. {
      440. for (int i = 0; i < actionMap.keyboardKeys.Count; i++)
      441. {
      442. if (IsKeyPressed(actionMap.keyboardKeys[i]))
      443. {
      444. return true;
      445. }
      446. }
      447. if (currentGamePadState.IsConnected)
      448. {
      449. for (int i = 0; i < actionMap.gamePadButtons.Count; i++)
      450. {
      451. if (IsGamePadButtonsPressed(actionMap.gamePadButtons[i]))
      452. {
      453. return true;
      454. }
      455. }
      456. }
      457. return false;
      458. }
      459. private static bool IsActionMapTriggered(ActionMap actionMap)
      460. {
      461. for (int i = 0; i < actionMap.gamePadButtons.Count; i++)
      462. {
      463. if (IsGamePadButtonTriggered(actionMap.gamePadButtons[i]))
      464. {
      465. return true;
      466. }
      467. }
      468. if (currentGamePadState.IsConnected)
      469. {
      470. for (int i = 0; i < actionMap.gamePadButtons.Count; i++)
      471. {
      472. if (IsGamePadButtonTriggered(actionMap.gamePadButtons[i]))
      473. {
      474. return true;
      475. }
      476. }
      477. }
      478. return false;
      479. }
      480. public static void Initialize()
      481. {
      482. ResetActionMaps();
      483. }
      484. public static void Update()
      485. {
      486. //Update Keyboard
      487. previousKeyboardState = currentKeyboardState;
      488. currentKeyboardState = Keyboard.GetState();
      489. //Update Gamepade
      490. previousGamePadState = currentGamePadState;
      491. currentGamePadState = GamePad.GetState(PlayerIndex.One);
      492. }
      493. }
      494. }
      Alles anzeigen


      Ihr müsst nur euren Namespace anpassen.
      Auserdem müsst ihr in eurer Game.cs folgende dinge adden.


      im feld

      Quellcode

      1. [COLOR=red]protected override void Initialize[/COLOR]

      Quellcode

      1. protected override void Initialize()
      2. {
      3. [COLOR=red] GameControl.Initialize();[/COLOR]
      4. }
      und im feld

      Quellcode

      1. [COLOR=red]protected override void Update(GameTime gameTime)[/COLOR]

      Quellcode

      1. protected override void Update(GameTime gameTime)
      2. {
      3. [COLOR=red] GameControl.Update();[/COLOR]
      4. }
      und schon könnt ihr euren Xbox Controller benutzen und eure
      Tastaturbelegung anpassen!


      Für diesen Code habe ich ca 2 Tage harte Arbeit gebraucht.

      Es sind:
      -600 Zeilen
      -493 Absätze
      -20.564 Zeichen
      -1.143 Wörter


      Ich werde schauen das ich noch mehr solche Dateien Releasen werde, da ich
      ja weniger Zeit habe ein paar XNA Tutorials zu schreiben.


      Wer Support brauch bei diesem Code, kann sich gerne an mich wenden.
    • Werbung zur Unterstützung des Forums ( Bitte AddBlocker deaktivieren )