1- using  LDtk ; 
1+ using  System ; 
2+ 
3+ using  LDtk ; 
24
35using  Microsoft . Xna . Framework ; 
46using  Microsoft . Xna . Framework . Graphics ; 
@@ -8,66 +10,66 @@ namespace Example
810{ 
911    public  class  LDtkExample  :  Game 
1012    { 
11-         GraphicsDeviceManager  graphics ; 
12-         SpriteBatch  spriteBatch ; 
13-         Project  projectFile ; 
13+         private   readonly   GraphicsDeviceManager  graphics ; 
14+         private   SpriteBatch  spriteBatch ; 
15+         private   Project  projectFile ; 
1416
15-         Vector3  cameraPosition ; 
16-         Vector3  cameraOrigin ; 
17-         float  cameraZoom  =  1f ; 
17+         // Camera 
18+         private  Vector3  cameraPosition ; 
19+         private  Vector3  cameraOrigin ; 
20+         private  float  cameraZoom  =  1f ; 
21+         private  readonly  int  currentLevel  =  1 ; 
22+         private  readonly  bool [ ]  activeLayers  =  {  true ,  true ,  true ,  true ,  true ,  true ,  true ,  true ,  true ,  true ,  true ,  true ,  true ,  true ,  true ,  } ; 
1823
19-         int   currentLevel   =   0 ; 
24+         KeyboardState   oldKeyboardState ; 
2025
2126        public  LDtkExample ( ) 
2227        { 
2328            graphics  =  new  GraphicsDeviceManager ( this ) ; 
29+             IsFixedTimeStep  =  false ; 
2430        } 
2531
2632        protected  override  void  Initialize ( ) 
2733        { 
28-             MonogameStuff ( ) ; 
29- 
30-             projectFile  =  new  Project ( "Data/AutoLayers_4_Advanced.ldtk" ) ; 
31-             projectFile . Render ( spriteBatch ,  0 ) ; 
34+             MonogameInitialize ( ) ; 
3235
33-             Window . ClientSizeChanged   +=   ( o ,   e )   =>   cameraOrigin   =   new  Vector3 ( GraphicsDevice . Viewport . Width   /   2f ,   GraphicsDevice . Viewport . Height   /   2f ,   0 ) ; 
34-             cameraOrigin   =   new   Vector3 ( GraphicsDevice . Viewport . Width   /   2f ,   GraphicsDevice . Viewport . Height   /   2f ,   0 ) ; 
36+             projectFile   =   new  Project ( spriteBatch ,   "samples/Test_file_for_API_showing_all_features.ldtk" ) ; 
37+             projectFile . Render ( currentLevel ) ; 
3538
3639            base . Initialize ( ) ; 
3740        } 
3841
39-         void  MonogameStuff ( ) 
42+         private  void  OnWindowResized ( ) 
43+         { 
44+             cameraOrigin  =  new  Vector3 ( GraphicsDevice . Viewport . Width  /  2f ,  GraphicsDevice . Viewport . Height  /  2f ,  0 ) ; 
45+             cameraZoom  =  MathF . Max ( 1 ,  GraphicsDevice . Viewport . Height  /  160 ) ; 
46+         } 
47+ 
48+         private  void  MonogameInitialize ( ) 
4049        { 
4150            Window . AllowUserResizing  =  true ; 
4251            IsMouseVisible  =  true ; 
4352            spriteBatch  =  new  SpriteBatch ( GraphicsDevice ) ; 
44-         } 
53+              IsFixedTimeStep   =   true ; 
4554
46-         protected  override  void  Draw ( GameTime  gameTime ) 
47-         { 
48-             Level  level  =  projectFile . Levels [ currentLevel ] ; 
49-             GraphicsDevice . Clear ( level . BgColor ) ; 
55+             TargetElapsedTime  =  TimeSpan . FromSeconds ( 1d  /  60d ) ; 
5056
51-             spriteBatch . Begin ( samplerState :  SamplerState . PointClamp ,  transformMatrix :  Matrix . CreateTranslation ( cameraPosition )  *  Matrix . CreateScale ( cameraZoom )  *  Matrix . CreateTranslation ( cameraOrigin ) ) ; 
52-             { 
53-                 for ( int  i  =  0 ;  i  <  level . layers . Length ;  i ++ ) 
54-                 { 
55-                     spriteBatch . Draw ( level . layers [ i ] ,  Vector2 . Zero ,  Color . White ) ; 
56-                 } 
57-             } 
58-             spriteBatch . End ( ) ; 
57+             graphics . ApplyChanges ( ) ; 
5958
60-             base . Draw ( gameTime ) ; 
59+             Window . ClientSizeChanged  +=  ( o ,  e )  =>  OnWindowResized ( ) ; 
60+             OnWindowResized ( ) ; 
6161        } 
6262
6363        protected  override  void  Update ( GameTime  gameTime ) 
6464        { 
65-             var  keyboard  =  Keyboard . GetState ( ) ; 
65+             double  deltaTime  =  gameTime . ElapsedGameTime . TotalSeconds ; 
66+ 
67+             KeyboardState  keyboard  =  Keyboard . GetState ( ) ; 
6668
6769            float  h  =  ( keyboard . IsKeyDown ( Keys . D )  ?  - 1f  :  0f )  +  ( keyboard . IsKeyDown ( Keys . A )  ?  1f  :  0f ) ; 
6870            float  v  =  ( keyboard . IsKeyDown ( Keys . S )  ?  - 1f  :  0f )  +  ( keyboard . IsKeyDown ( Keys . W )  ?  1f  :  0f ) ; 
6971
70-             cameraPosition  +=  new  Vector3 ( h ,  v ,  0 )  *  50f  *  ( float ) gameTime . ElapsedGameTime . TotalSeconds ; 
72+             cameraPosition  +=  new  Vector3 ( h ,  v ,  0 )  *  100  *  ( float ) deltaTime ; 
7173
7274            if ( keyboard . IsKeyDown ( Keys . R ) ) 
7375            { 
@@ -84,7 +86,46 @@ protected override void Update(GameTime gameTime)
8486                cameraZoom  -=  ( float ) gameTime . ElapsedGameTime . TotalSeconds ; 
8587            } 
8688
89+             if ( keyboard . IsKeyDown ( Keys . D1 )  ==  false  &&  oldKeyboardState . IsKeyDown ( Keys . D1 )  ==  true ) 
90+             { 
91+                 activeLayers [ 0 ]  =  ! activeLayers [ 0 ] ; 
92+             } 
93+             if ( keyboard . IsKeyDown ( Keys . D2 )  ==  false  &&  oldKeyboardState . IsKeyDown ( Keys . D2 )  ==  true ) 
94+             { 
95+                 activeLayers [ 1 ]  =  ! activeLayers [ 1 ] ; 
96+             } 
97+             if ( keyboard . IsKeyDown ( Keys . D3 )  ==  false  &&  oldKeyboardState . IsKeyDown ( Keys . D3 )  ==  true ) 
98+             { 
99+                 activeLayers [ 2 ]  =  ! activeLayers [ 2 ] ; 
100+             } 
101+             if ( keyboard . IsKeyDown ( Keys . D4 )  ==  false  &&  oldKeyboardState . IsKeyDown ( Keys . D4 )  ==  true ) 
102+             { 
103+                 activeLayers [ 3 ]  =  ! activeLayers [ 3 ] ; 
104+             } 
105+ 
106+             oldKeyboardState  =  keyboard ; 
107+ 
87108            base . Update ( gameTime ) ; 
88109        } 
110+ 
111+         protected  override  void  Draw ( GameTime  gameTime ) 
112+         { 
113+             Level  level  =  projectFile . Levels [ currentLevel ] ; 
114+             GraphicsDevice . Clear ( level . BgColor ) ; 
115+ 
116+             spriteBatch . Begin ( samplerState :  SamplerState . PointClamp ,  transformMatrix :  Matrix . CreateTranslation ( cameraPosition )  *  Matrix . CreateScale ( cameraZoom )  *  Matrix . CreateTranslation ( cameraOrigin ) ) ; 
117+             { 
118+                 for ( int  i  =  level . layers . Length  -  1 ;  i  >=  0 ;  i -- ) 
119+                 { 
120+                     if ( activeLayers [ i ]  ==  true ) 
121+                     { 
122+                         spriteBatch . Draw ( level . layers [ i ] ,  Vector2 . Zero ,  Color . White ) ; 
123+                     } 
124+                 } 
125+             } 
126+             spriteBatch . End ( ) ; 
127+ 
128+             base . Draw ( gameTime ) ; 
129+         } 
89130    } 
90- } 
131+ } 
0 commit comments