Files
CINC/systems/SelectionSystem.js
T

42 lines
1.1 KiB
JavaScript

// systems/SelectionSystem.js
import { clearSelection, addToSelection, setActiveUnit } from './ControlMode.js';
export class SelectionSystem {
constructor(scene) {
this.scene = scene;
this.selectionBox = null;
scene.events.on('selection-start', this.onStart, this);
scene.events.on('selection-end', this.onEnd, this);
}
onStart(pos) {
if (!this.selectionBox) {
this.selectionBox = this.scene.add.rectangle(pos.x, pos.y, 1, 1, 0x00ff00, 0.3)
.setStrokeStyle(1, 0xffffff)
.setVisible(true);
} else {
this.selectionBox.setPosition(pos.x, pos.y).setSize(1, 1).setVisible(true);
}
}
onEnd(rect) {
if (this.selectionBox) {
this.selectionBox.setVisible(false);
}
const units = this.scene.unitGroup.getChildren();
const selected = units.filter(unit => {
const unitRect = unit.getBounds();
return Phaser.Geom.Rectangle.Overlaps(rect, unitRect);
});
clearSelection();
selected.forEach(addToSelection);
if (selected.length > 0) {
setActiveUnit(selected[0]); // 激活第一个
this.scene.events.emit('units-selected', selected);
}
}
}