瀏覽代碼

推箱子优化

chelios 3 年之前
父節點
當前提交
05c7557fd3

+ 120 - 0
assets/Script/game/element/JG0109.ts

@@ -0,0 +1,120 @@
+import BaseEvent from "../fight/evnet/base/BaseEvent";
+
+/**
+ * 推箱子、石头
+ */
+const {ccclass, property} = cc._decorator;
+
+@ccclass
+export default class JG0109 extends BaseEvent {
+
+    @property({
+        displayName:'推图标',
+        type:cc.SpriteFrame
+    })
+    pullIcon: cc.SpriteFrame = null;
+
+    /**
+     * 推的方向
+     * 上下左右
+     * 1,2,3,4
+     */
+    private pullDir = -1;
+    /**
+     * 当前是否推的状态
+     */
+    private isPull = false
+    /**
+     * 主角进入碰撞区域
+     *  @param tag 碰撞组件编号
+     */
+    public onBegin(tag: number) {
+        let pullDir = this.getPullDir()
+        // cc.log('pullDir : ',pullDir)
+        if(pullDir > 0){
+            this.isPull = true;
+            this.pullDir = pullDir
+            this.showOptTouch(this.pullIcon,
+                () => {
+                   this.pull()
+               },()=>{
+                   this.isPull = false;
+               })
+        }
+    }
+    /**
+     * 主角离开碰撞区域
+     * @param tag 碰撞组件编号
+     */
+    public onEnd(tag: number) {
+        this.closeOptTouch()
+        this.isPull = false;
+    }
+    /**
+     * 接触到石头后确定方向
+     * @returns 
+     */
+    public getPullDir():number{
+        let mainSprite = this.ff.mainSprite
+        let pbc = this.node.getComponent(cc.PhysicsBoxCollider)
+        let x0,y0,x1,y1;
+        x0 = this.node.x + pbc.offset.x - pbc.size.width/2 - 10
+        x1 = this.node.x + pbc.offset.x + pbc.size.width/2 + 10
+        y0 = this.node.y + pbc.offset.y - pbc.size.height/2 - 20//下
+        y1 = this.node.y + pbc.offset.y + pbc.size.height/2 + 20//上
+
+        let x = mainSprite.node.x
+        let y = mainSprite.node.y
+
+        if(x > x0 && x < x1){//当前角色在上下2个方向
+           if(y > y1){
+                return 1;
+           }else{
+               return 2;
+           }
+        }else if(y > y0 && y < y1){
+            if(x < x0){
+                return 3
+            }else{
+                return 4
+            }
+        }
+        return -1
+    }
+
+    private pull(){
+        let pos = cc.v2()
+        if(this.pullDir == 1){
+            pos.y = -64
+        }else if(this.pullDir == 2){
+            pos.y = 64
+        }else if(this.pullDir == 3){
+            pos.x = 64
+        }else if(this.pullDir == 4){
+            pos.x = -64
+        }
+        let tx = this.node.x + pos.x
+        let ty = this.node.y + pos.y
+        if(this.ff.mMap.checkCollision(tx,ty)){
+            return
+        }
+        cc.tween(this.node).sequence(
+            cc.delayTime(0.6),
+            cc.callFunc(()=>{
+                if(this.isPull){
+                    cc.tween(this.node).sequence(
+                        cc.moveBy(0.5,pos),
+                        cc.callFunc(()=>{})
+                    ).start()
+                }
+            }),
+            cc.delayTime(0.5),
+            cc.callFunc(()=>{
+                if(this.isPull){
+                    this.pull()
+                }
+            })
+        ).start()
+    }
+
+}

+ 1 - 1
assets/Script/game/fight/evnet/FStoreTS.ts.meta

@@ -1,6 +1,6 @@
 {
   "ver": "1.0.8",
-  "uuid": "7cfebc90-b13c-4b3a-adac-669b5607425c",
+  "uuid": "d2b61c5a-ec3a-459b-8f3c-4b8e27d36110",
   "isPlugin": false,
   "loadPluginInWeb": true,
   "loadPluginInNative": true,

+ 34 - 0
assets/Script/game/fight/EventButton.ts

@@ -11,6 +11,9 @@ export default class EventButton extends cc.Component {
 
     private callback:()=>void;
 
+    private startCallback:()=>void;
+    private endCallback:()=>void;
+
     public setCallback(callback:()=>void){
         this.callback = callback;
     }
@@ -20,4 +23,35 @@ export default class EventButton extends cc.Component {
             this.callback();
         }
     }
+    /**
+     * 注册按压事件
+     */
+    public onTouchEvent(startCallback:()=>void,endCallback:()=>void){
+        this.startCallback = startCallback;
+        this.endCallback = endCallback;
+        this.node.on(cc.Node.EventType.TOUCH_START, this._touchShootStartEvent, this);
+        this.node.on(cc.Node.EventType.TOUCH_END, this._touchShootEndEvent, this);
+        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchShootEndEvent, this);
+    }
+    /**
+     * 取消注册
+     */
+    public offTouchEvent(){
+        this.startCallback = null;
+        this.endCallback = null;
+        this.node.off(cc.Node.EventType.TOUCH_START)
+        this.node.off(cc.Node.EventType.TOUCH_END)
+        this.node.off(cc.Node.EventType.TOUCH_CANCEL)
+    }
+
+    private _touchShootStartEvent() {
+        if(this.startCallback){
+            this.startCallback();
+        }
+    }
+    private _touchShootEndEvent() {
+        if(this.endCallback){
+            this.endCallback();
+        }
+    }
 }

+ 26 - 12
assets/Script/game/fight/FControl.ts

@@ -110,24 +110,14 @@ export default class FControl extends cc.Component {
             callback()
         })
     }
+    
+
     public closeEventBt() {
         this.mEventButton.node.active = false
         this.mShooting.active = true
         this.mEventButton.setCallback(null)
     }
 
-
-    private _touchRunningStartEvent() {
-        if (this.ff && this.ff.mainSprite) {
-            this.ff.mainSprite.setRuning(true);
-        }
-    }
-    private _touchRunningEndEvent() {
-        if (this.ff && this.ff.mainSprite) {
-            this.ff.mainSprite.setRuning(false);
-        }
-    }
-
     private _touchShootStartEvent() {
         if (this.ff && this.ff.mainSprite) {
             this.ff.mainSprite.setShooting(true);
@@ -138,6 +128,30 @@ export default class FControl extends cc.Component {
             this.ff.mainSprite.setShooting(false);
         }
     }
+   /**
+    * 注册长按事件按钮
+    * @param spriteFrame 事件按钮图标
+    * @param startCallback 事件按钮按下
+    * @param endCallback 事件按钮取消
+    */
+    public showEventBtTouch(spriteFrame: cc.SpriteFrame,startCallback:()=>void,endCallback:()=>void){
+        this.ff.mainSprite.setShooting(false);
+        this.ff.mainSprite.status == SpriteType.NONE
+        this.mEventButton.node.active = true
+        this.mEventButton.mIcon.spriteFrame = spriteFrame
+        this.mShooting.active = false
+
+        this.mEventButton.onTouchEvent(startCallback,endCallback)
+    }
+    /**
+     * 取消注册事件长按
+     */
+    public closeEventBtTouch(){
+        this.mEventButton.node.active = false
+        this.mShooting.active = true
+        this.mEventButton.offTouchEvent()
+    }
+
 
     public onclickSkill1() {
         if (!this.skillOK1) {

+ 0 - 118
assets/Script/game/fight/evnet/FStoreTS.ts

@@ -1,118 +0,0 @@
-import { SpriteActionType } from "../object/FSprite";
-import BaseEvent from "./base/BaseEvent";
-
-const {ccclass, property} = cc._decorator;
-/**
- * 推石头
- * 
- * tag == 1 左右推动
- * tag == 2 上下推动
- */
-@ccclass
-export default class FStoreTS extends BaseEvent {
-
-    @property({
-        displayName: '提示按钮图标',
-        type: cc.SpriteFrame
-    })
-    mTipsIcon: cc.SpriteFrame = null;
-
-    private mRigidBody:cc.RigidBody = null
-    /**
-     * 推动中
-     */
-    private isPull = false;
-    /**
-     * 当前移动方向
-     */
-    private moveVt = cc.v2()
-
-    start(){
-        this.mRigidBody = this.node.getComponent(cc.RigidBody);
-    }
-
-    onBegin(tag:number){
-        this.startEvent()
-        if(tag == 1){
-            this.moveVt.y = 0
-            let myX = this.ff.mainSprite.node.x
-            if(myX < this.node.x){
-                this.moveVt.x = 7
-            }else{
-                this.moveVt.x = -7
-            }
-        }else if(tag == 2){
-            this.moveVt.x = 0
-            let myY = this.ff.mainSprite.node.y
-            if(myY < this.node.y){
-                this.moveVt.y = 7
-            }else{
-                this.moveVt.y = -7
-            }
-        }
-    }
-
-    onEnd(tag:number){
-        this.endEvent()
-    }
-
-    private startEvent(){
-        let fControl = this.ff.control;
-        fControl.mEventButton.node.on(cc.Node.EventType.TOUCH_START, this._touchRunningStartEvent, this);
-        fControl.mEventButton.node.on(cc.Node.EventType.TOUCH_MOVE, this._touchRunningMoveEvent, this);
-        fControl.mEventButton.node.on(cc.Node.EventType.TOUCH_END, this._touchRunningEndEvent, this);
-        fControl.mEventButton.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchRunningEndEvent, this);
-        this.showOpt(this.mTipsIcon, () => {
-        })
-    }
-    private endEvent(){
-        this.closeOpt()
-        
-        let fControl = this.ff.control;
-        fControl.mEventButton.node.off(cc.Node.EventType.TOUCH_START);
-        fControl.mEventButton.node.off(cc.Node.EventType.TOUCH_MOVE);
-        fControl.mEventButton.node.off(cc.Node.EventType.TOUCH_END);
-        fControl.mEventButton.node.off(cc.Node.EventType.TOUCH_CANCEL);
-        this.isPull = false;
-    }
-    private  _touchRunningMoveEvent(){
-        cc.log('_touchRunningMoveEvent')
-    }
-    private  _touchRunningStartEvent(){
-        cc.log('_touchRunningStartEvent')
-        this.isPull = true;
-        cc.tween(this).sequence(
-            cc.delayTime(0.01),
-            cc.callFunc(()=>{
-                this.mRigidBody.type = cc.RigidBodyType.Dynamic
-            })
-        ).start()
-        
-        let mainSprite = this.ff.mainSprite
-
-        // mainSprite.spine.setAnimation(0, SpriteActionType.run, true);
-
-    }
-    private _touchRunningEndEvent(){
-        cc.log('_touchRunningEndEvent')
-        this.isPull = false;
-        cc.tween(this).sequence(
-            cc.delayTime(0.01),
-            cc.callFunc(()=>{
-                this.mRigidBody.type = cc.RigidBodyType.Static
-            })
-        ).start()
-        // let mainSprite = this.ff.mainSprite
-        // mainSprite.spine.setAnimation(0, SpriteActionType.stand, true);
-    }
-
-    public update(dt){
-        if(this.isPull){
-            let mainSprite = this.ff.mainSprite
-            mainSprite.node.x += this.moveVt.x
-            mainSprite.node.y += this.moveVt.y
-        }
-    }
-
-
-}

+ 11 - 0
assets/Script/game/fight/evnet/base/BaseEvent.ts

@@ -99,12 +99,23 @@ export default class BaseEvent extends cc.Component {
     public showOpt(spriteFrame: cc.SpriteFrame, callback: () => void) {
         this.ff.control.showEventBt(spriteFrame, callback)
     }
+    /**
+     * 
+     * @param spriteFrame 
+     * @param callback 
+     */
+    public showOptTouch(spriteFrame: cc.SpriteFrame, startCallback: () => void,endCallback: () => void){
+        this.ff.control.showEventBtTouch(spriteFrame,startCallback,endCallback)
+    }
     /**
      * 关闭界面上可操作按钮
      */
     public closeOpt() {
         this.ff.control.closeEventBt()
     }
+    public closeOptTouch(){
+        this.ff.control.closeEventBtTouch()
+    }
     /**
      * 全屏按钮(用于处理用户点击任意位置)
      * @param callback 

+ 29 - 12
assets/Script/game/fight/map/FMap.ts

@@ -51,26 +51,27 @@ export default class FMap extends cc.Component {
 
         this.rooms = this.mRooms.children
 
+        this.addCollider()
 
+        // cc.log('xxxxxxxxxxxxxx = ',this.checkCollision(64,64))
 
-        this.addCollider()
     }
     /**
      * 添加碰撞
      */
     private addCollider(){
         //地图边界碰撞墙
-        let width   = this.node.width;
-        let height  = this.node.height;
-        let node = new cc.Node();
-        node.group = 'map'
-        let body = node.addComponent(cc.RigidBody);
-        body.type = cc.RigidBodyType.Static;
-        this._addBound(node, width/2, height, width, 20);
-        this._addBound(node, width/2, 0, width, 20);
-        this._addBound(node, 0, height/2, 20, height);
-        this._addBound(node, width, height/2, 20, height);
-        node.parent = this.node;
+        // let width   = this.node.width;
+        // let height  = this.node.height;
+        // let node = new cc.Node();
+        // node.group = 'map'
+        // let body = node.addComponent(cc.RigidBody);
+        // body.type = cc.RigidBodyType.Static;
+        // this._addBound(node, width/2, height, width, 20);
+        // this._addBound(node, width/2, 0, width, 20);
+        // this._addBound(node, 0, height/2, 20, height);
+        // this._addBound(node, width, height/2, 20, height);
+        // node.parent = this.node;
         
         //地形碰撞处理
         if(this.mColliderTiled){
@@ -377,4 +378,20 @@ export default class FMap extends cc.Component {
 
         return false;
     }
+    /**
+     * 检查当前坐标是否在碰撞区域
+     * @param x 
+     * @param y 
+     */
+    public checkCollision(x,y){
+        let size = this.mColliderTiled.getLayerSize();
+        let dx = x/64
+        let dy = size.height - y/64
+        if(dy > size.height){
+            dy = size.height - 1
+        }
+        let t = this.mColliderTiled.getTileGIDAt(dx,dy);
+        return t > 0
+    }
+
 }