ShopCard.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { Picture } from "./CanvasArtist.js"
  2. import { Theme } from "./components/Theme.js"
  3. import { Point } from "./spatial/Point.js"
  4. import TextWrapper from "./TextWrapper.js"
  5. import "./ShufflePolyfill.js"
  6. export default class ShopCard {
  7. constructor() {
  8. this.textWrapper = new TextWrapper()
  9. this.init()
  10. }
  11. init() {
  12. this.generateRequest()
  13. }
  14. generateRequest() {
  15. this.availableItems = ["bones", "skulls", "bones", "bones", "bones"]
  16. this.item = this.availableItems.shuffle().pop()
  17. switch(this.item) {
  18. case "bones":
  19. this.generateBonesRequest()
  20. break
  21. case "skulls":
  22. this.generateSkullsRequest()
  23. break;
  24. }
  25. }
  26. generateBonesRequest() {
  27. this.amount = Math.floor(4 * Math.random()) + 3
  28. this.rate = Math.floor((500 * Math.random() + 100)/100)
  29. let options =[
  30. `I need ${this.amount} bones so I can bury them for my family. My family loves bones!`,
  31. `Do you think I could get ${this.amount} bones?`,
  32. `I only need ${this.amount}; do you have any extra bones I can have?`,
  33. `I really need some fresh bones! I need ${this.amount} immediately!`
  34. ]
  35. this.message = options.shuffle()[0]
  36. let availableCustomers = [Picture.DogHead1, Picture.DogHead2]
  37. this.customerPicture = availableCustomers.shuffle().pop()
  38. }
  39. generateSkullsRequest() {
  40. this.amount = Math.floor(4 * Math.random()) + 1
  41. this.rate = Math.floor((5000 * Math.random() + 1000)/100)
  42. let options =[
  43. `Did you see those skulls? I must get my hands on ${this.amount} of them!`,
  44. `I only wish for ${this.amount} skulls.`,
  45. `Do you take cash? I heard you have ${this.amount} skulls for sale...`,
  46. `Skulls are tres chique right now. Give me ${this.amount} immediately!`
  47. ]
  48. this.message = options.shuffle()[0]
  49. let availableCustomers = [Picture.DogHead1, Picture.DogHead2]
  50. this.customerPicture = availableCustomers.shuffle().pop()
  51. }
  52. update(delta, canvasBounds) {
  53. this.canvasBounds = canvasBounds
  54. this.cardHeight = Math.max(this.canvasBounds.height / 6, 100)
  55. this.fontScale = this.canvasBounds.width / 500
  56. this.screenWidth = this.canvasBounds.width
  57. }
  58. draw(ctx, options) {
  59. let textFontSize = Math.min(Math.floor(24 * this.fontScale), 24)
  60. ctx.strokeStyle = Theme.Colors.black
  61. ctx.lineWidth = 1
  62. ctx.fillStyle = Theme.Colors.umber
  63. ctx.beginPath()
  64. ctx.roundRect(8, 0, this.screenWidth - 16, this.cardHeight, 8)
  65. ctx.fill()
  66. ctx.fillStyle = Theme.Colors.seagreen
  67. if(this.hovered) {
  68. ctx.fillStyle = Theme.Colors.ivory
  69. }
  70. ctx.beginPath()
  71. ctx.arc(this.screenWidth - (16 + this.cardHeight / 4), 2 * this.cardHeight / 4, this.cardHeight / 4, 0, 2 * Math.PI)
  72. ctx.fill()
  73. ctx.font = `${textFontSize}px ${Theme.Fonts.Header}`
  74. ctx.textAlign = "center"
  75. ctx.fillStyle = Theme.Colors.ivory
  76. if(this.hovered) {
  77. ctx.fillStyle = Theme.Colors.seagreen
  78. }
  79. ctx.fillText(`$${this.amount * this.rate}`, this.screenWidth - (16 + this.cardHeight / 4), 2 * (this.cardHeight / 4) + textFontSize / 4)
  80. ctx.fillStyle = Theme.Colors.umber
  81. ctx.beginPath()
  82. ctx.roundRect(16, 8, 9 * this.cardHeight / 12, this.cardHeight - 16, 8)
  83. ctx.fill()
  84. ctx.save()
  85. ctx.translate(16 + (9 * this.cardHeight / 12) / 2, ((this.cardHeight - 16) / 2))
  86. this.customerPicture(ctx, {})
  87. ctx.restore()
  88. ctx.fillStyle = Theme.Colors.ivory
  89. ctx.font = `${textFontSize}px ${Theme.Fonts.Header}`
  90. ctx.textAlign = "left"
  91. ctx.save()
  92. ctx.translate(32 + (9 * this.cardHeight / 12), 8 + textFontSize)
  93. this.textWrapper.draw(ctx, this.message, { width: this.screenWidth - (16 + this.cardHeight / 4) - 32 - (9 * this.cardHeight / 12) })
  94. ctx.restore()
  95. }
  96. mouseMove(event, cardOffset) {
  97. let mousePoint = new Point(
  98. event.clientX,
  99. event.clientY,
  100. )
  101. this.hovered = false
  102. if(mousePoint.distanceTo(new Point(this.screenWidth - (16 + this.cardHeight / 4), (2 * this.cardHeight / 4) + cardOffset)) < this.cardHeight / 4) {
  103. this.hovered = true
  104. document.body.style.cursor = "pointer"
  105. }
  106. }
  107. onClick(handle) {
  108. if(this.hovered) {
  109. handle(this.item, this.rate, this.amount)
  110. }
  111. }
  112. }