Tile.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { Theme } from "../Theme.js"
  2. class Tile {
  3. constructor(scene, position, type) {
  4. this.scene = scene
  5. this.tileSize = 64
  6. this.position = position
  7. this.type = type
  8. this.offset = {x: 0, y: 0}
  9. this.baseColor = Object.values(Theme.Colors.TileColors)[this.type]
  10. this.currentColor = this.baseColor
  11. this.isHovered = false
  12. }
  13. setOffset(offset){
  14. this.offset = offset
  15. }
  16. draw(ctx) {
  17. ctx.fillStyle = this.currentColor
  18. ctx.save()
  19. ctx.translate(this.offset.x, this.offset.y)
  20. ctx.beginPath()
  21. ctx.roundRect(this.position.x * (Tile.Size + Tile.PADDING), this.position.y * (Tile.Size + Tile.PADDING), Tile.Size, Tile.Size, 8)
  22. ctx.fill()
  23. if(this.isHovered) {
  24. ctx.strokeStyle = Theme.Colors.ivory
  25. ctx.lineWidth = 4;
  26. ctx.beginPath()
  27. ctx.roundRect(this.position.x * (Tile.Size + Tile.PADDING), this.position.y * (Tile.Size + Tile.PADDING), Tile.Size, Tile.Size, 8)
  28. ctx.stroke()
  29. }
  30. ctx.restore()
  31. }
  32. setTileSize(newSize) {
  33. if(Tile.Size != newSize) {
  34. Tile.Size = newSize
  35. }
  36. }
  37. onInputMove(position, isDown) {
  38. this.currentColor = this.baseColor
  39. if(this.isColliding(position)) {
  40. this.isHovered = true
  41. document.body.style.cursor = "pointer"
  42. if(isDown) {
  43. this.scene.trackSelectedTile(this)
  44. this.wasClickedOn = true
  45. }
  46. } else {
  47. this.isHovered = false
  48. }
  49. }
  50. onInputDown(position) {
  51. if(this.isColliding(position)) {
  52. this.scene.trackSelectedTile(this)
  53. this.wasClickedOn = true
  54. }
  55. }
  56. onInputUp(position) {
  57. if(this.isColliding(position)) {
  58. this.wasClickedOn = false
  59. } else if(this.wasClickedOn) {
  60. this.wasClickedOn = false
  61. }
  62. }
  63. isColliding(position) {
  64. let top = this.offset.y + this.position.y * (Tile.Size + Tile.PADDING)
  65. let left = this.offset.x + this.position.x * (Tile.Size + Tile.PADDING)
  66. let bottom = top + Tile.Size
  67. let right = left + Tile.Size
  68. return position.x > left && position.x < right && position.y > top && position.y < bottom
  69. }
  70. onResize(canvasBounds, boardSize) {
  71. let narrowest = canvasBounds.width
  72. let isVertical = true
  73. if (canvasBounds.width > canvasBounds.height) {
  74. narrowest = canvasBounds.height
  75. isVertical = false
  76. }
  77. this.setTileSize(Math.min(64, Math.max(40, Math.floor((narrowest - boardSize.x * 4) / boardSize.x))))
  78. const offset = {
  79. x: -3.5 * (Tile.PADDING + Tile.Size),
  80. y: -10 * (Tile.PADDING + Tile.Size) + canvasBounds.height / 2
  81. }
  82. if(!isVertical) {
  83. offset.x = 3.5 * (Tile.PADDING + Tile.Size) - canvasBounds.width / 2
  84. offset.y = -3.5 * (Tile.PADDING + Tile.Size)
  85. }
  86. this.setOffset(offset)
  87. }
  88. onRemove() {
  89. this.scene.removeTile(this)
  90. }
  91. }
  92. Tile.PADDING = 4
  93. Tile.Size = 64
  94. export default Tile