Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Content;
- namespace GameTemplate
- {
- public class LevelManager
- {
- public Camera2d Tehcamera;
- ContentManager Content;
- public List<GameObject> LoadedGO;
- public List<GameObject> Walls;
- public List<Enemy> Enemies;
- public static Player ThePlayer;
- public static Vector2 TileSize = new Vector2(32, 32);
- public LevelManager(ContentManager TheContent)
- {
- LoadedGO = new List<GameObject>();
- Walls = new List<GameObject>();
- Enemies = new List<Enemy>();
- ThePlayer = null;
- Content = TheContent;
- LoadLevel(0);
- }
- public void LoadLevel(int Index)
- {
- //clear the list
- LoadedGO = new List<GameObject>();
- Walls = new List<GameObject>();
- Enemies = new List<Enemy>();
- ThePlayer = null;
- //load in the objects based on the txt file
- System.IO.StreamReader LevelText = new System.IO.StreamReader("Content/Level" + Index.ToString() + ".txt");
- string CurrentLine = LevelText.ReadLine();//read the line from the txt file
- int CurrentYindex = 0;
- while (CurrentLine != null)//go thu each line
- {
- for (int x = 0; x < CurrentLine.Length; ++x)//go thu each letter
- {
- switch (CurrentLine[x])
- {
- case 'a':
- SpawnWater(new Vector2(x * TileSize.X, CurrentYindex * TileSize.Y));
- break;
- case 'P':
- SpawnPlayer(new Vector2(x * TileSize.X, CurrentYindex * TileSize.Y));
- break;
- case 'E':
- SpawnEnemy(new Vector2(x * TileSize.X, CurrentYindex * TileSize.Y));
- break;
- case 'w':
- SpawnWall(new Vector2(x * TileSize.X, CurrentYindex * TileSize.Y));
- break;
- case 's':
- SpawnPath(new Vector2(x * TileSize.X, CurrentYindex * TileSize.Y));
- break;
- default:
- SpawnGrass(new Vector2(x * TileSize.X, CurrentYindex * TileSize.Y));
- break;
- }
- }
- CurrentLine = LevelText.ReadLine();//read the line from the txt file
- CurrentYindex++;
- }
- //tell the enemies about the player
- for (int i = 0; i < Enemies.Count; ++i)
- {
- Enemies[i].SetTarget(ThePlayer);
- }
- }
- public void SewtCamera(Camera2d cam)
- {
- Tehcamera = cam;
- if (ThePlayer != null)
- {
- ThePlayer.SewtCamera(cam);
- }
- }
- void SpawnWall(Vector2 SpawnPosition)
- {
- GameObject TempNewGO = new GameObject(SpawnPosition,
- Content.Load<Texture2D>("Wall"));
- LoadedGO.Add(TempNewGO);
- Walls.Add(TempNewGO);
- }
- void SpawnGrass(Vector2 SpawnPosition)
- {
- LoadedGO.Add(new GameObject(SpawnPosition,
- Content.Load<Texture2D>("Grass")));
- }
- void SpawnWater(Vector2 SpawnPosition)
- {
- GameObject TempNewGo = new GameObject(SpawnPosition, Content.Load<Texture2D>("walk"));
- TempNewGo.isAnimated = true;
- TempNewGo.TotalFrameCount = 12;
- TempNewGo.TimePerFrame = 0.05f;
- LoadedGO.Add(TempNewGo);
- }
- void SpawnPath(Vector2 SpawnPosition)
- {
- LoadedGO.Add(new GameObject(SpawnPosition,
- Content.Load<Texture2D>("Stone")));
- }
- void SpawnPlayer(Vector2 SpawnPosition)
- {
- Player TempNewGO = new Player(SpawnPosition,
- Content.Load<Texture2D>("Player"));
- LoadedGO.Add(TempNewGO);
- ThePlayer = TempNewGO;
- ThePlayer.SewtCamera(Tehcamera);
- //player spawns on grass
- SpawnGrass(SpawnPosition);
- }
- void SpawnEnemy(Vector2 SpawnPosition)
- {
- Enemy TempNewGO = new Enemy(SpawnPosition,
- Content.Load<Texture2D>("Enemy"));
- LoadedGO.Add(TempNewGO);
- Enemies.Add(TempNewGO);
- //Enemy spawns on grass
- SpawnGrass(SpawnPosition);
- }
- public void Update(float DT)
- {
- for (int i = 0; i < Enemies.Count; ++i)
- {
- for (int ii = i+1; ii < Enemies.Count; ++ii)
- {
- if (Enemies[i].IsTouching(Enemies[ii]))
- {
- Enemies[i].HitWall(Enemies[ii]);
- Enemies[ii].HitWall(Enemies[i]);
- }
- }
- }
- //check collisiona
- for (int i = 0; i < Walls.Count; ++i)
- {
- for (int ii = 0; ii < Enemies.Count; ++ii)
- {
- if (Enemies[ii].IsTouching(Walls[i]))
- {
- Enemies[ii].HitWall(Walls[i]);
- }
- if (Enemies[ii].IsTouching(ThePlayer))
- {
- Enemies[ii].HitWall(ThePlayer);
- }
- }
- if (ThePlayer.IsTouching(Walls[i]))
- {
- ThePlayer.HitWall(Walls[i]);
- }
- }
- //update everything in the level
- for (int i = 0; i < LoadedGO.Count; ++i)
- {
- LoadedGO[i].Update(DT);
- }
- }
- public void Draw(SpriteBatch spriteBatch)
- {
- //draw everything in the level
- for (int i = 0; i < LoadedGO.Count; ++i)
- {
- LoadedGO[i].Draw(spriteBatch);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement