rt-graphic-component/components/Platform/Platform.js
2023-12-25 13:25:01 +08:00

102 lines
3.4 KiB
JavaScript

import { JlGraphic, JlGraphicTemplate, getRectangleCenter } from 'jl-graphic';
import { Container, Graphics, Color, Rectangle } from 'pixi.js';
import { platformConstsMap } from './PlatformConfig.js';
class RectGraphic extends Container {
static Type = 'RectPlatForm';
rectGraphic;
constructor() {
super();
this.rectGraphic = new Graphics();
this.addChild(this.rectGraphic);
}
draw(platformConsts) {
const rectGraphic = this.rectGraphic;
const fillColor = platformConsts.rectColor;
rectGraphic
.clear()
.lineStyle(platformConsts.lineWidth, new Color(fillColor))
.beginFill(fillColor, 1)
.drawRect(0, 0, platformConsts.width, platformConsts.height).endFill;
rectGraphic.pivot = getRectangleCenter(new Rectangle(0, 0, platformConsts.width, platformConsts.height));
}
clear() {
this.rectGraphic.clear();
}
}
class DoorGraphic extends Container {
static Type = 'Door';
doorGraphic;
doorCloseGraphic;
constructor() {
super();
this.doorGraphic = new Graphics();
this.doorCloseGraphic = new Graphics();
this.addChild(this.doorGraphic);
this.addChild(this.doorCloseGraphic);
}
draw(platformConsts, doorConstsConfig) {
const doorGraphic = this.doorGraphic;
const doorCloseGraphic = this.doorCloseGraphic;
let lineColor = doorConstsConfig.doorGreen;
doorGraphic.clear()
.lineStyle(platformConsts.lineWidth, new Color(lineColor))
.moveTo(-platformConsts.width / 2 - platformConsts.lineWidth / 2, 0)
.lineTo(-doorConstsConfig.doorOpenSpacing, 0)
.moveTo(doorConstsConfig.doorOpenSpacing, 0)
.lineTo(platformConsts.width / 2 + platformConsts.lineWidth / 2, 0);
//屏蔽门闭合
doorCloseGraphic.clear()
.lineStyle(platformConsts.lineWidth, new Color(lineColor))
.moveTo(-doorConstsConfig.doorOpenSpacing, 0)
.lineTo(doorConstsConfig.doorOpenSpacing, 0);
}
clear() {
this.doorGraphic.clear();
this.doorCloseGraphic.clear();
}
}
class Platform extends JlGraphic {
static Type = 'Platform';
categoryType;
rectGraphic = new RectGraphic();
doorGraphic = new DoorGraphic();
constructor(categoryType) {
super(Platform.Type);
this.categoryType = categoryType;
this.addChild(this.rectGraphic);
this.addChild(this.doorGraphic);
}
get datas() {
return this.getDatas();
}
get states() {
return this.getStates();
}
doRepaint() {
this.doorGraphic.clear();
const platformConsts = platformConstsMap.get(this.categoryType);
if (platformConsts) {
this.rectGraphic.draw(platformConsts);
if (platformConsts.doorGraphic) {
this.doorGraphic.draw(platformConsts, platformConsts.doorGraphic);
}
}
}
}
class PlatformTemplate extends JlGraphicTemplate {
categoryType;
constructor(dataTemplate, stateTemplate, gategoryConsts) {
super(Platform.Type, { dataTemplate, stateTemplate });
this.categoryType = gategoryConsts;
}
new() {
const g = new Platform(this.categoryType);
g.loadData(this.datas);
g.loadState(this.states);
return g;
}
}
export { Platform, PlatformTemplate };