import { GameObjectBaseType, GameResource } from '@gedit/render-engine'; import { setGameObjectBaseData, getVisible } from '../../utils'; import { drawPolygon, getSizeWidthAndHeight, PolygonStarGameObjectData, getPolygonPoint, } from '@gedit/canvas-draw'; import { PixiGraphicsGameObject } from './common'; export class StarGameObject extends PixiGraphicsGameObject { static type = GameObjectBaseType.STAR; protected textureCanvas: HTMLCanvasElement = document.createElement('canvas'); protected textures: PIXI.Texture; protected baseTexture: PIXI.CanvasSource; protected dataJSON: string; create(resource: GameResource): void { // graphics 不能画渐变;延用 canvas-draw 画 // this.rendererObject = new PIXI.Graphics(); this.rendererObject = new PIXI.Sprite(); this.scene.container.addChild?.(this.rendererObject); // this.textureCanvas = this.scene.rootTextureCanvas; this.baseTexture = new PIXI.CanvasSource({ resource: this.textureCanvas, }); this.textures = new PIXI.Texture({ source: this.baseTexture, }); this.rendererObject.texture = this.textures; super.create.apply(this, arguments); } setDepth(depth: number): void { if (this.rendererObject) { this.rendererObject!.zIndex = depth; } } dispose(): void { this.rendererObject?.destroy(); super.dispose(); } draw(data: PolygonStarGameObjectData): void { const { style, star, size } = data; // console.time('ddd'); const { sides } = star; const { line = [] } = style || {}; this.sizeBoxBounds = getSizeWidthAndHeight( data, line, getPolygonPoint( size.width / 2, size.height / 2, size.width, size.height, sides ) ); drawPolygon( this.textureCanvas, data, GameObjectBaseType.STAR, star.distance, this.sizeBoxBounds ); this.textures.destroy(); this.baseTexture = new PIXI.CanvasSource({ resource: this.textureCanvas, }); this.textures = new PIXI.Texture({ source: this.baseTexture, }); this.rendererObject.texture = this.textures; } update(data: PolygonStarGameObjectData): void { const { style, radius, star, shadow, ...otherData } = data; const { size } = otherData; const dataJSON = JSON.stringify({ style, radius, star, size, shadow }); if (dataJSON !== this.dataJSON) { this.draw(data); } if (this.rendererObject) { setGameObjectBaseData(this, otherData, true); this.rendererObject.visible = getVisible(data, this); } this.dataJSON = dataJSON; super.update(data); } }