Program.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using AtomicEngine;
  5. using AtomicPlayer;
  6. class BunnyMark : ScriptObject
  7. {
  8. class Bunny
  9. {
  10. public Node node;
  11. public Vector2 position = new Vector2 ();
  12. public Vector2 speed = new Vector2 ();
  13. }
  14. public BunnyMark ()
  15. {
  16. var player = Atomic.GetSubsystem<Player> ();
  17. player.LoadScene ("Scenes/Scene.scene");
  18. scene = player.CurrentScene;
  19. SubscribeToEvent ("Update", handleUpdate);
  20. SubscribeToEvent ("MouseButtonDown", handleMouseButtonDown);
  21. SubscribeToEvent ("MouseButtonUp", handleMouseButtonUp);
  22. var graphics = Atomic.GetSubsystem<Graphics> ();
  23. var cache = Atomic.GetSubsystem<ResourceCache> ();
  24. halfWidth = graphics.Width * pixelSize * 0.5f;
  25. halfHeight = graphics.Height * pixelSize * 0.5f;
  26. maxX = halfWidth;
  27. minX = -halfWidth;
  28. maxY = halfHeight;
  29. minY = -halfHeight;
  30. // TODO: generic version of this
  31. var sheet = (SpriteSheet2D)cache.GetResource ("SpriteSheet2D", "Sprites/bunnys_sheet.xml");
  32. var bunny1 = sheet.GetSprite ("bunny1");
  33. var bunny2 = sheet.GetSprite ("bunny2");
  34. var bunny3 = sheet.GetSprite ("bunny3");
  35. var bunny4 = sheet.GetSprite ("bunny4");
  36. var bunny5 = sheet.GetSprite ("bunny5");
  37. bunnyTextures = new Sprite2D[] { bunny1, bunny2, bunny3, bunny4, bunny5 };
  38. bunnyType = 2;
  39. currentTexture = bunnyTextures [bunnyType];
  40. }
  41. void handleMouseButtonDown (VariantMap eventData)
  42. {
  43. isAdding = true;
  44. }
  45. void handleMouseButtonUp (VariantMap eventData)
  46. {
  47. isAdding = false;
  48. bunnyType++;
  49. bunnyType %= 5;
  50. currentTexture = bunnyTextures [bunnyType];
  51. }
  52. void handleUpdate (VariantMap eventData)
  53. {
  54. if (isAdding) {
  55. var scale = new Vector2 ();
  56. var initPos = new Vector2 (minX, maxY);
  57. for (var i = 0; i < amount; i++) {
  58. var bunny = new Bunny ();
  59. bunnies.Add (bunny);
  60. var node = scene.CreateChild ();
  61. bunny.node = node;
  62. var sprite = (StaticSprite2D)node.CreateComponent ("StaticSprite2D");
  63. sprite.BlendMode = BlendMode.BLEND_ALPHA;
  64. sprite.Sprite = currentTexture;
  65. node.Position2D = bunny.position = initPos;
  66. bunny.speed.x = (float)(random.NextDouble () * 10);
  67. bunny.speed.y = (float)(random.NextDouble () * 10) - 5;
  68. scale.x = scale.y = (0.5f + ((float)random.NextDouble ()) * 0.5f);
  69. node.Scale2D = scale;
  70. node.Rotation2D = (((float)random.NextDouble ()) - 0.5f);
  71. }
  72. }
  73. foreach (var bunny in bunnies) {
  74. var px = bunny.position.x;
  75. var py = bunny.position.y;
  76. var speedX = bunny.speed.x;
  77. var speedY = bunny.speed.y;
  78. px += speedX * .002f;
  79. py += speedY * .002f;
  80. if (px > maxX) {
  81. speedX *= -1;
  82. px = maxX;
  83. } else if (px < minX) {
  84. speedX *= -1;
  85. px = minX;
  86. }
  87. if (py > maxY) {
  88. speedY = 0;
  89. py = maxY;
  90. } else if (py < minY) {
  91. speedY *= -0.95f;
  92. if (((float)random.NextDouble ()) > 0.5f) {
  93. speedY -= ((float)random.NextDouble ()) * 6f;
  94. }
  95. py = minY;
  96. }
  97. bunny.speed.x = speedX;
  98. bunny.speed.y = speedY + gravity;
  99. bunny.position.x = px;
  100. bunny.position.y = py;
  101. bunny.node.Position2D = bunny.position;
  102. ;
  103. }
  104. }
  105. Random random = new Random ();
  106. List<Bunny> bunnies = new List<Bunny> ();
  107. int amount = 3;
  108. float gravity = -0.5f;
  109. Scene scene;
  110. Sprite2D[] bunnyTextures;
  111. bool isAdding = false;
  112. int bunnyType = 2;
  113. Sprite2D currentTexture;
  114. float pixelSize = 0.01f;
  115. float halfWidth;
  116. float halfHeight;
  117. float maxX;
  118. float minX;
  119. float maxY;
  120. float minY;
  121. }
  122. enum MyEnum
  123. {
  124. Peaceful,
  125. Friendly,
  126. Aggressive
  127. }
  128. class Spinner : CSComponent
  129. {
  130. [Inspector]
  131. public int MyField = 42;
  132. [Inspector("Sprites/star.png")]
  133. public Sprite2D MySprite;
  134. [Inspector]
  135. public Vector3 Vector3Field = new Vector3(1, 2, 3);
  136. [Inspector]
  137. public MyEnum Attitude = MyEnum.Friendly;
  138. [Inspector]
  139. public Sprite2D[] SpritesField;
  140. }
  141. /*
  142. class Spinner : CSComponent
  143. {
  144. public float Speed = 1.0f;
  145. override public void Start()
  146. {
  147. myObject = new MyObject ();
  148. SubscribeToEvent (myObject, "MyEvent", handleMyObjectEvent);
  149. var renderer = Atomic.GetSubsystem<Renderer> ();
  150. SubscribeToEvent (renderer, "BeginViewUpdate", handleEvent);
  151. SubscribeToEvent (this, "SelfEvent", handleSelfEvent);
  152. SendEvent ("SelfEvent");
  153. }
  154. void handleSelfEvent(VariantMap eventData)
  155. {
  156. Console.WriteLine ("Got Self Event");
  157. }
  158. void handleMyObjectEvent(VariantMap eventData)
  159. {
  160. //Console.WriteLine ("Got My Event");
  161. }
  162. void handleEvent(VariantMap eventData)
  163. {
  164. View view = eventData.Get<View> ("view");
  165. view.Camera.Zoom = zoom;
  166. zoom += .01f;
  167. myObject.SendEvent ("MyEvent");
  168. }
  169. override public void Update(float timeStep)
  170. {
  171. Node.Yaw (timeStep * 75 * Speed);
  172. }
  173. float zoom = 1.0f;
  174. MyObject myObject;
  175. }
  176. */
  177. class MyGame
  178. {
  179. public static void Main (string[] args)
  180. {
  181. Atomic.RegisterAssemblyComponents (typeof(MyGame).Assembly);
  182. var bunnyMark = new BunnyMark ();
  183. Atomic.Run ();
  184. }
  185. }