不积跬步无以至千里,不积小流无以成江海

 Administrator     2024-07-02      游戏学习      304

cocos 获取玩家的尺寸,屏幕的尺寸

import { _decorator, Camera, Component, director, EventKeyboard, EventTouch, Input, input, KeyCode, Label, Node, UITransform, view } from 'cc';
const { ccclass, property } = _decorator;




@ccclass('testMove')

export class testMove extends Component {

    @property(Camera) cam:Camera = null;
    @property(Node) camNode:Node = null;
    @property(Node) mapbg:Node = null;
    @property(Node) role:Node = null;

    @property(Label) label:Label = null;

    score:number = 0
    // 定义一个枚举
    moveLength:number = 20;

    xRange:number = 0;
    yRange:number = 0;


    xPlayer:number = 0;
    yPlayer:number = 0;

    xViewPort:number = 0;
    yViewPort:number = 0;


    start() {
        this.moveEvent();
        // 
        // const test = view.getVisibleSize();
        // const test = this.mapbg.getComponent(UITransform)

        this.schedule(()=>{
            this.label.string = `${this.score++}`
        },0.5)

        // console.log(test.contentSize.width,test.contentSize.height)
    }

    moveEvent(){
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown,this)
        input.on(Input.EventType.KEY_PRESSING, this.onKeyDown,this)
        // input.on(Input.EventType.TOUCH_START, this.onTouchDown,this)
        // input.on(Input.EventType.TOUCH_MOVE, this.onTouchDown,this)
    }

    // onTouchDown(event:EventTouch){
    // //    const a =  event.getUILocation()
    //    const b = event.getUIDelta()

    //    console.log(b)
    // }

    onKeyDown(event:EventKeyboard){
        switch(event.keyCode){
            case KeyCode.KEY_A:
                this.move(Direction.LEFT)
                break;
            case KeyCode.KEY_D:
                this.move(Direction.RIGHT)
                break;
            case KeyCode.KEY_W:
                this.move(Direction.UP)
                break;
            case KeyCode.KEY_S:
                this.move(Direction.DOWN)
                break;
            }
    }
    move(dir:Direction){
        // 获取最大的移动距离
        this.xRange = this.mapbg.getComponent(UITransform).width/2-view.getVisibleSize().width/2;
        this.yRange = this.mapbg.getComponent(UITransform).height/2-view.getVisibleSize().height/2;
        // 获取玩家的方块大小
        this.xPlayer = this.role.getComponent(UITransform).width/2;
        this.yPlayer = this.role.getComponent(UITransform).height/2;
        // 获取屏幕的大小
        this.xViewPort = view.getVisibleSize().width/2;
        this.yViewPort = view.getVisibleSize().height/2;

        // 获取玩家的x和y坐标
        const xRole = this.role.position.x;
        const yRole = this.role.position.y;
        // 获取相机的x和y坐标
        const xCamera = this.camNode.position.x;
        const yCamera = this.camNode.position.y;
        // console.log(this.xRange,this.yRange)
        switch(dir){
            // 方向往左移动
            case Direction.LEFT:
                // 1 如果相机的位置小于最左边的位置,则相机位置左边最大,且不会变化
                if(xCamera <= -this.xRange){
                    this.camNode.setPosition(-this.xRange,this.camNode.position.y,0)
                    // 1.1如果角色的位置小于最左边的位置,则角色位置左边最大,且不会变化
                    if(xRole <= -(this.xRange+view.getVisibleSize().width/2-this.role.getComponent(UITransform).width/2)){
                        this.role.setPosition(-(this.xRange+view.getVisibleSize().width/2-this.role.getComponent(UITransform).width/2),this.role.position.y,0)
                    // 1.2如果角色的位置小于最左边的位置,则角色位置左边最大,且不会变化
                    }else{
                        this.role.setPosition(this.role.position.x-this.moveLength,this.role.position.y,0)
                    }
                // 2 如果相机的位置在地图内,则移动相机往左运动
                }else{
                    // 如果相机在最右边的位置,则地图不动,只角色移动
                    if(this.role.position.x >= this.xRange){
                        this.role.setPosition(this.role.position.x-this.moveLength,this.role.position.y,0)

                    // 如果相机不在在最右边的位置,则地图和角色都移动
                    }else{
                        this.role.setPosition(this.role.position.x-this.moveLength,this.role.position.y,0)
                        this.camNode.setPosition(this.camNode.position.x-this.moveLength,this.camNode.position.y,0)
                    }
                    console.log("左移")
                }
                break;
            case Direction.RIGHT:
                // 如果摄像机位置大于最大移动距离,则不能再移动
                if(this.camNode.position.x >= this.xRange){
                    this.camNode.setPosition(this.xRange,this.camNode.position.y,0)
                    console.log(this.role.position.x <= this.xRange+this.xViewPort-this.xPlayer)
                    if(this.role.position.x <= this.xRange+this.xViewPort-this.xPlayer*2){
                        this.role.setPosition(this.role.position.x+this.moveLength,this.role.position.y,0)
                        console.log("右移3")
                    }else{
                        this.role.setPosition((this.xRange+view.getVisibleSize().width/2-this.role.getComponent(UITransform).width/2),this.role.position.y,0)
                        console.log("右移4")
                    }
                    // console.log("右移1")
                // 相机不在右边,移动到左边 
                }else{
                    // // 如果角色在最右边,移动到左边
                    // if(this.role.position.x <= -this.xRange){
                    //     this.role.setPosition(this.role.position.x+this.moveLength,this.role.position.y,0)
                    // }else{
                        this.role.setPosition(this.role.position.x+this.moveLength,this.role.position.y,0)
                        this.camNode.setPosition(this.camNode.position.x+this.moveLength,this.camNode.position.y,0)
                    // }
                  
                    console.log("右移2")
                }
                break;
            case Direction.UP:
                // 如果相机在最端了,这相机不移动了
                if(this.camNode.position.y >= this.yRange){
                    this.camNode.setPosition(this.camNode.position.x,this.yRange,0)
                // 如果角色没有移到最顶端,这游戏角色可以移动
                    // 如果角色在相机范围内,游戏角色可以移动
                    if(this.role.position.y < this.yRange+this.yViewPort-this.yPlayer){
                        this.role.setPosition(this.role.position.x,this.role.position.y+this.moveLength,0)
                    }else{
                        this.role.setPosition(this.role.position.x,this.yRange+this.yViewPort-this.yPlayer,0)
                    }
                }else{
                // 如果相机不在最顶端,则相机和角色都移动
                    this.camNode.setPosition(this.camNode.position.x,this.camNode.position.y+this.moveLength,0)
                    this.role.setPosition(this.role.position.x,this.role.position.y+this.moveLength,0)
                    console.log("上移")
                }
                break;
            case Direction.DOWN:
                if(this.camNode.position.y <= -this.yRange){
                    this.camNode.setPosition(this.camNode.position.x,-this.yRange,0)
                    if(this.role.position.y < -(this.yRange+this.yViewPort-this.yPlayer)){
                        this.role.setPosition(this.role.position.x,this.role.position.y+this.moveLength,0)
                    }else{
                        this.role.setPosition(this.role.position.x,-(this.yRange+this.yViewPort-this.yPlayer),0)
                    }
                }else{
                    this.camNode.setPosition(this.camNode.position.x,this.camNode.position.y-this.moveLength,0)
                    this.role.setPosition(this.role.position.x,this.role.position.y-this.moveLength,0)
                    console.log("下移")
                }
               break;
            }
    }

    update(deltaTime: number) {
        // this.move(dir:Direction)
    }
}

export enum Direction{
    LEFT = 1,
    RIGHT = 2,
    UP = 3,
    DOWN = 4
}