GameCamera.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { _decorator, Camera, Component, isValid, Node } from 'cc';
  2. import { Cell } from './Cell';
  3. import { GameMgr } from '../../module_basic/scripts/GameMgr';
  4. const { ccclass, property } = _decorator;
  5. const TWEEN_TIME = 500;//ms
  6. @ccclass('GameCamera')
  7. export class GameCamera extends Component {
  8. @property(Cell) target:Cell;
  9. private _camera:Camera;
  10. public get camera():Camera{
  11. return this._camera;
  12. }
  13. start() {
  14. this._camera = this.node.getComponent(Camera);
  15. this._originalOrthoHeight = this._camera.orthoHeight;
  16. this._targetHeight = this._camera.orthoHeight;
  17. this._startHeight = this._targetHeight;
  18. this._defaultRadius = GameMgr.inst.gameData? GameMgr.inst.gameData.defaultPlayerCellRadius:375;
  19. }
  20. private _originalOrthoHeight = 1.0;
  21. private _defaultRadius = 1.0;
  22. private _tweenTimeStart = 0;
  23. private _startHeight = 0;
  24. private _targetHeight = 0;
  25. lateUpdate(deltaTime: number) {
  26. if(isValid(this.target)){
  27. let tp = this.target.node.worldPosition;
  28. let sp = this.node.worldPosition;
  29. this.node.setWorldPosition(tp.x,tp.y,sp.z);
  30. let targetSize = this.target.player.radius;
  31. let newHeight = this._originalOrthoHeight * (1.0 + Math.sqrt((targetSize/this._defaultRadius - 1.0)) * 0.5);
  32. if(this._targetHeight != newHeight){
  33. this._targetHeight = newHeight;
  34. this._startHeight = this._camera.orthoHeight;
  35. this._tweenTimeStart = Date.now();
  36. }
  37. }
  38. if(this._tweenTimeStart){
  39. let t = (Date.now() - this._tweenTimeStart) / TWEEN_TIME;
  40. if(t >= 1.0){
  41. this._camera.orthoHeight = this._targetHeight;
  42. this._tweenTimeStart = 0;
  43. }
  44. else{
  45. this._camera.orthoHeight = this._startHeight * (1.0 - t) + this._targetHeight * t;
  46. }
  47. }
  48. }
  49. }