hud.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. var HUD = function(options) {
  2. var defaults = {
  3. color: 100,
  4. intensity: 10,
  5. widgets: [],
  6. };
  7. var e = $.extend({}, defaults, options);
  8. for(x in e) this[x] = e[x];
  9. this.type = 'HUD';
  10. }
  11. HUD.prototype.render = function(ctx) {
  12. for(var i = 0; i < this.widgets.length; i++) {
  13. var w = this.widgets[i];
  14. ctx.save();
  15. ctx.translate(w.pos.x, w.pos.y);
  16. w.w.render(ctx);
  17. ctx.restore();
  18. }
  19. };
  20. HUD.prototype.frameMove = function(te) {
  21. for(var i = 0; i < this.widgets.length; i++) {
  22. this.widgets[i].w.frameMove(te);
  23. }
  24. };
  25. HUD.prototype.addWidget = function(w, x, y) {
  26. this.widgets.push({
  27. w: w,
  28. pos: pt(x,y),
  29. });
  30. }
  31. var HUDHearts = function(_otter) {
  32. this.otter = _otter;
  33. this.maxHearts = 5;
  34. }
  35. HUDHearts.prototype.render = function(ctx) {
  36. var hearts = Math.ceil(this.otter.hp / (100 / this.maxHearts));
  37. //console.log(hearts);
  38. var left = 0;
  39. for(var i = 0; i < hearts; i++) {
  40. ctx.drawImage(images.heart, (this.maxHearts * 25) - left, 0);
  41. left += 25;
  42. };
  43. // draw a half-heart if needed
  44. };
  45. HUDHearts.prototype.frameMove = function(te) {
  46. };
  47. var HUDBubbles = function(_otter) {
  48. this.otter = _otter;
  49. this.maxBubbles = 7;
  50. }
  51. HUDBubbles.prototype.render = function(ctx) {
  52. var bubbles = Math.ceil(this.otter.breath / (this.otter.maxBreath / this.maxBubbles));
  53. //console.log(bubbles);
  54. var left = 0;
  55. for(var i = 0; i < bubbles; i++) {
  56. ctx.drawImage(images.bubble_icon, left, 0);
  57. left += 25;
  58. };
  59. // draw a half-heart if needed
  60. };
  61. HUDBubbles.prototype.frameMove = function(te) {
  62. };
  63. var HUDList = function(options) {
  64. var defaults = {
  65. list: [],
  66. icon: null,
  67. textOffset: pt(0,0),
  68. imgScale: 1,
  69. };
  70. var e = $.extend({}, defaults, options);
  71. for(x in e) this[x] = e[x];
  72. this.type = 'HUDList';
  73. }
  74. HUDList.prototype.render = function(ctx) {
  75. var num = 0;
  76. for(var i = 0; i < this.list.length; i++) {
  77. if(this.list[i].hp > 0) num++;
  78. }
  79. //console.log(bubbles);
  80. ctx.save();
  81. ctx.font = ' 25px Sans Serif';
  82. ctx.fillText(
  83. num,
  84. (this.icon.width * this.imgScale) + this.textOffset.x,
  85. this.textOffset.y);
  86. ctx.scale(this.imgScale, this.imgScale);
  87. ctx.drawImage(this.icon, 0, 0);
  88. ctx.restore();
  89. };
  90. HUDList.prototype.frameMove = function(te) {
  91. };