123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- import { Animation, BoxCollider2D, CCClass, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, NodeEventType, Tween, UITransform, Vec2, Vec3, _decorator, animation, instantiate, tween, v2, v3 } from 'cc';
- import { GameManager } from '../../Manager/GameManager';
- import { Player } from './Player';
- import { AudioManage } from '../../Manager/AudioManage';
- import { Global } from '../../Common/Global';
- const { ccclass, property } = _decorator;
- @ccclass('GameMain')
- export class GameMain extends Component {
- @property(Node)
- jingchaNode: Node = null;
- @property(Node)
- jingchaImg: Node = null;
- @property(Node)
- player: Node = null;
- @property(Node)
- mainNode: Node = null;
- private _moveDirection: Vec2 | undefined;
- private jc_w: number = 0;//player一半的宽度
- private jc_h: number = 0;//player一半的高度
- private collider;
- //是否在移动中
- private isMove = false;
- private isGameover = false;
- private _tween = new Tween();
- jingcha: Node = null;
- /**当前剩余平民数量 */
- private pingminNum: number = 0;
- onLoad() {
- this.jc_w = this.jingchaNode.getComponent(UITransform).contentSize.width / 2;
- this.jc_h = this.jingchaNode.getComponent(UITransform).contentSize.height / 2;
- this.jingchaNode.name = 'jingcha';
- this.node.on(NodeEventType.TOUCH_START, this.jingchaMove, this);
- }
- start() {
- this.move();
- }
- //冲刺
- private jingchaMove(e): void {
- //冲刺时打开碰撞
- if (this.collider) {
- this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
- }
- this.node.off(NodeEventType.TOUCH_START, this.jingchaMove, this);
- let uiposition = e.getUILocation();
- let pos = this.node.getComponent(UITransform).convertToNodeSpaceAR(v3(uiposition.x, uiposition.y, 0))
- let _x;
- let _y;
- let time = _x
- this.isMove = true;
- if (pos.x > this.jingcha.position.x) {
- _x = pos.x - this.jingcha.position.x;
- this.jingcha.setScale(v3(1, 1, 1));
- } else {
- _x = this.jingcha.position.x - pos.x;
- this.jingcha.setScale(v3(-1, 1, 1));
- }
- if (pos.y > this.jingcha.position.y) {
- _y = pos.y - this.jingcha.position.y;
- } else {
- _y = this.jingcha.position.y - pos.y;
- }
- if (Math.abs(_x) > Math.abs(_y)) {
- time = Math.abs(_x);
- } else {
- time = Math.abs(_y)
- }
- this._tween = new Tween(this.jingcha).to(time / (GameManager.getInstance().speed * 10), { position: new Vec3(pos.x, pos.y, 0) }).call(() => {
- this.isMove = false;
- this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
- this.move();
- // this.scheduleOnce(() => {
- this.node.on(NodeEventType.TOUCH_START, this.jingchaMove, this);
- // }, .5)
- }).start();
- }
- private updateZindex() {
- this.mainNode.children.sort((a, b) => {
- return b.position.y - a.position.y;
- })
- }
- startGame(): void {
- this.isGameover = false;
- this.pingminNum = 0;
- this.mainNode.removeAllChildren();
- this.jingcha = instantiate(this.jingchaNode);
- this.mainNode.addChild(this.jingcha);
- this.collider = this.jingcha.getComponent(BoxCollider2D);
- this.move();
- //刷新平民的数量
- for (let i = 0; i < GameManager.getInstance().maxPingminNum; i++) {
- let node = instantiate(this.player);
- node.active = true;
- node.getComponent(Player).setData("pingmin");
- this.mainNode.addChild(node);
- this.pingminNum++;
- node.position = v3(GameManager.getInstance().getRand(-560, 560), GameManager.getInstance().getRand(-430, 430), 0);
- }
- //刷新小偷的数量
- for (let i = 0; i < GameManager.getInstance().playerNum; i++) {
- let node = instantiate(this.player);
- node.active = true;
- node.getComponent(Player).setData("xiaotou");
- this.mainNode.addChild(node);
- node.position = v3(GameManager.getInstance().getRand(-560, 560), GameManager.getInstance().getRand(-430, 430), 0);
- }
- this.jingcha.position = v3(this.jingcha.position.x, this.jingcha.position.y, 0)
- }
- public move() {
- 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._moveDirection = v2(x, y);
- }
- //碰撞事件
- onBeginContact(hero_collier: Collider2D, box_collier: Collider2D, contact: IPhysics2DContact | null) {
- let anim = hero_collier.node.getChildByName("jingcha").getComponent(Animation);
- let anim2 = box_collier.node.getChildByName("player").getComponent(Animation);
- anim.play("siwang");
- anim2.play("siwang");
- anim2.on(Animation.EventType.FINISHED, (Event, state) => {
- if (box_collier.node.name == "xiaotou") {
- //在此加分
- console.log("撞击小偷");
- AudioManage.instance.playSound(Global.Audio_Success);
- GameManager.getInstance().addScore(GameManager.getInstance().AddScoreNum);
- //删除一个小偷就要增加一个
- // let node = instantiate(this.player);
- // node.active = true;
- // node.getComponent(Player).setData("xiaotou");
- // this.mainNode.addChild(node);
- // node.position = v3(-560, GameManager.getInstance().getRand(-430, 430), 0);
- } else {
- //在此减分
- console.log("撞击平民");
- AudioManage.instance.playSound(Global.Audio_GameFail);
- GameManager.getInstance().minusScore(GameManager.getInstance().MinusScoreNum);
- this.pingminNum--;
- if (this.pingminNum <= 0) {
- //失败
- // GameManager.getInstance().saveDataToServer(1);
- AudioManage.instance.pauseMusic();
- GameManager.getInstance().gameFail();
- }
- }
- box_collier.node.removeFromParent();
- box_collier.node.destroy();
- this.move();
- }, this);
- this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
- this._tween.stop().removeSelf();
- this.isMove = false;
- // this.scheduleOnce(() => {
- this.node.on(NodeEventType.TOUCH_START, this.jingchaMove, this);
- anim.getComponent(Animation).play("animation");
- // }, .5)
- }
- gameOver() {
- this.isGameover = true;
- this.mainNode.removeAllChildren();
- }
- update(deltaTime: number) {
- if (!this._moveDirection || this.isGameover) return;
- //改变层级
- this.updateZindex();
- if (this.isMove) {
- return;
- }
- // 计算移动位置
- this.jingcha.translate(v3(this._moveDirection.x * GameManager.getInstance().speed * deltaTime, this._moveDirection.y * GameManager.getInstance().speed * deltaTime, 0));
- if (this._moveDirection.x > 0) {
- this.jingcha.setScale(v3(1, 1, 1));
- } else {
- this.jingcha.setScale(v3(-1, 1, 1));
- }
- if (this.jingcha.position.x > this.node.getComponent(UITransform).contentSize.width / 2 - this.jc_w) {
- this._moveDirection = v2(-GameManager.getInstance().getRand(0.8, 1), this._moveDirection.y);
- }
- else if (this.jingcha.position.x < -(this.node.getComponent(UITransform).contentSize.width / 2 - this.jc_w)) {
- this._moveDirection = v2(GameManager.getInstance().getRand(0.8, 1), this._moveDirection.y);
- }
- if (this.jingcha.position.y > this.node.getComponent(UITransform).contentSize.height / 2 - this.jc_h) {
- this._moveDirection = v2(this._moveDirection.x, -GameManager.getInstance().getRand(0.8, 1));
- }
- else if (this.jingcha.position.y < -(this.node.getComponent(UITransform).contentSize.height / 2 - this.jc_h)) {
- this._moveDirection = v2(this._moveDirection.x, GameManager.getInstance().getRand(0.8, 1));
- }
- }
- }
|