MyGame.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Urho;
  6. using Urho.Gui;
  7. using Urho.Actions;
  8. using Urho.Shapes;
  9. namespace $safeprojectname$
  10. {
  11. public class MyGame : Application
  12. {
  13. public MyGame() : base(new ApplicationOptions { })
  14. {
  15. }
  16. protected override void Start()
  17. {
  18. CreateScene();
  19. // Subscribe to Esc key:
  20. Input.SubscribeToKeyDown(args => { if (args.Key == Key.Esc) Engine.Exit(); });
  21. }
  22. async void CreateScene()
  23. {
  24. // UI text
  25. var helloText = new Text(Context);
  26. helloText.Value = "Hello World from UrhoSharp";
  27. helloText.HorizontalAlignment = HorizontalAlignment.Center;
  28. helloText.VerticalAlignment = VerticalAlignment.Top;
  29. helloText.SetColor(new Color(r: 0f, g: 1f, b: 1f));
  30. helloText.SetFont(font: ResourceCache.GetFont("Fonts/Font.ttf"), size: 30);
  31. UI.Root.AddChild(helloText);
  32. // 3D scene with Octree
  33. var scene = new Scene(Context);
  34. scene.CreateComponent<Octree>();
  35. // Box
  36. Node boxNode = scene.CreateChild(name: "Box node");
  37. boxNode.Position = new Vector3(x: 0, y: 0, z: 5);
  38. boxNode.SetScale(0f);
  39. boxNode.Rotation = new Quaternion(x: 60, y: 0, z: 30);
  40. StaticModel boxModel = boxNode.CreateComponent<StaticModel>();
  41. boxModel.Model = ResourceCache.GetModel("Models/Box.mdl");
  42. boxModel.SetMaterial(ResourceCache.GetMaterial("Materials/BoxMaterial.xml"));
  43. // Light
  44. Node lightNode = scene.CreateChild(name: "light");
  45. var light = lightNode.CreateComponent<Light>();
  46. light.Range = 10;
  47. light.Brightness = 1.5f;
  48. // Camera
  49. Node cameraNode = scene.CreateChild(name: "camera");
  50. Camera camera = cameraNode.CreateComponent<Camera>();
  51. // Viewport
  52. Renderer.SetViewport(0, new Viewport(Context, scene, camera, null));
  53. // Do actions
  54. await boxNode.RunActionsAsync(new EaseBounceOut(new ScaleTo(duration: 1f, scale: 1)));
  55. await boxNode.RunActionsAsync(new RepeatForever(
  56. new RotateBy(duration: 1, deltaAngleX: 90, deltaAngleY: 0, deltaAngleZ: 0)));
  57. }
  58. }
  59. }