Advertisement
Guest User

TestChambers.cs

a guest
May 6th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.92 KB | None | 0 0
  1. using Microsoft.Xna.Framework.Audio;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using xTile;
  5. using xTile.Display;
  6. using xTile.ObjectModel;
  7.  
  8. using Krypton;
  9. using Krypton.Lights;
  10. namespace RenaissanceAdventure.level
  11. {
  12.     public class TestChambers:TiAPI.Component.tileGameLevel
  13.     {
  14.         KryptonEngine krypton;
  15.         private Texture2D mLightTexture;
  16.         Light2D playerLight;
  17.        
  18.         public TestChambers(Map map,Game1 instance)
  19.         {
  20.             TileMap = map;
  21.             DisplayWidth = TileMap.DisplayWidth;
  22.             DisplayHeight = TileMap.DisplayHeight;
  23.             this.krypton = new KryptonEngine(instance, "KryptonEffect");
  24.            
  25.         }
  26.         public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager Content,GraphicsDevice device)
  27.         {
  28.             ID = "TestChambers";
  29.             BackColor = Color.Black;
  30.             levelHeading = Content.Load<Texture2D>("sprites\\gui\\levelHeader\\TestChamber");
  31.             usesLighting = true;
  32.             base.LoadContent(Content);
  33.              BGMCue = "Cue_PuzzleGame";
  34.             GameObjects.Add(Game1.player);
  35.             // Create a new simple point light texture to use for the lights
  36.             this.mLightTexture = LightTextureBuilder.CreatePointLight(device, 512);
  37.            
  38.             // Create some lights
  39.             playerLight = new Light2D()
  40.             {
  41.                 Texture = mLightTexture,
  42.                 Range = 96,
  43.                 Color = new Color(255, 255, 255),
  44.                 Intensity = 0.5f,
  45.                 Angle = MathHelper.TwoPi * 0,
  46.                 X = 100,
  47.                 Y = -100,
  48.                
  49.             };
  50.              
  51.            
  52.             krypton.AmbientColor = new Color(48, 48, 48, 255);
  53.         }
  54.  
  55.         public override void InitalizeGFX()
  56.         {
  57.             this.krypton.Initialize();
  58.             base.InitalizeGFX();
  59.         }
  60.         void addLight(Color c, Vector2 position, int range)
  61.         {
  62.             Light2D lt = new Light2D()
  63.             {
  64.                 Texture = mLightTexture,
  65.                 Range = range,
  66.                 Color = c,
  67.                 Intensity = 1.0f,
  68.                 Angle = MathHelper.TwoPi * 0,
  69.                 X = position.X,
  70.                 Y = position.Y,
  71.  
  72.             };
  73.             krypton.Lights.Add(lt);
  74.         }
  75.         public override void InitalizeLogic()
  76.         {
  77.            
  78.             krypton.Lights.Clear();
  79.             krypton.Lights.Add(playerLight);
  80.             SolidRectangles.Clear(); actionTiles.Clear();
  81.             foreach (xTile.Layers.Layer current in TileMap.Layers)
  82.             {
  83.                 if (current.Id == "lightingLayer")
  84.                 {
  85.                     PropertyValue r = 0;
  86.                     PropertyValue g = 0;
  87.                     PropertyValue b = 0;
  88.                     PropertyValue range = 0;
  89.                     int xx = 0, yy = 0;
  90.                     do
  91.                     {
  92.                         for (xx = 0; xx < current.LayerWidth; xx++)
  93.                         {
  94.                             if (current.Tiles[xx, yy] != null)
  95.                             {
  96.                                 current.Tiles[xx, yy].Properties.TryGetValue("r", out r);
  97.                                 current.Tiles[xx, yy].Properties.TryGetValue("g", out g);
  98.                                 current.Tiles[xx, yy].Properties.TryGetValue("b", out b);
  99.                                 current.Tiles[xx, yy].Properties.TryGetValue("range", out range);
  100.                                 if (r == null)
  101.                                 {
  102.                                     r = 255;
  103.                                 }
  104.                                 if (g == null)
  105.                                 {
  106.                                     g = 255;
  107.                                 }
  108.                                 if (b == null)
  109.                                 {
  110.                                     b = 255;
  111.                                 }
  112.                                 if (range == null)
  113.                                 {
  114.                                     range = 32;
  115.                                 }
  116.                                 addLight(new Color(r, g, b), new Vector2(xx*8, -yy*8), range);
  117.                             }
  118.  
  119.  
  120.  
  121.                         }
  122.                         yy++;
  123.                     } while (yy < current.LayerHeight);
  124.                 }
  125.                 PropertyValue Logic = 0;
  126.                 PropertyValue solid = 0;
  127.                 PropertyValue action = "";
  128.                 PropertyValue pseudo = 0;
  129.                 PropertyValue spawnpoint = "";
  130.                 PropertyValue Entity = "";
  131.                 current.Properties.TryGetValue("Logic", out Logic);
  132.                 current.Properties.TryGetValue("solid", out solid);
  133.  
  134.                 if (solid == null)
  135.                 {
  136.                     solid = 0;
  137.                 }
  138.                 if (Logic == null)
  139.                 {
  140.                     Logic = 0;
  141.                 }
  142.                 if (Logic == 1)
  143.                 {
  144.                     int spx = 0, spy = 0;
  145.                     int xx = 0, yy = 0;
  146.                     do
  147.                     {
  148.                         for (xx = 0; xx < current.LayerWidth; xx++)
  149.                         {
  150.                             if (current.Tiles[xx, yy] != null)
  151.                             {
  152.                                 current.Tiles[xx, yy].Properties.TryGetValue("SpawnPlayer", out pseudo);
  153.                                 current.Tiles[xx, yy].Properties.TryGetValue("action", out action);
  154.                                 current.Tiles[xx, yy].Properties.TryGetValue("spawnpoint", out spawnpoint);
  155.                                 current.Tiles[xx, yy].Properties.TryGetValue("Entity", out Entity);
  156.                             }
  157.                             if (action == null)
  158.                             {
  159.                                 action = "";
  160.                             }
  161.                             if (pseudo == null)
  162.                             {
  163.                                 pseudo = 0;
  164.                             }
  165.                             if (pseudo == 1)
  166.                             {
  167.                                 spx = xx * current.TileWidth;
  168.                                 spy = yy * current.TileHeight;
  169.                                 pseudo = 0;
  170.                             }
  171.                             if (spawnpoint == null)
  172.                             {
  173.                                 spawnpoint = "";
  174.                             }
  175.                             if (spawnpoint == Game1.player.LastLevelID && Game1.player.LastLevelID != this.ID && !string.IsNullOrEmpty(Game1.player.LastLevelID))
  176.                             {
  177.                                 spx = xx * current.TileWidth;
  178.                                 spy = yy * current.TileHeight;
  179.                                 spawnpoint = "";
  180.                                 //Game1.player.Position = new Vector2(xx * current.TileWidth, yy * current.TileHeight);
  181.                             }
  182.                             if (Entity == null)
  183.                             {
  184.                                 Entity = "";
  185.                             }
  186.                             if (Entity == "SoundEntity")
  187.                             {
  188.                                 //this.GameObjects.Add(new Objects.Entity.TestSoundEntity(new Vector2(xx * current.TileHeight, yy * current.TileHeight)));
  189.                                 Entity = "";
  190.                             }
  191.                             if (action != "")
  192.                             {
  193.                                 if (action.ToString().StartsWith("Health."))
  194.                                 {
  195.  
  196.                                     actionTiles.Add(new TiAPI.Component.ActionTile(action, new Rectangle(xx * current.TileWidth, yy * current.TileHeight, current.TileWidth, current.TileHeight), Color.Red));
  197.  
  198.                                 }
  199.                                 else
  200.                                 {
  201.                                     actionTiles.Add(new TiAPI.Component.ActionTile(action, new Rectangle(xx * current.TileWidth, yy * current.TileHeight, current.TileWidth, current.TileHeight)));
  202.                                 }
  203.                                 action = null;
  204.                             }
  205.  
  206.  
  207.                         }
  208.                         yy++;
  209.                     } while (yy < current.LayerHeight);
  210.  
  211.                     Game1.player.Position = new Vector2(spx, spy);
  212.                    
  213.                 }
  214.                 if (solid == 1)
  215.                 {
  216.                     int xsx, ysy = 0;
  217.                     do
  218.                     {
  219.                         for (xsx = 0; xsx < current.LayerWidth; xsx++)
  220.                         {
  221.                             if (current.Tiles[xsx, ysy] != null)
  222.                             {
  223.                                 SolidRectangles.Add(new Rectangle(xsx * current.TileWidth, ysy * current.TileHeight, current.TileWidth, current.TileHeight));
  224.  
  225.                             }
  226.  
  227.                         }
  228.  
  229.                         ysy++;
  230.                     } while (ysy < current.LayerHeight);
  231.  
  232.                 }
  233.             }
  234.            
  235.         }
  236.         public override void Update(GameTime gt,xTile.Dimensions.Location vportLoc)
  237.         {
  238.             LightCount = krypton.Lights.Count;
  239.  
  240.             base.Update(gt, vportLoc);
  241.            
  242.         }
  243.         /// <summary>
  244.         /// Overridden class of tileGameLevel
  245.         /// </summary>
  246.         /// <param name="device">The XTile DisplayDevice</param>
  247.         /// <param name="viewport">XTile's Camera</param>
  248.         /// <param name="offset">View Offset</param>
  249.         /// <param name="wrap">Wrap around, true if no walls.</param>
  250.         /// <param name="spritebatch"></param>
  251.         /// <param name="ViewportLoc">Viewport location (probably overkill)</param>
  252.         public override void Draw(IDisplayDevice device, xTile.Dimensions.Rectangle viewport, xTile.Dimensions.Location offset, bool wrap, SpriteBatch spritebatch, xTile.Dimensions.Location ViewportLoc)
  253.         {
  254.             playerLight.IsOn = true;
  255.             this.playerLight.Position = new Vector2(Game1.player.Position.X, -Game1.player.Position.Y);
  256.             Matrix world = Matrix.CreateWorld(new Vector3(0, 0, 0), Vector3.Forward, Vector3.Up);
  257.             Matrix view = Matrix.CreateTranslation(new Vector3(-ViewportLoc.X-viewport.Width/2, ViewportLoc.Y+viewport.Height/2, 0));
  258.             Matrix projection = Matrix.CreateOrthographic(viewport.Width, viewport.Height, 0, 0);
  259.  
  260.             // Assign the matrix and pre-render the light map.
  261.             // Make sure not to change the position of any lights or shadow hulls after this call, as it won't take effect till the next frame!
  262.             this.krypton.Matrix = world*view*projection;
  263.             this.krypton.Bluriness = 0;
  264.             this.krypton.LightMapPrepare();
  265.            
  266.             // ----- DRAW STUFF HERE ----- //
  267.             // By drawing here, you ensure that your scene is properly lit by krypton.
  268.             spritebatch.Begin();
  269.             spritebatch.Draw(Game1.backgroundColor, new Rectangle(0, 0, viewport.Width, viewport.Height), Color.White);
  270.             spritebatch.End();
  271.             base.Draw(device, viewport, offset, wrap, spritebatch, ViewportLoc);
  272.             // Drawing after KryptonEngine.Draw will cause you objects to be drawn on top of the light map (can be useful, fyi)
  273.             // ----- DRAW STUFF HERE ----- //
  274.             this.krypton.Draw(null);
  275.            
  276.             base.DrawHeading(device, viewport, offset, wrap, spritebatch, ViewportLoc);
  277.            
  278.            
  279.            
  280.             if (Game1.ShowBounds)
  281.             {
  282.                 foreach (Rectangle item in SolidRectangles)
  283.                 {
  284.                     if (item.Intersects(new Rectangle(viewport.X, viewport.Y, viewport.Width, viewport.Height)))
  285.                     {
  286.                         spritebatch.Begin();
  287.                         spritebatch.Draw(Game1.bounds, new Rectangle(-ViewportLoc.X + item.X, -ViewportLoc.Y + item.Y, item.Width, item.Height), Color.Black);
  288.                         spritebatch.End();
  289.                     }
  290.                 }
  291.                 foreach (TiAPI.Component.ActionTile item in actionTiles)
  292.                 {
  293.                     item.draw(spritebatch, ViewportLoc);
  294.                 }
  295.                 foreach (Krypton.Lights.Light2D item in krypton.Lights)
  296.                 {
  297.                     spritebatch.Begin();
  298.                     spritebatch.Draw(Game1.bounds, new Rectangle(-ViewportLoc.X + (int)item.X - (int)item.Bounds.Width / 2, -ViewportLoc.Y - (int)item.Y - (int)item.Bounds.Height / 2, (int)item.Bounds.Width, (int)item.Bounds.Height), item.Color);
  299.                     spritebatch.End();
  300.                 }
  301.             }
  302.         }
  303.     }
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement