92 lines
2.0 KiB
JavaScript
92 lines
2.0 KiB
JavaScript
import { Game } from "./Game.js";
|
|
import { Point } from "./Point.js";
|
|
import { renderCube } from "../../3D/cube.js"
|
|
|
|
class Ball
|
|
{
|
|
/**
|
|
*
|
|
* @param {Game} game
|
|
* @param {Point} position
|
|
* @param {Number} angle
|
|
* @param {Number} speed
|
|
* @param {Number} size
|
|
*/
|
|
constructor(game, size, position, angle, speed)
|
|
{
|
|
/**
|
|
* @type {Game}
|
|
*/
|
|
this.game = game;
|
|
|
|
/**
|
|
* @type {Point}
|
|
*/
|
|
this.position = position === undefined ? new Point() : position;
|
|
|
|
/**
|
|
* @type {Number}
|
|
*/
|
|
this.size = size;
|
|
|
|
/**
|
|
* @type {Number}
|
|
*/
|
|
this.angle = angle;
|
|
|
|
/**
|
|
* @type {Number}
|
|
*/
|
|
this.speed = speed;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {CanvasRenderingContext2D} ctx
|
|
*/
|
|
draw(ctx)
|
|
{
|
|
if(ctx instanceof CanvasRenderingContext2D)
|
|
{
|
|
ctx.rect(this.position.x - this.size / 2, this.position.y - this.size / 2, this.size, this.size);
|
|
}
|
|
else if(ctx instanceof WebGLRenderingContext)
|
|
{
|
|
const size = this.size * 3;
|
|
const posx = (this.position.x - this.size / 2) - this.game.config.size_x / 2;
|
|
const posy = (this.position.y - this.size / 2) - this.game.config.size_y / 2;
|
|
renderCube(ctx, posx, 0, posy, 0, size, size, size);
|
|
}
|
|
else
|
|
{
|
|
alert('Unknown rendering context type');
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {CanvasRenderingContext2D} ctx
|
|
*/
|
|
render(ctx)
|
|
{
|
|
let distance = this.speed * (this.game.time.deltaTime() / 1000);
|
|
|
|
this.position.x = this.position.x + distance * Math.cos(this.angle);
|
|
this.position.y = this.position.y - distance * Math.sin(this.angle);
|
|
|
|
this.draw(ctx);
|
|
}
|
|
|
|
from_json(data)
|
|
{
|
|
this.position = this.position.from_json(data.position);
|
|
this.size = data.size;
|
|
this.angle = data.angle;
|
|
this.speed = data.speed;
|
|
|
|
return this;
|
|
}
|
|
}
|
|
|
|
export { Ball };
|