示例#1
0
        public List <WorldItem> LoadWorldItemsFromJArray(JArray list, float drawLayer)
        {
            List <WorldItem> worldItemList = new List <WorldItem>();

            foreach (JToken token in list)
            {
                JObject   itemObject   = (JObject)token.ToObject(typeof(JObject));
                int       x            = (int)itemObject.GetValue("x").ToObject(typeof(int));
                int       y            = (int)itemObject.GetValue("y").ToObject(typeof(int));
                string    internalName = (string)itemObject.GetValue("name").ToObject(typeof(string));
                WorldItem item         = new WorldItem(UIManager, GetWorldItemTypeFromName(internalName), new Vector2(x, y), drawLayer);
                worldItemList.Add(item);
            }
            return(worldItemList);
        }
示例#2
0
        public void SaveLevel(string filename)
        {
            LevelElement levelElement = (LevelElement)UIManager.GetUIElement("level");
            JObject      levelObject  = new JObject();

            levelObject.Add("width", JToken.FromObject((int)levelElement.LevelSize.X));
            levelObject.Add("height", JToken.FromObject((int)levelElement.LevelSize.Y));
            if (levelElement.Gravity.X != 0 || levelElement.Gravity.Y != 0)
            {
                JObject physicsObject = new JObject();
                physicsObject.Add("gravityx", JToken.FromObject(levelElement.Gravity.X));
                physicsObject.Add("gravityy", JToken.FromObject(levelElement.Gravity.Y));
                levelObject.Add("physics", JToken.FromObject(physicsObject));
            }
            JArray layerArray = new JArray();

            foreach (KeyValuePair <int, WorldLayer> pair in WorldLayers)
            {
                WorldLayer worldLayer  = pair.Value;
                JObject    layerObject = new JObject();
                layerObject.Add("layer", JToken.FromObject(worldLayer.Layer));
                JArray objectArray = new JArray();
                JArray tileArray   = new JArray();
                for (int j = 0; j < worldLayer.WorldItems.Count; j++)
                {
                    WorldItem item       = worldLayer.WorldItems[j];
                    JObject   itemObject = new JObject();
                    itemObject.Add("name", JToken.FromObject(item.ItemType.InternalName));
                    itemObject.Add("x", JToken.FromObject((int)item.Position.X));
                    itemObject.Add("y", JToken.FromObject((int)item.Position.Y));
                    if (item.ItemType.IsTile)
                    {
                        tileArray.Add(JToken.FromObject(itemObject));
                    }
                    else
                    {
                        objectArray.Add(JToken.FromObject(itemObject));
                    }
                }
                layerObject.Add("objects", JToken.FromObject(objectArray));
                layerObject.Add("tiles", JToken.FromObject(tileArray));
                layerArray.Add(JToken.FromObject(layerObject));
            }
            levelObject.Add("layers", JToken.FromObject(layerArray));
            string json = levelObject.ToString();

            File.WriteAllText(filename, json);
        }
示例#3
0
        public override void MousePressed(MouseState mouseState, Vector2 offset)
        {
            PlatformerEditor actualGame = (PlatformerEditor)UIManager.Game;

            if (mouseState.MiddlePressed())
            {
                PanIsPressed = true;
            }
            else if (mouseState.LeftPressed())
            {
                Vector2 mousePos = new Vector2(mouseState.X, mouseState.Y) - offset - Position - SoftOffset;
                if (actualGame.CurrentWorldLayer != null && actualGame.CurrentWorldItemType != null)
                {
                    WorldItem item = new WorldItem(UIManager, actualGame.CurrentWorldItemType, SnapPosition(mousePos), actualGame.CurrentWorldLayer.DrawLayer);
                    actualGame.CurrentWorldLayer.WorldItems.Add(item);
                }
                else
                {
                    PanIsPressed = true;
                }
            }
            else if (mouseState.RightPressed())
            {
                if (actualGame.CurrentWorldLayer != null)
                {
                    Vector2 mousePos = GetMousePosition(offset);
                    for (int i = 0; i < actualGame.CurrentWorldLayer.WorldItems.Count; i++)
                    {
                        WorldItem item = actualGame.CurrentWorldLayer.WorldItems[i];
                        if (PlatformerMath.PointInRectangle(new Rectangle(item.Position.ToPoint(), item.Size.ToPoint()), mousePos))
                        {
                            actualGame.CurrentWorldLayer.WorldItems.RemoveAt(i);
                            break;
                        }
                    }
                }
            }
            base.MousePressed(mouseState, offset);
        }
        public override void Draw(SpriteBatch spriteBatch, Vector2 offset)
        {
            PlatformerEditor actualGame = (PlatformerEditor)UIManager.Game;

            spriteBatch.End();
            spriteBatch.GraphicsDevice.SetRenderTarget(Graphics);
            //draw grid
            spriteBatch.Begin(UIManager.SortMode);
            spriteBatch.GraphicsDevice.Clear(Color.Transparent);
            //Vector2 actualOffset = offset + SoftOffset;// + Position;
            Vector2 actualOffset = SoftOffset;

            for (int i = (int)actualOffset.X; i <= actualOffset.X + LevelSize.X; i += (int)Snap.X)
            {
                spriteBatch.DrawLine(new Vector2(i, actualOffset.Y), new Vector2(i, actualOffset.Y + LevelSize.Y), Color.Black);
            }
            for (int i = (int)actualOffset.Y; i <= actualOffset.Y + LevelSize.Y; i += (int)Snap.Y)
            {
                spriteBatch.DrawLine(new Vector2(actualOffset.X, i), new Vector2(actualOffset.X + LevelSize.X, i), Color.Black);
            }
            spriteBatch.End();
            //draw layers
            List <int> layerKeys = new List <int>(actualGame.WorldLayers.Keys); //TODO: this is probably slow. make it not

            layerKeys.Sort((a, b) =>
            {
                return(Math.Sign(b - a));
            });
            for (int i = 0; i < layerKeys.Count; i++)
            {
                WorldLayer worldLayer = actualGame.WorldLayers[layerKeys[i]];
                if (!worldLayer.IsVisible)
                {
                    continue;
                }
                spriteBatch.Begin(UIManager.SortMode);
                for (int j = 0; j < worldLayer.WorldItems.Count; j++)
                {
                    WorldItem item = worldLayer.WorldItems[j];
                    item.Draw(spriteBatch, actualOffset);
                }
                spriteBatch.End();
            }
            //draw mouse item
            if (actualGame.CurrentWorldItemType == null && CurrentWorldItem != null)
            {
                CurrentWorldItem = null;
            }
            else if ((actualGame.CurrentWorldItemType != null && CurrentWorldItem == null) || (actualGame.CurrentWorldItemType != null && CurrentWorldItem.ItemType != actualGame.CurrentWorldItemType))
            {
                CurrentWorldItem = new WorldItem(UIManager, actualGame.CurrentWorldItemType, SnapPosition(GetMousePosition(offset)), 0.6f);
            }
            else if (CurrentWorldItem != null)
            {
                CurrentWorldItem.Position = SnapPosition(GetMousePosition(offset));
            }
            spriteBatch.Begin(UIManager.SortMode);
            CurrentWorldItem?.Draw(spriteBatch, actualOffset);
            spriteBatch.End();
            spriteBatch.GraphicsDevice.SetRenderTarget(null);
            spriteBatch.Begin(UIManager.SortMode);
            spriteBatch.Draw(Graphics, Position + offset, Color.White);
        }