import { DCGame } from "../../DataCenter/DCGame";
import EventMgr from "../Module/EventMgr/EventMgr";
import { UICtrl } from "../UICtrl/UICtrl";
import { DragGameAnswerObject } from "./DragGameAnswerObject";
import { DragGameOptionObject_001 } from "./DragGameOptionObject";

//拖拽管理器
const { ccclass, property } = cc._decorator;

@ccclass
export class DragGameManager extends UICtrl {
	onLoad(): void {
		super.onLoad();
		//注册事件
		this.regEvent();

		this.initProperty()
	}

	initProperty() {

	}

	protected onDestroy(): void {
		//注销事件
		this.unEvent();
	}

	private regEvent() {
		this.unEvent();
		EventMgr.reg("checkAnswer", this.checkAnswer.bind(this))
	}

	private unEvent() {
		EventMgr.remove("checkAnswer");
	}

	//检查结果
	checkAnswer(nodAnswer, option_i) {
		let scrAnswer = nodAnswer.getComponent(DragGameAnswerObject)
		//获得目标点 一般是根据配置id从列表中获取到当前的节点，现在直接在初始化中设置好了，
		let nod = DCGame.getInstance().objOption;
		//查看当前节点的矩阵跟之前矩阵的包含情况
		let OptionAABB = nod.getComponent(DragGameOptionObject_001).setAABB();
		let AnswerAABB = this.getCallerAABB(nodAnswer);
		var isIntersect = OptionAABB.containsRect(AnswerAABB);//判断是否被包含

		console.log("是否包含", isIntersect)
		if (isIntersect) {
			scrAnswer.AnswerRight();
		} else {
			scrAnswer.AnswerError();
		}
	}

	getCallerAABB(caller) {
		let svLeftBottomPoint = caller.parent.convertToWorldSpaceAR(
			cc.v2(
				caller.x,
				caller.y
			)
		);
		var posNode_1 = cc.rect(
			svLeftBottomPoint.x,
			svLeftBottomPoint.y,
			0,
			0
		);
		return posNode_1
	}
}