123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import { BoxCollider2D, CCClass, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, Sprite, SpriteFrame, UITransform, Vec2, _decorator, v2, v3 } from 'cc';
- import { GameManager } from '../../Manager/GameManager';
- const { ccclass, property } = _decorator;
- @ccclass('Player')
- export class Player extends Component {
- @property(Node)
- player: Node = null;
- @property(SpriteFrame)
- pingmin: SpriteFrame = null;
- @property(SpriteFrame)
- xiaotou: SpriteFrame = null;
- // 移动方向
- private _moveDirection: Vec2 | undefined;
- private _w: number = 0;
- private _h: number = 0;
- private sc: number = 1;
- onLoad() {
- }
- start() {
- }
- public move(direction: Vec2) {
- this._moveDirection = direction;
- }
- public setData(type: string): void {
- let x = GameManager.getInstance().getRand(-1, 1);
- let y = GameManager.getInstance().getRand(-1, 1);
- if (x == 0 ) {
- x = 0.5;
- }
- if (y == 0 ) {
- y = 0.5;
- }
- this.move(v2(x, y));
- this._w = this.node.getComponent(UITransform).contentSize.width / 2;
- this._h = this.node.getComponent(UITransform).contentSize.height / 2;
-
- this.node.name = type;
- if (type == 'xiaotou') {
- this.sc = 1;
- if(x < 0){
- this.node.setScale(v3(-1,1,1))
- }
- this.player.getComponent(Sprite).spriteFrame = this.xiaotou;
- } else {
- this.sc = -1;
- if(x < 0){
- this.node.setScale(v3(1,1,1))
- }else{
- this.node.setScale(v3(-1,1,1))
- }
- this.player.getComponent(Sprite).spriteFrame = this.pingmin;
- }
- }
- update(deltaTime: number) {
- if (!this._moveDirection) return;
- // 计算移动位置
- this.node.translate(v3(this._moveDirection.x * GameManager.getInstance().speed * deltaTime, this._moveDirection.y * GameManager.getInstance().speed * deltaTime, 0));
- if (this.node.position.x > this.node.parent.getComponent(UITransform).contentSize.width / 2 - this._w) {
- this._moveDirection = v2(-1, this._moveDirection.y);
- this.node.setScale(v3(-this.sc, 1, 1));
- }
- else if (this.node.position.x < -(this.node.parent.getComponent(UITransform).contentSize.width / 2 - this._w)) {
- this._moveDirection = v2(1, this._moveDirection.y);
- this.node.setScale(v3(this.sc, 1, 1));
- }
- if (this.node.position.y > this.node.parent.getComponent(UITransform).contentSize.height / 2 - this._h) {
- this._moveDirection = v2(this._moveDirection.x, -1);
- }
- else if (this.node.position.y < -(this.node.parent.getComponent(UITransform).contentSize.height / 2 - this._h)) {
- this._moveDirection = v2(this._moveDirection.x, 1);
- }
- }
- }
|