<template>
	<view class="content">
	</view>
</template>

<script>
	import AccAndOri from "@/util/util-js/AccAndOri.js"

	export default {
		data() {
			return {
				bUnload: false
			};
		},
		onLoad(option) {
			// console.log("game-play-sub ==",option)
			if (!option) {
				console.error("游戏没有option参数");
				return;
			}
			/**
			 * $on 之后要调用 $off,不然会重复绑定
			 */

			uni.$on("game-load", () => {
				uni.$emit("setOnceGameOption", option);
			});

			uni.$on("game-unload", (data) => {
				let {
					globalAcc = null,
					globalOri = null,
					bGameOpenListen
				} = data;
				if(bGameOpenListen){
					//如果开启加速计,取消加速计监听
					console.log(globalAcc,globalOri);
					if(globalAcc){
						AccAndOri.unBindAcc(globalAcc);
						this.$store.state.globalAcc = null;
					}
					//如果开启陀螺仪,取消陀螺仪监听
					if(globalOri){
						AccAndOri.unBindOri(globalOri);
						this.$store.state.globalOri = null;
					}
				}
				// console.log("this.bUnload",this.bUnload);
				if (!this.bUnload) {
					this.bUnload = true;
					uni.navigateBack({
						delta: 1
					})
				}

			});
			/**
			 * 操作加速计
			 */
			uni.$on("bindAcc", (data) => {
				let {
					callback
				} = data;

				AccAndOri.bindAcc((accId) => {
					callback(accId);
				});
			})
			uni.$on("unBindAcc", (id) => {
				AccAndOri.unBindAcc(id);
			})

			/**
			 * 操作陀螺仪
			 */
			uni.$on("bindOri", (data) => {
				let {
					callback
				} = data;

				AccAndOri.bindOri((oriId) => {
					callback(oriId);
				});
			})
			uni.$on("unBindOri", (id) => {
				AccAndOri.unBindOri(id);
			})
		},
		onUnload() {
			uni.$off("game-load");

			uni.$off("game-unload");

			/**
			 * 操作加速计
			 */
			uni.$off("bindAcc");
			uni.$off("unBindAcc");

			/**
			 * 操作陀螺仪
			 */
			uni.$off("bindOri");
			uni.$off("unBindOri");
		},
		methods: {}
	}
</script>

<style>
	.content {
		align-content: center;
		height: 750rpx;
		background-color: #F4F5F6;
	}
</style>
