Sample2D.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. //
  2. // Copyright (c) 2008-2018 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/Urho2D/AnimatedSprite2D.h>
  23. #include <Urho3D/Urho2D/AnimationSet2D.h>
  24. #include <Urho3D/UI/BorderImage.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/Context.h>
  32. #include <Urho3D/Core/CoreEvents.h>
  33. #include <Urho3D/Engine/Engine.h>
  34. #include <Urho3D/IO/File.h>
  35. #include <Urho3D/IO/FileSystem.h>
  36. #include <Urho3D/UI/Font.h>
  37. #include <Urho3D/Input/Input.h>
  38. #include <Urho3D/Urho2D/ParticleEffect2D.h>
  39. #include <Urho3D/Urho2D/ParticleEmitter2D.h>
  40. #include <Urho3D/Resource/ResourceCache.h>
  41. #include <Urho3D/Urho2D/RigidBody2D.h>
  42. #include <Urho3D/Scene/Scene.h>
  43. #include <Urho3D/Audio/Sound.h>
  44. #include <Urho3D/Audio/SoundSource.h>
  45. #include <Urho3D/Core/StringUtils.h>
  46. #include <Urho3D/UI/Text.h>
  47. #include <Urho3D/Graphics/Texture2D.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/Scene/ValueAnimation.h>
  54. #include <Urho3D/UI/Window.h>
  55. #include "Utilities2D/Mover.h"
  56. #include "Sample2D.h"
  57. Sample2D::Sample2D(Context* context) :
  58. Object(context)
  59. {
  60. }
  61. void Sample2D::CreateCollisionShapesFromTMXObjects(Node* tileMapNode, TileMapLayer2D* tileMapLayer, TileMapInfo2D info)
  62. {
  63. // Create rigid body to the root node
  64. auto* body = tileMapNode->CreateComponent<RigidBody2D>();
  65. body->SetBodyType(BT_STATIC);
  66. // Generate physics collision shapes and rigid bodies from the tmx file's objects located in "Physics" layer
  67. for (int i = 0; i < tileMapLayer->GetNumObjects(); ++i)
  68. {
  69. TileMapObject2D* tileMapObject = tileMapLayer->GetObject(i); // Get physics objects
  70. // Create collision shape from tmx object
  71. switch (tileMapObject->GetObjectType())
  72. {
  73. case OT_RECTANGLE:
  74. {
  75. CreateRectangleShape(tileMapNode, tileMapObject, tileMapObject->GetSize(), info);
  76. }
  77. break;
  78. case OT_ELLIPSE:
  79. {
  80. CreateCircleShape(tileMapNode, tileMapObject, tileMapObject->GetSize().x_ / 2, info); // Ellipse is built as a Circle shape as it doesn't exist in Box2D
  81. }
  82. break;
  83. case OT_POLYGON:
  84. {
  85. CreatePolygonShape(tileMapNode, tileMapObject);
  86. }
  87. break;
  88. case OT_POLYLINE:
  89. {
  90. CreatePolyLineShape(tileMapNode, tileMapObject);
  91. }
  92. break;
  93. }
  94. }
  95. }
  96. CollisionBox2D* Sample2D::CreateRectangleShape(Node* node, TileMapObject2D* object, Vector2 size, TileMapInfo2D info)
  97. {
  98. auto* shape = node->CreateComponent<CollisionBox2D>();
  99. shape->SetSize(size);
  100. if (info.orientation_ == O_ORTHOGONAL)
  101. shape->SetCenter(object->GetPosition() + size / 2);
  102. else
  103. {
  104. shape->SetCenter(object->GetPosition() + Vector2(info.tileWidth_ / 2, 0.0f));
  105. shape->SetAngle(45.0f); // If our tile map is isometric then shape is losange
  106. }
  107. shape->SetFriction(0.8f);
  108. if (object->HasProperty("Friction"))
  109. shape->SetFriction(ToFloat(object->GetProperty("Friction")));
  110. return shape;
  111. }
  112. CollisionCircle2D* Sample2D::CreateCircleShape(Node* node, TileMapObject2D* object, float radius, TileMapInfo2D info)
  113. {
  114. auto* shape = node->CreateComponent<CollisionCircle2D>();
  115. Vector2 size = object->GetSize();
  116. if (info.orientation_ == O_ORTHOGONAL)
  117. shape->SetCenter(object->GetPosition() + size / 2);
  118. else
  119. {
  120. shape->SetCenter(object->GetPosition() + Vector2(info.tileWidth_ / 2, 0.0f));
  121. }
  122. shape->SetRadius(radius);
  123. shape->SetFriction(0.8f);
  124. if (object->HasProperty("Friction"))
  125. shape->SetFriction(ToFloat(object->GetProperty("Friction")));
  126. return shape;
  127. }
  128. CollisionPolygon2D* Sample2D::CreatePolygonShape(Node* node, TileMapObject2D* object)
  129. {
  130. auto* shape = node->CreateComponent<CollisionPolygon2D>();
  131. int numVertices = object->GetNumPoints();
  132. shape->SetVertexCount(numVertices);
  133. for (int i = 0; i < numVertices; ++i)
  134. shape->SetVertex(i, object->GetPoint(i));
  135. shape->SetFriction(0.8f);
  136. if (object->HasProperty("Friction"))
  137. shape->SetFriction(ToFloat(object->GetProperty("Friction")));
  138. return shape;
  139. }
  140. CollisionChain2D* Sample2D::CreatePolyLineShape(Node* node, TileMapObject2D* object)
  141. {
  142. auto* shape = node->CreateComponent<CollisionChain2D>();
  143. int numVertices = object->GetNumPoints();
  144. shape->SetVertexCount(numVertices);
  145. for (int i = 0; i < numVertices; ++i)
  146. shape->SetVertex(i, object->GetPoint(i));
  147. shape->SetFriction(0.8f);
  148. if (object->HasProperty("Friction"))
  149. shape->SetFriction(ToFloat(object->GetProperty("Friction")));
  150. return shape;
  151. }
  152. Node* Sample2D::CreateCharacter(TileMapInfo2D info, float friction, Vector3 position, float scale)
  153. {
  154. auto* cache = GetSubsystem<ResourceCache>();
  155. Node* spriteNode = scene_->CreateChild("Imp");
  156. spriteNode->SetPosition(position);
  157. spriteNode->SetScale(scale);
  158. auto* animatedSprite = spriteNode->CreateComponent<AnimatedSprite2D>();
  159. // Get scml file and Play "idle" anim
  160. auto* animationSet = cache->GetResource<AnimationSet2D>("Urho2D/imp/imp.scml");
  161. animatedSprite->SetAnimationSet(animationSet);
  162. animatedSprite->SetAnimation("idle");
  163. animatedSprite->SetLayer(3); // Put character over tile map (which is on layer 0) and over Orcs (which are on layer 2)
  164. auto* impBody = spriteNode->CreateComponent<RigidBody2D>();
  165. impBody->SetBodyType(BT_DYNAMIC);
  166. impBody->SetAllowSleep(false);
  167. auto* shape = spriteNode->CreateComponent<CollisionCircle2D>();
  168. shape->SetRadius(1.1f); // Set shape size
  169. shape->SetFriction(friction); // Set friction
  170. shape->SetRestitution(0.1f); // Bounce
  171. return spriteNode;
  172. }
  173. Node* Sample2D::CreateTrigger()
  174. {
  175. Node* node = scene_->CreateChild(); // Clones will be renamed according to object type
  176. auto* body = node->CreateComponent<RigidBody2D>();
  177. body->SetBodyType(BT_STATIC);
  178. auto* shape = node->CreateComponent<CollisionBox2D>(); // Create box shape
  179. shape->SetTrigger(true);
  180. return node;
  181. }
  182. Node* Sample2D::CreateEnemy()
  183. {
  184. auto* cache = GetSubsystem<ResourceCache>();
  185. Node* node = scene_->CreateChild("Enemy");
  186. auto* staticSprite = node->CreateComponent<StaticSprite2D>();
  187. staticSprite->SetSprite(cache->GetResource<Sprite2D>("Urho2D/Aster.png"));
  188. auto* body = node->CreateComponent<RigidBody2D>();
  189. body->SetBodyType(BT_STATIC);
  190. auto* shape = node->CreateComponent<CollisionCircle2D>(); // Create circle shape
  191. shape->SetRadius(0.25f); // Set radius
  192. return node;
  193. }
  194. Node* Sample2D::CreateOrc()
  195. {
  196. auto* cache = GetSubsystem<ResourceCache>();
  197. Node* node = scene_->CreateChild("Orc");
  198. node->SetScale(scene_->GetChild("Imp", true)->GetScale());
  199. auto* animatedSprite = node->CreateComponent<AnimatedSprite2D>();
  200. auto* animationSet = cache->GetResource<AnimationSet2D>("Urho2D/Orc/Orc.scml");
  201. animatedSprite->SetAnimationSet(animationSet);
  202. animatedSprite->SetAnimation("run"); // Get scml file and Play "run" anim
  203. animatedSprite->SetLayer(2); // Make orc always visible
  204. auto* body = node->CreateComponent<RigidBody2D>();
  205. auto* shape = node->CreateComponent<CollisionCircle2D>();
  206. shape->SetRadius(1.3f); // Set shape size
  207. shape->SetTrigger(true);
  208. return node;
  209. }
  210. Node* Sample2D::CreateCoin()
  211. {
  212. auto* cache = GetSubsystem<ResourceCache>();
  213. Node* node = scene_->CreateChild("Coin");
  214. node->SetScale(0.5);
  215. auto* animatedSprite = node->CreateComponent<AnimatedSprite2D>();
  216. auto* animationSet = cache->GetResource<AnimationSet2D>("Urho2D/GoldIcon.scml");
  217. animatedSprite->SetAnimationSet(animationSet); // Get scml file and Play "idle" anim
  218. animatedSprite->SetAnimation("idle");
  219. animatedSprite->SetLayer(4);
  220. auto* body = node->CreateComponent<RigidBody2D>();
  221. body->SetBodyType(BT_STATIC);
  222. auto* shape = node->CreateComponent<CollisionCircle2D>(); // Create circle shape
  223. shape->SetRadius(0.32f); // Set radius
  224. shape->SetTrigger(true);
  225. return node;
  226. }
  227. Node* Sample2D::CreateMovingPlatform()
  228. {
  229. auto* cache = GetSubsystem<ResourceCache>();
  230. Node* node = scene_->CreateChild("MovingPlatform");
  231. node->SetScale(Vector3(3.0f, 1.0f, 0.0f));
  232. auto* staticSprite = node->CreateComponent<StaticSprite2D>();
  233. staticSprite->SetSprite(cache->GetResource<Sprite2D>("Urho2D/Box.png"));
  234. auto* body = node->CreateComponent<RigidBody2D>();
  235. body->SetBodyType(BT_STATIC);
  236. auto* shape = node->CreateComponent<CollisionBox2D>(); // Create box shape
  237. shape->SetSize(Vector2(0.32f, 0.32f)); // Set box size
  238. shape->SetFriction(0.8f); // Set friction
  239. return node;
  240. }
  241. void Sample2D::PopulateMovingEntities(TileMapLayer2D* movingEntitiesLayer)
  242. {
  243. // Create enemy (will be cloned at each placeholder)
  244. Node* enemyNode = CreateEnemy();
  245. Node* orcNode = CreateOrc();
  246. Node* platformNode = CreateMovingPlatform();
  247. // Instantiate enemies and moving platforms at each placeholder (placeholders are Poly Line objects defining a path from points)
  248. for (int i=0; i < movingEntitiesLayer->GetNumObjects(); ++i)
  249. {
  250. // Get placeholder object
  251. TileMapObject2D* movingObject = movingEntitiesLayer->GetObject(i); // Get placeholder object
  252. if (movingObject->GetObjectType() == OT_POLYLINE)
  253. {
  254. // Clone the enemy and position it at placeholder point
  255. Node* movingClone;
  256. Vector2 offset = Vector2(0.0f, 0.0f);
  257. if (movingObject->GetType() == "Enemy")
  258. {
  259. movingClone = enemyNode->Clone();
  260. offset = Vector2(0.0f, -0.32f);
  261. }
  262. else if (movingObject->GetType() == "Orc")
  263. movingClone = orcNode->Clone();
  264. else if (movingObject->GetType() == "MovingPlatform")
  265. movingClone = platformNode->Clone();
  266. else
  267. continue;
  268. movingClone->SetPosition2D(movingObject->GetPoint(0) + offset);
  269. // Create script object that handles entity translation along its path
  270. auto* mover = movingClone->CreateComponent<Mover>();
  271. // Set path from points
  272. PODVector<Vector2> path = CreatePathFromPoints(movingObject, offset);
  273. mover->path_ = path;
  274. // Override default speed
  275. if (movingObject->HasProperty("Speed"))
  276. mover->speed_ = ToFloat(movingObject->GetProperty("Speed"));
  277. }
  278. }
  279. // Remove nodes used for cloning purpose
  280. enemyNode->Remove();
  281. orcNode->Remove();
  282. platformNode->Remove();
  283. }
  284. void Sample2D::PopulateCoins(TileMapLayer2D* coinsLayer)
  285. {
  286. // Create coin (will be cloned at each placeholder)
  287. Node* coinNode = CreateCoin();
  288. // Instantiate coins to pick at each placeholder
  289. for (int i=0; i < coinsLayer->GetNumObjects(); ++i)
  290. {
  291. TileMapObject2D* coinObject = coinsLayer->GetObject(i); // Get placeholder object
  292. Node* coinClone = coinNode->Clone();
  293. coinClone->SetPosition2D(coinObject->GetPosition() + coinObject->GetSize() / 2 + Vector2(0.0f, 0.16f));
  294. }
  295. // Remove node used for cloning purpose
  296. coinNode->Remove();
  297. }
  298. void Sample2D::PopulateTriggers(TileMapLayer2D* triggersLayer)
  299. {
  300. // Create trigger node (will be cloned at each placeholder)
  301. Node* triggerNode = CreateTrigger();
  302. // Instantiate triggers at each placeholder (Rectangle objects)
  303. for (int i=0; i < triggersLayer->GetNumObjects(); ++i)
  304. {
  305. TileMapObject2D* triggerObject = triggersLayer->GetObject(i); // Get placeholder object
  306. if (triggerObject->GetObjectType() == OT_RECTANGLE)
  307. {
  308. Node* triggerClone = triggerNode->Clone();
  309. triggerClone->SetName(triggerObject->GetType());
  310. auto* shape = triggerClone->GetComponent<CollisionBox2D>();
  311. shape->SetSize(triggerObject->GetSize());
  312. triggerClone->SetPosition2D(triggerObject->GetPosition() + triggerObject->GetSize() / 2);
  313. }
  314. }
  315. }
  316. float Sample2D::Zoom(Camera* camera)
  317. {
  318. auto* input = GetSubsystem<Input>();
  319. float zoom_ = camera->GetZoom();
  320. if (input->GetMouseMoveWheel() != 0)
  321. {
  322. zoom_ = Clamp(zoom_ + input->GetMouseMoveWheel() * 0.1f, CAMERA_MIN_DIST, CAMERA_MAX_DIST);
  323. camera->SetZoom(zoom_);
  324. }
  325. if (input->GetKeyDown(KEY_PAGEUP))
  326. {
  327. zoom_ = Clamp(zoom_ * 1.01f, CAMERA_MIN_DIST, CAMERA_MAX_DIST);
  328. camera->SetZoom(zoom_);
  329. }
  330. if (input->GetKeyDown(KEY_PAGEDOWN))
  331. {
  332. zoom_ = Clamp(zoom_ * 0.99f, CAMERA_MIN_DIST, CAMERA_MAX_DIST);
  333. camera->SetZoom(zoom_);
  334. }
  335. return zoom_;
  336. }
  337. PODVector<Vector2> Sample2D::CreatePathFromPoints(TileMapObject2D* object, Vector2 offset)
  338. {
  339. PODVector<Vector2> path;
  340. for (int i=0; i < object->GetNumPoints(); ++i)
  341. path.Push(object->GetPoint(i) + offset);
  342. return path;
  343. }
  344. void Sample2D::CreateUIContent(const String& demoTitle, int remainingLifes, int remainingCoins)
  345. {
  346. auto* cache = GetSubsystem<ResourceCache>();
  347. auto* ui = GetSubsystem<UI>();
  348. // Set the default UI style and font
  349. ui->GetRoot()->SetDefaultStyle(cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
  350. auto* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  351. // We create in-game UIs (coins and lifes) first so that they are hidden by the fullscreen UI (we could also temporary hide them using SetVisible)
  352. // Create the UI for displaying the remaining coins
  353. auto* coinsUI = ui->GetRoot()->CreateChild<BorderImage>("Coins");
  354. coinsUI->SetTexture(cache->GetResource<Texture2D>("Urho2D/GoldIcon.png"));
  355. coinsUI->SetSize(50, 50);
  356. coinsUI->SetImageRect(IntRect(0, 64, 60, 128));
  357. coinsUI->SetAlignment(HA_LEFT, VA_TOP);
  358. coinsUI->SetPosition(5, 5);
  359. auto* coinsText = coinsUI->CreateChild<Text>("CoinsText");
  360. coinsText->SetAlignment(HA_CENTER, VA_CENTER);
  361. coinsText->SetFont(font, 24);
  362. coinsText->SetTextEffect(TE_SHADOW);
  363. coinsText->SetText(String(remainingCoins));
  364. // Create the UI for displaying the remaining lifes
  365. auto* lifeUI = ui->GetRoot()->CreateChild<BorderImage>("Life");
  366. lifeUI->SetTexture(cache->GetResource<Texture2D>("Urho2D/imp/imp_all.png"));
  367. lifeUI->SetSize(70, 80);
  368. lifeUI->SetAlignment(HA_RIGHT, VA_TOP);
  369. lifeUI->SetPosition(-5, 5);
  370. auto* lifeText = lifeUI->CreateChild<Text>("LifeText");
  371. lifeText->SetAlignment(HA_CENTER, VA_CENTER);
  372. lifeText->SetFont(font, 24);
  373. lifeText->SetTextEffect(TE_SHADOW);
  374. lifeText->SetText(String(remainingLifes));
  375. // Create the fullscreen UI for start/end
  376. auto* fullUI = ui->GetRoot()->CreateChild<Window>("FullUI");
  377. fullUI->SetStyleAuto();
  378. fullUI->SetSize(ui->GetRoot()->GetWidth(), ui->GetRoot()->GetHeight());
  379. fullUI->SetEnabled(false); // Do not react to input, only the 'Exit' and 'Play' buttons will
  380. // Create the title
  381. auto* title = fullUI->CreateChild<BorderImage>("Title");
  382. title->SetMinSize(fullUI->GetWidth(), 50);
  383. title->SetTexture(cache->GetResource<Texture2D>("Textures/HeightMap.png"));
  384. title->SetFullImageRect();
  385. title->SetAlignment(HA_CENTER, VA_TOP);
  386. auto* titleText = title->CreateChild<Text>("TitleText");
  387. titleText->SetAlignment(HA_CENTER, VA_CENTER);
  388. titleText->SetFont(font, 24);
  389. titleText->SetText(demoTitle);
  390. // Create the image
  391. auto* spriteUI = fullUI->CreateChild<BorderImage>("Sprite");
  392. spriteUI->SetTexture(cache->GetResource<Texture2D>("Urho2D/imp/imp_all.png"));
  393. spriteUI->SetSize(238, 271);
  394. spriteUI->SetAlignment(HA_CENTER, VA_CENTER);
  395. spriteUI->SetPosition(0, - ui->GetRoot()->GetHeight() / 4);
  396. // Create the 'EXIT' button
  397. auto* exitButton = ui->GetRoot()->CreateChild<Button>("ExitButton");
  398. exitButton->SetStyleAuto();
  399. exitButton->SetFocusMode(FM_RESETFOCUS);
  400. exitButton->SetSize(100, 50);
  401. exitButton->SetAlignment(HA_CENTER, VA_CENTER);
  402. exitButton->SetPosition(-100, 0);
  403. auto* exitText = exitButton->CreateChild<Text>("ExitText");
  404. exitText->SetAlignment(HA_CENTER, VA_CENTER);
  405. exitText->SetFont(font, 24);
  406. exitText->SetText("EXIT");
  407. SubscribeToEvent(exitButton, E_RELEASED, URHO3D_HANDLER(Sample2D, HandleExitButton));
  408. // Create the 'PLAY' button
  409. auto* playButton = ui->GetRoot()->CreateChild<Button>("PlayButton");
  410. playButton->SetStyleAuto();
  411. playButton->SetFocusMode(FM_RESETFOCUS);
  412. playButton->SetSize(100, 50);
  413. playButton->SetAlignment(HA_CENTER, VA_CENTER);
  414. playButton->SetPosition(100, 0);
  415. auto* playText = playButton->CreateChild<Text>("PlayText");
  416. playText->SetAlignment(HA_CENTER, VA_CENTER);
  417. playText->SetFont(font, 24);
  418. playText->SetText("PLAY");
  419. // SubscribeToEvent(playButton, E_RELEASED, HANDLER(Urho2DPlatformer, HandlePlayButton));
  420. // Create the instructions
  421. auto* instructionText = ui->GetRoot()->CreateChild<Text>("Instructions");
  422. instructionText->SetText("Use WASD keys or Arrows to move\nPageUp/PageDown/MouseWheel to zoom\nF5/F7 to save/reload scene\n'Z' to toggle debug geometry\nSpace to fight");
  423. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  424. instructionText->SetTextAlignment(HA_CENTER); // Center rows in relation to each other
  425. instructionText->SetAlignment(HA_CENTER, VA_CENTER);
  426. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  427. // Show mouse cursor
  428. auto* input = GetSubsystem<Input>();
  429. input->SetMouseVisible(true);
  430. }
  431. void Sample2D::HandleExitButton(StringHash eventType, VariantMap& eventData)
  432. {
  433. auto* engine = GetSubsystem<Engine>();
  434. engine->Exit();
  435. }
  436. void Sample2D::SaveScene(bool initial)
  437. {
  438. String filename = demoFilename_;
  439. if (!initial)
  440. filename += "InGame";
  441. File saveFile(context_, GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Scenes/" + filename + ".xml", FILE_WRITE);
  442. scene_->SaveXML(saveFile);
  443. }
  444. void Sample2D::CreateBackgroundSprite(TileMapInfo2D info, float scale, const String& texture, bool animate)
  445. {
  446. auto* cache = GetSubsystem<ResourceCache>();
  447. Node* node = scene_->CreateChild("Background");
  448. node->SetPosition(Vector3(info.GetMapWidth(), info.GetMapHeight(), 0) / 2);
  449. node->SetScale(scale);
  450. auto* sprite = node->CreateComponent<StaticSprite2D>();
  451. sprite->SetSprite(cache->GetResource<Sprite2D>(texture));
  452. SetRandomSeed(Time::GetSystemTime()); // Randomize from system clock
  453. sprite->SetColor(Color(Random(0.0f, 1.0f), Random(0.0f, 1.0f), Random(0.0f, 1.0f), 1.0f));
  454. // Create rotation animation
  455. if (animate)
  456. {
  457. SharedPtr<ValueAnimation> animation(new ValueAnimation(context_));
  458. animation->SetKeyFrame(0, Variant(Quaternion(0.0f, 0.0f, 0.0f)));
  459. animation->SetKeyFrame(1, Variant(Quaternion(0.0f, 0.0f, 180.0f)));
  460. animation->SetKeyFrame(2, Variant(Quaternion(0.0f, 0.0f, 0.0f)));
  461. node->SetAttributeAnimation("Rotation", animation, WM_LOOP, 0.05f);
  462. }
  463. }
  464. void Sample2D::SpawnEffect(Node* node)
  465. {
  466. auto* cache = GetSubsystem<ResourceCache>();
  467. Node* particleNode = node->CreateChild("Emitter");
  468. particleNode->SetScale(0.5 / node->GetScale().x_);
  469. auto* particleEmitter = particleNode->CreateComponent<ParticleEmitter2D>();
  470. particleEmitter->SetLayer(2);
  471. particleEmitter->SetEffect(cache->GetResource<ParticleEffect2D>("Urho2D/sun.pex"));
  472. }
  473. void Sample2D::PlaySoundEffect(const String& soundName)
  474. {
  475. auto* cache = GetSubsystem<ResourceCache>();
  476. auto* source = scene_->CreateComponent<SoundSource>();
  477. auto* sound = cache->GetResource<Sound>("Sounds/" + soundName);
  478. if (sound != nullptr) {
  479. source->SetAutoRemoveMode(REMOVE_COMPONENT);
  480. source->Play(sound);
  481. }
  482. }