修改信号机实现方式
This commit is contained in:
parent
0b3b8013ba
commit
49b9c83949
@ -1,68 +0,0 @@
|
||||
import { Container } from '@pixi/display';
|
||||
import { Graphics } from 'pixi.js';
|
||||
import { SignalColorEnum, signalConsts } from './Signal';
|
||||
|
||||
export class Lamp extends Container {
|
||||
circleLamp: Graphics = new Graphics();
|
||||
logicMode: Graphics = new Graphics();
|
||||
radiusX = 0;
|
||||
radiusY = 0;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.addChild(this.circleLamp);
|
||||
this.addChild(this.logicMode);
|
||||
}
|
||||
paint(radiusX: number, radiusY: number) {
|
||||
this.radiusX = radiusX;
|
||||
this.radiusY = radiusY;
|
||||
this.createLamp();
|
||||
}
|
||||
createLamp(color?: string) {
|
||||
this.circleLamp.clear();
|
||||
this.circleLamp.lineStyle(
|
||||
signalConsts.lampLineWidth,
|
||||
SignalColorEnum.lampLineColor
|
||||
);
|
||||
if (!color) {
|
||||
color = '0X' + this.getCanvas().properties.backgroundColor.substring(1);
|
||||
}
|
||||
this.circleLamp.beginFill(color, 1);
|
||||
this.circleLamp.drawCircle(
|
||||
this.radiusX,
|
||||
this.radiusY,
|
||||
signalConsts.lampRadius
|
||||
);
|
||||
this.circleLamp.endFill();
|
||||
}
|
||||
createLogicMode() {
|
||||
this.logicMode
|
||||
.clear()
|
||||
.lineStyle(
|
||||
signalConsts.logicModeLineWidth,
|
||||
SignalColorEnum.logicModeColor
|
||||
)
|
||||
.moveTo(
|
||||
this.radiusX - signalConsts.logicModeDistance,
|
||||
this.radiusY + signalConsts.logicModeDistance
|
||||
)
|
||||
.lineTo(
|
||||
this.radiusX + signalConsts.logicModeDistance,
|
||||
this.radiusY - signalConsts.logicModeDistance
|
||||
)
|
||||
.moveTo(
|
||||
this.radiusX - signalConsts.logicModeDistance,
|
||||
this.radiusY - signalConsts.logicModeDistance
|
||||
)
|
||||
.lineTo(
|
||||
this.radiusX + signalConsts.logicModeDistance,
|
||||
this.radiusY + signalConsts.logicModeDistance
|
||||
);
|
||||
}
|
||||
logicModeClear() {
|
||||
this.logicMode.clear();
|
||||
}
|
||||
lampClear() {
|
||||
this.circleLamp.clear();
|
||||
}
|
||||
}
|
@ -37,7 +37,7 @@ export class LampMainBody extends JlGraphic {
|
||||
static Type = 'LampMainBody';
|
||||
lampNum = 1;
|
||||
lampPost: Graphics = new Graphics();
|
||||
lamps: Lamp[] = [];
|
||||
lamps: Graphics = new Graphics();
|
||||
redFlashAnimation: GraphicAnimation | null = null;
|
||||
mirror = false;
|
||||
deltaTime = 0;
|
||||
@ -45,8 +45,12 @@ export class LampMainBody extends JlGraphic {
|
||||
|
||||
constructor() {
|
||||
super(LampMainBody.Type);
|
||||
this.addChild(this.lampPost);
|
||||
this.addChild(this.lamps);
|
||||
}
|
||||
paint(mt: graphicData.Signal.Model, mirror: boolean, states: ISignalState) {
|
||||
this.lampPost.clear();
|
||||
this.lamps.clear();
|
||||
this.mirror = mirror;
|
||||
this.states = states;
|
||||
if (
|
||||
@ -57,8 +61,6 @@ export class LampMainBody extends JlGraphic {
|
||||
} else {
|
||||
this.lampNum = 3;
|
||||
}
|
||||
this.removeChildren(0);
|
||||
this.lampPost = new Graphics();
|
||||
let lpp = new Point(signalConsts.levelLampPostLength, 0);
|
||||
if (mirror) {
|
||||
lpp = calculateMirrorPoint(new Point(0, 0), lpp);
|
||||
@ -69,22 +71,6 @@ export class LampMainBody extends JlGraphic {
|
||||
.lineTo(0, signalConsts.verticalLampPostLength / 2)
|
||||
.moveTo(0, 0)
|
||||
.lineTo(lpp.x, lpp.y);
|
||||
this.addChild(this.lampPost);
|
||||
|
||||
this.lamps = [];
|
||||
for (let i = 0; i < this.lampNum; i++) {
|
||||
const lamp = new Lamp();
|
||||
this.addChild(lamp);
|
||||
const radiusX =
|
||||
(1 + i * 2) * signalConsts.lampRadius +
|
||||
signalConsts.levelLampPostLength;
|
||||
let lrp = new Point(radiusX, 0);
|
||||
if (mirror) {
|
||||
lrp = calculateMirrorPoint(new Point(0, 0), lrp);
|
||||
}
|
||||
lamp.paint(lrp.x, lrp.y);
|
||||
this.lamps.push(lamp);
|
||||
}
|
||||
this.chagneState(this.states);
|
||||
}
|
||||
doRepaint() {
|
||||
@ -112,69 +98,50 @@ export class LampMainBody extends JlGraphic {
|
||||
whiteFlashA.pause();
|
||||
}
|
||||
}
|
||||
paintLamp(colors: string[]) {
|
||||
this.lamps.lineStyle(
|
||||
signalConsts.lampLineWidth,
|
||||
SignalColorEnum.lampLineColor
|
||||
);
|
||||
for (let i = 0; i < this.lampNum; i++) {
|
||||
const radiusX =
|
||||
(1 + i * 2) * signalConsts.lampRadius +
|
||||
signalConsts.levelLampPostLength;
|
||||
let lrp = new Point(radiusX, 0);
|
||||
if (this.mirror) {
|
||||
lrp = calculateMirrorPoint(new Point(0, 0), lrp);
|
||||
}
|
||||
const color = colors[i] ? colors[i] : SignalColorEnum.closeLamp;
|
||||
this.lamps.beginFill(color, 1);
|
||||
this.lamps.drawCircle(
|
||||
lrp.x,
|
||||
lrp.y,
|
||||
signalConsts.lampRadius
|
||||
);
|
||||
this.lamps.endFill();
|
||||
}
|
||||
}
|
||||
chagneState(states: ISignalState) {
|
||||
this.stopAnmiation();
|
||||
try {
|
||||
if (states.aspect === state.Signal.Aspect.H) {
|
||||
this.lamps[0].createLamp(SignalColorEnum.redLamp);
|
||||
this.lamps.forEach((lamp, index) => {
|
||||
if (index !== 0) {
|
||||
lamp.createLamp(SignalColorEnum.closeLamp);
|
||||
}
|
||||
});
|
||||
this.paintLamp([SignalColorEnum.redLamp]);
|
||||
} else if (states.aspect === state.Signal.Aspect.L) {
|
||||
this.lamps[1].createLamp(SignalColorEnum.greenLamp);
|
||||
this.lamps.forEach((lamp, index) => {
|
||||
if (index !== 1) {
|
||||
lamp.createLamp(SignalColorEnum.closeLamp);
|
||||
}
|
||||
});
|
||||
this.paintLamp(['', SignalColorEnum.greenLamp]);
|
||||
} else if (states.aspect === state.Signal.Aspect.U) {
|
||||
this.lamps[2].createLamp(SignalColorEnum.yellowLamp);
|
||||
this.lamps.forEach((lamp, index) => {
|
||||
if (index !== 2) {
|
||||
lamp.createLamp(SignalColorEnum.closeLamp);
|
||||
}
|
||||
});
|
||||
this.paintLamp(['', '', SignalColorEnum.yellowLamp]);
|
||||
} else if (states.aspect === state.Signal.Aspect.HU) {
|
||||
this.lamps[0].createLamp(SignalColorEnum.redLamp);
|
||||
this.lamps[1].createLamp(SignalColorEnum.closeLamp);
|
||||
this.lamps[2].createLamp(SignalColorEnum.yellowLamp);
|
||||
this.paintLamp([SignalColorEnum.redLamp, '', SignalColorEnum.yellowLamp]);
|
||||
} else if (states.aspect === state.Signal.Aspect.A) {
|
||||
this.lamps[0].createLamp(SignalColorEnum.blueLamp);
|
||||
this.lamps[1].createLamp(SignalColorEnum.closeLamp);
|
||||
this.paintLamp([SignalColorEnum.blueLamp]);
|
||||
} else if (states.aspect === state.Signal.Aspect.B) {
|
||||
this.lamps[0].createLamp(SignalColorEnum.whiteLamp);
|
||||
this.lamps[1].createLamp(SignalColorEnum.closeLamp);
|
||||
this.paintLamp([SignalColorEnum.whiteLamp]);
|
||||
} else if (states.aspect === state.Signal.Aspect.OFF) {
|
||||
this.lamps.forEach((lamp) =>
|
||||
lamp.createLamp(SignalColorEnum.closeLamp)
|
||||
);
|
||||
this.paintLamp([]);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('信号机状态处理异常!', error);
|
||||
}
|
||||
}
|
||||
createFlashAnmiation(
|
||||
name: string,
|
||||
color: string,
|
||||
lampIndex: number
|
||||
): GraphicAnimation {
|
||||
const bgColor = '0X000000';
|
||||
const flashAnmiation = GraphicAnimation.init({
|
||||
name: name,
|
||||
run: (dt: number) => {
|
||||
this.deltaTime += dt;
|
||||
if (this.deltaTime > 60) {
|
||||
this.deltaTime = 0;
|
||||
this.lamps[lampIndex].createLamp(color);
|
||||
} else if (this.deltaTime > 30) {
|
||||
this.lamps.forEach((lamp) => {
|
||||
lamp.createLamp(bgColor);
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
return flashAnmiation;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user