/Code/IntroGame/DemoScreens/DemoScreenMultipleControllers.cpp

http://angel-engine.googlecode.com/ · C++ · 163 lines · 105 code · 17 blank · 41 comment · 10 complexity · 7dab130aaf3da4455a039c28b413a120 MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) 2008-2012, Shane Liesegang
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright
  11. // notice, this list of conditions and the following disclaimer in the
  12. // documentation and/or other materials provided with the distribution.
  13. // * Neither the name of the copyright holder nor the names of any
  14. // contributors may be used to endorse or promote products derived from
  15. // this software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //////////////////////////////////////////////////////////////////////////////
  29. #include "stdafx.h"
  30. #include "DemoScreenMultipleControllers.h"
  31. DemoScreenMultipleControllers::DemoScreenMultipleControllers()
  32. {
  33. }
  34. void DemoScreenMultipleControllers::Start()
  35. {
  36. //Create two Actors that we're going to manipulate with the two
  37. // controllers. All the actual interesting stuff happens in the
  38. // DemoScreenMultipleControllers::Update function.
  39. a = new Actor();
  40. a->SetSize(4.0f);
  41. a->SetPosition(-4.0f, 0.0f);
  42. a->SetColor(1.0f, 1.0f, 0.0f, 0.5f);
  43. theWorld.Add(a);
  44. a2 = new Actor();
  45. a2->SetSize(4.0f);
  46. a2->SetPosition(4.0f, 0.0f);
  47. a2->SetColor(1.0f, 1.0f, 0.0f, 0.5f);
  48. theWorld.Add(a2);
  49. //Demo housekeeping below this point.
  50. #pragma region Demo Housekeeping
  51. t = new TextActor("Console", "These two actors are connected to different controllers.");
  52. t->SetPosition(0, 3.5);
  53. t->SetAlignment(TXT_Center);
  54. t2 = new TextActor("Console", "You can use multiple controllers for two-player games.");
  55. t2->SetPosition(0, -4);
  56. t2->SetAlignment(TXT_Center);
  57. t3 = new TextActor("Console", "(If you only have one [or zero] controllers connected, \nthis screen is kind of boring.)");
  58. t3->SetPosition(0, -8);
  59. t3->SetAlignment(TXT_Center);
  60. theWorld.Add(t);
  61. theWorld.Add(t2);
  62. theWorld.Add(t3);
  63. TextActor *fileLoc = new TextActor("ConsoleSmall", "DemoScreenMultipleControllers.cpp");
  64. fileLoc->SetPosition(MathUtil::ScreenToWorld(5, 763));
  65. fileLoc->SetColor(.3f, .3f, .3f);
  66. theWorld.Add(fileLoc);
  67. _objects.push_back(fileLoc);
  68. _objects.push_back(t);
  69. _objects.push_back(t2);
  70. _objects.push_back(t3);
  71. _objects.push_back(a);
  72. _objects.push_back(a2);
  73. #pragma endregion
  74. }
  75. void DemoScreenMultipleControllers::Update(float dt)
  76. {
  77. //We can access the controllers via the shortcuts:
  78. // controllerOne (identical to theController, if you like that
  79. // nomenclature better in a one-player setting)
  80. // controllerTwo (exactly what you think it is)
  81. //
  82. //The movements we're applying here are the same as the ones in
  83. // DemoScreenMovingActor.cpp
  84. //We always want to do the IsConnected check before reading input, since
  85. // you may get junk values otherwise.
  86. if (controllerOne.IsConnected())
  87. {
  88. Vec2i movementL = controllerOne.GetLeftThumbstick();
  89. Vector2 position;
  90. if (movementL.X)
  91. {
  92. position.X = -4.0f + (3.0f * ((float)movementL.X / 32768.0f));
  93. }
  94. else
  95. {
  96. position.X = -4.0f;
  97. }
  98. if (movementL.Y)
  99. {
  100. position.Y = 3.0f * ((float)movementL.Y / 32768.0f);
  101. }
  102. else
  103. {
  104. position.Y = 0.0f;
  105. }
  106. a->SetPosition(position);
  107. if (controllerOne.IsBButtonDown())
  108. {
  109. a->SetRotation(a->GetRotation() + (90.0f * dt));
  110. if (a->GetRotation() > 360.0f)
  111. {
  112. a->SetRotation(a->GetRotation() - 360.0f);
  113. }
  114. }
  115. }
  116. if (controllerTwo.IsConnected())
  117. {
  118. Vec2i movementL = controllerTwo.GetLeftThumbstick();
  119. Vector2 position;
  120. if (movementL.X)
  121. {
  122. position.X = 4.0f + (3.0f * ((float)movementL.X / 32768.0f));
  123. }
  124. else
  125. {
  126. position.X = 4.0f;
  127. }
  128. if (movementL.Y)
  129. {
  130. position.Y = 3.0f * ((float)movementL.Y / 32768.0f);
  131. }
  132. else
  133. {
  134. position.Y = 0.0f;
  135. }
  136. a2->SetPosition(position);
  137. if (controllerTwo.IsBButtonDown())
  138. {
  139. a2->SetRotation(a2->GetRotation() + (90.0f * dt));
  140. if (a2->GetRotation() > 360.0f)
  141. {
  142. a2->SetRotation(a2->GetRotation() - 360.0f);
  143. }
  144. }
  145. }
  146. }