game: core: use draw method instead draw_CLASSNAME

This commit is contained in:
2024-01-16 23:27:56 +01:00
parent 516ccdc297
commit e0990db8d1
5 changed files with 128 additions and 78 deletions

View File

@ -2,7 +2,46 @@
class Ball
{
constructor (position_x, position_y, velocity_x, velocity_y)
constructor(game)
{
this.game = game;
this.position_x = game.config.size_x / 2;
this.position_y = game.config.size_y / 2;
this.velocity_x = game.config.ball_speed_start;
this.velocity_y = game.config.ball_speed_start;
}
_collision(old_pos_x, old_pos_y, new_pos_x, new_pos_y)
{
return 0;
}
draw(ctx)
{
ctx.rect(this.position_x, this.position_y, this.game.config.ball_size, this.game.config.ball_size);
}
render()
{
new_pos_x = this.position_x + this.velocity_x * this.game.time.deltaTime();
new_pos_y = this.position_y + this.velocity_y * this.game.time.deltaTime();
if (this._collision(this.position_x, this.position_y, new_pos_x, new_pos_y))
{
this.velocity_x = -this.velocity_x;
this.velocity_y = -this.velocity_y;
new_pos_x = this.position_x + this.velocity_x * this.game.time.deltaTime();
new_pos_y = this.position_y + this.velocity_y * this.game.time.deltaTime();
}
this.position_x = new_pos_x
this.position_y = new_pos_y
this.velocity_x = this.velocity_x + this.game.config.ball_speed_inc;
this.velocity_y = this.velocity_y + this.game.config.ball_speed_inc;
}
update (position_x, position_y, velocity_x, velocity_y)
{
this.position_x = position_x;
this.position_y = position_y;