Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class BaseObject {
  2.     constructor(x, y, speed, angle, width, height) {
  3.         this.x = x;
  4.         this.y = y;
  5.         this.speed = speed;
  6.         this.angle = angle;
  7.         this.width = width;
  8.         this.height = height;
  9.         this.color = "blue";
  10.     }
  11.     render(canvas) {
  12.         canvas.save();
  13.         let hw = this.width / 2;
  14.         let hh = this.height / 2;
  15.         canvas.translate(this.x + hw, this.y + hh);
  16.         canvas.fillStyle = this.color;
  17.         let rad = this.degToRad(this.angle);
  18.         canvas.rotate(rad);
  19.         canvas.fillRect(-hw, -hh, this.width, this.height);
  20.         canvas.restore();
  21.         canvas.fillStyle = "#0000FF";
  22.         canvas.beginPath();
  23.         let leftTop = this.getLeftTopPoint();
  24.         canvas.arc(leftTop.x, leftTop.y, 5, 0, Math.PI * 2, true);
  25.         canvas.fill();
  26.         canvas.beginPath();
  27.         let rightBottom = this.getRightBottomPoint();
  28.         canvas.arc(rightBottom.x, rightBottom.y, 5, 0, Math.PI * 2, true);
  29.         canvas.fill();
  30.     }
  31.     forward() {
  32.         let d = this.theoremSin(this.angle + 90, this.speed);
  33.         this.x += d.deltaX;
  34.         this.y += d.deltaY;
  35.     }
  36.  
  37.     theoremSin(angle, distance) {
  38.         let alpha = angle;
  39.         let beta = 90 - alpha;
  40.         let gamma = Math.PI / 2;
  41.         let deltaX = Math.sin(this.degToRad(beta)) * distance
  42.                      / Math.sin(gamma);
  43.         let deltaY = Math.sin(this.degToRad(alpha)) * distance
  44.                      / Math.sin(gamma);
  45.         return { deltaX, deltaY }
  46.     }
  47.    
  48.     degToRad(deg) {
  49.         return deg * Math.PI / 180;
  50.     }
  51.     collision() {
  52.  
  53.     }
  54.  
  55.     // возвращает объект с полями x и y
  56.     getLeftTopPoint() {
  57.         let hw = this.width / 2;
  58.         let hh = this.height / 2;
  59.         let point = this.rotatePoint(-hw, -hh, this.angle);
  60.         return {x: point.x + this.x + hw, y: point.y + this.y + hh}
  61.     }
  62.  
  63.     // возвращает объект с полями x и y
  64.     getRightBottomPoint() {
  65.         let hw = this.width / 2;
  66.         let hh = this.height / 2;
  67.         let point = this.rotatePoint(hw, hh, this.angle);
  68.         return {x: point.x + this.x + hw, y: point.y + this.y + hh}
  69.     }
  70.  
  71.     isPointInObject(point) {
  72.        
  73.     }
  74.  
  75.     rotatePoint(x, y, angle) {
  76.         let a = this.degToRad(angle);
  77.         let x1 = x * Math.cos(a) - y * Math.sin(a);
  78.         let y1 = x * Math.sin(a) + y * Math.cos(a);
  79.         return {x: x1, y: y1}
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement