UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

108 lines (93 loc) 2.02 kB
import { circle_intersects_circle } from "./circle_intersects_circle.js"; /** * A circle or a 2-dimensional sphere. Represented as a position (x,y) and radius (r) */ class Circle { /** * * @param {number} [x=0] * @param {number} [y=0] * @param {number} [r=0] radius */ constructor(x = 0, y = 0, r = 0) { /** * Position along X axis * @type {Number} */ this.x = x; /** * Position along Y axis * @type {Number} */ this.y = y; /** * Radius * @type {Number} */ this.r = r; } /** * * @param {Vector2} target */ readPosition(target) { target.set(this.x, this.y); } /** * * @param {Number} deltaX * @param {Number} deltaY */ move(deltaX, deltaY) { this.x += deltaX; this.y += deltaY; } /** * * @param {Circle} other * @returns {boolean} */ overlaps(other) { const x0 = this.x; const y0 = this.y; const r0 = this.r; const x1 = other.x; const y1 = other.y; const r1 = other.r; return circle_intersects_circle(x0, y0, r0, x1, y1, r1); } /** * * @param {Circle} other * @returns {boolean} */ equals(other) { return this.x === other.x && this.y === other.y && this.r === other.r; } /** * * @param {number} x * @param {number} y * @param {number} r */ set(x, y, r) { this.x = x; this.y = y; this.r = r; } /** * * @param {Circle} other */ copy(other) { this.set(other.x, other.y, other.r); } /** * * @returns {Circle} */ clone() { return new Circle(this.x, this.y, this.r); } } export default Circle;