Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace PacmanGame
- {
- class Map
- {
- Random rnd = new Random();
- public Tile[,] tiles;
- //public Rectangle[,] hitboxes;
- List<string> strings = new List<string>();
- SpriteFont font;
- Vector2 pos;
- public static int score;
- List<Entity> entities = new List<Entity>();
- Player player;
- Ghost ghost;
- public static bool touchwall = false;
- public Map(SpriteFont font,int posX,int posY)
- {
- this.font = font;
- this.tiles = new Tile[20, 20];
- this.pos = new Vector2(posX, posY);
- }
- public virtual void Content()
- {
- StreamReader sr = new StreamReader(@"Mapping.txt");
- //text = sr.ReadLine();
- while (!sr.EndOfStream)
- {
- strings.Add(sr.ReadLine());
- }
- sr.Close();
- for (int i = 0; i < tiles.GetLength(0); i++)
- {
- for (int j = 0; j < tiles.GetLength(1); j++)
- {
- int posX = i * 32 + (int)pos.X;
- int posY = j * 32 + (int)pos.Y;
- tiles[j, i] = new Tile(Game1.spritesheet, posX, posY);
- }
- }
- for (int i = 0; i < strings.Count; i++)
- {
- string temp = strings[i];
- for (int j = 0; j < temp.Length; j++)
- {
- if (temp[j] == 'X')
- {
- int posX = j * 32 + (int)pos.X;
- int posY = i * 32 + (int)pos.Y;
- player = new Player(Game1.spritesheet, new Vector2(posX + 16,posY + 16));
- player.Content();
- entities.Add(player);
- }
- if (temp[j] == 'G')
- {
- int posX = j * 32 + (int)pos.X;
- int posY = i * 32 + (int)pos.Y;
- ghost = new Ghost(Game1.spritesheet, new Vector2(posX + 16, posY + 16));
- ghost.SetDir(rnd.Next(1, 4));
- ghost.Content();
- entities.Add(ghost);
- }
- }
- }
- }
- public virtual void Update(GameTime gameTime)
- {
- //entities.ForEach(i => Console.Write("{0}\t", i));
- for (int i = 0; i < entities.Count; i++)
- {
- entities[i].Update(gameTime);
- }
- foreach (object e in entities)
- {
- if (e is Ghost)
- {
- Ghost ghost = (Ghost)e;
- if (player.hitbox.Intersects(ghost.hitbox))
- {
- if (Game1.currentGameState == PacmanGame.Game1.GameState.GameRun)
- {
- entities.Remove(player);
- break;
- }
- else if (Game1.currentGameState == PacmanGame.Game1.GameState.PPGameRun)
- {
- entities.Remove(ghost);
- break;
- }
- }
- }
- }
- if(score == 229)
- {
- Game1.currentGameState = PacmanGame.Game1.GameState.Win;
- }
- {
- for (int i = 0; i < strings.Count; i++)
- {
- for (int j = 0; j < strings[i].Length; j++)
- {
- if (strings[i][j] == 'F' || strings[i][j] == 'P' || strings[i][j] == 'C' || strings[i][j] == 'X' || strings[i][j] == 'G')
- {
- if (tiles[i, j].hitbox.Contains(player.hitbox))
- {
- player.prevPos.X = tiles[i, j].pos.X + 16;
- player.prevPos.Y = tiles[i, j].pos.Y + 16;
- }
- if (tiles[i, j].hitbox.Contains(ghost.hitbox))
- {
- ghost.prevPos.X = tiles[i, j].pos.X + 16;
- ghost.prevPos.Y = tiles[i, j].pos.Y + 16;
- }
- }
- //vad wall gör
- if (strings[i][j] == 'W')
- {
- //if (IsCollide())
- //{
- // if (!touchwall == true)
- // {
- // touchwall = true;
- // Console.WriteLine("true.");
- // }
- //}
- //else
- //{
- // if (!touchwall == false)
- // {
- // touchwall = false;
- // Console.WriteLine("false.");
- // }
- //}
- if (ghost.hitbox.Intersects(tiles[i, j].hitbox))
- {
- foreach (object e in entities)
- {
- //ghost = (Ghost)e;
- if (e is Ghost)
- {
- Console.WriteLine("hit");
- ghost.pos.Y = ghost.prevPos.Y;
- ghost.pos.X = ghost.prevPos.X;
- ghost.SetDir(rnd.Next(1,4));
- }
- }
- }
- if(player.hitbox.Intersects(tiles[i,j].hitbox))
- {
- foreach (Entity e in entities)
- {
- if (e is Player)
- {
- Console.WriteLine("hit");
- (e as Player).walk = false;
- if (player.lasthit == Microsoft.Xna.Framework.Input.Keys.Up || player.lasthit == Microsoft.Xna.Framework.Input.Keys.Down)
- player.pos.Y = player.prevPos.Y;
- else
- player.pos.X = player.prevPos.X;
- }
- }
- }
- }
- // vad wall gör
- // vad PP gör
- if (strings[i][j] == 'P' && tiles[i,j].eaten == false)
- {
- if (tiles[i, j].hitbox.Intersects(player.hitbox))
- {
- score++;
- tiles[i, j].eaten = true;
- }
- }
- // vad PP gör
- // vad Crystal gör
- if (strings[i][j] == 'C' && tiles[i, j].eaten == false)
- {
- if (tiles[i, j].hitbox.Intersects(player.hitbox))
- {
- score = score + 10;
- Game1.currentGameState = PacmanGame.Game1.GameState.PPGameRun;
- tiles[i, j].eaten = true;
- }
- }
- //vad Crystal gör
- }
- }
- }
- }
- //private bool IsCollide()
- //{
- // for (int i = 0; i < tiles.GetLength(0); i++)
- // {
- // for (int j = 0; j < tiles.GetLength(1); j++)
- // {
- // if (strings[i][j] == 'W')
- // if (player.hitbox.Intersects(tiles[i, j].hitbox))
- // return true;
- // }
- // }
- // return false;
- //}
- public virtual void Draw(SpriteBatch spriteBatch,GameTime gameTime)
- {
- for (int i = 0; i < strings.Count; i++)
- {
- for (int j = 0; j < strings[i].Length; j++)
- {
- if (strings[i][j] == 'W')
- tiles[i, j].Wall();
- tiles[i, j].Draw(spriteBatch);
- if (strings[i][j] == 'F' || strings[i][j] == 'X' ||strings[i][j] == 'G')
- {
- tiles[i, j].Floor();
- }
- if (strings[i][j] == 'P')
- {
- if (tiles[i, j].eaten == false)
- tiles[i, j].PP();
- else if (tiles[i, j].eaten == true)
- tiles[i, j].Floor();
- }
- if (strings[i][j] == 'C')
- {
- if (tiles[i, j].eaten == false)
- tiles[i, j].Crystal();
- else if (tiles[i, j].eaten == true)
- tiles[i, j].Floor();
- }
- }
- }
- for (int i = 0; i < entities.Count; i++)
- {
- entities[i].Draw(spriteBatch);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement