36 lines
1.4 KiB
JavaScript
36 lines
1.4 KiB
JavaScript
![]() |
import { Vector2 } from './Vector2.js';
|
||
|
export class ControlBall {
|
||
|
constructor(canvas, gameWidth, gameHeight, baseSpeed) {
|
||
|
this.gameWidth = gameWidth;
|
||
|
this.gameHeight = gameHeight;
|
||
|
this.baseSpeed = baseSpeed;
|
||
|
this.velocity = new Vector2();
|
||
|
this.canvas = canvas;
|
||
|
this.ctx = canvas.getContext('2d');
|
||
|
this.radius = Math.min(gameWidth, gameHeight) * 0.05;
|
||
|
this.maxSpeed = baseSpeed * 1.2;
|
||
|
this.position = new Vector2(gameWidth / 2, gameHeight / 2);
|
||
|
}
|
||
|
update(touchData) {
|
||
|
// 计算速度
|
||
|
const speed = this.baseSpeed * touchData.speedFactor;
|
||
|
this.velocity = touchData.normalizedPosition.scale(speed);
|
||
|
// 更新位置
|
||
|
this.position = this.position.add(this.velocity);
|
||
|
// 边界检测
|
||
|
this.position.x = Math.max(this.radius, Math.min(this.gameWidth - this.radius, this.position.x));
|
||
|
this.position.y = Math.max(this.radius, Math.min(this.gameHeight - this.radius, this.position.y));
|
||
|
}
|
||
|
draw() {
|
||
|
this.ctx.beginPath();
|
||
|
this.ctx.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
|
||
|
this.ctx.fillStyle = '#ff6b6b';
|
||
|
this.ctx.fill();
|
||
|
this.ctx.closePath();
|
||
|
}
|
||
|
reset() {
|
||
|
this.position = new Vector2(this.gameWidth / 2, this.gameHeight / 2);
|
||
|
this.velocity = new Vector2();
|
||
|
}
|
||
|
}
|