function lerpAngle(aim: number, cur: number, ratio: number): number {
var delta = cur - aim;
// 当角度差超过一个 180 度的时候,也就是 PI,无论是正时针,还是逆时针。
// 因为旋转超过 180 度,我们可以反方向旋转,这样旋转的角度会小一点。
// 例如:当我们需要顺时针旋转 270 度的时候,我们可以反向旋转 90 度来解决问题。也就是 [ -90 = 270 - 360 ].
if (delta > Math.PI) delta = delta - 2 * Math.PI;
if (delta < -Math.PI) delta = delta + 2 * Math.PI;
return aim + delta * ratio;
}
export default {
lerpDistance,
lerpAngle
}
// 鱼妈妈
class FishMonther{
x: number = cvs_width / 2; // 坐标轴 x
y: number = cvs_height / 2 ; // 坐标轴 y
bigEye = new Image(); // 眼睛
bigBody = new Image(); // 身体
BigTail = new Image(); // 尾巴
angle: number = 0; // 鱼的角度
constructor(){
this.bigEye.src = 'assets/img/bigEye0.png';
this.bigBody.src = 'assets/img/bigSwim0.png';
this.BigTail.src ='assets/img/bigTail0.png';
}
draw(){
this.x = utils.lerpDistance(mouse_x, this.x , .9)
this.y = utils.lerpDistance(mouse_y, this.y , .9)
let instance_X = mouse_x - this.x; // 边 a
let instance_Y = mouse_y - this.y; // 边 b
let ag = Math.atan2(instance_Y, instance_X) + Math.PI // [-PI, PI]
this.angle = utils.lerpAngle(ag, this.angle, .9)
ctx_one.save();
ctx_one.translate(this.x, this.y); // 定义相对定位的坐标中心点
ctx_one.rotate(this.angle);
ctx_one.scale(.7, .7);
ctx_one.drawImage(this.bigEye, -this.bigEye.width / 2, -this.bigEye.height / 2); // 居中,所以向左移动宽度的一半,向上移动宽度的一半
ctx_one.drawImage(this.bigBody, -this.bigBody.width / 2, -this.bigBody.height / 2);
ctx_one.drawImage(this.BigTail, -this.BigTail.width / 2 + 30, -this.BigTail.height / 2); // 这里的尾巴,往右移动30像素,让它在身体的后面。
ctx_one.restore();
}
}