|
@ -1,7 +1,7 @@ |
|
|
import { type Paddle } from './Paddle' |
|
|
import { type Paddle } from './Paddle' |
|
|
import { type Point, Rect } from './utils' |
|
|
import { type Point, Rect } from './utils' |
|
|
import { type MapDtoValidated } from '../dtos/MapDtoValidated' |
|
|
import { type MapDtoValidated } from '../dtos/MapDtoValidated' |
|
|
import { DEFAULT_BALL_SIZE, DEFAULT_BALL_INITIAL_SPEED } from './constants' |
|
|
import { DEFAULT_BALL_SIZE, DEFAULT_BALL_INITIAL_SPEED, GAME_TICKS } from './constants' |
|
|
|
|
|
|
|
|
export class Ball { |
|
|
export class Ball { |
|
|
rect: Rect |
|
|
rect: Rect |
|
@ -9,6 +9,7 @@ export class Ball { |
|
|
speed: Point |
|
|
speed: Point |
|
|
spawn: Point |
|
|
spawn: Point |
|
|
indexPlayerScored: number |
|
|
indexPlayerScored: number |
|
|
|
|
|
timeoutTime: number |
|
|
|
|
|
|
|
|
constructor ( |
|
|
constructor ( |
|
|
spawn: Point, |
|
|
spawn: Point, |
|
@ -20,6 +21,7 @@ export class Ball { |
|
|
this.initial_speed = speed |
|
|
this.initial_speed = speed |
|
|
this.spawn = spawn.clone() |
|
|
this.spawn = spawn.clone() |
|
|
this.indexPlayerScored = -1 |
|
|
this.indexPlayerScored = -1 |
|
|
|
|
|
this.timeoutTime = 0 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
getIndexPlayerScored (): number { |
|
|
getIndexPlayerScored (): number { |
|
@ -29,9 +31,14 @@ export class Ball { |
|
|
update (canvasRect: Rect, paddles: Paddle[], map: MapDtoValidated): void { |
|
|
update (canvasRect: Rect, paddles: Paddle[], map: MapDtoValidated): void { |
|
|
if (!canvasRect.contains_x(this.rect)) { |
|
|
if (!canvasRect.contains_x(this.rect)) { |
|
|
this.indexPlayerScored = this.playerScored() |
|
|
this.indexPlayerScored = this.playerScored() |
|
|
|
|
|
this.timeoutTime = 2000 |
|
|
} else { |
|
|
} else { |
|
|
this.indexPlayerScored = -1 |
|
|
this.indexPlayerScored = -1 |
|
|
this.move(canvasRect, paddles, map) |
|
|
if (this.timeoutTime <= 0) { |
|
|
|
|
|
this.move(canvasRect, paddles, map) |
|
|
|
|
|
} else { |
|
|
|
|
|
this.timeoutTime -= 1000 / GAME_TICKS |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|