You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

32 lines
770 B

import { DEFAULT_PADDLE_SIZE } from './constants'
import { type Point, Rect } from './utils'
export class Paddle {
rect: Rect
color: string | CanvasGradient | CanvasPattern = 'white'
mapSize: Point
constructor (
spawn: Point,
gameSize: Point,
size: Point = DEFAULT_PADDLE_SIZE
) {
this.rect = new Rect(spawn, size)
this.mapSize = gameSize
}
draw (context: CanvasRenderingContext2D): void {
this.rect.draw(context, this.color)
}
move (newY: number): void {
const offset: number = this.rect.size.y / 2
if (newY - offset < 0) {
this.rect.center.y = offset
} else if (newY + offset > this.mapSize.y) {
this.rect.center.y = this.mapSize.y - offset
} else {
this.rect.center.y = newY
}
}
}