UNPKG

@fuwu-yuan/bgew

Version:

Baguette Game Engine Web is a french 2D Web game engine

135 lines (134 loc) 4.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Oval = void 0; const entity_1 = require("../entity"); const collisionSystem_1 = require("../../collisionSystem"); class Oval extends entity_1.Entity { /** * * @param centerX Oval center X * @param centerY Oval center Y * @param radiusX Orizontal radius * @param radiusY Vertical radius * @param strokeColor * @param fillColor * @param hoverStrokeColor * @param hoverFillColor * @param clickStrokeColor * @param clickFillColor */ constructor(centerX, centerY, radiusX, radiusY, strokeColor = null, fillColor = null, hoverStrokeColor = null, hoverFillColor = null, clickStrokeColor = null, clickFillColor = null) { super(centerX, centerY, radiusX * 2, radiusY * 2); this._strokeColor = "rgba(0,0,0, 1.0)"; this._fillColor = "rgba(255, 255, 255, 1.0)"; this._hoverStrokeColor = "rgba(0,0,0, 1.0)"; this._hoverFillColor = "rgba(255, 255, 255, 1.0)"; this._clickStrokeColor = "rgba(0,0,0, 1.0)"; this._clickFillColor = "rgba(255, 255, 255, 1.0)"; this._radiusX = 0; this._radiusY = 0; this._clicked = false; this.radiusX = radiusX; this.radiusY = radiusY; this.onMouseEvent("mousedown", this.onMouseDown.bind(this)); this.onMouseEvent("mouseup", this.onMouseUp.bind(this)); this.onMouseEvent("mouseenter", this.onMouseEnter.bind(this)); this.onMouseEvent("mouseleave", this.onMouseLeave.bind(this)); if (strokeColor) { this.strokeColor = strokeColor; this.hoverStrokeColor = strokeColor; this.clickStrokeColor = strokeColor; } if (fillColor) { this.fillColor = fillColor; this.hoverFillColor = fillColor; this.clickFillColor = fillColor; } if (hoverStrokeColor) { this.hoverStrokeColor = hoverStrokeColor; } if (hoverFillColor) { this.hoverFillColor = hoverFillColor; } if (clickStrokeColor) { this.clickStrokeColor = clickStrokeColor; } if (clickFillColor) { this.clickFillColor = clickFillColor; } } init(board) { this.board = board; this._body = new collisionSystem_1.Circle(this, this.absX, this.absY, this.radiusX); if (this.board.collisionSystem) { this.board.collisionSystem.insert(this.body); } } onMouseDown(event) { this._clicked = true; } onMouseUp(event) { this._clicked = false; } onMouseEnter(event) { //this.board?.changeCursor("pointer"); } onMouseLeave(event) { //this.board?.changeCursor("default"); } get clicked() { return this._clicked; } set clicked(clicked) { this._clicked = clicked; } draw(ctx) { super.draw(ctx); //set color ctx.strokeStyle = this.strokeColor; ctx.fillStyle = this.fillColor; if (this.clicked) { ctx.strokeStyle = this.clickStrokeColor; ctx.fillStyle = this.clickFillColor; } else if (this.hovered) { ctx.strokeStyle = ctx.fillStyle = this.hoverStrokeColor; ctx.fillStyle = ctx.fillStyle = this.hoverFillColor; } //draw oval let path = this.getPath2D(); ctx.fill(path); ctx.stroke(path); } getPath2D() { let path = new Path2D(); path.ellipse(this.x, this.y, this.radiusX, this.radiusY, 0, 0, Math.PI * 2, false); return path; } update(delta) { super.update(delta); this._body.x = this.absX; this._body.y = this.absY; this._body.radius = this.radiusX; } /********************* * Getters & Setters * *********************/ get hoverFillColor() { return this._hoverFillColor; } set hoverFillColor(value) { this._hoverFillColor = value; } get hoverStrokeColor() { return this._hoverStrokeColor; } set hoverStrokeColor(value) { this._hoverStrokeColor = value; } get fillColor() { return this._fillColor; } set fillColor(value) { this._fillColor = value; } get strokeColor() { return this._strokeColor; } set strokeColor(value) { this._strokeColor = value; } get clickFillColor() { return this._clickFillColor; } set clickFillColor(value) { this._clickFillColor = value; } get clickStrokeColor() { return this._clickStrokeColor; } set clickStrokeColor(value) { this._clickStrokeColor = value; } get radiusX() { return this._radiusX; } set radiusX(value) { this._radiusX = value; } get radiusY() { return this._radiusY; } set radiusY(value) { this._radiusY = value; } } exports.Oval = Oval;