#
luxiaotao1123
2024-10-10 cbd9fde3a2f8d4f5c45bea1a5215ad843e8dabc6
zy-acs-flow/src/map/player.js
@@ -1,5 +1,6 @@
import * as PIXI from 'pixi.js';
import * as TWEEDLE from 'tweedle.js';
import * as Tool from './tool';
export default class Player {
@@ -16,6 +17,7 @@
        this.activateMapScale();
        this.activateMapPan();
        this.showCoordinates();
        this.startupTicker();
        const bunny = PIXI.Sprite.from('https://pixijs.com/assets/bunny.png');
@@ -27,6 +29,99 @@
            bunny.rotation += 0.1 * delta;
        });
    }
    activateMapMultiSelect = (fn) => {
        const mapMultiSelectHandle = (event) => {
            if (event.button !== 0) {
                return;
            }
            let isSelecting = false;
            if (!this.selectionBox) {
                this.selectionBox = new PIXI.Graphics();
                this.app.stage.addChild(this.selectionBox);
            }
            // select start pos
            const startPoint = new PIXI.Point();
            this.app.renderer.events.mapPositionToPoint(startPoint, event.clientX, event.clientY);
            let selectionStart = { x: startPoint.x, y: startPoint.y };
            // avoid trigger sprite event
            let hasHitSprite = false;
            for (let child of this.mapContainer.children) {
                if (child.data?.uuid) {
                    if (child.getBounds().contains(selectionStart.x, selectionStart.y)) {
                        hasHitSprite = true; break;
                    }
                }
            }
            if (!hasHitSprite) {
                isSelecting = true;
            }
            const handleMouseMove = (event) => {
                if (isSelecting) {
                    // select end pos
                    const endPoint = new PIXI.Point();
                    this.app.renderer.events.mapPositionToPoint(endPoint, event.clientX, event.clientY);
                    const selectionEnd = { x: endPoint.x, y: endPoint.y }
                    const width = Math.abs(selectionEnd.x - selectionStart.x);
                    const height = Math.abs(selectionEnd.y - selectionStart.y);
                    this.selectionBox.clear();
                    this.selectionBox.lineStyle(2, 0xCCCCCC, 1);
                    this.selectionBox.beginFill(0xCCCCCC, 0.2);
                    this.selectionBox.drawRect(Math.min(selectionStart.x, selectionEnd.x), Math.min(selectionStart.y, selectionEnd.y), width, height);
                    this.selectionBox.endFill();
                }
            }
            this.mapContainer.parent.on('mousemove', handleMouseMove);
            this.mapContainer.parent.on('mouseup', (event) => {
                if (isSelecting) {
                    this.clearSelectedSprites();
                    // sprite show style which be selected
                    this.mapContainer.children.forEach(child => {
                        if (Tool.isSpriteInSelectionBox(child, this.selectionBox)) {
                            this.selectedSprites.push(child);
                            Tool.markSprite(child);
                        }
                    })
                    isSelecting = false;
                    this.selectionBox.clear();
                    // complete
                    if (fn) {
                        fn(this.selectedSprites, () => {
                            // trigger at end of selectionBox when fn happen mouseup
                            this.selectedSprites = [];
                            this.activateMapMultiSelect(fn);
                        });
                    }
                }
                this.mapContainer.parent.off('mousemove', handleMouseMove);
            });
        }
        this.mapContainer.parent.off('mousedown');
        this.mapContainer.parent.on('mousedown', mapMultiSelectHandle)
    }
    clearSelectedSprites = () => {
        if (this.selectedSprites && this.selectedSprites.length > 0) {
            this.selectedSprites.forEach(child => {
                Tool.unMarkSprite(child);
            })
        }
        this.selectedSprites = [];
    }
    activateMapScale = () => {
@@ -87,6 +182,70 @@
        this.app.view.addEventListener('mousedown', mapPanHandle);
    }
    adaptScreen = () => {
        if (!this.mapContainer || !this.app) {
            return;
        }
        this.mapContainer.scale.set(1);
        this.mapContainer.position.set(0, 0);
        if (this.mapContainer.children.length === 0) {
            return;
        }
        let minX, maxX, minY, maxY;
        for (let sprite of this.mapContainer.children) {
            if (sprite?.data?.uuid) {
                let bounds = sprite.getBounds();
                minX = minX !== undefined ? Math.min(minX, bounds.x) : bounds.x;
                minY = minY !== undefined ? Math.min(minY, bounds.y) : bounds.y;
                maxX = maxX !== undefined ? Math.max(maxX, bounds.x + bounds.width) : bounds.x + bounds.width;
                maxY = maxY !== undefined ? Math.max(maxY, bounds.y + bounds.height) : bounds.y + bounds.height;
            }
        }
        this.scale = Math.min(
            this.app.renderer.width / (maxX - minX) * 0.8,
            this.app.renderer.height / (maxY - minY) * 0.8
        );
        let centerPoint = {
            x: (minX + maxX) / 2 * this.mapContainer.scale.x,
            y: (minY + maxY) / 2 * this.mapContainer.scale.y
        };
        new TWEEDLE.Tween(this.mapContainer.scale).easing(TWEEDLE.Easing.Quadratic.Out)
            .to({
                x: this.scale,
                y: this.scale
            }, 200).start();
        new TWEEDLE.Tween(this.mapContainer.position).easing(TWEEDLE.Easing.Quadratic.Out)
            .to({
                x: this.app.renderer.width / 2 - centerPoint.x * this.scale,
                y: this.app.renderer.height / 2 - centerPoint.y * this.scale
            }, 200).start();
    }
    showCoordinates = () => {
        const coordinatesText = new PIXI.Text('{ x: 0, y: 0 }', {
            fill: this.themeMode === 'dark' ? 0xffffff : 0x000000,
            fontSize: 13,
            fontFamily: 'MicrosoftYaHei',
            fontWeight: 'bold',
        });
        coordinatesText.name = 'xyStr'
        coordinatesText.position.set(10, 10);
        this.app.stage.addChild(coordinatesText);
        const mouseMoveInfoTextHandler = (event) => {
            const mouseX = (event.clientX - this.mapContainer.position.x) / this.scale;
            const mouseY = (event.clientY - this.mapContainer.position.y) / this.scale;
            coordinatesText.text = `{ x: ${mouseX.toFixed(2)}, y: ${mouseY.toFixed(2)} }`;
        };
        this.app.view.addEventListener('mousemove', mouseMoveInfoTextHandler);
    }
    showGridLines = () => {
        this.hideGridLines();
        if (!this.gridLineContainer) {
@@ -126,7 +285,6 @@
        }
    }
    startupTicker = () => {
        this.app.ticker.add((delta) => {
            TWEEDLE.Group.shared.update();
@@ -135,10 +293,6 @@
    resize = (width, height) => {
        this.app.renderer.resize(width, height);
        this.mapContainer.children.forEach((child) => {
            child.x = width / 2;
            child.y = height / 2;
        });
    }
    destroy = () => {