2
0

Urho2DPlatformer.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Audio/Audio.h>
  23. #include <Urho3D/Urho2D/AnimatedSprite2D.h>
  24. #include <Urho3D/Urho2D/AnimationSet2D.h>
  25. #include <Urho3D/UI/Button.h>
  26. #include <Urho3D/Graphics/Camera.h>
  27. #include <Urho3D/Urho2D/CollisionBox2D.h>
  28. #include <Urho3D/Urho2D/CollisionChain2D.h>
  29. #include <Urho3D/Urho2D/CollisionCircle2D.h>
  30. #include <Urho3D/Urho2D/CollisionPolygon2D.h>
  31. #include <Urho3D/Core/CoreEvents.h>
  32. #include <Urho3D/Graphics/DebugRenderer.h>
  33. #include <Urho3D/Engine/Engine.h>
  34. #include <Urho3D/UI/Font.h>
  35. #include <Urho3D/Graphics/Graphics.h>
  36. #include <Urho3D/Graphics/GraphicsEvents.h>
  37. #include <Urho3D/Input/Input.h>
  38. #include <Urho3D/Graphics/Octree.h>
  39. #include <Urho3D/Urho2D/PhysicsEvents2D.h>
  40. #include <Urho3D/Urho2D/PhysicsWorld2D.h>
  41. #include <Urho3D/Graphics/Renderer.h>
  42. #include <Urho3D/Resource/ResourceCache.h>
  43. #include <Urho3D/Urho2D/RigidBody2D.h>
  44. #include <Urho3D/Scene/Scene.h>
  45. #include <Urho3D/Scene/SceneEvents.h>
  46. #include <Urho3D/Core/StringUtils.h>
  47. #include <Urho3D/UI/Text.h>
  48. #include <Urho3D/Urho2D/TileMap2D.h>
  49. #include <Urho3D/Urho2D/TileMapLayer2D.h>
  50. #include <Urho3D/Urho2D/TmxFile2D.h>
  51. #include <Urho3D/UI/UI.h>
  52. #include <Urho3D/UI/UIEvents.h>
  53. #include <Urho3D/Graphics/Zone.h>
  54. #include <Urho3D/DebugNew.h>
  55. #include "Character2D.h"
  56. #include "Utilities2D/Sample2D.h"
  57. #include "Utilities2D/Mover.h"
  58. #include "Urho2DPlatformer.h"
  59. URHO3D_DEFINE_APPLICATION_MAIN(Urho2DPlatformer)
  60. Urho2DPlatformer::Urho2DPlatformer(Context* context) :
  61. Sample(context)
  62. {
  63. // Register factory for the Character2D component so it can be created via CreateComponent
  64. Character2D::RegisterObject(context);
  65. // Register factory and attributes for the Mover component so it can be created via CreateComponent, and loaded / saved
  66. Mover::RegisterObject(context);
  67. }
  68. void Urho2DPlatformer::Setup()
  69. {
  70. Sample::Setup();
  71. engineParameters_[EP_SOUND] = true;
  72. }
  73. void Urho2DPlatformer::Start()
  74. {
  75. // Execute base class startup
  76. Sample::Start();
  77. sample2D_ = new Sample2D(context_);
  78. // Set filename for load/save functions
  79. sample2D_->demoFilename_ = "Platformer2D";
  80. // Create the scene content
  81. CreateScene();
  82. // Create the UI content
  83. sample2D_->CreateUIContent("PLATFORMER 2D DEMO", character2D_->remainingLifes_, character2D_->remainingCoins_);
  84. auto* ui = GetSubsystem<UI>();
  85. Button* playButton = static_cast<Button*>(ui->GetRoot()->GetChild("PlayButton", true));
  86. SubscribeToEvent(playButton, E_RELEASED, URHO3D_HANDLER(Urho2DPlatformer, HandlePlayButton));
  87. // Hook up to the frame update events
  88. SubscribeToEvents();
  89. }
  90. void Urho2DPlatformer::CreateScene()
  91. {
  92. scene_ = new Scene(context_);
  93. sample2D_->scene_ = scene_;
  94. // Create the Octree, DebugRenderer and PhysicsWorld2D components to the scene
  95. scene_->CreateComponent<Octree>();
  96. scene_->CreateComponent<DebugRenderer>();
  97. /*PhysicsWorld2D* physicsWorld =*/ scene_->CreateComponent<PhysicsWorld2D>();
  98. // Create camera
  99. cameraNode_ = scene_->CreateChild("Camera");
  100. auto* camera = cameraNode_->CreateComponent<Camera>();
  101. camera->SetOrthographic(true);
  102. auto* graphics = GetSubsystem<Graphics>();
  103. camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
  104. camera->SetZoom(2.0f * Min((float)graphics->GetWidth() / 1280.0f, (float)graphics->GetHeight() / 800.0f)); // Set zoom according to user's resolution to ensure full visibility (initial zoom (2.0) is set for full visibility at 1280x800 resolution)
  105. // Setup the viewport for displaying the scene
  106. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, camera));
  107. auto* renderer = GetSubsystem<Renderer>();
  108. renderer->SetViewport(0, viewport);
  109. // Set background color for the scene
  110. Zone* zone = renderer->GetDefaultZone();
  111. zone->SetFogColor(Color(0.2f, 0.2f, 0.2f));
  112. // Create tile map from tmx file
  113. auto* cache = GetSubsystem<ResourceCache>();
  114. SharedPtr<Node> tileMapNode(scene_->CreateChild("TileMap"));
  115. auto* tileMap = tileMapNode->CreateComponent<TileMap2D>();
  116. tileMap->SetTmxFile(cache->GetResource<TmxFile2D>("Urho2D/Tilesets/Ortho.tmx"));
  117. const TileMapInfo2D& info = tileMap->GetInfo();
  118. // Create Spriter Imp character (from sample 33_SpriterAnimation)
  119. Node* spriteNode = sample2D_->CreateCharacter(info, 0.8f, Vector3(1.0f, 8.0f, 0.0f), 0.2f);
  120. character2D_ = spriteNode->CreateComponent<Character2D>(); // Create a logic component to handle character behavior
  121. // Generate physics collision shapes from the tmx file's objects located in "Physics" (top) layer
  122. TileMapLayer2D* tileMapLayer = tileMap->GetLayer(tileMap->GetNumLayers() - 1);
  123. sample2D_->CreateCollisionShapesFromTMXObjects(tileMapNode, tileMapLayer, info);
  124. // Instantiate enemies and moving platforms at each placeholder of "MovingEntities" layer (placeholders are Poly Line objects defining a path from points)
  125. sample2D_->PopulateMovingEntities(tileMap->GetLayer(tileMap->GetNumLayers() - 2));
  126. // Instantiate coins to pick at each placeholder of "Coins" layer (placeholders for coins are Rectangle objects)
  127. TileMapLayer2D* coinsLayer = tileMap->GetLayer(tileMap->GetNumLayers() - 3);
  128. sample2D_->PopulateCoins(coinsLayer);
  129. // Init coins counters
  130. character2D_->remainingCoins_ = coinsLayer->GetNumObjects();
  131. character2D_->maxCoins_ = coinsLayer->GetNumObjects();
  132. //Instantiate triggers (for ropes, ladders, lava, slopes...) at each placeholder of "Triggers" layer (placeholders for triggers are Rectangle objects)
  133. sample2D_->PopulateTriggers(tileMap->GetLayer(tileMap->GetNumLayers() - 4));
  134. // Create background
  135. sample2D_->CreateBackgroundSprite(info, 3.5, "Textures/HeightMap.png", true);
  136. // Check when scene is rendered
  137. SubscribeToEvent(E_ENDRENDERING, URHO3D_HANDLER(Urho2DPlatformer, HandleSceneRendered));
  138. }
  139. void Urho2DPlatformer::HandleSceneRendered(StringHash eventType, VariantMap& eventData)
  140. {
  141. UnsubscribeFromEvent(E_ENDRENDERING);
  142. // Save the scene so we can reload it later
  143. sample2D_->SaveScene(true);
  144. // Pause the scene as long as the UI is hiding it
  145. scene_->SetUpdateEnabled(false);
  146. }
  147. void Urho2DPlatformer::SubscribeToEvents()
  148. {
  149. // Subscribe HandleUpdate() function for processing update events
  150. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(Urho2DPlatformer, HandleUpdate));
  151. // Subscribe HandlePostUpdate() function for processing post update events
  152. SubscribeToEvent(E_POSTUPDATE, URHO3D_HANDLER(Urho2DPlatformer, HandlePostUpdate));
  153. // Subscribe to PostRenderUpdate to draw debug geometry
  154. SubscribeToEvent(E_POSTRENDERUPDATE, URHO3D_HANDLER(Urho2DPlatformer, HandlePostRenderUpdate));
  155. // Subscribe to Box2D contact listeners
  156. SubscribeToEvent(E_PHYSICSBEGINCONTACT2D, URHO3D_HANDLER(Urho2DPlatformer, HandleCollisionBegin));
  157. SubscribeToEvent(E_PHYSICSENDCONTACT2D, URHO3D_HANDLER(Urho2DPlatformer, HandleCollisionEnd));
  158. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  159. UnsubscribeFromEvent(E_SCENEUPDATE);
  160. }
  161. void Urho2DPlatformer::HandleCollisionBegin(StringHash eventType, VariantMap& eventData)
  162. {
  163. // Get colliding node
  164. auto* hitNode = static_cast<Node*>(eventData[PhysicsBeginContact2D::P_NODEA].GetPtr());
  165. if (hitNode->GetName() == "Imp")
  166. hitNode = static_cast<Node*>(eventData[PhysicsBeginContact2D::P_NODEB].GetPtr());
  167. String nodeName = hitNode->GetName();
  168. Node* character2DNode = scene_->GetChild("Imp", true);
  169. // Handle ropes and ladders climbing
  170. if (nodeName == "Climb")
  171. {
  172. if (character2D_->isClimbing_) // If transition between rope and top of rope (as we are using split triggers)
  173. character2D_->climb2_ = true;
  174. else
  175. {
  176. character2D_->isClimbing_ = true;
  177. auto* body = character2DNode->GetComponent<RigidBody2D>();
  178. body->SetGravityScale(0.0f); // Override gravity so that the character doesn't fall
  179. // Clear forces so that the character stops (should be performed by setting linear velocity to zero, but currently doesn't work)
  180. body->SetLinearVelocity(Vector2(0.0f, 0.0f));
  181. body->SetAwake(false);
  182. body->SetAwake(true);
  183. }
  184. }
  185. if (nodeName == "CanJump")
  186. character2D_->aboveClimbable_ = true;
  187. // Handle coins picking
  188. if (nodeName == "Coin")
  189. {
  190. hitNode->Remove();
  191. character2D_->remainingCoins_ -= 1;
  192. auto* ui = GetSubsystem<UI>();
  193. if (character2D_->remainingCoins_ == 0)
  194. {
  195. Text* instructions = static_cast<Text*>(ui->GetRoot()->GetChild("Instructions", true));
  196. instructions->SetText("!!! Go to the Exit !!!");
  197. }
  198. Text* coinsText = static_cast<Text*>(ui->GetRoot()->GetChild("CoinsText", true));
  199. coinsText->SetText(String(character2D_->remainingCoins_)); // Update coins UI counter
  200. sample2D_->PlaySoundEffect("Powerup.wav");
  201. }
  202. // Handle interactions with enemies
  203. if (nodeName == "Enemy" || nodeName == "Orc")
  204. {
  205. auto* animatedSprite = character2DNode->GetComponent<AnimatedSprite2D>();
  206. float deltaX = character2DNode->GetPosition().x_ - hitNode->GetPosition().x_;
  207. // Orc killed if character is fighting in its direction when the contact occurs (flowers are not destroyable)
  208. if (nodeName == "Orc" && animatedSprite->GetAnimation() == "attack" && (deltaX < 0 == animatedSprite->GetFlipX()))
  209. {
  210. static_cast<Mover*>(hitNode->GetComponent<Mover>())->emitTime_ = 1;
  211. if (!hitNode->GetChild("Emitter", true))
  212. {
  213. hitNode->GetComponent("RigidBody2D")->Remove(); // Remove Orc's body
  214. sample2D_->SpawnEffect(hitNode);
  215. sample2D_->PlaySoundEffect("BigExplosion.wav");
  216. }
  217. }
  218. // Player killed if not fighting in the direction of the Orc when the contact occurs, or when colliding with a flower
  219. else
  220. {
  221. if (!character2DNode->GetChild("Emitter", true))
  222. {
  223. character2D_->wounded_ = true;
  224. if (nodeName == "Orc")
  225. {
  226. auto* orc = static_cast<Mover*>(hitNode->GetComponent<Mover>());
  227. orc->fightTimer_ = 1;
  228. }
  229. sample2D_->SpawnEffect(character2DNode);
  230. sample2D_->PlaySoundEffect("BigExplosion.wav");
  231. }
  232. }
  233. }
  234. // Handle exiting the level when all coins have been gathered
  235. if (nodeName == "Exit" && character2D_->remainingCoins_ == 0)
  236. {
  237. // Update UI
  238. auto* ui = GetSubsystem<UI>();
  239. Text* instructions = static_cast<Text*>(ui->GetRoot()->GetChild("Instructions", true));
  240. instructions->SetText("!!! WELL DONE !!!");
  241. instructions->SetPosition(IntVector2(0, 0));
  242. // Put the character outside of the scene and magnify him
  243. character2DNode->SetPosition(Vector3(-20.0f, 0.0f, 0.0f));
  244. character2DNode->SetScale(1.5f);
  245. }
  246. // Handle falling into lava
  247. if (nodeName == "Lava")
  248. {
  249. auto* body = character2DNode->GetComponent<RigidBody2D>();
  250. body->ApplyForceToCenter(Vector2(0.0f, 1000.0f), true);
  251. if (!character2DNode->GetChild("Emitter", true))
  252. {
  253. character2D_->wounded_ = true;
  254. sample2D_->SpawnEffect(character2DNode);
  255. sample2D_->PlaySoundEffect("BigExplosion.wav");
  256. }
  257. }
  258. // Handle climbing a slope
  259. if (nodeName == "Slope")
  260. character2D_->onSlope_ = true;
  261. }
  262. void Urho2DPlatformer::HandleCollisionEnd(StringHash eventType, VariantMap& eventData)
  263. {
  264. // Get colliding node
  265. auto* hitNode = static_cast<Node*>(eventData[PhysicsEndContact2D::P_NODEA].GetPtr());
  266. if (hitNode->GetName() == "Imp")
  267. hitNode = static_cast<Node*>(eventData[PhysicsEndContact2D::P_NODEB].GetPtr());
  268. String nodeName = hitNode->GetName();
  269. Node* character2DNode = scene_->GetChild("Imp", true);
  270. // Handle leaving a rope or ladder
  271. if (nodeName == "Climb")
  272. {
  273. if (character2D_->climb2_)
  274. character2D_->climb2_ = false;
  275. else
  276. {
  277. character2D_->isClimbing_ = false;
  278. auto* body = character2DNode->GetComponent<RigidBody2D>();
  279. body->SetGravityScale(1.0f); // Restore gravity
  280. }
  281. }
  282. if (nodeName == "CanJump")
  283. character2D_->aboveClimbable_ = false;
  284. // Handle leaving a slope
  285. if (nodeName == "Slope")
  286. {
  287. character2D_->onSlope_ = false;
  288. // Clear forces (should be performed by setting linear velocity to zero, but currently doesn't work)
  289. auto* body = character2DNode->GetComponent<RigidBody2D>();
  290. body->SetLinearVelocity(Vector2::ZERO);
  291. body->SetAwake(false);
  292. body->SetAwake(true);
  293. }
  294. }
  295. void Urho2DPlatformer::HandleUpdate(StringHash eventType, VariantMap& eventData)
  296. {
  297. using namespace Update;
  298. // Zoom in/out
  299. if (cameraNode_)
  300. sample2D_->Zoom(cameraNode_->GetComponent<Camera>());
  301. auto* input = GetSubsystem<Input>();
  302. // Toggle debug geometry with 'Z' key
  303. if (input->GetKeyPress(KEY_Z))
  304. drawDebug_ = !drawDebug_;
  305. // Check for loading / saving the scene
  306. if (input->GetKeyPress(KEY_F5))
  307. sample2D_->SaveScene(false);
  308. if (input->GetKeyPress(KEY_F7))
  309. ReloadScene(false);
  310. }
  311. void Urho2DPlatformer::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  312. {
  313. if (!character2D_)
  314. return;
  315. Node* character2DNode = character2D_->GetNode();
  316. cameraNode_->SetPosition(Vector3(character2DNode->GetPosition().x_, character2DNode->GetPosition().y_, -10.0f)); // Camera tracks character
  317. }
  318. void Urho2DPlatformer::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
  319. {
  320. if (drawDebug_)
  321. {
  322. auto* physicsWorld = scene_->GetComponent<PhysicsWorld2D>();
  323. physicsWorld->DrawDebugGeometry();
  324. Node* tileMapNode = scene_->GetChild("TileMap", true);
  325. auto* map = tileMapNode->GetComponent<TileMap2D>();
  326. map->DrawDebugGeometry(scene_->GetComponent<DebugRenderer>(), false);
  327. }
  328. }
  329. void Urho2DPlatformer::ReloadScene(bool reInit)
  330. {
  331. String filename = sample2D_->demoFilename_;
  332. if (!reInit)
  333. filename += "InGame";
  334. File loadFile(context_, GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Scenes/" + filename + ".xml", FILE_READ);
  335. scene_->LoadXML(loadFile);
  336. // After loading we have to reacquire the weak pointer to the Character2D component, as it has been recreated
  337. // Simply find the character's scene node by name as there's only one of them
  338. Node* character2DNode = scene_->GetChild("Imp", true);
  339. if (character2DNode)
  340. character2D_ = character2DNode->GetComponent<Character2D>();
  341. // Set what number to use depending whether reload is requested from 'PLAY' button (reInit=true) or 'F7' key (reInit=false)
  342. int lifes = character2D_->remainingLifes_;
  343. int coins = character2D_->remainingCoins_;
  344. if (reInit)
  345. {
  346. lifes = LIFES;
  347. coins = character2D_->maxCoins_;
  348. }
  349. // Update lifes UI
  350. auto* ui = GetSubsystem<UI>();
  351. Text* lifeText = static_cast<Text*>(ui->GetRoot()->GetChild("LifeText", true));
  352. lifeText->SetText(String(lifes));
  353. // Update coins UI
  354. Text* coinsText = static_cast<Text*>(ui->GetRoot()->GetChild("CoinsText", true));
  355. coinsText->SetText(String(coins));
  356. }
  357. void Urho2DPlatformer::HandlePlayButton(StringHash eventType, VariantMap& eventData)
  358. {
  359. // Remove fullscreen UI and unfreeze the scene
  360. auto* ui = GetSubsystem<UI>();
  361. if (static_cast<Text*>(ui->GetRoot()->GetChild("FullUI", true)))
  362. {
  363. ui->GetRoot()->GetChild("FullUI", true)->Remove();
  364. scene_->SetUpdateEnabled(true);
  365. }
  366. else
  367. // Reload scene
  368. ReloadScene(true);
  369. // Hide Instructions and Play/Exit buttons
  370. Text* instructionText = static_cast<Text*>(ui->GetRoot()->GetChild("Instructions", true));
  371. instructionText->SetText("");
  372. Button* exitButton = static_cast<Button*>(ui->GetRoot()->GetChild("ExitButton", true));
  373. exitButton->SetVisible(false);
  374. Button* playButton = static_cast<Button*>(ui->GetRoot()->GetChild("PlayButton", true));
  375. playButton->SetVisible(false);
  376. // Hide mouse cursor
  377. auto* input = GetSubsystem<Input>();
  378. input->SetMouseVisible(false);
  379. }