Vine.js 1.6 KB

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