Slot.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { Ball } from "./Ball.js";
  2. export class Slot {
  3. constructor(id) {
  4. this.id = id;
  5. this.balls = [];
  6. }
  7. addBall(ball) {
  8. this.balls.push(ball);
  9. }
  10. removeBall() {
  11. return this.balls.pop();
  12. }
  13. draw(ctx, scaledCanvas) {
  14. ctx.beginPath();
  15. ctx.moveTo(0,0);
  16. ctx.lineTo(Slot.width, 0);
  17. ctx.lineTo(Slot.width, Slot.height);
  18. ctx.arcTo(Slot.width, Slot.height + Slot.width / 2, Slot.width / 2, Slot.height + Slot.width / 2, Slot.width / 2);
  19. ctx.arcTo(0, Slot.height + Slot.width / 2, 0, Slot.height, Slot.width / 2);
  20. ctx.closePath();
  21. ctx.fill();
  22. // ctx.stroke();
  23. for(let i = 0; i < this.balls.length; i++) {
  24. ctx.save();
  25. ctx.translate(Slot.width / 2, Slot.height - i * (2 * (Ball.radius + Ball.radiusPadding)) - Slot.width / 4);
  26. this.balls[i].draw(ctx, scaledCanvas);
  27. ctx.restore();
  28. }
  29. }
  30. update(delta) {
  31. }
  32. canAddBall(ballToAdd) {
  33. if(this.balls.length == 0) { return true; } //it's empty
  34. if(this.balls.length == 4) { return false; } //it's full
  35. if(this.balls[this.balls.length -1].colorId != ballToAdd.colorId) { return false; } //color doesn't match
  36. return true;
  37. }
  38. colorsAllMatch() {
  39. if(this.balls.length == 0) {
  40. return true;
  41. }
  42. if(this.balls.length == 4) {
  43. let ballColorId = this.balls[0].colorId;
  44. for(let i = 0; i < this.balls.length; i++) {
  45. let ball = this.balls[i];
  46. if(ball.colorId != ballColorId) {
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. return false;
  53. }
  54. }
  55. Slot.width = 40;
  56. Slot.widthPadding = 10;
  57. Slot.height = 160;
  58. Slot.heightPadding = 80;