marineparkclient/assets/script/module/touristModule.ts
2023-08-15 11:09:12 +08:00

409 lines
11 KiB
TypeScript

// Learn TypeScript:
// - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
const {ccclass, property} = cc._decorator;
import UIBase from "../../common/ui/base/UIBase";
import ToolFunc from "../ToolFunc";
import venueLayer from "../ui/UISingleVenueLayer";
export enum State{
IN = 0,
STAND = 1,
WALK = 2,
OUT = 3,
END = 4,
STOP = 5
}
@ccclass
export default class touristModule extends UIBase {
@property(cc.Sprite)
singleTourist:cc.Sprite = null;
@property
maxDelayTime = 3.0;
@property
minMoveTime = 3.0;
@property
moveDistantTime = 1.0;
parent = null;
startPos = cc.v2(0,-510)
endPos = cc.v2(0,510)
inPos = cc.v2(0,-400)
outPos = cc.v2(0,400)
actionIndex = 4;
arrMajorPos = [];
arrMajorRoutePos = [];
arrMiddleRoutePos = [];
radomActionTimes = 0;
touritstState = State.STOP;
activeMaxY:cc.Vec2 = cc.v2(0,0);
activeMinY:cc.Vec2 = cc.v2(0,0);
remainCount:number = 0;
routeMajorState:number = 0;
goalPos:cc.Vec2 = cc.v2(0,0);
arrGoalDirection:Array<boolean> = [true,true];
standKeepTimeCount:number = 0;
@property
speed:number= 50;
tempObj = null;
// LIFE-CYCLE CALLBACKS:
init(node:cc.Node):void
{
this.singleTourist.node.x = 0;
this.singleTourist.node.y = 0;
let jumpUpDown = cc.tween()
.by(0.1,{position:cc.v2(0,20)})
.by(0.1,{position:cc.v2(0,-20)});
this.parent = node.getComponent(venueLayer);
this.tempObj = cc.tween(this.singleTourist.node).then(jumpUpDown).repeatForever().start();
// this.arrMajorRoutePos = [
// ToolFunc.ins.getNodeRoutePosition(this.parent.singleAreaBtnArr[0]),
// ToolFunc.ins.getNodeRoutePosition(this.parent.singleAreaBtnArr[1]),
// ToolFunc.ins.getNodeRoutePosition(this.parent.singleAreaBtnArr[2]),
// ToolFunc.ins.getNodeRoutePosition(this.parent.singleAreaBtnArr[3])
// ]
this.arrMajorPos = [
this.inPos,
ToolFunc.ins.getNodePosition(this.parent.singleAreaBtnArr[3]),
ToolFunc.ins.getNodePosition(this.parent.singleAreaBtnArr[2]),
ToolFunc.ins.getNodePosition(this.parent.singleAreaBtnArr[1]),
ToolFunc.ins.getNodePosition(this.parent.singleAreaBtnArr[0]),
this.outPos,
]
// this.arrMiddleRoutePos = [
// ToolFunc.ins.getNodeMiddlePosition(this.parent.singleAreaBtnArr[0]),
// ToolFunc.ins.getNodeMiddlePosition(this.parent.singleAreaBtnArr[1]),
// ToolFunc.ins.getNodeMiddlePosition(this.parent.singleAreaBtnArr[2]),
// ToolFunc.ins.getNodeMiddlePosition(this.parent.singleAreaBtnArr[3])
// ]
this.touritstState = State.IN;
this.routeMajorState = 0;
//this.planRoad()
}
// planRoad():void
// {
// let tNode = this.node;
// let tParentNode = this.parent;
// let bezierobj =[tParentNode.startPos,this.arrMajorRoutePos[3],this.arrMajorRoutePos[3]];
// cc.tween(tNode)
// .then(cc.bezierTo(3,bezierobj))
// .call(() => this.planRoadSingle())
// .start()
// }
// planRoadSingle(tweenObj?):void
// {
// if (this.actionIndex === 0) {
// if(tweenObj)
// {
// tweenObj.stop();
// this.tempObj.stop();
// }
// this.recycleTourist();
// return
// }
// this.actionIndex = this.actionIndex - 1;
// let tParentNode = this.parent;
// let posTemp = this.arrMajorRoutePos[this.actionIndex];
// let bezier1 =[posTemp,this.arrMiddleRoutePos[this.actionIndex],this.arrMajorPos[this.actionIndex]];
// posTemp = this.arrMajorPos[this.actionIndex];
// if (this.actionIndex === 0) {
// let tweenObj = cc.tween(this.node)
// .then(cc.bezierTo(this.minMoveTime+(Math.random()*this.moveDistantTime),bezier1))
// .delay(this.maxDelayTime * Math.random())
// .to((this.minMoveTime+(Math.random()*this.moveDistantTime)),{position:this.parent.endPos})
// .delay(this.maxDelayTime * Math.random())
// .call(() => this.planRoadSingle(tweenObj))
// .start()
// return
// }
// let bezier2 =[posTemp,this.arrMiddleRoutePos[this.actionIndex - 1],this.arrMajorRoutePos[this.actionIndex - 1]];
// cc.tween(this.node)
// .then(cc.bezierTo(this.minMoveTime+(Math.random()*this.moveDistantTime),bezier1))
// .delay(this.maxDelayTime * Math.random())
// .then(cc.bezierTo(this.minMoveTime+(Math.random()*this.moveDistantTime),bezier2))
// .delay(this.maxDelayTime * Math.random())
// .call(() => this.planRoadSingle())
// .start()
// }
recycleTourist():void
{
this.parent.recycleTourist(this.node);
}
onLoad () {
}
moveToNode(node:cc.Node,posX:cc.Vec2,posY:cc.Vec2,dt):void
{
let distance = ToolFunc.ins.getDistance( posX.y, posY.y);
let height = distance[0];
let heightDirection = distance[1];
distance = ToolFunc.ins.getDistance( posX.x, posY.x);
let width = distance[0];
let widthDirection = distance[1];
let trd = Math.sqrt(Math.pow(height,2)+Math.pow(width,2));
let time = trd/this.speed;
let Vtx = (width/time)*widthDirection;
let Vty = (height/time)*heightDirection;
this.node.x = this.node.x + (Vtx*dt)
this.node.y = this.node.y + (Vty*dt)
}
onStateStand():void{
this.touritstState = State.STAND;
this.standKeepTimeCount = 0;
}
// private _touritstState : number;
// public get touritstState() : number {
// return this._touritstState;
// }
// public set touritstState(v : number) {
// this._touritstState = v;
// }
update(dt)
{
if(this.touritstState === State.IN)
{
this.moveToNode(this.node,this.startPos,this.inPos,dt);
//启动
if(this.node.y >= this.inPos.y)
{
this.routeMajorState++;
this.touritstState++;
//this.touritstState = State.STAND;
this.onStateStand();
}
}
else if(this.touritstState === State.WALK)
{
//移动函数 根据新定位的坐标点 时间 逐帧前进
this.moveToNode(this.node,ToolFunc.ins.getPosition(this.node),this.goalPos,dt);
let nodePos = ToolFunc.ins.getPosition(this.node);
if(this.compareWithTwoNode(nodePos,this.goalPos)) //如果到地方了
{
//this.touritstState = State.STAND;
this.onStateStand();
this.standKeepTimeCount = 0;
}
}
else if(this.touritstState === State.STAND)
{
if((this.standKeepTimeCount === 0) && ToolFunc.ins.radomIfDoAction())
{
this.talk();
}
this.standKeepTimeCount ++;
if(this.standKeepTimeCount === 60)
{
//选择新的坐标点 确定移动时间
this.standKeepTimeCount = 0;
this.choiceNode();
}
}
else if(this.touritstState === State.OUT)
{
this.moveToNode(this.node,ToolFunc.ins.getPosition(this.node),this.outPos,dt);
//启动
if(this.node.y >= this.outPos.y)
{
this.routeMajorState++;
this.touritstState++;
//this.touritstState = State.STAND;
this.touritstState = State.END
}
}
else if(this.touritstState === State.END)
{
this.moveToNode(this.node,ToolFunc.ins.getPosition(this.node),this.endPos,dt);
//启动
if(this.node.y >= this.endPos.y)
{
this.touritstState = State.STOP
this.recycleTourist()
}
}
}
choiceNode():void
{
if(this.routeMajorState >= 5)
{
this.touritstState = State.OUT;
return
}
if(ToolFunc.ins.radomIfDoAction() || this.remainCount >=3)
{
//升级移动
this.remainCount = 0;
if(ToolFunc.ins.radomIfDoAction())
{
//跳过场馆直接进入升级平行移动
this.routeMajorState++;
this.getParallelNode();
}
else
{
//进入场馆
this.goalPos = this.arrMajorPos[this.routeMajorState];
this.routeMajorState++;
}
}
else
{
//同级平行移动
this.getParallelNode();
}
let nodePos = ToolFunc.ins.getPosition(this.node);
this.recordWithTwoNode(nodePos,this.goalPos);
this.touritstState = State.WALK;
}
getParallelNode():void//获取平行移动节点
{
this.remainCount++;
if(this.routeMajorState>=6)
{
let aaa;
}
let height = (this.arrMajorPos[this.routeMajorState].y - this.node.y) * Math.random() + this.node.y;
let width = this.parent.node.width * Math.random() - (this.parent.node.width)*0.5;
this.goalPos = cc.v2(width,height);
}
recordWithTwoNode(startPos:cc.Vec2,endPos:cc.Vec2):void
{
if(startPos.x >= endPos.x)
{
this.arrGoalDirection[0] = false;
}
else
{
this.arrGoalDirection[0] = true;
}
if(startPos.y >= endPos.y)
{
this.arrGoalDirection[1] = false;
}
else
{
this.arrGoalDirection[1] = true;
}
}
compareWithTwoNode(startPos:cc.Vec2,endPos:cc.Vec2):boolean
{
let bSurpassX = false;
let bSurpassY = false;
let isRight = this.arrGoalDirection[0];
let isUp = this.arrGoalDirection[1];
if(isRight)
{
if(startPos.x >= endPos.x)
{
bSurpassX = true;
}
}
else{
if(startPos.x <= endPos.x)
{
bSurpassX = true;
}
}
if(isUp)
{
if(startPos.y >= endPos.y)
{
bSurpassY = true;
}
}
else{
if(startPos.y <= endPos.y)
{
bSurpassY = true;
}
}
if(bSurpassX && bSurpassY)
{
return true;
}
return false;
}
reset():void
{
this.tempObj.stop();
this.singleTourist.node.x = 0;
this.singleTourist.node.y = 0;
}
talk():void
{
}
start () {
}
// update (dt) {}
}