Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.33 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8.  
  9. namespace PacmanGame
  10. {
  11.     class Map
  12.     {
  13.         Random rnd = new Random();
  14.         public Tile[,] tiles;
  15.         //public Rectangle[,] hitboxes;
  16.         List<string> strings = new List<string>();
  17.        
  18.         SpriteFont font;
  19.         Vector2 pos;
  20.         public static int score;
  21.  
  22.         List<Entity> entities = new List<Entity>();
  23.         Player player;
  24.         Ghost ghost;
  25.         public static bool touchwall = false;
  26.  
  27.  
  28.         public Map(SpriteFont font,int posX,int posY)
  29.         {
  30.             this.font = font;
  31.             this.tiles = new Tile[20, 20];
  32.             this.pos = new Vector2(posX, posY);
  33.  
  34.  
  35.         }
  36.  
  37.         public virtual void Content()
  38.         {
  39.                        
  40.             StreamReader sr = new StreamReader(@"Mapping.txt");
  41.             //text = sr.ReadLine();
  42.  
  43.             while (!sr.EndOfStream)
  44.             {
  45.                 strings.Add(sr.ReadLine());
  46.             }
  47.  
  48.             sr.Close();
  49.  
  50.              for (int i = 0; i < tiles.GetLength(0); i++)
  51.             {
  52.                 for (int j = 0; j < tiles.GetLength(1); j++)
  53.                 {
  54.  
  55.  
  56.                     int posX = i * 32 + (int)pos.X;
  57.                     int posY = j * 32 + (int)pos.Y;
  58.  
  59.                     tiles[j, i] = new Tile(Game1.spritesheet, posX, posY);
  60.  
  61.                 }
  62.             }
  63.  
  64.              for (int i = 0; i < strings.Count; i++)
  65.              {
  66.                  string temp = strings[i];
  67.                  for (int j = 0; j < temp.Length; j++)
  68.                  {
  69.                      if (temp[j] == 'X')
  70.                      {
  71.                          int posX = j * 32 + (int)pos.X;
  72.                          int posY = i * 32 + (int)pos.Y;
  73.                          player = new Player(Game1.spritesheet, new Vector2(posX + 16,posY + 16));
  74.                          player.Content();
  75.                          entities.Add(player);
  76.                      }
  77.  
  78.                      if (temp[j] == 'G')
  79.                      {
  80.                          int posX = j * 32 + (int)pos.X;
  81.                          int posY = i * 32 + (int)pos.Y;
  82.                          ghost = new Ghost(Game1.spritesheet, new Vector2(posX + 16, posY + 16));
  83.                          ghost.SetDir(rnd.Next(1, 4));
  84.                          ghost.Content();
  85.                          entities.Add(ghost);
  86.                      }
  87.                  }
  88.  
  89.              }
  90.  
  91.  
  92.         }
  93.  
  94.         public virtual void Update(GameTime gameTime)
  95.         {
  96.  
  97.             //entities.ForEach(i => Console.Write("{0}\t", i));
  98.  
  99.             for (int i = 0; i < entities.Count; i++)
  100.             {
  101.                 entities[i].Update(gameTime);
  102.  
  103.  
  104.             }
  105.  
  106.  
  107.             foreach (object e in entities)
  108.             {
  109.                 if (e is Ghost)
  110.                 {
  111.                     Ghost ghost = (Ghost)e;
  112.                     if (player.hitbox.Intersects(ghost.hitbox))
  113.                     {
  114.                         if (Game1.currentGameState == PacmanGame.Game1.GameState.GameRun)
  115.                         {
  116.                             entities.Remove(player);
  117.                             break;
  118.                         }
  119.                         else if (Game1.currentGameState == PacmanGame.Game1.GameState.PPGameRun)
  120.                         {
  121.                             entities.Remove(ghost);
  122.                             break;
  123.                         }
  124.                            
  125.                     }
  126.  
  127.                 }
  128.  
  129.             }
  130.  
  131.  
  132.             if(score == 229)
  133.             {
  134.                 Game1.currentGameState = PacmanGame.Game1.GameState.Win;
  135.             }
  136.  
  137.  
  138.             {
  139.                
  140.                 for (int i = 0; i < strings.Count; i++)
  141.                 {
  142.                     for (int j = 0; j < strings[i].Length; j++)
  143.                     {
  144.  
  145.                         if (strings[i][j] == 'F' || strings[i][j] == 'P' || strings[i][j] == 'C' || strings[i][j] == 'X' || strings[i][j] == 'G')
  146.                         {
  147.                             if (tiles[i, j].hitbox.Contains(player.hitbox))
  148.                             {                      
  149.                                 player.prevPos.X = tiles[i, j].pos.X + 16;
  150.                                 player.prevPos.Y = tiles[i, j].pos.Y + 16;
  151.                             }
  152.  
  153.                             if (tiles[i, j].hitbox.Contains(ghost.hitbox))
  154.                             {
  155.                                 ghost.prevPos.X = tiles[i, j].pos.X + 16;
  156.                                 ghost.prevPos.Y = tiles[i, j].pos.Y + 16;
  157.                             }
  158.                         }
  159.  
  160.  
  161.                         //vad wall gör
  162.                         if (strings[i][j] == 'W')
  163.                         {
  164.  
  165.                             //if (IsCollide())
  166.                             //{
  167.                             //    if (!touchwall == true)
  168.                             //    {
  169.                             //        touchwall = true;
  170.                             //        Console.WriteLine("true.");
  171.  
  172.                             //    }
  173.                             //}
  174.  
  175.  
  176.                             //else
  177.                             //{
  178.                             //    if (!touchwall == false)
  179.                             //    {
  180.                             //        touchwall = false;
  181.                             //        Console.WriteLine("false.");
  182.  
  183.                             //    }
  184.                             //}
  185.                             if (ghost.hitbox.Intersects(tiles[i, j].hitbox))
  186.                             {
  187.                                                                
  188.                                 foreach (object e in entities)
  189.                                 {
  190.                                     //ghost = (Ghost)e;
  191.                                     if (e is Ghost)
  192.                                     {                                        
  193.                                         Console.WriteLine("hit");
  194.                                         ghost.pos.Y = ghost.prevPos.Y;
  195.                                         ghost.pos.X = ghost.prevPos.X;
  196.                                         ghost.SetDir(rnd.Next(1,4));
  197.  
  198.                                     }
  199.  
  200.                                 }
  201.                             }
  202.  
  203.                             if(player.hitbox.Intersects(tiles[i,j].hitbox))
  204.                             {
  205.                                 foreach (Entity e in entities)
  206.                                 {
  207.                                     if (e is Player)
  208.                                     {
  209.                                         Console.WriteLine("hit");
  210.                                         (e as Player).walk = false;
  211.                                         if (player.lasthit == Microsoft.Xna.Framework.Input.Keys.Up || player.lasthit == Microsoft.Xna.Framework.Input.Keys.Down)
  212.                                             player.pos.Y = player.prevPos.Y;
  213.                                         else
  214.                                             player.pos.X = player.prevPos.X;
  215.  
  216.                                     }
  217.                                 }
  218.  
  219.                             }
  220.  
  221.  
  222.                         }
  223.                            
  224.                         // vad wall gör
  225.  
  226.  
  227.                         // vad PP gör
  228.                         if (strings[i][j] == 'P' && tiles[i,j].eaten == false)
  229.                         {
  230.  
  231.                             if (tiles[i, j].hitbox.Intersects(player.hitbox))
  232.                             {
  233.                                 score++;
  234.                                 tiles[i, j].eaten = true;
  235.  
  236.                             }
  237.                         }
  238.  
  239.                         // vad PP gör
  240.  
  241.                         // vad Crystal gör
  242.                         if (strings[i][j] == 'C' && tiles[i, j].eaten == false)
  243.                         {
  244.  
  245.                             if (tiles[i, j].hitbox.Intersects(player.hitbox))
  246.                             {
  247.                                 score = score + 10;
  248.                                 Game1.currentGameState = PacmanGame.Game1.GameState.PPGameRun;
  249.                                 tiles[i, j].eaten = true;
  250.  
  251.                             }
  252.                         }
  253.                         //vad Crystal gör
  254.  
  255.                     }
  256.                 }
  257.  
  258.             }
  259.         }
  260.  
  261.         //private bool IsCollide()
  262.         //{
  263.         //    for (int i = 0; i < tiles.GetLength(0); i++)
  264.         //    {
  265.         //        for (int j = 0; j < tiles.GetLength(1); j++)
  266.         //        {
  267.         //            if (strings[i][j] == 'W')
  268.         //            if (player.hitbox.Intersects(tiles[i, j].hitbox))
  269.         //                return true;
  270.  
  271.         //        }                
  272.         //    }
  273.         //    return false;
  274.         //}
  275.  
  276.  
  277.         public virtual void Draw(SpriteBatch spriteBatch,GameTime gameTime)
  278.         {
  279.             for (int i = 0; i < strings.Count; i++)
  280.             {
  281.  
  282.                 for (int j = 0; j < strings[i].Length; j++)
  283.                 {
  284.                     if (strings[i][j] == 'W')
  285.                         tiles[i, j].Wall();
  286.  
  287.                     tiles[i, j].Draw(spriteBatch);
  288.  
  289.                     if (strings[i][j] == 'F' || strings[i][j] == 'X' ||strings[i][j] == 'G')
  290.                     {
  291.                         tiles[i, j].Floor();
  292.                     }
  293.  
  294.  
  295.                     if (strings[i][j] == 'P')
  296.                     {
  297.                         if (tiles[i, j].eaten == false)
  298.                         tiles[i, j].PP();
  299.                         else if (tiles[i, j].eaten == true)
  300.                         tiles[i, j].Floor();
  301.  
  302.                     }
  303.  
  304.                     if (strings[i][j] == 'C')
  305.                     {
  306.                         if (tiles[i, j].eaten == false)
  307.                         tiles[i, j].Crystal();
  308.                         else if (tiles[i, j].eaten == true)
  309.                         tiles[i, j].Floor();
  310.  
  311.                     }
  312.  
  313.                 }
  314.             }
  315.  
  316.             for (int i = 0; i < entities.Count; i++)
  317.             {
  318.                 entities[i].Draw(spriteBatch);
  319.             }
  320.  
  321.         }
  322.  
  323.     }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement