Untitled

 avatar
unknown
plain_text
2 years ago
12 kB
6
Indexable
import CameraTransposer from "./CameraTransposer";
import GameMgr from "./Gamemgr";

const { ccclass, property } = cc._decorator;
@ccclass
export default class Player extends cc.Component {

    @property()
    playerSpeed: number = 200;
    @property()
    jumpSpeed: number = 300;
    @property()
    dashSpeed: number = 350;


    @property(cc.Node)
    gameMgr: cc.Node = null;
    @property(cc.ParticleSystem)
    particle: cc.ParticleSystem = null;
    @property()
    reverse_gravity: boolean = false;



    private animation: cc.Animation = null;
    private rigidbody: cc.RigidBody = null;

    private moveDir = 0;
    public windspeed = 0;
    private faceRight: boolean = true;

    public canjump: boolean = false;
    public canDash: boolean = false;
    private isDashing: boolean = false;
    private dying: boolean = false;
    private onGround: boolean = false;

    private isClimbing: boolean = false;
    public canClimb: boolean = false;
    private endurance: number = 50.0;

    public inBubble: boolean = false;
    onLoad(): void {
        this.animation = this.node.getComponent(cc.Animation);
        this.rigidbody = this.node.getComponent(cc.RigidBody);
    }

    start() {

    }

    update(dt) {
        this.playanimation();
        if (this.isClimbing) {
            if (this.faceRight)
                this.node.scaleX = -1
            else
                this.node.scaleX = 1;
        }
        else if (this.moveDir != 0) {
            this.node.scaleX = this.moveDir
        }

        if (this.dying) {
            this.node.setPosition(cc.v2(77, 158));
            this.dying = false;
        }
        if (this.node.y < 0) {
            this.node.setPosition(cc.v2(77, 158));
        }
        if (this.inBubble) {
            return;
        }
        if (!this.isDashing) {
            this.node.x += (this.playerSpeed * this.moveDir - this.windspeed) * dt;
        }
        if (this.moveDir != 0) {
            this.node.scaleX = this.moveDir;
        }
        if (this.canClimb && this.gameMgr.getComponent(GameMgr).z_pressed) {
            this.rigidbody.linearVelocity = cc.v2(0, 0);
            this.isClimbing = true;
            this.getComponent(cc.RigidBody).gravityScale = 0;
        }

        if (this.isClimbing && this.gameMgr.getComponent(GameMgr).z_pressed) {
            this.handleClimbing();
        } else if (this.isClimbing && !this.gameMgr.getComponent(GameMgr).z_pressed) {
            this.rigidbody.linearVelocity = cc.v2(0, 0);
            this.isClimbing = false;
            this.getComponent(cc.RigidBody).gravityScale = 1;
            this.endurance += this.endurance == 50 ? 0 : this.onGround ? 0.1 : 0;
        } else if (!this.isClimbing && this.gameMgr.getComponent(GameMgr).z_pressed) {
            this.isClimbing = false;
            this.getComponent(cc.RigidBody).gravityScale = 1;
            this.endurance += this.endurance == 50 ? 0 : this.onGround ? 0.1 : 0;
        } else {
            this.getComponent(cc.RigidBody).gravityScale = 1;
            this.endurance += this.endurance == 50 ? 0 : this.onGround ? 0.1 : 0;
        }

        if (this.reverse_gravity) {
            this.node.scaleY = -1;
        }
    }

    handleClimbing() {
        let down_pressed = cc.find("gameMgr").getComponent(GameMgr).down_pressed;
        let up_pressed = cc.find("gameMgr").getComponent(GameMgr).up_pressed;
        if (this.endurance <= 0) {
            // this.rigidbody.linearVelocity = cc.v2(0, 0);
            this.isClimbing = false;
            this.canClimb = false;
            this.getComponent(cc.RigidBody).gravityScale = 1;
            return;
        } else {
            this.endurance -= 0.1;
        }
        if (down_pressed) {
            this.node.y -= 1;
        } else if (up_pressed) {
            this.node.y += 0.5;
        }
    }

    playerMove(left_pressed: boolean, right_pressed: boolean) {
        if (left_pressed) {
            this.faceRight = false;
            this.moveDir = -1;
        } else if (right_pressed) {
            this.faceRight = true;
            this.moveDir = 1;
        } else {
            this.moveDir = 0;
        }
    }

    playerJump(c_pressed: boolean) {
        if (c_pressed && this.canjump && !this.inBubble) {
            this.canjump = false;
            let lv = this.rigidbody.linearVelocity;
            if (this.reverse_gravity) {
                lv.y = -this.jumpSpeed;
            } else {
                lv.y = this.jumpSpeed;
            }
            this.rigidbody.linearVelocity = lv;
            //console.log("jump")
        }
    }

    playerDash(x_pressed: boolean) {
        if (x_pressed && this.canDash && !this.isDashing && !this.inBubble) {
            //console.log("dash");
            this.canDash = false;
            this.isDashing = true;
            this.scheduleOnce(() => {
                this.isDashing = false;
                this.rigidbody.linearVelocity = cc.v2(0, 0);
            }, 0.25)

            this.rigidbody.linearVelocity = cc.v2(0, 0);

            this.scheduleOnce(() => {
                let lv = cc.v2(0, 0);
                let left_pressed = cc.find("gameMgr").getComponent(GameMgr).left_pressed;
                let right_pressed = cc.find("gameMgr").getComponent(GameMgr).right_pressed;
                let down_pressed = cc.find("gameMgr").getComponent(GameMgr).down_pressed;
                let up_pressed = cc.find("gameMgr").getComponent(GameMgr).up_pressed;

                if (left_pressed || right_pressed) {
                    lv.x = right_pressed ? 1 : -1;
                } else {
                    if (down_pressed || up_pressed) {
                        lv.x = 0;
                    } else {
                        lv.x = this.faceRight ? 1 : -1;
                    }
                }

                if (down_pressed || up_pressed) {
                    lv.y = up_pressed ? 1 : -1;
                } else {
                    lv.y = 0;
                }
                lv.normalizeSelf();
                this.handleparticle(lv);
                lv.x = lv.x * this.dashSpeed;
                lv.y = lv.y * this.dashSpeed * 1.5;
                this.rigidbody.linearVelocity = lv;
                cc.find("Canvas/Main Camera").getComponent(CameraTransposer).shakeEffect(1);
            }, 0.1)
        }
    }

    handleparticle(lv: cc.Vec2) {
        if (lv.x > 0 && lv.y == 0) {
            this.particle.angle = 180
        } else if (lv.x < 0 && lv.y == 0) {
            this.particle.angle = 0;
        } else if (lv.x == 0 && lv.y > 0) {
            this.particle.angle = 270
        } else if (lv.x == 0 && lv.y < 0) {
            this.particle.angle = 90
        } else if (lv.x > 0 && lv.y > 0) {
            this.particle.angle = 225
        } else if (lv.x > 0 && lv.y < 0) {
            this.particle.angle = 135
        } else if (lv.x < 0 && lv.y > 0) {
            this.particle.angle = 315
        } else if (lv.x < 0 && lv.y < 0) {
            this.particle.angle = 45
        }
        console.log("particle")
        this.particle.resetSystem();
    }

    playanimation() {
        if (this.isClimbing && !this.animation.getAnimationState("playerClimbing").isPlaying) {
            this.animation.play("playerClimbing");
        }

        else if (!this.isClimbing && this.isDashing && !this.animation.getAnimationState("playerDash").isPlaying) {
            this.animation.play("playerDash");
        }
        else if (!this.isClimbing && !this.isDashing && this.moveDir == 0 && this.rigidbody.linearVelocity.y == 0 && !this.animation.getAnimationState("playerIdle").isPlaying) {
            this.animation.play("playerIdle");
        }
        else if (!this.isClimbing && !this.isDashing && this.rigidbody.linearVelocity.y == 0 && this.moveDir != 0 && !this.animation.getAnimationState("playerMove").isPlaying) {
            this.animation.play("playerMove");
        }
        else if (!this.isClimbing && !this.isDashing && this.rigidbody.linearVelocity.y > 0 && !this.animation.getAnimationState("playerJump").isPlaying) {
            this.animation.play("playerJump");
        }
        else if (!this.isClimbing && !this.isDashing && this.rigidbody.linearVelocity.y < 0 && !this.animation.getAnimationState("playerFall").isPlaying) {
            this.animation.play("playerFall");
        }
    }

    playerAction() {
        let easeRate: number = 2;
        var move_down = cc.moveBy(0.2, 0, -30).easing(cc.easeInOut(easeRate));
        this.node.runAction(move_down);
    }

    onBeginContact(contact, self, other) {
        //console.log(other.node.name)
        if (other.node.name == "nails") {
            this.dying = true;
            //console.log(this.dying);
        }

        this.onGround = false;
        if (this.reverse_gravity == false && (other.node.name == "map" || other.node.name == "moving_block") && contact.getWorldManifold().normal.y < 0) {
            //this.rigidbody.linearVelocity = cc.v2(0, 0);
            this.canDash = true;
            this.canjump = true;
            this.onGround = true;
        } else if (this.reverse_gravity == true && (other.node.name == "map" || other.node.name == "moving_block") && contact.getWorldManifold().normal.y > 0) {
            //this.rigidbody.linearVelocity = cc.v2(0, 0);
            this.canDash = true;
            this.canjump = true;
            this.onGround = true;
        }


        if (this.reverse_gravity == false && (other.node.name == "map" || other.node.name == "moving_block") && contact.getWorldManifold().normal.y < 0 && contact.getWorldManifold().normal.x == 0) {
            this.canClimb = false;
            this.canjump = true;
            this.onGround = true;
        } else if (this.reverse_gravity == true && (other.node.name == "map" || other.node.name == "moving_block") && contact.getWorldManifold().normal.y > 0 && contact.getWorldManifold().normal.x == 0) {
            this.canClimb = false;
            this.canjump = true;
            this.onGround = true;
        }

        if ((other.node.name == "map" || other.node.name == "moving_block") && contact.getWorldManifold().normal.x != 0) {
            this.canClimb = true;
        }


        this.inBubble = false;
        if (other.node.name == "bubble" || other.node.name == "bubble2") {
            this.rigidbody.linearVelocity = cc.v2(0, 0);
            this.isDashing = false;
            this.canjump = false;
            this.canDash = false;
            this.inBubble = true;
        }

        if (other.node.name == "cloud") {
            this.canjump = true;
            this.canDash = true;
        }

    }

    onEndContact(contact, self, other) {
        if ((other.node.name == "map" || other.node.name == "moving_block") && contact.getWorldManifold().normal.x != 0) {
            //this.rigidbody.linearVelocity = cc.v2(0, 0);
            //this.isClimbing = false;
            this.canClimb = false;
            this.canjump = true;
            this.getComponent(cc.RigidBody).gravityScale = 1;
        }

        if ((other.node.name == "map" || other.node.name == "moving_block") && contact.getWorldManifold().normal.y != 0) {
            this.onGround = false;
        }

        if (other.node.name == "cloud") {
            this.canjump = false;
        }
    }
}
Editor is loading...