Sample2D.cpp 21 KB

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