game: add: vector colision (Not work)

This commit is contained in:
2024-02-01 13:18:11 +01:00
parent ab325ae489
commit d332ef8103
12 changed files with 260 additions and 153 deletions

View File

@ -7,33 +7,37 @@ class Ball
/**
*
* @param {Game} game
* @param {Number} position_x
* @param {Number} position_y
* @param {Number} velocity_x
* @param {Number} velocity_y
* @param {Point} position
* @param {Number} angle
* @param {Number} speed
* @param {Number} size
*/
constructor(game, position_x, position_y, velocity_x, velocity_y)
constructor(game, size, position, angle, speed)
{
/**
* @type {Game}
*/
this.game = game;
/**
* @type {Point}
*/
this.position = position === undefined ? new Point() : position;
/**
* @type {Number}
*/
this.position_x = position_x;
this.size = size;
/**
* @type {Number}
*/
this.position_y = position_y;
this.angle = angle;
/**
* @type {Number}
*/
this.velocity_x = velocity_x;
/**
* @type {Number}
*/
this.velocity_y = velocity_y;
this.speed = speed;
}
/**
@ -42,40 +46,35 @@ class Ball
*/
draw(ctx)
{
ctx.rect(this.position_x, this.position_y, this.game.config.ball_size, this.game.config.ball_size);
ctx.rect(this.position.x - this.size / 2, this.position.y - this.size / 2, this.game.config.ball_size, this.game.config.ball_size);
}
render()
/**
*
* @param {CanvasRenderingContext2D} ctx
*/
render(ctx)
{
/**
* @type {Number}
*/
let new_pos_x = this.position_x + this.velocity_x * this.game.time.deltaTime();
/**
* @type {Number}
*/
let new_pos_y = position_y + this.velocity_y * this.game.time.deltaTime();
let distance = this.speed * (this.game.time.deltaTime() / 1000)
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
let angle_radian = this.angle * Math.PI / 180
this.velocity_x = this.velocity_x + this.game.config.ball_speed_inc;
this.velocity_y = this.velocity_y + this.game.config.ball_speed_inc;
console.log(angle_radian)
this.position.x = this.position.x + distance * Math.cos(angle_radian);
this.position.y = this.position.y + distance * Math.sin(angle_radian);
this.draw(ctx);
}
update (position_x, position_y, velocity_x, velocity_y)
from_json (data)
{
this.position_x = position_x;
this.position_y = position_y;
this.velocity_x = velocity_x;
this.velocity_y = velocity_y;
this.position = this.position.from_json(data.position);
this.size = data.size;
this.angle = data.angle;
this.speed = data.speed;
return this
}
}