11import * as THREE from 'three' ;
22import { GameComponentBase } from '../GameComponentBase' ;
33
4- type CameraMode = 'default' | 'top' ;
5-
4+ export enum CameraMode {
5+ Default = 'default' ,
6+ Top = 'top'
7+ }
68enum CameraScene {
79 Default = '1' ,
810 Top = '2' ,
@@ -13,7 +15,7 @@ export class CameraComponent extends GameComponentBase {
1315 private readonly defaultPosition : THREE . Vector3 = new THREE . Vector3 ( 5 , 2 , 30 ) ;
1416 private readonly topViewOffset : THREE . Vector3 = new THREE . Vector3 ( 0 , 35 , 20 ) ;
1517 private readonly topViewLookOffset : THREE . Vector3 = new THREE . Vector3 ( 0 , 0 , - 15 ) ;
16- private mode : CameraMode = 'default' ;
18+ private mode : CameraMode = CameraMode . Default ;
1719 private targetProvider : ( ) => THREE . Vector3 ;
1820
1921 constructor ( targetProvider : ( ) => THREE . Vector3 ) {
@@ -26,8 +28,13 @@ export class CameraComponent extends GameComponentBase {
2628 return this . camera ;
2729 }
2830
29- public async load ( scene : THREE . Scene ) : Promise < void > {
30- this . camera . position . copy ( this . defaultPosition ) ;
31+ public async load ( scene : THREE . Scene , initialMode : CameraMode = CameraMode . Default ) : Promise < void > {
32+ this . mode = initialMode ;
33+ if ( this . mode === CameraMode . Top ) {
34+ this . updateCameraTracking ( true ) ;
35+ } else {
36+ this . camera . position . copy ( this . defaultPosition ) ;
37+ }
3138 scene . add ( this . camera ) ;
3239 window . addEventListener ( 'keydown' , this . handleCameraChange ) ;
3340 }
@@ -38,22 +45,22 @@ export class CameraComponent extends GameComponentBase {
3845
3946 private handleCameraChange = ( event : KeyboardEvent ) : void => {
4047 if ( event . key === CameraScene . Top ) {
41- this . mode = 'top' ;
48+ this . mode = CameraMode . Top ;
4249 this . updateCameraTracking ( true ) ;
4350 }
4451 if ( event . key === CameraScene . Default ) {
45- this . mode = 'default' ;
52+ this . mode = CameraMode . Default ;
4653 this . camera . position . copy ( this . defaultPosition ) ;
4754 this . camera . lookAt ( new THREE . Vector3 ( 0 , 0 , 0 ) ) ;
4855 }
4956 }
5057
5158 private updateCameraTracking ( force : boolean = false ) : void {
52- if ( this . mode !== 'top' && ! force ) {
59+ if ( this . mode !== CameraMode . Top && ! force ) {
5360 return ;
5461 }
5562
56- if ( this . mode === 'top' ) {
63+ if ( this . mode === CameraMode . Top ) {
5764 const targetPosition = this . targetProvider ( ) ;
5865 this . camera . position . set (
5966 targetPosition . x + this . topViewOffset . x ,
0 commit comments