// loads the specified file into the editor
        private List<Entity2D> loadLevel(string fileName, bool loadIDs = true)
        {
            // TODO ugly! this must have to be set with the editor and loaded from xml
            if (GamerManager.getMainPlayer() == null)
            {
                GamerManager.createGamerEntity(PlayerIndex.One, true);
            }
            GamerManager.getMainPlayer().mode = Player.tMode.Arcade;

            //fileName = SB.content.RootDirectory + "/xml/levels/" + fileName + ".xml";
            List<Entity2D> list = new List<Entity2D>();
            if (File.Exists(fileName))
            {
                XDocument xml_doc = XDocument.Load(fileName);

                int id;
                IEnumerable<XElement> nodes;

                if (loadIDs)
                {
                    nodes = xml_doc.Descendants("level");
                    XElement levelNode = nodes.First();
                    if (levelNode.Attributes("nextEntityID").Count() > 0)
                    {
                        Entity2D.NEXT_ENTITY_ID = levelNode.Attribute("nextEntityID").Value.toInt();
                    }

                    if (levelNode.Attributes("BGColor").Count() > 0)
                    {
                        SB.BGColor = levelNode.Attribute("BGColor").Value.toColor();
                    }

                    if (levelNode.Attributes("CameraMode").Count() > 0)
                    {
                        CameraManager.Instance.cameraMode = (CameraManager.tCameraMode)Enum.Parse(typeof(CameraManager.tCameraMode), levelNode.Attribute("CameraMode").Value, true);
                    }
                }

                // read static props
                nodes = xml_doc.Descendants("staticProp");
                foreach (XElement node in nodes)
                {
                    id = -1;
                    if (loadIDs && node.Attributes("id").Count() > 0) id = node.Attribute("id").Value.toInt();
                    RenderableEntity2D re = new RenderableEntity2D("staticProps", node.Attribute("entityName").Value, Vector3.Zero, 0, Color.White, true, id);
                    re.worldMatrix = node.Attribute("worldMatrix").Value.toMatrix();
                    if (re.worldMatrix.AnyNanCoord())
                        continue;

                    if (node.Attributes("color").Count() > 0)
                    {
                        re.color = node.Attribute("color").Value.toColor();
                    }
                    if (node.Attributes("flipH").Count() > 0)
                    {
                        re.flipHorizontal = node.Attribute("flipH").Value.toBool();
                        re.flipVertical = node.Attribute("flipV").Value.toBool();
                    }
                    if (node.Attributes("living").Count() > 0)
                    {
                        re.living = node.Attribute("living").Value.toBool();
                    }
                    if (node.Attributes("livingIntensityMin").Count() > 0)
                    {
                        re.livingIntensityMin = node.Attribute("livingIntensityMin").Value.toFloat();
                        re.livingIntensityMax = node.Attribute("livingIntensityMax").Value.toFloat();
                        re.livingSpeedMin = node.Attribute("livingSpeedMin").Value.toFloat();
                        re.livingSpeedMax = node.Attribute("livingSpeedMax").Value.toFloat();
                    }

                    LevelManager.Instance.addStaticProp(re);
                    re.setInit();
                    list.Add(re);
                }

                // read animated props
                nodes = xml_doc.Descendants("animatedProp");
                foreach (XElement node in nodes)
                {
                    id = -1;
                    if (loadIDs && node.Attributes("id").Count() > 0) id = node.Attribute("id").Value.toInt();
                    AnimatedEntity2D ae = new AnimatedEntity2D("animatedProps", node.Attribute("entityName").Value, Vector3.Zero, 0, Color.White, true, id);
                    ae.worldMatrix = node.Attribute("worldMatrix").Value.toMatrix();
                    if (ae.worldMatrix.AnyNanCoord())
                        continue;
                    if (node.Attributes("color").Count() > 0)
                    {
                        ae.color = node.Attribute("color").Value.toColor();
                    }
                    if (node.Attributes("flipH").Count() > 0)
                    {
                        ae.flipHorizontal = node.Attribute("flipH").Value.toBool();
                        ae.flipVertical = node.Attribute("flipV").Value.toBool();
                    }
                    if (node.Attributes("living").Count() > 0)
                    {
                        ae.living = node.Attribute("living").Value.toBool();
                    }
                    if (node.Attributes("livingIntensityMin").Count() > 0)
                    {
                        ae.livingIntensityMin = node.Attribute("livingIntensityMin").Value.toFloat();
                        ae.livingIntensityMax = node.Attribute("livingIntensityMax").Value.toFloat();
                        ae.livingSpeedMin = node.Attribute("livingSpeedMin").Value.toFloat();
                        ae.livingSpeedMax = node.Attribute("livingSpeedMax").Value.toFloat();
                    }

                    LevelManager.Instance.addAnimatedProp(ae);
                    ae.setInit();
                    list.Add(ae);
                }

                // read enemies
                nodes = xml_doc.Descendants("enemy");
                foreach (XElement node in nodes)
                {
                    id = -1;
                    if (loadIDs && node.Attributes("id").Count() > 0) id = node.Attribute("id").Value.toInt();
                    string name = node.Attribute("entityName").Value;
                    Matrix world = node.Attribute("worldMatrix").Value.toMatrix();
                    if (world.AnyNanCoord())
                        continue;
                    RenderableEntity2D e = (RenderableEntity2D)EnemyManager.Instance.addEnemy(name, world.Translation, id);
                    if (node.Attributes("color").Count() > 0)
                    {
                        e.color = node.Attribute("color").Value.toColor();
                    }
                    if (node.Attributes("flipH").Count() > 0)
                    {
                        e.flipHorizontal = node.Attribute("flipH").Value.toBool();
                        e.flipVertical = node.Attribute("flipV").Value.toBool();
                    }

                    if (node.Attributes("living").Count() > 0)
                    {
                        e.living = node.Attribute("living").Value.toBool();
                    }
                    if (node.Attributes("livingIntensityMin").Count() > 0)
                    {
                        e.livingIntensityMin = node.Attribute("livingIntensityMin").Value.toFloat();
                        e.livingIntensityMax = node.Attribute("livingIntensityMax").Value.toFloat();
                        e.livingSpeedMin = node.Attribute("livingSpeedMin").Value.toFloat();
                        e.livingSpeedMax = node.Attribute("livingSpeedMax").Value.toFloat();
                    }

                    e.setInit();
                    list.Add(e);
                }

                //Enemy spaws zones
                nodes = xml_doc.Descendants("enemySpawnZone");
                foreach (XElement node in nodes)
                {
                    string name = node.Attribute("enemy").Value;
                    Rectangle rect = node.Attribute("zone").Value.toRectangle();
                    int count = node.Attribute("count").Value.toInt();

                    EnemyManager.Instance.addEnemySpawnZone(new EnemySpawnZone(name, rect, count));
                }

                // read level collisions
                nodes = xml_doc.Descendants("levelCollision");
                foreach (XElement node in nodes)
                {
                    Line l = new Line(node.Attribute("p1").Value.toVector2(), node.Attribute("p2").Value.toVector2());
                    LevelManager.Instance.addLevelCollision(l);
                }

                //Camera
                nodes = xml_doc.Descendants("cameraNode");
                foreach (XElement node in nodes)
                {
                    Vector3 position = node.Attribute("position").Value.toVector3();
                    Vector3 target = node.Attribute("target").Value.toVector3();
                    int nodeId = node.Attribute("id").Value.toInt();
                    bool isFirst = node.Attribute("isFirst").Value.toBool();
                    NetworkNode<CameraData> cameraNode = new NetworkNode<CameraData>(new CameraData(target, nodeId, isFirst), position);
                    if (node.Attributes("speed").Count() > 0)
                        cameraNode.value.speed = node.Attribute("speed").Value.toFloat();

                    if (node.Attributes("link").Count() > 0)
                        cameraNode.value.next = node.Attribute("link").Value.toInt();

                    CameraManager.Instance.addNode(cameraNode);
                }
                //link camera nodes
                foreach (NetworkNode<CameraData> cameraNode in CameraManager.Instance.getNodes().getNodes())
                {
                    cameraNode.addLinkedNode(CameraManager.Instance.getNode(cameraNode.value.next));
                }
                CameraManager.Instance.setupCamera();

                // player
                if (GamerManager.getMainPlayer() == null)
                {
                    GamerManager.createGamerEntity(PlayerIndex.One, true);
                }

                nodes = xml_doc.Descendants("player");
                foreach (XElement node in nodes)
                {
                    int playerId = node.Attribute("playerId").Value.toInt();
                    Vector3 pos = node.Attribute("pos").Value.toVector3();
                    GamerManager.getMainPlayer().initPos = pos;
                }

                if (fileName == "Content/xml/levels/mapa.xml")
                {
                    GamerManager.getMainPlayer().renderState = RenderableEntity2D.tRenderState.NoRender;
                }
                else
                {
                    EntityManager.Instance.registerEntity(GamerManager.getMainPlayer());
                    GamerManager.getMainPlayer().renderState = RenderableEntity2D.tRenderState.Render;
                    GamerManager.getMainPlayer().position = GamerManager.getMainPlayer().initPos;
                }
                GamerManager.getMainPlayer().initLevel();

                //particles
                nodes = xml_doc.Descendants("particle");
                foreach (XElement node in nodes)
                {
                    string name = node.Attribute("name").Value;
                    Vector3 position = node.Attribute("position").Value.toVector3();
                    Vector3 direction = node.Attribute("direction").Value.toVector3();
                    Color color = node.Attribute("color").Value.toColor();
                    if(!position.AnyNanCoord() && !direction.AnyNanCoord())
                        ParticleManager.Instance.addParticles(name, position, direction, color);
                }

                //triggers
                //nodes = xml_doc.Descendants("trigger");
                //foreach (XElement node in nodes)
                //{
                //    Vector2 position = node.Attribute("position").Value.toVector2();
                //    Trigger trigger = new Trigger();
                //    trigger.position = position;

                //    IEnumerable<XElement> moreNodes = node.Descendants("condition");
                //    foreach (XElement func in moreNodes)
                //    {
                //        trigger.addFunction(true, func.Attribute("functionName").Value);
                //    }

                //    moreNodes = node.Descendants("consecuence");
                //    foreach (XElement func in moreNodes)
                //    {
                //        trigger.addFunction(false, func.Attribute("functionName").Value);
                //    }

                //    TriggerManager.Instance.addTrigger(trigger);
                //}

                if (loadIDs)
                {
                    // groups
                    nodes = xml_doc.Descendants("group"); // read enemies
                    foreach (XElement node in nodes)
                    {
                        IEnumerable<XElement> ids = node.Descendants("entity");
                        List<int> idList = new List<int>();
                        foreach (XElement entityId in ids)
                        {
                            int entityID = entityId.Attribute("id").Value.toInt();
                            if (EntityManager.Instance.getEntityByID(entityID) != null)
                            {
                                idList.Add(entityID);
                            }
                        }
                        if(idList.Count > 1)
                            LevelManager.Instance.addGroup(idList);
                    }
                }
            }

            return list;
        }
示例#2
0
 private void addCameraLink(NetworkNode<CameraData> src, NetworkNode<CameraData> dst)
 {
     src.addLinkedNode(dst);
 }
        public void loadXMLfake()
        {
            if (cameraNodes.getNodes().Count > 0)
                return;

            NetworkNode<CameraData> first = new NetworkNode<CameraData>(new CameraData(new Vector3(0, 0, 0), 0, true), new Vector3(0, 0, 1400));
            NetworkNode<CameraData> second = new NetworkNode<CameraData>(new CameraData(new Vector3(0, 20000, 0), 1), new Vector3(0, 20000, 1400));
            NetworkNode<CameraData> third = new NetworkNode<CameraData>(new CameraData(new Vector3(10000, 20000, 0), 2), new Vector3(10000, 20000, 1400));

            first.addLinkedNode(second);
            second.addLinkedNode(third);

            cameraNodes.addNode(first);
            cameraNodes.addNode(second);
            cameraNodes.addNode(third);

            currentNode = first;
            nextNode = second;

            Camera2D.position = cameraNodes.getNodes()[0].position;
        }