/* 文件名:MapCamera.nut 路径:User/Object/Map/MapCamera.nut 创建日期:2024-12-22 21:56 文件用途: */ class MapCamera { //摄像机坐标 X = 0; Y = 0; Z = 0; NextX = 0; //镜头可行坐标 MovableAreaX = 0; MovableAreaY = 0; //背景偏移量 BackgroundOffset = 0; //背景层移动速率 BackgroundMoveSpeed = 1.0; //人物中线长度 CharacterLineLength = 0; //地图对象 MyMapParent = null; //跟随对象 MyFromParent = null; //摄像机朝向 Direction = 1; //摄像机朝向时间 DirectionTime = 1; //缩放比率 CameraRate = 1.0; constructor(gMap) { MyMapParent = gMap.weakref(); } /* * @函数作用:设置跟随对象 */ function SetFromParent(FromParent) { MyFromParent = FromParent.weakref(); } /* * @函数作用: */ function SetPosition(gx, gy, gz) { X = gx * CameraRate; Y = gy * CameraRate; Z = gz * CameraRate; } /* * @函数作用: 增加坐标偏移 */ function AddPosition(Type, Value) { switch (Type) { case 0: //X X += Value; break; case 1: //Y Y += Value; break; case 2: //Z Z += Value; break; } } /* * @函数作用: 通过父对象同步自身坐标 */ function SyncPosByFromParent(MillisecondsDuration) { if (MyFromParent) { local R_X, R_Y, R_Z; // //X轴移动特殊判断 // R_X = MyFromParent.X; // //为超出中线长度时摄像机不会改变位置 // if (R_X <= (X + CharacterLineLength) && R_X >= (X - CharacterLineLength)) R_X = X; // //超出以后 补足位置 // else if (R_X > (X + CharacterLineLength)) R_X = R_X - CharacterLineLength; // else if (R_X<(X - CharacterLineLength)) R_X = R_X + CharacterLineLength; //判断是否超出可行区域 R_X = Math.Min(Math.Max(MyFromParent.X, 533), MovableAreaX - 533); R_Y = Math.Min(Math.Max(MyFromParent.Y, 300), MovableAreaY - 300); R_Z = MyFromParent.Z; SetPosition(R_X, R_Y, R_Z); } } /* * @函数作用:Update */ function OnUpdate(Dt) { //同步自身与跟随对象的坐标 SyncPosByFromParent(Dt); //刷新地图图层的坐标 foreach(Name, Layer in MyMapParent.LayerObject) { //X屏幕宽度一般 Y屏幕宽度一半 +Z轴运算 在加统一120的偏移 在家background的偏移 if (Name == "distantback") { Layer.SetPosition((-X + 533) * BackgroundMoveSpeed, -Y + Z + 300 + 120 + BackgroundOffset); } else { Layer.SetPosition((-X + 533), -Y + Z + 300 + 120 + BackgroundOffset); } } } }