Exemplo n.º 1
0
 /// <summary>
 /// finds any object with the given object name
 /// </summary>
 /// <param name="name">the name to check for</param>
 /// <returns>an object with the given name, null if it could not be found</returns>
 public GameObject FindObject(string name)
 {
     for (int i = 0; i < GameObjectList.Count; i++)
     {
         GameObject obj = GameObjectList[i];
         if (obj.GetType().IsEquivalentTo(PEngine.GetTypeFromName(name)))
         {
             return(obj);
         }
     }
     return(null);
 }
Exemplo n.º 2
0
 /// <summary>
 /// checks for a collision against a rectangle and all other objects of the given name
 /// </summary>
 /// <param name="collider">the rectangle to check</param>
 /// <param name="name">the name of the object to check against</param>
 /// <returns>the object we collide with</returns>
 public GameObject FindCollision(Rectangle collider, string name)
 {
     for (int i = 0; i < GameObjectList.Count; i++)
     {
         GameObject obj = GameObjectList[i];
         if (obj.GetType().IsEquivalentTo(PEngine.GetTypeFromName(name)))
         {
             Rectangle objRect = new Rectangle((obj.Position + obj.Sprite.Offset).ToPoint(), obj.Sprite.Size.ToPoint());//new Rectangle((int)obj.Position.X, (int)obj.Position.Y, (int)(obj.Sprite.Size.X, (int)obj.Sprite.Size.Y);
             if (PlatformerMath.RectangleInRectangle(collider, objRect))
             {
                 return(obj);
             }
         }
     }
     return(null);
 }
Exemplo n.º 3
0
        /// <summary>
        /// creates an instance of a room
        /// </summary>
        /// <param name="engine">the parent engine</param>
        public Room(PEngine engine)
        {
            Engine               = engine;
            Sounds               = new SoundManager();
            Background           = new SpriteData();
            Background.LayerData = new LayerData(0);
            Width          = 512;
            Height         = 512;
            ViewPosition   = new Vector2(0, 0);
            GameObjectList = new List <GameObject>();
            GameTileList   = new List <GameTile>();
            LightList      = new List <Light>();
            Physics        = null;
            var pp = Engine.Game.GraphicsDevice.PresentationParameters;

            MainTarget  = new RenderTarget2D(Engine.Game.GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight);
            LightTarget = new RenderTarget2D(Engine.Game.GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight);
        }
Exemplo n.º 4
0
        /// <summary>
        /// creates an instance of a room
        /// </summary>
        /// <param name="engine">the parent engine</param>
        public Room(PEngine engine)
        {
            Engine          = engine;
            Sounds          = new SoundManager();
            Background      = null;
            Width           = 512;
            Height          = 512;
            ViewPosition    = new Vector2(0, 0);
            GameObjectList  = new List <GameObject>();
            GameTileList    = new List <GameTile>();
            LightList       = new List <Light>();
            LightingEnabled = false;
            Physics         = null;
            var pp = Engine.Game.GraphicsDevice.PresentationParameters;

            MainTarget      = new RenderTarget2D(Engine.Game.GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight);
            LightTarget     = new RenderTarget2D(Engine.Game.GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight);
            CurrentFileName = "";
        }
Exemplo n.º 5
0
 /// <summary>
 /// updates this game object
 /// </summary>
 public virtual void Update()
 {
     Touching = false;
     if (Velocity.X != 0 || Velocity.Y != 0)
     {
         for (int i = 0; i < Room.GameObjectList.Count; i++)
         {
             GameObject obj = Room.GameObjectList[i];
             if (obj.GetType().IsEquivalentTo(PEngine.GetTypeFromName("obj_block")))
             {
                 //feel like there should be a better way to do collisions but idk that way
                 Rectangle targetRect = obj.GetHitbox();
                 targetRect.Location += obj.Position.ToPoint();
                 Rectangle fromRect = PlatformerMath.AddVectorToRect(GetHitbox(), Position, PlatformerMath.VectorCeil(new Vector2(0, Velocity.Y)));
                 while (PlatformerMath.RectangleInRectangle(fromRect, targetRect))
                 {
                     Touching = true;
                     if (Math.Abs(Velocity.Y) < CollisionPrecision)
                     {
                         Velocity.Y = 0;
                         break;
                     }
                     else
                     {
                         Velocity.Y -= Math.Sign(Velocity.Y) * CollisionPrecision;
                         fromRect    = PlatformerMath.AddVectorToRect(GetHitbox(), Position, PlatformerMath.VectorCeil(new Vector2(0, Velocity.Y)));
                     }
                 }
                 fromRect = PlatformerMath.AddVectorToRect(GetHitbox(), Position, PlatformerMath.VectorCeil(new Vector2(Velocity.X, 0)));
                 while (PlatformerMath.RectangleInRectangle(fromRect, targetRect))
                 {
                     Touching = true;
                     if (Math.Abs(Velocity.X) < CollisionPrecision)
                     {
                         Velocity.X = 0;
                         break;
                     }
                     else
                     {
                         Velocity.X -= Math.Sign(Velocity.X) * CollisionPrecision;
                         fromRect    = PlatformerMath.AddVectorToRect(GetHitbox(), Position, PlatformerMath.VectorCeil(new Vector2(Velocity.X, 0)));
                     }
                 }
                 fromRect = PlatformerMath.AddVectorToRect(GetHitbox(), Position, PlatformerMath.VectorCeil(Velocity));
                 while (PlatformerMath.RectangleInRectangle(fromRect, targetRect))
                 {
                     Touching = true;
                     if (Math.Abs(Velocity.X) < CollisionPrecision)
                     {
                         Velocity.X = 0;
                         break;
                     }
                     else
                     {
                         Velocity.X -= Math.Sign(Velocity.X) * CollisionPrecision;
                     }
                     if (Math.Abs(Velocity.Y) < CollisionPrecision)
                     {
                         Velocity.Y = 0;
                         break;
                     }
                     else
                     {
                         Velocity.Y -= Math.Sign(Velocity.Y) * CollisionPrecision;
                     }
                     fromRect = PlatformerMath.AddVectorToRect(GetHitbox(), Position, PlatformerMath.VectorCeil(Velocity));
                 }
             }
         }
     }
     Position += Velocity;
     Sprite?.Update();
 }
Exemplo n.º 6
0
        /// <summary>
        /// loads the room from a file
        /// </summary>
        /// <param name="filename">the filename with the room data</param>
        public Room Load(string filename)
        {
            CurrentFileName = filename;
            //get the json
            string json = File.ReadAllText(filename, Encoding.UTF8);
            //turn it into an internal json object
            JObject obj = JObject.Parse(json);

            //width
            Width = (int)obj.GetValue("width").ToObject(typeof(int));
            //height
            Height = (int)obj.GetValue("height").ToObject(typeof(int));
            //physics
            if (obj.ContainsKey("physics"))
            {
                JObject physicsObj = (JObject)obj.GetValue("physics").ToObject(typeof(JObject));
                float   gravityX   = (float)physicsObj.GetValue("gravityx").ToObject(typeof(float));
                float   gravityY   = (float)physicsObj.GetValue("gravityy").ToObject(typeof(float));
                Physics         = new PhysicsSim();
                Physics.Gravity = new Vector2(gravityX, gravityY);
            }
            //background
            if (obj.ContainsKey("background"))
            {
                JObject backObj        = (JObject)obj.GetValue("background").ToObject(typeof(JObject));
                string  backgroundType = (string)backObj.GetValue("type").ToObject(typeof(string));
                if (backgroundType.Equals("static"))
                {
                    string backAssetName = (string)backObj.GetValue("name").ToObject(typeof(string));
                    Background = new StaticBackground(this, backAssetName);
                }
                else if (backgroundType.Equals("parallax"))
                {
                    JArray   backAssetNames = (JArray)backObj.GetValue("names").ToObject(typeof(JArray));
                    string   targetName     = (string)backObj.GetValue("target").ToObject(typeof(string));
                    string[] names          = new string[backAssetNames.Count];
                    float[]  speeds         = new float[backAssetNames.Count];
                    for (int i = 0; i < backAssetNames.Count; i++)
                    {
                        JObject backLayer = (JObject)backAssetNames[i].ToObject(typeof(JObject));
                        names[i]  = (string)backLayer.GetValue("name").ToObject(typeof(string));
                        speeds[i] = (float)backLayer.GetValue("speed").ToObject(typeof(float));
                    }
                    Background = new ParallaxBackground(this, names, speeds, targetName);
                }
            }
            //world layers
            JArray layerArray = (JArray)obj.GetValue("layers").ToObject(typeof(JArray));

            foreach (JToken item in layerArray)
            {
                JObject layerObject = (JObject)item.ToObject(typeof(JObject));
                int     layer       = (int)layerObject.GetValue("layer").ToObject(typeof(int));
                JArray  objectArray = (JArray)layerObject.GetValue("objects").ToObject(typeof(JArray));
                foreach (JToken gameObjectToken in objectArray)
                {
                    JObject gameObjectData = (JObject)gameObjectToken.ToObject(typeof(JObject));
                    string  internalName   = (string)gameObjectData.GetValue("name").ToObject(typeof(string));
                    Type    type           = PEngine.GetTypeFromName(internalName);
                    if (type == null)
                    {
                        ConsoleManager.WriteLine("could not find object name \"" + internalName + "\"", "err");
                        continue;
                    }
                    Vector2    position   = new Vector2((int)gameObjectData.GetValue("x").ToObject(typeof(int)), (int)gameObjectData.GetValue("y").ToObject(typeof(int)));
                    GameObject gameObject = (GameObject)type.GetConstructor(new Type[] { typeof(Room), typeof(Vector2) }).Invoke(new object[] { this, position });
                    gameObject.Sprite.LayerData.Layer = layer;
                    GameObjectList.Add(gameObject);
                }
                JArray tileArray = (JArray)layerObject.GetValue("tiles").ToObject(typeof(JArray));
                foreach (JToken tileToken in tileArray)
                {
                    JObject tileData     = (JObject)tileToken.ToObject(typeof(JObject));
                    string  internalName = (string)tileData.GetValue("name").ToObject(typeof(string));
                    Type    type         = PEngine.GetTypeFromName(internalName);
                    if (type == null)
                    {
                        ConsoleManager.WriteLine("could not find tile name \"" + internalName + "\"", "err");
                        continue;
                    }
                    Vector2  position = new Vector2((int)tileData.GetValue("x").ToObject(typeof(int)), (int)tileData.GetValue("y").ToObject(typeof(int)));
                    GameTile tile     = (GameTile)type.GetConstructor(new Type[] { typeof(Room), typeof(Vector2) }).Invoke(new object[] { this, position });
                    tile.Sprite.LayerData.Layer = layer;
                    GameTileList.Add(tile);
                }
            }
            LoadAssets(Engine.Assets);
            return(this);
        }
Exemplo n.º 7
0
        /// <summary>
        /// loads the room from a file
        /// </summary>
        /// <param name="filename">the filename with the room data</param>
        public Room Load(string filename)
        {
            //get the json
            string json = File.ReadAllText(filename, Encoding.UTF8);
            //turn it into an internal json object
            JObject obj = JObject.Parse(json);

            //width
            Width = (int)obj.GetValue("width").ToObject(typeof(int));
            //height
            Height = (int)obj.GetValue("height").ToObject(typeof(int));
            //physics
            if (obj.ContainsKey("physics"))
            {
                JObject physicsObj = (JObject)obj.GetValue("physics").ToObject(typeof(JObject));
                float   gravityX   = (float)physicsObj.GetValue("gravityx").ToObject(typeof(float));
                float   gravityY   = (float)physicsObj.GetValue("gravityy").ToObject(typeof(float));
                Physics         = new PhysicsSim();
                Physics.Gravity = new Vector2(gravityX, gravityY);
            }
            //world layers
            JArray layerArray = (JArray)obj.GetValue("layers").ToObject(typeof(JArray));

            foreach (JToken item in layerArray)
            {
                JObject layerObject = (JObject)item.ToObject(typeof(JObject));
                int     layer       = (int)layerObject.GetValue("layer").ToObject(typeof(int));
                JArray  objectArray = (JArray)layerObject.GetValue("objects").ToObject(typeof(JArray));
                foreach (JToken gameObjectToken in objectArray)
                {
                    JObject gameObjectData = (JObject)gameObjectToken.ToObject(typeof(JObject));
                    string  internalName   = (string)gameObjectData.GetValue("name").ToObject(typeof(string));
                    Type    type           = PEngine.GetTypeFromName(internalName);
                    if (type == null)
                    {
                        ConsoleManager.WriteLine("could not find object name \"" + internalName + "\"", "err");
                        continue;
                    }
                    Vector2    position   = new Vector2((int)gameObjectData.GetValue("x").ToObject(typeof(int)), (int)gameObjectData.GetValue("y").ToObject(typeof(int)));
                    GameObject gameObject = (GameObject)type.GetConstructor(new Type[] { typeof(Room), typeof(Vector2) }).Invoke(new object[] { this, position });
                    gameObject.Sprite.LayerData.Layer = layer;
                    GameObjectList.Add(gameObject);
                }
                JArray tileArray = (JArray)layerObject.GetValue("tiles").ToObject(typeof(JArray));
                foreach (JToken tileToken in tileArray)
                {
                    JObject tileData     = (JObject)tileToken.ToObject(typeof(JObject));
                    string  internalName = (string)tileData.GetValue("name").ToObject(typeof(string));
                    Type    type         = PEngine.GetTypeFromName(internalName);
                    if (type == null)
                    {
                        ConsoleManager.WriteLine("could not find tile name \"" + internalName + "\"", "err");
                        continue;
                    }
                    Vector2  position = new Vector2((int)tileData.GetValue("x").ToObject(typeof(int)), (int)tileData.GetValue("y").ToObject(typeof(int)));
                    GameTile tile     = (GameTile)type.GetConstructor(new Type[] { typeof(Room), typeof(Vector2) }).Invoke(new object[] { this, position });
                    tile.Sprite.LayerData.Layer = layer;
                    GameTileList.Add(tile);
                }
            }
            LoadAssets(Engine.Assets);
            return(this);
        }