| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- import { DataEventId } from "../../game/data/GameData";
- import Util from "../util/Util";
- const { ccclass, property } = cc._decorator;
- /**
- * @description 地图移动控制器
- * @author Soonsky
- */
- @ccclass
- export class MapMove extends cc.Component {
- @property({ type: cc.Node, tooltip: "地图容器" }) map: cc.Node = null;
- private canvas: cc.Node = null;
- private startPoint: cc.Vec2;
- private onScale = false;
- private tarScale = 1.5;
- start() {
- this.canvas = cc.find('Canvas');
- this.node.on(cc.Node.EventType.TOUCH_START, this.onMapTouchBegin, this);
- this.map.scale = 1.2;
- this.node.setPosition(cc.v2(72, 348));
- }
- onEnable() {
- this.scheduleOnce(() => {
- this.onScale = true;
- }, 0.3)
- }
- onDestroy() {
- this.node.off(cc.Node.EventType.TOUCH_START, this.onMapTouchBegin, this);
- }
- private isDoubleTouch = false;
- private onMapTouchBegin(e): void {
- this.startPoint = e.touch._point;
- this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
- this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
- this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
- }
- private onTouchMove(e: cc.Event.EventTouch): void {
- let touches = e.getTouches();
- if (touches.length == 1) {
- if (this.isDoubleTouch) {
- return;
- }
- let point = e.touch.getLocation();
- let offsetX = point.x - this.startPoint.x;
- let offsetY = point.y - this.startPoint.y;
- this.move(new cc.Vec2(offsetX, offsetY));
- this.startPoint = point;
- } else if (touches.length == 2) {
- this.isDoubleTouch = true;
- let b_distance = cc.Vec2.distance(touches[0].getStartLocation(), touches[1].getStartLocation());
- let e_distance = cc.Vec2.distance(touches[0].getLocation(), touches[1].getLocation());
- // let target = this.map.scaleX;
- // if (b_distance > e_distance) {
- // target = Util.clamp(target - 0.01, 1.3, 1.7);
- // } else {
- // target = Util.clamp(target + 0.01, 1.3, 1.7);
- // }
- // this.map.scale = target
- // this.move(this.node.position);
- this.tarScale = this.map.scale;
- if (b_distance > e_distance) {
- this.tarScale = Util.clamp(this.tarScale - 0.1, 1.3, 1.7);
- } else {
- this.tarScale = Util.clamp(this.tarScale + 0.1, 1.3, 1.7);
- }
- }
- }
- private onTouchEnd(e: cc.Event.EventTouch): void {
- this.startPoint = cc.Vec2.ZERO;
- this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
- this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
- this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
- this.isDoubleTouch = false;
- // if (e.getTouches().length == 0) {
- // }
- }
- update() {
- if (!this.onScale) {
- return;
- }
- let target = this.map.scale;
- if (target < this.tarScale) {
- target += 0.005;
- if (target >= this.tarScale) {
- target = this.tarScale;
- }
- } else {
- target -= 0.005;
- if (target <= this.tarScale) {
- target = this.tarScale;
- }
- }
- this.map.scale = target
- }
- private move(offset: cc.Vec2): void {
- this.startPoint = cc.Vec2.ZERO;
- this.node.setPosition(this.confirmPosition(new cc.Vec2(this.node.position.x + offset.x, this.node.position.y + offset.y)));
- }
- /**确认移动到的坐标是否合法 */
- private confirmPosition(pos: cc.Vec2): cc.Vec3 {
- let clampX = Util.clamp(pos.x, -(this.map.width * (this.map.scaleX - 0.2) / 2 - this.canvas.width / 2), this.map.width * (this.map.scaleX - 0.2) / 2 - this.canvas.width / 2);
- let clampY = Util.clamp(pos.y, -(this.map.height * (this.map.scaleY - 0.2) / 2 - this.canvas.height / 2), this.map.height * (this.map.scaleY - 0.2) / 2 - this.canvas.height / 2);
- return new cc.Vec3(clampX, clampY, 0);
- }
- /**将视角中心移动到指定位置 */
- public moveTo(pos: cc.Vec3): void {
- let toPoint = new cc.Vec2(-pos.x * this.map.scaleX, -pos.y * this.map.scaleY);
- cc.tween(this.node).to(0.5, { position: this.confirmPosition(toPoint) }, { easing: "sineOut" }).start();
- }
- private clickMapBookBtn() {
- mk.ui.openPanel('module/farmMap/farmMap');
- mk.data.sendDataEvent(DataEventId.button_click, "图鉴icon");
- }
- private clickSpeedup() {
- mk.data.sendDataEvent(DataEventId.button_click, "全体加速icon");
- }
- }
|