Vine.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Rope Vine
  2. "atomic component";
  3. var component = function (self) {
  4. var NUM_OBJECTS = 10;
  5. var node = self.node;
  6. node.position = [0, 0, 0];
  7. self.start = function() {
  8. var x = self.startPosition[0];
  9. var y = self.startPosition[1];
  10. // create the node body
  11. var groundBody = node.createComponent("RigidBody2D");
  12. groundBody.castShadows = false;
  13. var prevBody = groundBody;
  14. for (var i = 0; i < NUM_OBJECTS; i++) {
  15. var vnode = node.scene.createChild("RigidBody");
  16. var sprite2D = vnode.createComponent("StaticSprite2D");
  17. sprite2D.sprite = Atomic.cache.getResource("Sprite2D", "Sprites/vine.png");
  18. var vbody = vnode.createComponent("RigidBody2D");
  19. vbody.castShadows = false;
  20. vbody.bodyType = Atomic.BT_DYNAMIC;
  21. // Create box
  22. var vbox = vnode.createComponent("CollisionBox2D");
  23. // Set friction
  24. vbox.friction = .2;
  25. // Set mask bits.
  26. vbox.maskBits = 0xFFFF & ~0x0002;
  27. vnode.position = [x + 0.5 + 1.0 * i, y, 0.0];
  28. vbox.size = [1.0, 0.1];
  29. vbox.density = 5.0;
  30. vbox.categoryBits = 0x0001;
  31. if (i == NUM_OBJECTS - 1)
  32. vbody.angularDamping = 0.4;
  33. var joint = vnode.createComponent("ConstraintRevolute2D");
  34. joint.otherBody = prevBody;
  35. joint.anchor = [x + i, y];
  36. joint.collideConnected = false;
  37. prevBody = vbody;
  38. }
  39. var constraintRope = node.createComponent("ConstraintRope2D");
  40. constraintRope.otherBody = prevBody;
  41. constraintRope.ownerBodyAnchor = [x, y];
  42. constraintRope.maxLength = (NUM_OBJECTS + 0.01);
  43. }
  44. }
  45. exports.component = component;