Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BaseObject {
- constructor(x, y, speed, angle, width, height) {
- this.x = x;
- this.y = y;
- this.speed = speed;
- this.angle = angle;
- this.width = width;
- this.height = height;
- this.color = "blue";
- }
- render(canvas) {
- canvas.save();
- let hw = this.width / 2;
- let hh = this.height / 2;
- canvas.translate(this.x + hw, this.y + hh);
- canvas.fillStyle = this.color;
- let rad = this.degToRad(this.angle);
- canvas.rotate(rad);
- canvas.fillRect(-hw, -hh, this.width, this.height);
- canvas.restore();
- canvas.fillStyle = "#0000FF";
- canvas.beginPath();
- let leftTop = this.getLeftTopPoint();
- canvas.arc(leftTop.x, leftTop.y, 5, 0, Math.PI * 2, true);
- canvas.fill();
- canvas.beginPath();
- let rightBottom = this.getRightBottomPoint();
- canvas.arc(rightBottom.x, rightBottom.y, 5, 0, Math.PI * 2, true);
- canvas.fill();
- }
- forward() {
- let d = this.theoremSin(this.angle + 90, this.speed);
- this.x += d.deltaX;
- this.y += d.deltaY;
- }
- theoremSin(angle, distance) {
- let alpha = angle;
- let beta = 90 - alpha;
- let gamma = Math.PI / 2;
- let deltaX = Math.sin(this.degToRad(beta)) * distance
- / Math.sin(gamma);
- let deltaY = Math.sin(this.degToRad(alpha)) * distance
- / Math.sin(gamma);
- return { deltaX, deltaY }
- }
- degToRad(deg) {
- return deg * Math.PI / 180;
- }
- collision() {
- }
- // возвращает объект с полями x и y
- getLeftTopPoint() {
- let hw = this.width / 2;
- let hh = this.height / 2;
- let point = this.rotatePoint(-hw, -hh, this.angle);
- return {x: point.x + this.x + hw, y: point.y + this.y + hh}
- }
- // возвращает объект с полями x и y
- getRightBottomPoint() {
- let hw = this.width / 2;
- let hh = this.height / 2;
- let point = this.rotatePoint(hw, hh, this.angle);
- return {x: point.x + this.x + hw, y: point.y + this.y + hh}
- }
- isPointInObject(point) {
- }
- rotatePoint(x, y, angle) {
- let a = this.degToRad(angle);
- let x1 = x * Math.cos(a) - y * Math.sin(a);
- let y1 = x * Math.sin(a) + y * Math.cos(a);
- return {x: x1, y: y1}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement