dialog.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. function Dialog(game, camera) {
  2. this.system = null;
  3. var speakers = [];
  4. this.textOffset = 150;
  5. this.textRowOffset = 24;
  6. this.profileCardBounds = {x: 0, y: -100, width: 150, height: 210};
  7. this.dialogBounds = {left: 30, right: 30};
  8. this.conversationIndex = 0;
  9. this.displayDialog = false;
  10. var conversation = [];
  11. this.init = function(systemObject, canvas) {
  12. this.system = systemObject;
  13. this.canvas = canvas;
  14. };
  15. this.beginConversation = function(text, subjects) {
  16. if(!this.displayDialog) {
  17. this.displayDialog = true;
  18. conversation = text;
  19. speakers = subjects;
  20. this.conversationIndex = 0;
  21. }
  22. };
  23. this.update = function(delta) {
  24. //this.conversationIndex = (this.conversationIndex + 1) % conversation.length;
  25. };
  26. this.draw = function(context) {
  27. if(!this.displayDialog) {
  28. return;
  29. }
  30. context.save();
  31. context.translate(this.dialogBounds.left, this.canvas.height - 150);
  32. context.globalAlpha = 0.6;
  33. context.beginPath();
  34. context.fillStyle = "white";
  35. context.rect(0, 0, this.canvas.width - (this.dialogBounds.left + this.dialogBounds.right), 120);
  36. context.fill();
  37. context.globalAlpha = 1;
  38. var message = conversation[this.conversationIndex];
  39. var subject = speakers[this.conversationIndex];
  40. context.font = "24px sans-serif";
  41. this.textRowOffset = 24;
  42. this.profileCardBounds = {x: 0, y: -100, width: 150, height: 210};
  43. this.dialogBounds = {left: 30, right: 30};
  44. this.textOffset = 0;
  45. if(subject == 0) {
  46. this.textOffset = this.profileCardBounds.width;
  47. }
  48. if(this.canvas.width < 768) {
  49. context.font = "12px sans-serif";
  50. this.textRowOffset = 12;
  51. this.profileCardBounds = {x: 0, y: -50, width: 75, height: 105};
  52. this.dialogBounds = {left: 10, right: 10};
  53. if(subject == 0) {
  54. this.textOffset = this.profileCardBounds.width;
  55. }
  56. }
  57. if(subject == 0) {
  58. context.beginPath();
  59. context.fillStyle = "crimson";
  60. context.rect(this.profileCardBounds.x + 10, this.profileCardBounds.y, this.profileCardBounds.width, this.profileCardBounds.height);
  61. context.fill();
  62. }
  63. if(subject == 1) {
  64. context.beginPath();
  65. context.fillStyle = "cornflowerblue";
  66. context.rect(this.canvas.width - (this.profileCardBounds.width + this.dialogBounds.right + this.dialogBounds.left + 10), this.profileCardBounds.y, this.profileCardBounds.width, this.profileCardBounds.height);
  67. context.fill();
  68. }
  69. message = message.split(" ");
  70. context.textAlign = "left";
  71. context.textBaseline = "hanging";
  72. context.fillStyle = "black";
  73. var displayText = [];
  74. var index = 0;
  75. var row = 0;
  76. while(message.length > 0) {
  77. var textMetrics = context.measureText(displayText.join(" ") + message[index]);
  78. if(textMetrics.width >= this.canvas.width - (this.profileCardBounds.width + this.dialogBounds.right + this.dialogBounds.left + 40)) {
  79. message.splice(0, index);
  80. index = 0;
  81. context.fillText(displayText.join(" "), this.textOffset + 20, (this.textRowOffset * row++) + 10);
  82. displayText = [];
  83. }
  84. displayText.push(message[index++]);
  85. }
  86. context.restore();
  87. };
  88. this.handleAdvance = function(button, x, y) {
  89. if(!this.displayDialog) {
  90. return false;
  91. }
  92. if(x > this.dialogBounds.left && x < this.canvas.width - this.dialogBounds.right) {
  93. if(y > this.canvas.height - 150 && y < this.canvas.height - 30) {
  94. if(button == 0) {
  95. this.conversationIndex++;
  96. if(this.conversationIndex > conversation.length - 1) {
  97. this.displayDialog = false;
  98. }
  99. return true;
  100. } else if (button == null) {
  101. return true;
  102. }
  103. }
  104. }
  105. return false;
  106. }
  107. };