| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- cc.Class({
- extends: cc.Component,
- properties: {
- isHiddenTouch: {
- default: false,
- tooltip: "初始化时候是否绑定touch"
- },
- //是否显示房屋信息
- isShowBuildingInfo: { default: false },
- // 是否是在预制生成
- _isInstance: { default: false, visible: false },
- //如果进入编辑状态
- _ifCanEdit: { default: false, visible: false },
- //触摸多久进入编辑状态
- touchTimer: { default: 0.8, visible: false },
- // //绘制底部颜色区域
- // Draw: cc.Node,
- //触摸一开始的位置
- _touchPosition: {
- default: new cc.Vec2(),
- visible: false,
- serializable: false
- },
- _EndPosition: {
- default: new cc.Vec2(),
- visible: false,
- serializable: false
- },
- //如果自动移动
- isAutoMove: { default: false, visible: false },
- targetNode: cc.Node,
- //编辑时候,记录旧的遮挡位置
- _OldIndex: 0,
- //是否显示节点
- isShowInfo: { default: false },
- infoNode: cc.Node,
- //如果是在新手引导时候建造的建筑
- //只要拖拽到虚影对应的位置,才能成功建造
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad() {
- this._buildingsInfo = this.node.getComponent('buildingsInfo');
- this.buildInfo = this._buildingsInfo.buildInfo;
- this.title = this._buildingsInfo.title;
- if (!this.isHiddenTouch) {
- this.node.on(cc.Node.EventType.TOUCH_START, this.TouchStartFunction, this);
- this.node.on(cc.Node.EventType.TOUCH_END, this.TouchEndFunction, this);
- this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.TouchCancelFunction, this);
- }
- // console.log("this.isHiddenTouch",this.isHiddenTouch);
- this.Draw = this._buildingsInfo._buildZone;
- if (!this.Draw)
- this.Draw = this.node.getChildByName('Draw');
- this.Draw.active = false;
- },
- start() {
- this.InitStartPos = this._buildingsInfo.InitStartPos;
- this._camera = GlobalD.game.MainCamera;
- this._canvas = GlobalD.game.Canvas;
- this._tileMap = GlobalD.TiledMap;
- },
- TouchStartFunction() {
- this._touchPosition = this.node.getPosition();
- this._isMove = false;
- },
- TouchCancelFunction() {
- this.unschedule(this.callback);
- },
- TouchEndFunction() {
- console.log("buildings touch end!");
- // if (this.getOnClickTags()) {
- // return;
- // }
- // GlobalD.GameControl._isBuildingMove = false;
- this.unschedule(this.callback);
- // console.log("this.isShowBuildingInfo:", this.isShowBuildingInfo);
- //如果没有移动就判断点击
- if (!this._isMove
- // && this.isShowBuildingInfo
- && !this._ifCanEdit
- && !GlobalD.GameControl.isUICameraMove
- && !GlobalD.GameControl._isBuildingCanEdit) {
- // GlobalD.game._ManageUIScript.onShowBuildingInfo(this.buildInfo);
- if (this.isShowInfo) {
- //关闭其他
- let _landBuildingsInfo = this.node.parent.children;
- for (let i = _landBuildingsInfo.length - 1; i >= 0; i--) {
- let resetLandInfo = _landBuildingsInfo[i];
- if (resetLandInfo.name == "Labour_205_Holy_Farmland") {
- let _buildInfo = resetLandInfo.getComponent("buildingsInfo").buildInfo;
- let leaseFarmlandInfoScript = resetLandInfo.getComponent("LeaseFarmlandInfo");
- if (_buildInfo.id == this.buildInfo.id) {
- //判断是否是自己的土地,和是否租赁的状态
- if (leaseFarmlandInfoScript && (leaseFarmlandInfoScript.leaseLandInfo == null || !leaseFarmlandInfoScript.bSelfLand)) {
- return;
- }
- //判断是否显示ui
- if (leaseFarmlandInfoScript && 1 === leaseFarmlandInfoScript.leaseLandInfo.isPlant && leaseFarmlandInfoScript.leaseLandInfo.seedInfo) {
- //显示工具ui
- GlobalD.game._ManageUIScript.onSwitchHideUI();
- }
- leaseFarmlandInfoScript.onSetInfoBoxActive(true);
- //记录当前选中的buildInfo
- GlobalD.game.onSetCurrentSelectLeaseLandInfo(leaseFarmlandInfoScript.leaseLandInfo);
- } else {
- leaseFarmlandInfoScript.onSetInfoBoxActive(false);
- }
- } else if (resetLandInfo.name == "Labour_206_Holy_Animal") {
- //牧场
- let _buildInfo = resetLandInfo.getComponent("buildingsInfo").buildInfo;
- let leaseAnimalInfoScript = resetLandInfo.getComponent("LeaseAnimalInfo");
- if (_buildInfo.id == this.buildInfo.id) {
- //判断是否是自己的土地,和是否租赁的状态
- if (leaseAnimalInfoScript && (leaseAnimalInfoScript.leaseLandInfo == null || !leaseAnimalInfoScript.bSelfLand)) {
- return;
- }
- leaseAnimalInfoScript.onSetInfoBoxActive(true);
- //记录当前选中的buildInfo
- GlobalD.game.onSetCurrentSelectLeaseLandInfo(leaseAnimalInfoScript.leaseLandInfo);
- } else {
- leaseAnimalInfoScript.onSetInfoBoxActive(false);
- }
- }
- }
- }
- }
- },
- MoveFunction(event) {
- // cc.log("MoveFunction");
- var delta = event.touch.getDelta();
- this._touchPosition.x += delta.x / this._camera.zoomRatio;
- this._touchPosition.y += delta.y / this._camera.zoomRatio;
- // var worldPos = this.node.parent.convertToWorldSpaceAR(this._touchPosition);
- //取得的节点减去canvas的偏移
- // let tempPos = cc.v2(worldPos.x - this._canvas.x, worldPos.y - this._canvas.y);
- this._currentTiledValue = this._tileMap._tilePosFromLocation(this._touchPosition);
- //不相等的时候才绘制
- if (this._currentTiledValue.sub(this._EndPosition).mag() != 0) {
- this._EndPosition = this._currentTiledValue;
- let endPos = this._tileMap._getTheMiddleLocationFromtilePos(this._currentTiledValue);
- this.node.setPosition(endPos);
- let isHas = this._canNotBuild = GlobalD.game.areTheraOverlappingAreas(this._currentTiledValue, this.buildInfo.occupyArea, 1);
- // this.title.string = "(" + (this._currentTiledValue.x) + "," + (this._currentTiledValue.y) + ")" + isHas;
- if (task.virtualShadowPos)
- this._canNotBuild = this._EndPosition.sub(task.virtualShadowPos).mag() == 0 ? false : true;
- GlobalD.game.onSpawnEditorBuildingTip(this.Draw, this._currentTiledValue, this.buildInfo.occupyArea, this._canNotBuild, this.buildInfo.buildType);
- }
- this._isMove = true;
- // GlobalD.GameControl._isBuildingMove = true;
- },
- // 自动移动,在ManagerControl 里面调用
- onAutoMove(delta) {
- this.isAutoMove = true;
- this._touchPosition.x += delta.x;
- this._touchPosition.y += delta.y;
- this._currentTiledValue = this._tileMap._tilePosFromLocation(this._touchPosition);
- //不相等的时候才绘制
- if (this._currentTiledValue.sub(this._EndPosition).mag() != 0) {
- this._EndPosition = this._currentTiledValue;
- let endPos = this._tileMap._getTheMiddleLocationFromtilePos(this._currentTiledValue);
- this.node.setPosition(endPos);
- let isHas = this._canNotBuild = GlobalD.game.areTheraOverlappingAreas(this._currentTiledValue, this.buildInfo.occupyArea, 1);
- // this.title.string = "(" + (this._currentTiledValue.x) + "," + (this._currentTiledValue.y) + ")" + isHas;
- if (task.virtualShadowPos)
- this._canNotBuild = this._EndPosition.sub(task.virtualShadowPos).mag() == 0 ? false : true;
- GlobalD.game.onSpawnEditorBuildingTip(this.Draw, this._currentTiledValue, this.buildInfo.occupyArea, this._canNotBuild, this.buildInfo.buildType);
- }
- },
- onCancleAutoMove() {
- this.isAutoMove = false;
- },
- /**
- * 判断是否可以被优先点击
- * @returns {*}
- */
- getOnClickTags: function () {
- if (this.getComponent("Collect") != null) {
- return this.getComponent("Collect").getOnClickTag();
- } else {
- return false;
- }
- }
- });
|