Sample2D.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. RigidBody2D* 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. CollisionBox2D* 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. CollisionCircle2D* 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. CollisionPolygon2D* 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. CollisionChain2D* 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. ResourceCache* cache = GetSubsystem<ResourceCache>();
  160. Node* spriteNode = scene_->CreateChild("Imp");
  161. spriteNode->SetPosition(position);
  162. spriteNode->SetScale(scale);
  163. AnimatedSprite2D* animatedSprite = spriteNode->CreateComponent<AnimatedSprite2D>();
  164. animatedSprite->SetAnimation(cache->GetResource<AnimationSet2D>("Urho2D/imp/imp.scml"), "idle"); // Get scml file and Play "idle" anim
  165. animatedSprite->SetLayer(2); // Put character over tile map (which is on layer 0) and over Orcs (which are on layer 1)
  166. RigidBody2D* impBody = spriteNode->CreateComponent<RigidBody2D>();
  167. impBody->SetBodyType(BT_DYNAMIC);
  168. impBody->SetAllowSleep(false);
  169. CollisionCircle2D* 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. RigidBody2D* body = node->CreateComponent<RigidBody2D>();
  179. body->SetBodyType(BT_STATIC);
  180. CollisionBox2D* shape = node->CreateComponent<CollisionBox2D>(); // Create box shape
  181. shape->SetTrigger(true);
  182. return node;
  183. }
  184. Node* Sample2D::CreateEnemy()
  185. {
  186. ResourceCache* cache = GetSubsystem<ResourceCache>();
  187. Node* node = scene_->CreateChild("Enemy");
  188. StaticSprite2D* staticSprite = node->CreateComponent<StaticSprite2D>();
  189. staticSprite->SetSprite(cache->GetResource<Sprite2D>("Urho2D/Aster.png"));
  190. RigidBody2D* body = node->CreateComponent<RigidBody2D>();
  191. body->SetBodyType(BT_STATIC);
  192. CollisionCircle2D* shape = node->CreateComponent<CollisionCircle2D>(); // Create circle shape
  193. shape->SetRadius(0.25f); // Set radius
  194. return node;
  195. }
  196. Node* Sample2D::CreateOrc()
  197. {
  198. ResourceCache* cache = GetSubsystem<ResourceCache>();
  199. Node* node = scene_->CreateChild("Orc");
  200. node->SetScale(scene_->GetChild("Imp", true)->GetScale());
  201. AnimatedSprite2D* animatedSprite = node->CreateComponent<AnimatedSprite2D>();
  202. animatedSprite->SetAnimation(cache->GetResource<AnimationSet2D>("Urho2D/Orc/Orc.scml"), "run"); // Get scml file and Play "run" anim
  203. animatedSprite->SetLayer(1); // Make orc always visible
  204. RigidBody2D* body = node->CreateComponent<RigidBody2D>();
  205. CollisionCircle2D* 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. ResourceCache* cache = GetSubsystem<ResourceCache>();
  213. Node* node = scene_->CreateChild("Coin");
  214. node->SetScale(0.5);
  215. AnimatedSprite2D* animatedSprite = node->CreateComponent<AnimatedSprite2D>();
  216. animatedSprite->SetAnimation(cache->GetResource<AnimationSet2D>("Urho2D/GoldIcon.scml"), "idle"); // Get scml file and Play "idle" anim
  217. RigidBody2D* body = node->CreateComponent<RigidBody2D>();
  218. body->SetBodyType(BT_STATIC);
  219. CollisionCircle2D* shape = node->CreateComponent<CollisionCircle2D>(); // Create circle shape
  220. shape->SetRadius(0.32f); // Set radius
  221. shape->SetTrigger(true);
  222. return node;
  223. }
  224. Node* Sample2D::CreateMovingPlatform()
  225. {
  226. ResourceCache* cache = GetSubsystem<ResourceCache>();
  227. Node* node = scene_->CreateChild("MovingPlatform");
  228. node->SetScale(Vector3(3.0f, 1.0f, 0.0f));
  229. StaticSprite2D* staticSprite = node->CreateComponent<StaticSprite2D>();
  230. staticSprite->SetSprite(cache->GetResource<Sprite2D>("Urho2D/Box.png"));
  231. RigidBody2D* body = node->CreateComponent<RigidBody2D>();
  232. body->SetBodyType(BT_STATIC);
  233. CollisionBox2D* shape = node->CreateComponent<CollisionBox2D>(); // Create box shape
  234. shape->SetSize(Vector2(0.32f, 0.32f)); // Set box size
  235. shape->SetFriction(0.8f); // Set friction
  236. return node;
  237. }
  238. void Sample2D::PopulateMovingEntities(TileMapLayer2D* movingEntitiesLayer)
  239. {
  240. // Create enemy (will be cloned at each placeholder)
  241. Node* enemyNode = CreateEnemy();
  242. Node* orcNode = CreateOrc();
  243. Node* platformNode = CreateMovingPlatform();
  244. // Instantiate enemies and moving platforms at each placeholder (placeholders are Poly Line objects defining a path from points)
  245. for (int i=0; i < movingEntitiesLayer->GetNumObjects(); ++i)
  246. {
  247. // Get placeholder object
  248. TileMapObject2D* movingObject = movingEntitiesLayer->GetObject(i); // Get placeholder object
  249. if (movingObject->GetObjectType() == OT_POLYLINE)
  250. {
  251. // Clone the enemy and position it at placeholder point
  252. Node* movingClone;
  253. Vector2 offset = Vector2(0.0f, 0.0f);
  254. if (movingObject->GetType() == "Enemy")
  255. {
  256. movingClone = enemyNode->Clone();
  257. offset = Vector2(0.0f, -0.32f);
  258. }
  259. else if (movingObject->GetType() == "Orc")
  260. movingClone = orcNode->Clone();
  261. else if (movingObject->GetType() == "MovingPlatform")
  262. movingClone = platformNode->Clone();
  263. else
  264. continue;
  265. movingClone->SetPosition2D(movingObject->GetPoint(0) + offset);
  266. // Create script object that handles entity translation along its path
  267. Mover* mover = movingClone->CreateComponent<Mover>();
  268. // Set path from points
  269. PODVector<Vector2> path = CreatePathFromPoints(movingObject, offset);
  270. mover->path_ = path;
  271. // Override default speed
  272. if (movingObject->HasProperty("Speed"))
  273. mover->speed_ = ToFloat(movingObject->GetProperty("Speed"));
  274. }
  275. }
  276. // Remove nodes used for cloning purpose
  277. enemyNode->Remove();
  278. orcNode->Remove();
  279. platformNode->Remove();
  280. }
  281. void Sample2D::PopulateCoins(TileMapLayer2D* coinsLayer)
  282. {
  283. // Create coin (will be cloned at each placeholder)
  284. Node* coinNode = CreateCoin();
  285. // Instantiate coins to pick at each placeholder
  286. for (int i=0; i < coinsLayer->GetNumObjects(); ++i)
  287. {
  288. TileMapObject2D* coinObject = coinsLayer->GetObject(i); // Get placeholder object
  289. Node* coinClone = coinNode->Clone();
  290. coinClone->SetPosition2D(coinObject->GetPosition() + coinObject->GetSize() / 2 + Vector2(0.0f, 0.16f));
  291. }
  292. // Remove node used for cloning purpose
  293. coinNode->Remove();
  294. }
  295. void Sample2D::PopulateTriggers(TileMapLayer2D* triggersLayer)
  296. {
  297. // Create trigger node (will be cloned at each placeholder)
  298. Node* triggerNode = CreateTrigger();
  299. // Instantiate triggers at each placeholder (Rectangle objects)
  300. for (int i=0; i < triggersLayer->GetNumObjects(); ++i)
  301. {
  302. TileMapObject2D* triggerObject = triggersLayer->GetObject(i); // Get placeholder object
  303. if (triggerObject->GetObjectType() == OT_RECTANGLE)
  304. {
  305. Node* triggerClone = triggerNode->Clone();
  306. triggerClone->SetName(triggerObject->GetType());
  307. CollisionBox2D* shape = triggerClone->GetComponent<CollisionBox2D>();
  308. shape->SetSize(triggerObject->GetSize());
  309. triggerClone->SetPosition2D(triggerObject->GetPosition() + triggerObject->GetSize() / 2);
  310. }
  311. }
  312. }
  313. float Sample2D::Zoom(Camera* camera)
  314. {
  315. Input* input = GetSubsystem<Input>();
  316. float zoom_ = camera->GetZoom();
  317. if (input->GetMouseMoveWheel() != 0)
  318. zoom_ = Clamp(zoom_ + input->GetMouseMoveWheel() * 0.1f, CAMERA_MIN_DIST, CAMERA_MAX_DIST);
  319. camera->SetZoom(zoom_);
  320. if (input->GetKeyDown(KEY_PAGEUP))
  321. {
  322. zoom_ = Clamp(zoom_ * 1.01f, CAMERA_MIN_DIST, CAMERA_MAX_DIST);
  323. camera->SetZoom(zoom_);
  324. }
  325. if (input->GetKeyDown(KEY_PAGEDOWN))
  326. {
  327. zoom_ = Clamp(zoom_ * 0.99f, CAMERA_MIN_DIST, CAMERA_MAX_DIST);
  328. camera->SetZoom(zoom_);
  329. }
  330. return zoom_;
  331. }
  332. PODVector<Vector2> Sample2D::CreatePathFromPoints(TileMapObject2D* object, Vector2 offset)
  333. {
  334. PODVector<Vector2> path;
  335. for (int i=0; i < object->GetNumPoints(); ++i)
  336. path.Push(object->GetPoint(i) + offset);
  337. return path;
  338. }
  339. void Sample2D::CreateUIContent(String demoTitle, int remainingLifes, int remainingCoins)
  340. {
  341. ResourceCache* cache = GetSubsystem<ResourceCache>();
  342. UI* ui = GetSubsystem<UI>();
  343. // Set the default UI style and font
  344. ui->GetRoot()->SetDefaultStyle(cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
  345. Font* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  346. // 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)
  347. // Create the UI for displaying the remaining coins
  348. BorderImage* coinsUI = ui->GetRoot()->CreateChild<BorderImage>("Coins");
  349. coinsUI->SetTexture(cache->GetResource<Texture2D>("Urho2D/GoldIcon.png"));
  350. coinsUI->SetSize(50, 50);
  351. coinsUI->SetImageRect(IntRect(0, 64, 60, 128));
  352. coinsUI->SetAlignment(HA_LEFT, VA_TOP);
  353. coinsUI->SetPosition(5, 5);
  354. Text* coinsText = coinsUI->CreateChild<Text>("CoinsText");
  355. coinsText->SetAlignment(HA_CENTER, VA_CENTER);
  356. coinsText->SetFont(font, 24);
  357. coinsText->SetTextEffect(TE_SHADOW);
  358. coinsText->SetText(String(remainingCoins));
  359. // Create the UI for displaying the remaining lifes
  360. BorderImage* lifeUI = ui->GetRoot()->CreateChild<BorderImage>("Life");
  361. lifeUI->SetTexture(cache->GetResource<Texture2D>("Urho2D/imp/imp_all.png"));
  362. lifeUI->SetImageRect(IntRect(2, 153, 238, 298));
  363. lifeUI->SetSize(80, 50);
  364. lifeUI->SetAlignment(HA_RIGHT, VA_TOP);
  365. lifeUI->SetPosition(-5, 5);
  366. Text* lifeText = lifeUI->CreateChild<Text>("LifeText");
  367. lifeText->SetAlignment(HA_CENTER, VA_CENTER);
  368. lifeText->SetFont(font, 24);
  369. lifeText->SetTextEffect(TE_SHADOW);
  370. lifeText->SetText(String(remainingLifes));
  371. // Create the fullscreen UI for start/end
  372. Window* fullUI = ui->GetRoot()->CreateChild<Window>("FullUI");
  373. fullUI->SetStyleAuto();
  374. fullUI->SetSize(ui->GetRoot()->GetWidth(), ui->GetRoot()->GetHeight());
  375. fullUI->SetEnabled(false); // Do not react to input, only the 'Exit' and 'Play' buttons will
  376. // Create the title
  377. BorderImage* title = fullUI->CreateChild<BorderImage>("Title");
  378. title->SetMinSize(fullUI->GetWidth(), 50);
  379. title->SetTexture(cache->GetResource<Texture2D>("Textures/HeightMap.png"));
  380. title->SetFullImageRect();
  381. title->SetAlignment(HA_CENTER, VA_TOP);
  382. Text* titleText = title->CreateChild<Text>("TitleText");
  383. titleText->SetAlignment(HA_CENTER, VA_CENTER);
  384. titleText->SetFont(font, 24);
  385. titleText->SetText(demoTitle);
  386. // Create the image
  387. BorderImage* spriteUI = fullUI->CreateChild<BorderImage>("Sprite");
  388. spriteUI->SetTexture(cache->GetResource<Texture2D>("Urho2D/imp/imp_all.png"));
  389. spriteUI->SetSize(240, 150);
  390. spriteUI->SetImageRect(IntRect(2, 153, 238, 149));
  391. spriteUI->SetAlignment(HA_CENTER, VA_CENTER);
  392. spriteUI->SetPosition(0, - ui->GetRoot()->GetHeight() / 4);
  393. // Create the 'EXIT' button
  394. Button* exitButton = ui->GetRoot()->CreateChild<Button>("ExitButton");
  395. exitButton->SetStyleAuto();
  396. exitButton->SetFocusMode(FM_RESETFOCUS);
  397. exitButton->SetSize(100, 50);
  398. exitButton->SetAlignment(HA_CENTER, VA_CENTER);
  399. exitButton->SetPosition(-100, 0);
  400. Text* exitText = exitButton->CreateChild<Text>("ExitText");
  401. exitText->SetAlignment(HA_CENTER, VA_CENTER);
  402. exitText->SetFont(font, 24);
  403. exitText->SetText("EXIT");
  404. SubscribeToEvent(exitButton, E_RELEASED, HANDLER(Sample2D, HandleExitButton));
  405. // Create the 'PLAY' button
  406. Button* playButton = ui->GetRoot()->CreateChild<Button>("PlayButton");
  407. playButton->SetStyleAuto();
  408. playButton->SetFocusMode(FM_RESETFOCUS);
  409. playButton->SetSize(100, 50);
  410. playButton->SetAlignment(HA_CENTER, VA_CENTER);
  411. playButton->SetPosition(100, 0);
  412. Text* playText = playButton->CreateChild<Text>("PlayText");
  413. playText->SetAlignment(HA_CENTER, VA_CENTER);
  414. playText->SetFont(font, 24);
  415. playText->SetText("PLAY");
  416. // SubscribeToEvent(playButton, E_RELEASED, HANDLER(Urho2DPlatformer, HandlePlayButton));
  417. // Create the instructions
  418. Text* instructionText = ui->GetRoot()->CreateChild<Text>("Instructions");
  419. 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");
  420. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  421. instructionText->SetTextAlignment(HA_CENTER); // Center rows in relation to each other
  422. instructionText->SetAlignment(HA_CENTER, VA_CENTER);
  423. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  424. // Show mouse cursor
  425. Input* input = GetSubsystem<Input>();
  426. input->SetMouseVisible(true);
  427. }
  428. void Sample2D::HandleExitButton(StringHash eventType, VariantMap& eventData)
  429. {
  430. Engine* engine = GetSubsystem<Engine>();
  431. engine->Exit();
  432. }
  433. void Sample2D::SaveScene(bool initial)
  434. {
  435. String filename = demoFilename_;
  436. if (!initial)
  437. filename += "InGame";
  438. File saveFile(context_, GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Scenes/" + filename + ".xml", FILE_WRITE);
  439. scene_->SaveXML(saveFile);
  440. }
  441. void Sample2D::CreateBackgroundSprite(TileMapInfo2D info, float scale, String texture, bool animate)
  442. {
  443. ResourceCache* cache = GetSubsystem<ResourceCache>();
  444. Node* node = scene_->CreateChild("Background");
  445. node->SetPosition(Vector3(info.GetMapWidth(), info.GetMapHeight(), 0) / 2);
  446. node->SetScale(scale);
  447. StaticSprite2D* sprite = node->CreateComponent<StaticSprite2D>();
  448. sprite->SetSprite(cache->GetResource<Sprite2D>(texture));
  449. SetRandomSeed(Time::GetSystemTime()); // Randomize from system clock
  450. sprite->SetColor(Color(Random(0.0f, 1.0f), Random(0.0f, 1.0f), Random(0.0f, 1.0f), 1.0f));
  451. // Create rotation animation
  452. if (animate)
  453. {
  454. SharedPtr<ValueAnimation> animation(new ValueAnimation(context_));
  455. animation->SetKeyFrame(0, Variant(Quaternion(0.0f, 0.0f, 0.0f)));
  456. animation->SetKeyFrame(1, Variant(Quaternion(0.0f, 0.0f, 180.0f)));
  457. animation->SetKeyFrame(2, Variant(Quaternion(0.0f, 0.0f, 0.0f)));
  458. node->SetAttributeAnimation("Rotation", animation, WM_LOOP, 0.05f);
  459. }
  460. }
  461. void Sample2D::SpawnEffect(Node* node)
  462. {
  463. ResourceCache* cache = GetSubsystem<ResourceCache>();
  464. Node* particleNode = node->CreateChild("Emitter");
  465. particleNode->SetScale(0.5 / node->GetScale().x_);
  466. ParticleEmitter2D* particleEmitter = particleNode->CreateComponent<ParticleEmitter2D>();
  467. particleEmitter->SetEffect(cache->GetResource<ParticleEffect2D>("Urho2D/sun.pex"));
  468. }
  469. void Sample2D::PlaySound(String soundName)
  470. {
  471. ResourceCache* cache = GetSubsystem<ResourceCache>();
  472. Node* soundNode = scene_->CreateChild("Sound");
  473. SoundSource* source = soundNode->CreateComponent<SoundSource>();
  474. source->Play(cache->GetResource<Sound>("Sounds/" + soundName));
  475. }