sea_urchn_animal.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. var Urchin = function(options) {
  2. var defaults = {
  3. speed: 3,
  4. color: '#c96fc5',
  5. colorCore: '#531f2c',
  6. position: pt(200,350),
  7. nextpos: pt(200,350),
  8. alpha: 1,
  9. innerRadius: 10,
  10. outerRadius: 20,
  11. spineRandomness: 2,
  12. spineDensity: 20,
  13. theta: 0,
  14. state: 'falling',
  15. currentFood: null,
  16. eatRate: 10,
  17. direction: Math.random() > .5 ? 1 : -1,
  18. force: pt(0,0),
  19. bouyancy: 2,
  20. terminalVelocity: 5,
  21. ghost: false,
  22. colliders: [],
  23. minBound: 10,
  24. bumping: 0,
  25. hp: 100,
  26. caughtBy: null,
  27. };
  28. var e = $.extend({}, defaults, options);
  29. for(x in e) this[x] = e[x];
  30. this.type = 'Urchin';
  31. this.init();
  32. }
  33. Urchin.prototype.render = function(ctx) {
  34. ctx.save();
  35. ctx.translate(this.position.x, this.position.y);
  36. ctx.rotate(this.theta);
  37. ctx.beginPath();
  38. ctx.moveTo(this.innerpoints[0].x, this.innerpoints[0].y);
  39. for(var i = 0; i < this.spineDensity; i++) {
  40. ctx.lineTo(this.innerpoints[i].x, this.innerpoints[i].y);
  41. ctx.lineTo(this.outerpoints[i].x, this.outerpoints[i].y);
  42. }
  43. ctx.lineWidth = 2;
  44. ctx.closePath();
  45. ctx.globalAlpha = this.alpha;
  46. ctx.fillStyle = this.colorCore;
  47. ctx.fill();
  48. if(this.state == 'dead')
  49. ctx.strokeStyle = 'black';
  50. else
  51. ctx.strokeStyle = this.color;
  52. ctx.stroke();
  53. ctx.restore();
  54. };
  55. Urchin.prototype.frameMove = function(te) {
  56. this.checkState(te);
  57. //if(this.caught) this.isMoving = false;
  58. if(this.state == 'dead') {
  59. this.alpha = Math.max(this.alpha - te * .1, 0);
  60. };
  61. if(this.state == 'caught') {
  62. var c = this.caughtBy;
  63. var tmpx = c.holdVector.x * Math.cos(c.theta) - c.holdVector.y * Math.sin(c.theta);
  64. var tmpy = c.holdVector.x * Math.sin(c.theta) + c.holdVector.y * Math.cos(c.theta);
  65. this.nextpos.x = c.position.x + tmpx;
  66. this.nextpos.y = c.position.y + tmpy;
  67. return;
  68. }
  69. if(this.position.y < 440) {
  70. this.force.y = 50.9;
  71. }
  72. else {
  73. this.force.y = 0;
  74. }
  75. if(this.bumping & COL_LEFT) this.direction = 1;
  76. if(this.bumping & COL_RIGHT) this.direction = -1;
  77. if(this.bumping & COL_X) this.force.x = 50 * this.direction;
  78. this.nextpos.y = this.position.y + this.force.y * te;
  79. this.nextpos.x = this.position.x + this.force.x * te;
  80. if(this.state == 'foraging') {
  81. this.theta = (this.theta + te * .1 * this.direction) % 360;
  82. }
  83. this.colliders = [];
  84. };
  85. Urchin.prototype.checkState = function(te) {
  86. if(this.state == 'default') this.state = 'falling';
  87. if(this.hp == 0) {
  88. this.state = 'dead';
  89. this.type = 'DeadUrchin';
  90. return;
  91. }
  92. // the sea otter sets the caught state
  93. if(this.state == 'caught') {
  94. this.currentFood = null;
  95. this.force.x = 0;
  96. this.force.y = 0;
  97. return;
  98. }
  99. // check to see if it hits the floor
  100. if(this.state == 'falling' && this.position.y >= 438) {
  101. this.state = 'foraging';
  102. // set out
  103. this.force.x = 50 * this.direction;
  104. return;
  105. }
  106. // check for kelp collisions
  107. if(this.state == 'foraging') {
  108. for(var i = 0; i < this.colliders.length; i++) {
  109. if(this.colliders[i].type == 'Kelp') {
  110. this.state = 'eating';
  111. this.currentFood = this.colliders[i];
  112. this.force.x = 0;
  113. };
  114. }
  115. }
  116. if(this.state == 'eating') {
  117. this.currentFood.subHP(this.eatRate * te);
  118. if(this.currentFood.hp == 0) {
  119. this.state = 'foraging';
  120. this.force.x = 50 * this.direction;
  121. }
  122. }
  123. }
  124. Urchin.prototype.subHP = function(amount) {
  125. var old = this.hp;
  126. this.hp -= amount;
  127. if(this.hp < 0) this.hp = 0;
  128. if(this.hp > 100) this.hp = 100;
  129. return old - this.hp;
  130. }
  131. Urchin.prototype.init = function() {
  132. this.innerpoints = [];
  133. this.outerpoints = [];
  134. var spinearc = 2*Math.PI / this.spineDensity;
  135. for(var i = 0; i < this.spineDensity; i++) {
  136. var x,y;
  137. x = Math.cos(i * spinearc) * this.innerRadius;
  138. y = Math.sin(i * spinearc) * this.innerRadius;
  139. this.innerpoints.push(pt(x,y));
  140. }
  141. var halfspinearc = spinearc / 2;
  142. for(var i = 0; i < this.spineDensity; i++) {
  143. var x,y;
  144. x = Math.cos(i * spinearc + halfspinearc) * this.outerRadius + ((Math.random() * 2 - 1) * this.spineRandomness);
  145. y = Math.sin(i * spinearc + halfspinearc) * this.outerRadius + ((Math.random() * 2 - 1) * this.spineRandomness);
  146. this.outerpoints.push(pt(x,y));
  147. }
  148. }