mud.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. var Mud = function(options) {
  2. var defaults = {
  3. speed: 3,
  4. color: 100,
  5. intensity: 10,
  6. wavelen: 120,
  7. phase: 0,
  8. };
  9. var e = $.extend({}, defaults, options);
  10. for(x in e) this[x] = e[x];
  11. this.type = 'Mud';
  12. this.init();
  13. }
  14. Mud.prototype.render = function(ctx) {
  15. var maxw = ctx.canvas.width;
  16. var cx, xy;
  17. // ctx.drawBox(150,150, 'green', 6);
  18. ctx.save();
  19. ctx.translate(0, 450);
  20. ctx.beginPath();
  21. ctx.moveTo(0, 0);
  22. for(var i = 0; i < this.points.length; i++) {
  23. ctx.lineTo(this.points[i].x * 1000, -this.points[i].y * 30);
  24. }
  25. ctx.lineWidth = 3;
  26. ctx.lineTo(1200, 0);
  27. ctx.lineTo(1200, 1200);
  28. ctx.lineTo(-200, 1200);
  29. ctx.lineTo(-200, 0);
  30. ctx.closePath();
  31. //
  32. ctx.fillStyle = "#9A7D3A";
  33. ctx.fill();
  34. ctx.strokeStyle = '#7A5D1A';
  35. ctx.stroke();
  36. // for(var i = 0; i < this.points.length; i++) {
  37. // ctx.drawBox(this.points[i].x * 800, -this.points[i].y * 30, 'red', 2);
  38. // }
  39. ctx.restore();
  40. };
  41. Mud.prototype.frameMove = function(te) {
  42. // // this.
  43. // console.log(te);
  44. // console.log(this.phase);
  45. // this.phase += 0.8 * te;
  46. // this.phase = this.phase % (Math.PI * 2);
  47. };
  48. Mud.prototype.collides = function(p, r) {
  49. // check to see if it's deep enough ever
  50. /*
  51. var mindepth = 450 - (30 * .2);
  52. if(p.y + r < mindepth) return false;*/
  53. // check if it collided with us at that spot
  54. // var closest = closestSpan(p.x);
  55. // // find the y for it
  56. // var slope = (closest[1].y - closest[0].y) / (closest[1].x - closest[0].x);
  57. //
  58. // var ty =
  59. // fancy shit up there, but the floor is basically flat.
  60. return (p.y + r > 450);
  61. }
  62. function closestSpan(tx) {
  63. for(var i = 0; i < points.length - 1; i++) { // shitty linear seach, do binary later
  64. if(points[i].x * 1000 <= tx && points[i+1] >= tx)
  65. return [points[i], points[i+1]];
  66. }
  67. return null;
  68. }
  69. Mud.prototype.init = function() {
  70. var pointct = this.lvlwidth / 20;
  71. var maxVariation = .2;
  72. var points = [
  73. pt(0.0, 0.0),
  74. pt(1.0, 0.0)
  75. ];
  76. function subdivide(leftpt, depth) {
  77. if(depth <= 0) return;
  78. var lefty = points[leftpt];
  79. var righty = points[leftpt + 1];
  80. var x = ((righty.x - lefty.x) / 2) + lefty.x;
  81. var y = ((righty.y + lefty.y) / 2) + (Math.random() * maxVariation);
  82. points.splice(leftpt + 1, 0, pt(x,y));
  83. subdivide(leftpt + 1, depth - 1);
  84. subdivide(leftpt, depth - 1);
  85. }
  86. subdivide(0, 4);
  87. this.points = points;
  88. }