dialog.js 3.6 KB

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