Sample2D.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. //
  2. // Copyright (c) 2008-2014 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/Urho3D.h>
  23. #include <Urho3D/Urho2D/AnimatedSprite2D.h>
  24. #include <Urho3D/Urho2D/AnimationSet2D.h>
  25. #include <Urho3D/UI/BorderImage.h>
  26. #include <Urho3D/UI/Button.h>
  27. #include <Urho3D/Graphics/Camera.h>
  28. #include <Urho3D/Urho2D/CollisionBox2D.h>
  29. #include <Urho3D/Urho2D/CollisionChain2D.h>
  30. #include <Urho3D/Urho2D/CollisionCircle2D.h>
  31. #include <Urho3D/Urho2D/CollisionPolygon2D.h>
  32. #include <Urho3D/Core/Context.h>
  33. #include <Urho3D/Core/CoreEvents.h>
  34. #include <Urho3D/Engine/Engine.h>
  35. #include <Urho3D/IO/File.h>
  36. #include <Urho3D/IO/FileSystem.h>
  37. #include <Urho3D/UI/Font.h>
  38. #include <Urho3D/Input/Input.h>
  39. #include <Urho3D/Urho2D/ParticleEffect2D.h>
  40. #include <Urho3D/Urho2D/ParticleEmitter2D.h>
  41. #include <Urho3D/Math/Random.h>
  42. #include <Urho3D/Resource/ResourceCache.h>
  43. #include <Urho3D/Urho2D/RigidBody2D.h>
  44. #include <Urho3D/Scene/Scene.h>
  45. #include <Urho3D/Audio/Sound.h>
  46. #include <Urho3D/Audio/SoundSource.h>
  47. #include <Urho3D/Core/StringUtils.h>
  48. #include <Urho3D/UI/Text.h>
  49. #include <Urho3D/Graphics/Texture2D.h>
  50. #include <Urho3D/Urho2D/TileMap2D.h>
  51. #include <Urho3D/Urho2D/TileMapLayer2D.h>
  52. #include <Urho3D/Urho2D/TmxFile2D.h>
  53. #include <Urho3D/UI/UI.h>
  54. #include <Urho3D/UI/UIEvents.h>
  55. #include <Urho3D/Scene/ValueAnimation.h>
  56. #include <Urho3D/UI/Window.h>
  57. #include "Utilities2D/Mover.h"
  58. #include "Sample2D.h"
  59. Sample2D::Sample2D(Context* context) :
  60. Object(context)
  61. {
  62. }
  63. void Sample2D::CreateCollisionShapesFromTMXObjects(Node* tileMapNode, TileMapLayer2D* tileMapLayer, TileMapInfo2D info)
  64. {
  65. // Create rigid body to the root node
  66. auto* body = tileMapNode->CreateComponent<RigidBody2D>();
  67. body->SetBodyType(BT_STATIC);
  68. // Generate physics collision shapes and rigid bodies from the tmx file's objects located in "Physics" layer
  69. for (int i = 0; i < tileMapLayer->GetNumObjects(); ++i)
  70. {
  71. TileMapObject2D* tileMapObject = tileMapLayer->GetObject(i); // Get physics objects
  72. // Create collision shape from tmx object
  73. switch (tileMapObject->GetObjectType())
  74. {
  75. case OT_RECTANGLE:
  76. {
  77. CreateRectangleShape(tileMapNode, tileMapObject, tileMapObject->GetSize(), info);
  78. }
  79. break;
  80. case OT_ELLIPSE:
  81. {
  82. CreateCircleShape(tileMapNode, tileMapObject, tileMapObject->GetSize().x_ / 2, info); // Ellipse is built as a Circle shape as it doesn't exist in Box2D
  83. }
  84. break;
  85. case OT_POLYGON:
  86. {
  87. CreatePolygonShape(tileMapNode, tileMapObject);
  88. }
  89. break;
  90. case OT_POLYLINE:
  91. {
  92. CreatePolyLineShape(tileMapNode, tileMapObject);
  93. }
  94. break;
  95. }
  96. }
  97. }
  98. CollisionBox2D* Sample2D::CreateRectangleShape(Node* node, TileMapObject2D* object, Vector2 size, TileMapInfo2D info)
  99. {
  100. auto* shape = node->CreateComponent<CollisionBox2D>();
  101. shape->SetSize(size);
  102. if (info.orientation_ == O_ORTHOGONAL)
  103. shape->SetCenter(object->GetPosition() + size / 2);
  104. else
  105. {
  106. shape->SetCenter(object->GetPosition() + Vector2(info.tileWidth_ / 2, 0.0f));
  107. shape->SetAngle(45.0f); // If our tile map is isometric then shape is losange
  108. }
  109. shape->SetFriction(0.8f);
  110. if (object->HasProperty("Friction"))
  111. shape->SetFriction(ToFloat(object->GetProperty("Friction")));
  112. return shape;
  113. }
  114. CollisionCircle2D* Sample2D::CreateCircleShape(Node* node, TileMapObject2D* object, float radius, TileMapInfo2D info)
  115. {
  116. auto* shape = node->CreateComponent<CollisionCircle2D>();
  117. Vector2 size = object->GetSize();
  118. if (info.orientation_ == O_ORTHOGONAL)
  119. shape->SetCenter(object->GetPosition() + size / 2);
  120. else
  121. {
  122. shape->SetCenter(object->GetPosition() + Vector2(info.tileWidth_ / 2, 0.0f));
  123. }
  124. shape->SetRadius(radius);
  125. shape->SetFriction(0.8f);
  126. if (object->HasProperty("Friction"))
  127. shape->SetFriction(ToFloat(object->GetProperty("Friction")));
  128. return shape;
  129. }
  130. CollisionPolygon2D* Sample2D::CreatePolygonShape(Node* node, TileMapObject2D* object)
  131. {
  132. auto* shape = node->CreateComponent<CollisionPolygon2D>();
  133. int numVertices = object->GetNumPoints();
  134. shape->SetVertexCount(numVertices);
  135. for (int i = 0; i < numVertices; ++i)
  136. shape->SetVertex(i, object->GetPoint(i));
  137. shape->SetFriction(0.8f);
  138. if (object->HasProperty("Friction"))
  139. shape->SetFriction(ToFloat(object->GetProperty("Friction")));
  140. return shape;
  141. }
  142. CollisionChain2D* Sample2D::CreatePolyLineShape(Node* node, TileMapObject2D* object)
  143. {
  144. auto* shape = node->CreateComponent<CollisionChain2D>();
  145. int numVertices = object->GetNumPoints();
  146. shape->SetVertexCount(numVertices);
  147. for (int i = 0; i < numVertices; ++i)
  148. shape->SetVertex(i, object->GetPoint(i));
  149. shape->SetFriction(0.8f);
  150. if (object->HasProperty("Friction"))
  151. shape->SetFriction(ToFloat(object->GetProperty("Friction")));
  152. return shape;
  153. }
  154. Node* Sample2D::CreateCharacter(TileMapInfo2D info, float friction, Vector3 position, float scale)
  155. {
  156. auto* cache = GetSubsystem<ResourceCache>();
  157. Node* spriteNode = scene_->CreateChild("Imp");
  158. spriteNode->SetPosition(position);
  159. spriteNode->SetScale(scale);
  160. auto* animatedSprite = spriteNode->CreateComponent<AnimatedSprite2D>();
  161. // Get scml file and Play "idle" anim
  162. auto* animationSet = cache->GetResource<AnimationSet2D>("Urho2D/imp/imp.scml");
  163. animatedSprite->SetAnimationSet(animationSet);
  164. animatedSprite->SetAnimation("idle");
  165. animatedSprite->SetLayer(3); // Put character over tile map (which is on layer 0) and over Orcs (which are on layer 2)
  166. auto* impBody = spriteNode->CreateComponent<RigidBody2D>();
  167. impBody->SetBodyType(BT_DYNAMIC);
  168. impBody->SetAllowSleep(false);
  169. auto* shape = spriteNode->CreateComponent<CollisionCircle2D>();
  170. shape->SetRadius(1.1f); // Set shape size
  171. shape->SetFriction(friction); // Set friction
  172. shape->SetRestitution(0.1f); // Bounce
  173. return spriteNode;
  174. }
  175. Node* Sample2D::CreateTrigger()
  176. {
  177. Node* node = scene_->CreateChild(); // Clones will be renamed according to object type
  178. auto* body = node->CreateComponent<RigidBody2D>();
  179. body->SetBodyType(BT_STATIC);
  180. auto* shape = node->CreateComponent<CollisionBox2D>(); // Create box shape
  181. shape->SetTrigger(true);
  182. return node;
  183. }
  184. Node* Sample2D::CreateEnemy()
  185. {
  186. auto* cache = GetSubsystem<ResourceCache>();
  187. Node* node = scene_->CreateChild("Enemy");
  188. auto* staticSprite = node->CreateComponent<StaticSprite2D>();
  189. staticSprite->SetSprite(cache->GetResource<Sprite2D>("Urho2D/Aster.png"));
  190. auto* body = node->CreateComponent<RigidBody2D>();
  191. body->SetBodyType(BT_STATIC);
  192. auto* shape = node->CreateComponent<CollisionCircle2D>(); // Create circle shape
  193. shape->SetRadius(0.25f); // Set radius
  194. return node;
  195. }
  196. Node* Sample2D::CreateOrc()
  197. {
  198. auto* cache = GetSubsystem<ResourceCache>();
  199. Node* node = scene_->CreateChild("Orc");
  200. node->SetScale(scene_->GetChild("Imp", true)->GetScale());
  201. auto* animatedSprite = node->CreateComponent<AnimatedSprite2D>();
  202. auto* animationSet = cache->GetResource<AnimationSet2D>("Urho2D/Orc/Orc.scml");
  203. animatedSprite->SetAnimationSet(animationSet);
  204. animatedSprite->SetAnimation("run"); // Get scml file and Play "run" anim
  205. animatedSprite->SetLayer(2); // Make orc always visible
  206. auto* body = node->CreateComponent<RigidBody2D>();
  207. auto* shape = node->CreateComponent<CollisionCircle2D>();
  208. shape->SetRadius(1.3f); // Set shape size
  209. shape->SetTrigger(true);
  210. return node;
  211. }
  212. Node* Sample2D::CreateCoin()
  213. {
  214. auto* cache = GetSubsystem<ResourceCache>();
  215. Node* node = scene_->CreateChild("Coin");
  216. node->SetScale(0.5);
  217. auto* animatedSprite = node->CreateComponent<AnimatedSprite2D>();
  218. auto* animationSet = cache->GetResource<AnimationSet2D>("Urho2D/GoldIcon.scml");
  219. animatedSprite->SetAnimationSet(animationSet); // Get scml file and Play "idle" anim
  220. animatedSprite->SetAnimation("idle");
  221. animatedSprite->SetLayer(4);
  222. auto* body = node->CreateComponent<RigidBody2D>();
  223. body->SetBodyType(BT_STATIC);
  224. auto* shape = node->CreateComponent<CollisionCircle2D>(); // Create circle shape
  225. shape->SetRadius(0.32f); // Set radius
  226. shape->SetTrigger(true);
  227. return node;
  228. }
  229. Node* Sample2D::CreateMovingPlatform()
  230. {
  231. auto* cache = GetSubsystem<ResourceCache>();
  232. Node* node = scene_->CreateChild("MovingPlatform");
  233. node->SetScale(Vector3(3.0f, 1.0f, 0.0f));
  234. auto* staticSprite = node->CreateComponent<StaticSprite2D>();
  235. staticSprite->SetSprite(cache->GetResource<Sprite2D>("Urho2D/Box.png"));
  236. auto* body = node->CreateComponent<RigidBody2D>();
  237. body->SetBodyType(BT_STATIC);
  238. auto* shape = node->CreateComponent<CollisionBox2D>(); // Create box shape
  239. shape->SetSize(Vector2(0.32f, 0.32f)); // Set box size
  240. shape->SetFriction(0.8f); // Set friction
  241. return node;
  242. }
  243. void Sample2D::PopulateMovingEntities(TileMapLayer2D* movingEntitiesLayer)
  244. {
  245. // Create enemy (will be cloned at each placeholder)
  246. Node* enemyNode = CreateEnemy();
  247. Node* orcNode = CreateOrc();
  248. Node* platformNode = CreateMovingPlatform();
  249. // Instantiate enemies and moving platforms at each placeholder (placeholders are Poly Line objects defining a path from points)
  250. for (int i=0; i < movingEntitiesLayer->GetNumObjects(); ++i)
  251. {
  252. // Get placeholder object
  253. TileMapObject2D* movingObject = movingEntitiesLayer->GetObject(i); // Get placeholder object
  254. if (movingObject->GetObjectType() == OT_POLYLINE)
  255. {
  256. // Clone the enemy and position it at placeholder point
  257. Node* movingClone;
  258. Vector2 offset = Vector2(0.0f, 0.0f);
  259. if (movingObject->GetType() == "Enemy")
  260. {
  261. movingClone = enemyNode->Clone();
  262. offset = Vector2(0.0f, -0.32f);
  263. }
  264. else if (movingObject->GetType() == "Orc")
  265. movingClone = orcNode->Clone();
  266. else if (movingObject->GetType() == "MovingPlatform")
  267. movingClone = platformNode->Clone();
  268. else
  269. continue;
  270. movingClone->SetPosition2D(movingObject->GetPoint(0) + offset);
  271. // Create script object that handles entity translation along its path
  272. auto* mover = movingClone->CreateComponent<Mover>();
  273. // Set path from points
  274. PODVector<Vector2> path = CreatePathFromPoints(movingObject, offset);
  275. mover->path_ = path;
  276. // Override default speed
  277. if (movingObject->HasProperty("Speed"))
  278. mover->speed_ = ToFloat(movingObject->GetProperty("Speed"));
  279. }
  280. }
  281. // Remove nodes used for cloning purpose
  282. enemyNode->Remove();
  283. orcNode->Remove();
  284. platformNode->Remove();
  285. }
  286. void Sample2D::PopulateCoins(TileMapLayer2D* coinsLayer)
  287. {
  288. // Create coin (will be cloned at each placeholder)
  289. Node* coinNode = CreateCoin();
  290. // Instantiate coins to pick at each placeholder
  291. for (int i=0; i < coinsLayer->GetNumObjects(); ++i)
  292. {
  293. TileMapObject2D* coinObject = coinsLayer->GetObject(i); // Get placeholder object
  294. Node* coinClone = coinNode->Clone();
  295. coinClone->SetPosition2D(coinObject->GetPosition() + coinObject->GetSize() / 2 + Vector2(0.0f, 0.16f));
  296. }
  297. // Remove node used for cloning purpose
  298. coinNode->Remove();
  299. }
  300. void Sample2D::PopulateTriggers(TileMapLayer2D* triggersLayer)
  301. {
  302. // Create trigger node (will be cloned at each placeholder)
  303. Node* triggerNode = CreateTrigger();
  304. // Instantiate triggers at each placeholder (Rectangle objects)
  305. for (int i=0; i < triggersLayer->GetNumObjects(); ++i)
  306. {
  307. TileMapObject2D* triggerObject = triggersLayer->GetObject(i); // Get placeholder object
  308. if (triggerObject->GetObjectType() == OT_RECTANGLE)
  309. {
  310. Node* triggerClone = triggerNode->Clone();
  311. triggerClone->SetName(triggerObject->GetType());
  312. auto* shape = triggerClone->GetComponent<CollisionBox2D>();
  313. shape->SetSize(triggerObject->GetSize());
  314. triggerClone->SetPosition2D(triggerObject->GetPosition() + triggerObject->GetSize() / 2);
  315. }
  316. }
  317. }
  318. float Sample2D::Zoom(Camera* camera)
  319. {
  320. auto* input = GetSubsystem<Input>();
  321. float zoom_ = camera->GetZoom();
  322. if (input->GetMouseMoveWheel() != 0)
  323. zoom_ = Clamp(zoom_ + input->GetMouseMoveWheel() * 0.1f, CAMERA_MIN_DIST, CAMERA_MAX_DIST);
  324. camera->SetZoom(zoom_);
  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(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, 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(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. }