Advertisement
Wolvenspud

LevelManager

Oct 18th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using Microsoft.Xna.Framework.Input;
  9. using Microsoft.Xna.Framework.Content;
  10. namespace GameTemplate
  11. {
  12.     public class LevelManager
  13.     {
  14.         public Camera2d Tehcamera;
  15.         ContentManager Content;
  16.         public List<GameObject> LoadedGO;
  17.         public List<GameObject> Walls;
  18.         public List<Enemy> Enemies;
  19.         public static Player ThePlayer;
  20.  
  21.        public static Vector2 TileSize = new Vector2(32, 32);
  22.  
  23.         public LevelManager(ContentManager TheContent)
  24.         {
  25.             LoadedGO = new List<GameObject>();
  26.             Walls = new List<GameObject>();
  27.             Enemies = new List<Enemy>();
  28.             ThePlayer = null;
  29.  
  30.             Content = TheContent;
  31.  
  32.             LoadLevel(0);
  33.  
  34.         }
  35.  
  36.         public void LoadLevel(int Index)
  37.         {
  38.             //clear the list
  39.             LoadedGO = new List<GameObject>();
  40.             Walls = new List<GameObject>();
  41.             Enemies = new List<Enemy>();
  42.             ThePlayer = null;
  43.  
  44.  
  45.             //load in the objects based on the txt file
  46.             System.IO.StreamReader LevelText = new System.IO.StreamReader("Content/Level" + Index.ToString() + ".txt");
  47.  
  48.             string CurrentLine = LevelText.ReadLine();//read the line from the txt file
  49.             int CurrentYindex = 0;
  50.            
  51.             while (CurrentLine != null)//go thu each line
  52.             {
  53.                 for (int x = 0; x < CurrentLine.Length; ++x)//go thu each letter
  54.                 {
  55.                     switch (CurrentLine[x])
  56.                     {
  57.                         case 'a':
  58.                             SpawnWater(new Vector2(x * TileSize.X, CurrentYindex * TileSize.Y));
  59.                             break;
  60.                         case 'P':
  61.                             SpawnPlayer(new Vector2(x * TileSize.X, CurrentYindex * TileSize.Y));
  62.                             break;
  63.                          case 'E':
  64.                             SpawnEnemy(new Vector2(x * TileSize.X, CurrentYindex * TileSize.Y));
  65.                             break;
  66.                         case 'w':
  67.                             SpawnWall(new Vector2(x * TileSize.X, CurrentYindex * TileSize.Y));
  68.                             break;
  69.                         case 's':
  70.                             SpawnPath(new Vector2(x * TileSize.X, CurrentYindex * TileSize.Y));
  71.                             break;
  72.                         default:
  73.                             SpawnGrass(new Vector2(x * TileSize.X, CurrentYindex * TileSize.Y));
  74.                             break;
  75.                     }
  76.                 }
  77.  
  78.  
  79.                 CurrentLine = LevelText.ReadLine();//read the line from the txt file
  80.                 CurrentYindex++;
  81.             }
  82.  
  83.             //tell the enemies about the player
  84.             for (int i = 0; i < Enemies.Count; ++i)
  85.             {
  86.                 Enemies[i].SetTarget(ThePlayer);
  87.             }
  88.  
  89.  
  90.             }
  91.         public void SewtCamera(Camera2d cam)
  92.         {
  93.             Tehcamera = cam;
  94.             if (ThePlayer != null)
  95.             {
  96.                 ThePlayer.SewtCamera(cam);
  97.             }
  98.         }
  99.  
  100.         void SpawnWall(Vector2 SpawnPosition)
  101.         {
  102.             GameObject TempNewGO = new GameObject(SpawnPosition,
  103.                                Content.Load<Texture2D>("Wall"));
  104.             LoadedGO.Add(TempNewGO);
  105.             Walls.Add(TempNewGO);
  106.         }
  107.         void SpawnGrass(Vector2 SpawnPosition)
  108.         {
  109.             LoadedGO.Add(new GameObject(SpawnPosition,
  110.                                    Content.Load<Texture2D>("Grass")));
  111.         }
  112.         void SpawnWater(Vector2 SpawnPosition)
  113.         {
  114.             GameObject TempNewGo = new GameObject(SpawnPosition, Content.Load<Texture2D>("walk"));
  115.  
  116.             TempNewGo.isAnimated = true;
  117.             TempNewGo.TotalFrameCount = 12;
  118.             TempNewGo.TimePerFrame = 0.05f;
  119.  
  120.             LoadedGO.Add(TempNewGo);
  121.         }
  122.         void SpawnPath(Vector2 SpawnPosition)
  123.         {
  124.             LoadedGO.Add(new GameObject(SpawnPosition,
  125.                                 Content.Load<Texture2D>("Stone")));
  126.         }
  127.         void SpawnPlayer(Vector2 SpawnPosition)
  128.         {
  129.             Player TempNewGO = new Player(SpawnPosition,
  130.                                Content.Load<Texture2D>("Player"));
  131.             LoadedGO.Add(TempNewGO);
  132.             ThePlayer = TempNewGO;
  133.             ThePlayer.SewtCamera(Tehcamera);
  134.             //player spawns on grass
  135.             SpawnGrass(SpawnPosition);
  136.         }
  137.         void SpawnEnemy(Vector2 SpawnPosition)
  138.         {
  139.             Enemy TempNewGO = new Enemy(SpawnPosition,
  140.                                 Content.Load<Texture2D>("Enemy"));
  141.             LoadedGO.Add(TempNewGO);
  142.             Enemies.Add(TempNewGO);
  143.  
  144.             //Enemy spawns on grass
  145.             SpawnGrass(SpawnPosition);
  146.         }
  147.  
  148.  
  149.         public void Update(float DT)
  150.         {
  151.             for (int i = 0; i < Enemies.Count; ++i)
  152.             {
  153.                 for (int ii = i+1; ii < Enemies.Count; ++ii)
  154.                 {
  155.                     if (Enemies[i].IsTouching(Enemies[ii]))
  156.                     {
  157.                         Enemies[i].HitWall(Enemies[ii]);
  158.                         Enemies[ii].HitWall(Enemies[i]);
  159.                     }
  160.                 }                    
  161.             }
  162.  
  163.             //check collisiona
  164.             for (int i = 0; i < Walls.Count; ++i)
  165.             {
  166.                 for (int ii = 0; ii < Enemies.Count; ++ii)
  167.                 {
  168.                     if (Enemies[ii].IsTouching(Walls[i]))
  169.                     {
  170.                         Enemies[ii].HitWall(Walls[i]);
  171.                     }
  172.                     if (Enemies[ii].IsTouching(ThePlayer))
  173.                     {
  174.                         Enemies[ii].HitWall(ThePlayer);
  175.                     }
  176.                 }
  177.  
  178.                 if (ThePlayer.IsTouching(Walls[i]))
  179.                 {
  180.                     ThePlayer.HitWall(Walls[i]);
  181.                 }
  182.             }
  183.  
  184.  
  185.  
  186.             //update everything in the level
  187.             for (int i = 0; i < LoadedGO.Count; ++i)
  188.             {
  189.                 LoadedGO[i].Update(DT);
  190.             }
  191.         }
  192.  
  193.         public void Draw(SpriteBatch spriteBatch)
  194.         {
  195.             //draw everything in the level
  196.             for (int i = 0; i < LoadedGO.Count; ++i)
  197.             {
  198.                 LoadedGO[i].Draw(spriteBatch);
  199.             }
  200.         }
  201.  
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement