kelp.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. var Kelp = function(options) {
  2. var defaults = {
  3. speed: 3,
  4. stipeColor: 'green',
  5. stipeColorDead: 'brown',
  6. bladeColor: 'olive',
  7. bladderColor: 'olive',
  8. holdfastColor: 'brown',
  9. position: pt(400,430),
  10. nextpos: pt(400,450),
  11. hp: 100,
  12. alpha: 1,
  13. stipeHeight: 350,
  14. bladeSeparation: 30,
  15. bladeLength: 40,
  16. holdfastType: Math.floor(Math.random() * 3) + 1,
  17. wiggleTheta: 0,
  18. wiggleSpeed: 1,
  19. ghost: false,
  20. colliders: [],
  21. minBound: 10,
  22. bumping: 0,
  23. };
  24. var e = $.extend({}, defaults, options);
  25. for(x in e) this[x] = e[x];
  26. this.type = 'Kelp';
  27. this.init();
  28. }
  29. Kelp.prototype.render = function(ctx) {
  30. ctx.save();
  31. ctx.translate(this.position.x, this.position.y);
  32. // draw the stipe. the base od the stipe is at 0,0. the holdfast goes down below it
  33. // ctx.beginPath();
  34. // ctx.moveTo(0, 0);
  35. var t = Math.cos(this.wiggleTheta);
  36. var t2 = Math.cos(this.wiggleTheta + 45);
  37. ctx.globalAlpha = this.alpha;
  38. // draw the left side
  39. ctx.beginPath();
  40. ctx.moveTo(0, 0);
  41. var t = Math.cos(this.wiggleTheta);
  42. var t2 = Math.cos(this.wiggleTheta + 45);
  43. var pc = Math.ceil(this.stipeHeight / 10);
  44. var inc = this.stipeHeight / pc;
  45. for(var i = 0; i < pc; i++) {
  46. //var y = ;
  47. var close = (i == 0) ? 0 : 8;
  48. ctx.lineTo(
  49. -close + Math.sin((i / 10) * Math.PI) * t * 10 - Math.sin((i / 7) * Math.PI) * t2 * 7
  50. , i * -inc);
  51. }
  52. // draw the top
  53. var ep = pc + 1;
  54. ctx.quadraticCurveTo(
  55. // control point
  56. Math.sin((ep / 10) * Math.PI) * t * 10 - Math.sin((ep / 7) * Math.PI) * t2 * 7
  57. , -inc * ep
  58. // end point
  59. ,8 + Math.sin(((pc-1) / 10) * Math.PI) * t * 10 - Math.sin(((pc-1) / 7) * Math.PI) * t2 * 7
  60. , -inc * (pc-1)
  61. );
  62. // draw the right side
  63. for(var i = pc - 1; i >= 0; i--) {
  64. //var y = ;
  65. var close = (i == 0) ? 0 : 8;
  66. ctx.lineTo(
  67. close + Math.sin((i / 10) * Math.PI) * t * 10 - Math.sin((i / 7) * Math.PI) * t2 * 7
  68. , i * -inc);
  69. }
  70. ctx.lineWidth = 1;
  71. ctx.closePath();
  72. ctx.fillStyle = this.bladeColor;
  73. ctx.fill();
  74. // draw the stipe
  75. ctx.beginPath();
  76. ctx.moveTo(0, 0);
  77. for(var i = 0; i < pc; i++) {
  78. //var y = ;
  79. ctx.lineTo(
  80. Math.sin((i / 10) * Math.PI) * t * 10 - Math.sin((i / 7) * Math.PI) * t2 * 7
  81. , i * -inc);
  82. }
  83. ctx.lineWidth = 2;
  84. ctx.strokeStyle = this.stipeColor;
  85. ctx.stroke();
  86. // draw the holdfasts
  87. ctx.translate( - images.holdfast1_1.width / 2, 0);
  88. var eatlevel = Math.min(5, 6 - Math.ceil(this.hp/20)); // hack
  89. ctx.drawImage(images['holdfast' + this.holdfastType + '_' + eatlevel], 0, 0);
  90. ctx.restore();
  91. };
  92. Kelp.prototype.renderBlade = function(ctx, pos, lr) {
  93. };
  94. Kelp.prototype.frameMove = function(te) {
  95. this.wiggleTheta = (this.wiggleTheta + te * this.wiggleSpeed) % 360;
  96. if(this.hp == 0) {
  97. this.ghost = true;
  98. this.alpha = Math.max(this.alpha - te * .2, 0);
  99. };
  100. this.colliders = [];
  101. };
  102. Kelp.prototype.subHP = function(amount) {
  103. var old = this.hp;
  104. this.hp -= amount;
  105. if(this.hp < 0) this.hp = 0;
  106. if(this.hp > 100) this.hp = 100;
  107. return old - this.hp;
  108. }
  109. Kelp.prototype.init = function() {
  110. this.stipeHeight = 260 + Math.random() * 100;
  111. this.nextpos = ptc(this.position);
  112. this.bonePos = [];
  113. this.boneVel = [];
  114. this.boneForce = [];
  115. var bonelen = 20;
  116. var bonecount = Math.ceil(this.stipeHeight / bonelen);
  117. var remainderHeight = this.stipeHeight - Math.floor(this.stipeHeight / bonelen);
  118. var len = 0;
  119. var y = 0;
  120. for(var i = 0; i < bonecount; i++) {
  121. len += bonelen;
  122. }
  123. function addBone(x,y) {
  124. this.bonePos.push(pt(x,y));
  125. this.boneVel.push(pt(0,0));
  126. this.boneForce.push(pt(0,-2));
  127. }
  128. this.wiggleTheta = Math.random() * 360;
  129. this.wiggleSpeed = Math.random() + .5;
  130. }