示例#1
0
 public Camera(Game game, Scene scene)
     : base(game)
 {
     this.scene = scene;
     Initialize();
     Position = Vector2.Zero;
 }
 public Transitioner(Game game, Scene scene)
     : base(game)
 {
     this.scene = scene;
     Initialize();
     blank = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
     blank.SetData(new[] { Color.Tomato });
 }
        public SelectionManager(Game game, Scene scene)
            : base(game)
        {
            this.scene = scene;

            //font = Game.Content.Load<SpriteFont>("fonts\\debugfont");
            //elementEmblem = Game.Content.Load<Texture2D>("icons\\elementemblem");
            //selectionEmblem = Game.Content.Load<Texture2D>("icons\\selectionemblem");
            //backgroundEmblem = Game.Content.Load<Texture2D>("icons\\backgroundemblem");
            //foregroundEmblem = Game.Content.Load<Texture2D>("icons\\foregroundemblem");

            System.Windows.Forms.Application.EnableVisualStyles();
            this.form = new Editor1.FormProperties(Game, scene);
            this.form.Show();

            Initialize();
        }
        public Element ElementFromXml(XmlTextReader reader, Scene scene)
        {
            thereIsEnergy = false;
            Element e = null;
            //switch (reader.Name)
            //{
                //case "Platform":
                //    e = new Platform(Game, scene, Vector2.Zero, Vector2.One);
                //    break;
                //case "Box":
                //    e = new Box(Game, scene, Vector2.Zero);
                //    break;
                //case "Container":
                //    e = new Container(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("FixedRotation"))
                //        ((Container)e).FixedRotation = bool.Parse(reader.Value);
                //    break;
                //case "Radio":
                //    e = new Radio(Game, scene, Vector2.Zero);
                //    break;
                //case "Minecart":
                //    e = new Minecart(Game, scene, Vector2.Zero);
                //    break;
                //case "Drop":
                //    e = new Drop(Game, scene, Vector2.Zero);
                //    break;
                //case "Stomper":
                //    e = new Stomper(Game, scene, Vector2.Zero);
                //    break;
                //case "Battery":
                //    e = new Battery(Game, scene, Vector2.Zero);
                //    break;
                //case "CrateGenerator":
                //    e = new CrateGenerator(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("CratesNumber"))
                //        ((CrateGenerator)e).CratesNumber = int.Parse(reader.Value);
                //    break;
                //case "Crane":
                //    e = new Crane(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("LeftShift"))
                //        ((Crane)e).LeftShift = float.Parse(reader.Value, CultureInfo.InvariantCulture);
                //    if (reader.MoveToAttribute("RightShift"))
                //        ((Crane)e).RightShift = float.Parse(reader.Value, CultureInfo.InvariantCulture);
                //    if (reader.MoveToAttribute("UpShift"))
                //        ((Crane)e).UpShift = float.Parse(reader.Value, CultureInfo.InvariantCulture);
                //    if (reader.MoveToAttribute("DownShift"))
                //        ((Crane)e).DownShift = float.Parse(reader.Value, CultureInfo.InvariantCulture);
                //    break;
                //case "Steam":
                //    e = new Steam(Game, scene, Vector2.Zero);
                //    break;
                //case "Spikes":
                //    e = new Spikes(Game, scene, Vector2.Zero);
                //    break;
                //case "Door":
                //    e = new Door(Game, scene, Vector2.Zero);
                //    Door door = (Door)e;
                //    if (reader.MoveToAttribute("Active"))
                //        door.Active = reader.Value == "True";
                //    door.UpdatePositions();
                //    break;
                //case "Crate":
                //    e = new Crate(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("Color"))
                //        ((Crate)e).Color = reader.Value;
                //    break;
                //case "Computer":
                //    e = new Computer(Game, scene, Vector2.Zero);
                //    break;
                //case "Lamp":
                //    e = new Lamp(Game, scene, Vector2.Zero);
                //    break;
                //case "Alarm":
                //    e = new Alarm(Game, scene, Vector2.Zero);
                //    break;
                //case "Closet":
                //    e = new Closet(Game, scene, Vector2.Zero);
                //    break;
                //case "Chandelier":
                //    e = new Chandelier(Game, scene, Vector2.Zero);
                //    break;
                //case "ExperimentalTube":
                //    e = new ExperimentalTube(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("OtherTubeId"))
                //        ((ExperimentalTube)e).OtherTubeId = reader.Value;
                //    break;
                //case "StoryTelling":
                //    e = new StoryTelling(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("Text"))
                //        ((StoryTelling)e).Text = reader.Value;
                //    break;
                //case "Hint":
                //    e = new Hint(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("Text"))
                //        ((Hint)e).Text = reader.Value;
                //    break;
                //case "ImageHint":
                //    e = new ImageHint(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("Scale"))
                //        ((ImageHint)e).Scale = float.Parse(reader.Value);
                //    if (reader.MoveToAttribute("TextureName"))
                //        ((ImageHint)e).TextureName = reader.Value;
                //    break;
                //case "Forklift":
                //    e = new Forklift(Game, scene, Vector2.Zero);
                //    break;
                //case "SecurityCamera":
                //    e = new SecurityCamera(Game, scene, Vector2.Zero);
                //    break;
                //case "Elevator":
                //    e = new Elevator(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("InitialPosition"))
                //        ((Elevator)e).InitialPosition = PositionFromString(reader.Value);
                //    if (reader.MoveToAttribute("FinalPosition"))
                //        ((Elevator)e).FinalPosition = PositionFromString(reader.Value);
                //    break;
                //case "MovingPlatform":
                //    e = new MovingPlatform(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("InitialPosition"))
                //        ((MovingPlatform)e).InitialPosition = PositionFromString(reader.Value);
                //    if (reader.MoveToAttribute("FinalPosition"))
                //        ((MovingPlatform)e).FinalPosition = PositionFromString(reader.Value);
                //    break;
                //case "CircularSaw":
                //    e = new CircularSaw(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("InitialPosition"))
                //        ((CircularSaw)e).InitialPosition = PositionFromString(reader.Value);
                //    if (reader.MoveToAttribute("FinalPosition"))
                //        ((CircularSaw)e).FinalPosition = PositionFromString(reader.Value);
                //    break;
                //case "LaserBarrier":
                //    e = new LaserBarrier(Game, scene, Vector2.Zero);
                //    break;
                //case "Checkpoint":
                //    e = new Checkpoint(Game, scene, Vector2.Zero);
                //    break;
                //case "Endpoint":
                //    e = new Endpoint(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("NextLevel"))
                //        ((Endpoint)e).NextLevel = reader.Value;
                //    if (reader.MoveToAttribute("AlsoEnergy"))
                //        ((Endpoint)e).AlsoEnergy = Boolean.Parse(reader.Value);
                //    break;
                //case "CameraScale":
                //    e = new CameraScale(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("ScaleTarget"))
                //        ((CameraScale)e).ScaleTarget = float.Parse(reader.Value, CultureInfo.InvariantCulture);
                //    break;
                //case "Sound":
                //    e = new Sound(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("SoundName"))
                //        ((Sound)e).SoundName = reader.Value;
                //    if (reader.MoveToAttribute("Volume"))
                //        ((Sound)e).Volume = float.Parse(reader.Value, CultureInfo.InvariantCulture);
                //    break;
                //case "PressurePlate":
                //    e = new PressurePlate(Game, scene, Vector2.Zero);
                //    break;
                //case "ElectricityBox":
                //    e = new ElectricityBox(Game, scene, Vector2.Zero);
                //    break;
                //case "Writings":
                //    e = new Writings(Game, scene, Vector2.Zero);
                //    break;
                //case "Lever":
                //    e = new Lever(Game, scene, Vector2.Zero);
                //    break;
                //case "Switch":
                //    e = new Switch(Game, scene, Vector2.Zero);
                //    break;
                //case "Socket":
                //    e = new Socket(Game, scene, Vector2.Zero);
                //    if (reader.MoveToAttribute("OtherSocketId"))
                //        ((Socket)e).OtherSocketId = reader.Value;
                //    break;
                //case "Ladder":
                //    int stepsNumber = 1;
                //    if (reader.MoveToAttribute("StepsNumber"))
                //        stepsNumber = int.Parse(reader.Value);
                //    e = new Ladder(Game, scene, stepsNumber, Vector2.Zero);
                //    break;
                //case "GlidePlatform":
                //    int stepsNumber2 = 1;
                //    if (reader.MoveToAttribute("StepsNumber"))
                //        stepsNumber2 = int.Parse(reader.Value);
                //    e = new GlidePlatform(Game, scene, Vector2.Zero, stepsNumber2);
                //    if (reader.MoveToAttribute("Velocity"))
                //        ((GlidePlatform)e).Velocity = float.Parse(reader.Value, CultureInfo.InvariantCulture);
                //    break;
                //case "TrainTrack":
                //    int stepsNumber3 = 1;
                //    if (reader.MoveToAttribute("StepsNumber"))
                //        stepsNumber3 = int.Parse(reader.Value);
                //    e = new TrainTrack(Game, scene, Vector2.Zero, stepsNumber3);
                //    break;
                //case "Energy":
                //    e = new Energy(Game, scene, Vector2.Zero);
                //    scene.Camera.Target = e;
                //    scene.InputManager.Target = (Energy)e;
                //    thereIsEnergy = true;
                //    break;
                //case "Character":
                //    e = new Character(Game, scene, Vector2.Zero);
                //    scene.Camera.Target = e;
                //    scene.InputManager.Target = (Character)e;
                //    break;
                //case "Stone":
                //    e = new Stone(Game, scene, Vector2.Zero);
                //    break;
                //case "Torch":
                //    e = new Torch(Game, scene, Vector2.Zero);
                //    break;
                //case "ConveyorBelt":
                //    float? angularSpeed = null;
                //    int? linksNumber = null;
                //    int? rotorsNumber = null;
                //    float? linkWidth = null;
                //    float? linkHeight = null;

                //    if (reader.MoveToAttribute("AngularSpeed"))
                //        angularSpeed = float.Parse(reader.Value, CultureInfo.InvariantCulture);
                //    if (reader.MoveToAttribute("LinksNumber"))
                //        linksNumber = int.Parse(reader.Value);
                //    if (reader.MoveToAttribute("RotorsNumber"))
                //        rotorsNumber = int.Parse(reader.Value);
                //    if (reader.MoveToAttribute("LinkWidth"))
                //        linkWidth = float.Parse(reader.Value, CultureInfo.InvariantCulture);
                //    if (reader.MoveToAttribute("LinkHeight"))
                //        linkHeight = float.Parse(reader.Value, CultureInfo.InvariantCulture);

                //    e = new ConveyorBelt(Game, scene, Vector2.Zero, angularSpeed, linksNumber, rotorsNumber, linkWidth, linkHeight);
                //    break;
                //case "ImpulsePlatform":
                //    int stepsNumber4 = 1;
                //    if (reader.MoveToAttribute("StepsNumber"))
                //        stepsNumber4 = int.Parse(reader.Value);
                //    e = new ImpulsePlatform(Game, scene, Vector2.Zero, stepsNumber4);
                //    if (reader.MoveToAttribute("Acceleration"))
                //        ((ImpulsePlatform)e).Acceleration = float.Parse(reader.Value, CultureInfo.InvariantCulture);
                //    break;
                //default: ToDo
                    return null;
            //}

            if (reader.MoveToAttribute("Id"))
                e.Id = reader.Value;
            if (reader.MoveToAttribute("Position"))
                e.Position = PositionFromString(reader.Value);
            if (reader.MoveToAttribute("Width"))
                e.Width = float.Parse(reader.Value, CultureInfo.InvariantCulture);
            if (reader.MoveToAttribute("Height"))
                e.Height = float.Parse(reader.Value, CultureInfo.InvariantCulture);
            if (reader.MoveToAttribute("Rotation"))
                e.Rotation = float.Parse(reader.Value, CultureInfo.InvariantCulture);

            if (e is IActivable && reader.MoveToAttribute("Active"))
                ((IActivable)e).Active = reader.Value == "True";

            if (e is Editor.Elements.Activator && reader.MoveToAttribute("ActivableElementId"))
                ((Editor.Elements.Activator)e).ActivableElementId = reader.Value;

            return e;
        }
        public void Update(GameTime gameTime, Scene scene)
        {
            KeyboardState keybState = Keyboard.GetState();

            if (Game.IsActive)
            {
                if (keybState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.LeftControl) && keybState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.S))
                {
                    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                    saveFileDialog1.Filter = "XML file|*.xml";
                    saveFileDialog1.Title = "Save an XML File";
                    DialogResult dr = saveFileDialog1.ShowDialog();
                    if (dr == DialogResult.OK)
                        System.IO.File.WriteAllText(saveFileDialog1.FileName, SceneToXml(scene));
                }
                else if (keybState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.LeftControl) && keybState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.O))
                {
                    OpenFileDialog openFileDialog1 = new OpenFileDialog();
                    openFileDialog1.Filter = "XML file|*.xml";
                    openFileDialog1.Title = "Open an XML File";
                    DialogResult dr = openFileDialog1.ShowDialog();
                    if (dr == DialogResult.OK)
                    {
                        scene.Clean();
                        SceneFromXml(openFileDialog1.FileName, scene);
                        Vector2 characterPosition = Vector2.Zero;
                        foreach (Element i in scene.Elements)
                            if (i is Character)
                                characterPosition = i.Position;
                        scene.Camera.Position = characterPosition;
                    }
                }
                else if (keybState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.LeftControl) && keybState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.N))
                {
                    if (MessageBox.Show("Are you sure you want to clean the scene?", "New scene", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        scene.Clean();
                    }
                }
            }
        }
        public String SceneToXml(Scene scene)
        {
            String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
            xml += "<Scene>\n";
            xml += "    <Backgrounds>\n";
            //foreach (Background i in scene.Backgrounds)
            //{
            //    xml += "        " + ElementToXml(i) + "\n";
            //}
            xml += "    </Backgrounds>\n";
            xml += "    <Elements>\n";
            //foreach (Element i in scene.Elements)
            //{
            //    if (i as Platform != null)
            //        xml += "        " + ElementToXml((Platform)i) + "\n";
            //    else if (i as Box != null)
            //        xml += "        " + ElementToXml((Box)i) + "\n";
            //    else if (i as Stomper != null)
            //        xml += "        " + ElementToXml((Stomper)i) + "\n";
            //    else if (i as Minecart != null)
            //        xml += "        " + ElementToXml((Minecart)i) + "\n";
            //    else if (i as Drop != null)
            //        xml += "        " + ElementToXml((Drop)i) + "\n";
            //    else if (i as Battery != null)
            //        xml += "        " + ElementToXml((Battery)i) + "\n";
            //    else if (i as Spikes != null)
            //        xml += "        " + ElementToXml((Spikes)i) + "\n";
            //    else if (i as Steam != null)
            //        xml += "        " + ElementToXml((Steam)i) + "\n";
            //    else if (i as Door != null)
            //        xml += "        " + ElementToXml((Door)i) + "\n";
            //    else if (i as Crate != null)
            //        xml += "        " + ElementToXml((Crate)i) + "\n";
            //    else if (i as CrateGenerator != null)
            //        xml += "        " + ElementToXml((CrateGenerator)i) + "\n";
            //    else if (i as Computer != null)
            //        xml += "        " + ElementToXml((Computer)i) + "\n";
            //    else if (i as SecurityCamera != null)
            //        xml += "        " + ElementToXml((SecurityCamera)i) + "\n";
            //    else if (i as Lamp != null)
            //        xml += "        " + ElementToXml((Lamp)i) + "\n";
            //    else if (i as Hint != null)
            //        xml += "        " + ElementToXml((Hint)i) + "\n";
            //    else if (i as ImageHint != null)
            //        xml += "        " + ElementToXml((ImageHint)i) + "\n";
            //    else if (i as StoryTelling != null)
            //        xml += "        " + ElementToXml((StoryTelling)i) + "\n";
            //    else if (i as Writings != null)
            //        xml += "        " + ElementToXml((Writings)i) + "\n";
            //    else if (i as Alarm != null)
            //        xml += "        " + ElementToXml((Alarm)i) + "\n";
            //    else if (i as Chandelier != null)
            //        xml += "        " + ElementToXml((Chandelier)i) + "\n";
            //    else if (i as ExperimentalTube != null)
            //        xml += "        " + ElementToXml((ExperimentalTube)i) + "\n";
            //    else if (i as Stone != null)
            //        xml += "        " + ElementToXml((Stone)i) + "\n";
            //    else if (i as Closet != null)
            //        xml += "        " + ElementToXml((Closet)i) + "\n";
            //    else if (i as Ladder != null)
            //        xml += "        " + ElementToXml((Ladder)i) + "\n";
            //    else if (i as LaserBarrier != null)
            //        xml += "        " + ElementToXml((LaserBarrier)i) + "\n";
            //    else if (i as PressurePlate != null)
            //        xml += "        " + ElementToXml((PressurePlate)i) + "\n";
            //    else if (i as Socket != null)
            //        xml += "        " + ElementToXml((Socket)i) + "\n";
            //    else if (i as Elevator != null)
            //        xml += "        " + ElementToXml((Elevator)i) + "\n";
            //    else if (i as MovingPlatform != null)
            //        xml += "        " + ElementToXml((MovingPlatform)i) + "\n";
            //    else if (i as ElectricityBox != null)
            //        xml += "        " + ElementToXml((ElectricityBox)i) + "\n";
            //    else if (i as Lever != null)
            //        xml += "        " + ElementToXml((Lever)i) + "\n";
            //    else if (i as Switch != null)
            //        xml += "        " + ElementToXml((Switch)i) + "\n";
            //    else if (i as Checkpoint != null)
            //        xml += "        " + ElementToXml((Checkpoint)i) + "\n";
            //    else if (i as Endpoint != null)
            //        xml += "        " + ElementToXml((Endpoint)i) + "\n";
            //    else if (i as Sound != null)
            //        xml += "        " + ElementToXml((Sound)i) + "\n";
            //    else if (i as Radio != null)
            //        xml += "        " + ElementToXml((Radio)i) + "\n";
            //    else if (i as Energy != null)
            //        xml += "        " + ElementToXml((Energy)i) + "\n";
            //    else if (i as Character != null)
            //        xml += "        " + ElementToXml((Character)i) + "\n";
            //    else if (i as Forklift != null)
            //        xml += "        " + ElementToXml((Forklift)i) + "\n";
            //    else if (i as ConveyorBelt != null)
            //        xml += "        " + ElementToXml((ConveyorBelt)i) + "\n";
            //    else if (i as ImpulsePlatform != null)
            //        xml += "        " + ElementToXml((ImpulsePlatform)i) + "\n";
            //    else if (i as GlidePlatform != null)
            //        xml += "        " + ElementToXml((GlidePlatform)i) + "\n";
            //    else if (i as TrainTrack != null)
            //        xml += "        " + ElementToXml((TrainTrack)i) + "\n";
            //    else if (i as Torch != null)
            //        xml += "        " + ElementToXml((Torch)i) + "\n";
            //    else if (i as Crane != null)
            //        xml += "        " + ElementToXml((Crane)i) + "\n";
            //    else if (i as CameraScale != null)
            //        xml += "        " + ElementToXml((CameraScale)i) + "\n";
            //    else if (i as Container != null)
            //        xml += "        " + ElementToXml((Container)i) + "\n";
            //    else if (i as CircularSaw != null)
            //        xml += "        " + ElementToXml((CircularSaw)i) + "\n";
            //    else
            //        throw new NotImplementedException(i.GetType().Name + " is still pendent to be converted into XML");
            //}
            xml += "    </Elements>\n";
            xml += "    <Foregrounds>\n";
            //foreach (Foreground i in scene.Foregrounds)
            //{
            //    xml += "        " + ElementToXml(i) + "\n";
            //}
            xml += "    </Foregrounds>\n";
            xml += "</Scene>\n";

            return xml;
        }
        public void SceneFromXml(String filename, Scene scene)
        {
            try
            {
                XmlTextReader reader = new XmlTextReader(filename);
                while (reader.Read())
                {
                    // Once we find the Backgrounds tag, we start a particular loop for all of them.
                    //if (reader.NodeType == XmlNodeType.Element && reader.Name == "Backgrounds")
                    //{
                    //    // We'll stay in this local loop until we find the end of the Backgrounds tag.
                    //    while (reader.Read() && (reader.NodeType != XmlNodeType.EndElement || reader.Name != "Backgrounds"))
                    //    {
                    //        if (reader.NodeType == XmlNodeType.Element)
                    //            scene.Backgrounds.Add(BackgroundFromXml(reader, scene));
                    //    }
                    //}     ToDo

                    // Once we find the Foregrounds tag, we start a particular loop for all of them.
                    //if (reader.NodeType == XmlNodeType.Element && reader.Name == "Foregrounds")
                    //{
                    //    // We'll stay in this local loop until we find the end of the Foregrouds tag.
                    //    while (reader.Read() && (reader.NodeType != XmlNodeType.EndElement || reader.Name != "Foregrounds"))
                    //    {
                    //        if (reader.NodeType == XmlNodeType.Element)
                    //            scene.Foregrounds.Add(ForegroundFromXml(reader, scene));
                    //    }
                    //}

                    // Once we find the Elements tag, we start a particular loop for all of them.
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "Elements")
                    {
                        // We'll stay in this local loop until we find the end of the Elements tag.
                        while (reader.Read() && (reader.NodeType != XmlNodeType.EndElement || reader.Name != "Elements"))
                        {
                            if (reader.NodeType == XmlNodeType.Element)
                            {
                                Element e = ElementFromXml(reader, scene);
                                if (e != null)
                                    scene.Elements.Add(e);
                            }
                        }

                        //if (thereIsEnergy)
                        //{
                        //    foreach (Element ele in scene.Elements)
                        //        if (ele is Energy)
                        //        {
                        //            scene.Camera.Target = ele;
                        //            scene.InputManager.Target = (Energy)ele;
                        //        }
                        //        else if (ele is Character)
                        //            ((Character)ele).State = new ComaCharacterState(scene, (Character)ele, false);
                        //}
                        //else
                        //{
                        //    Character previous = null;
                        //    foreach (Element ele in scene.Elements)
                        //    {
                        //        if (ele is Character && !(ele is Energy))
                        //        {
                        //            if (previous != null)
                        //                ((Character)previous).State = new ComaCharacterState(scene, (Character)previous, false);
                        //            previous = (Character)ele;
                        //        }
                        //    }
                        //}
                    }
                }

                reader.Close();

                LastLevel = System.IO.Path.GetFileNameWithoutExtension(filename);
                if (Levels.Contains(LastLevel))
                    System.IO.File.WriteAllText(@"Content\levels\lastlevel", LastLevel);
                else
                    System.IO.File.WriteAllText(@"Content\levels\lastlevel", "level1");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "\n\n" + e.StackTrace, "Error while loading the level", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }