Vine.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "atomic component";
  2. // Rope Vine
  3. var component = function (self) {
  4. var NUM_OBJECTS = 10;
  5. var node = self.node;
  6. //set current node position to 0, 0, 0
  7. node.position = [0, 0, 0];
  8. self.start = function() {
  9. var x = self.startPosition[0];
  10. var y = self.startPosition[1];
  11. // create the node body
  12. var groundBody = node.createComponent("RigidBody2D");
  13. //do not cast shadows
  14. groundBody.castShadows = false;
  15. var prevBody = groundBody;
  16. for (var i = 0; i < NUM_OBJECTS; i++) {
  17. //create a new vine node
  18. var vnode = node.scene.createChild("RigidBody");
  19. //add StaticSprite2D to the created node
  20. var sprite2D = vnode.createComponent("StaticSprite2D");
  21. //set its sprite
  22. sprite2D.sprite = Atomic.cache.getResource("Sprite2D", "Sprites/vine.png");
  23. //create rigid body component
  24. var vbody = vnode.createComponent("RigidBody2D");
  25. //do not cast shadows
  26. vbody.castShadows = false;
  27. //set body type to Dynamic
  28. vbody.bodyType = Atomic.BodyType2D.BT_DYNAMIC;
  29. // Create box
  30. var vbox = vnode.createComponent("CollisionBox2D");
  31. // Set friction
  32. vbox.friction = .2;
  33. // Set mask bits.
  34. vbox.maskBits = 0xFFFF & ~0x0002;
  35. //set vine node position
  36. vnode.position = [x + 0.5 + 1.0 * i, y, 0.0];
  37. //set box collision size
  38. vbox.size = [1.0, 0.1];
  39. //set density
  40. vbox.density = 5.0;
  41. //set category bits
  42. vbox.categoryBits = 0x0001;
  43. if (i == NUM_OBJECTS - 1)
  44. vbody.angularDamping = 0.4;
  45. //create joint component
  46. var joint = vnode.createComponent("ConstraintRevolute2D");
  47. //join it to the previous body
  48. joint.otherBody = prevBody;
  49. //set ancor
  50. joint.anchor = [x + i, y];
  51. joint.collideConnected = false;
  52. prevBody = vbody;
  53. }
  54. //create ConstraintRope2D component
  55. var constraintRope = node.createComponent("ConstraintRope2D");
  56. //set other body to the previous body
  57. constraintRope.otherBody = prevBody;
  58. constraintRope.ownerBodyAnchor = [x, y];
  59. constraintRope.maxLength = (NUM_OBJECTS + 0.01);
  60. };
  61. };
  62. exports.component = component;