Example #1
0
        public Room(string file, bool freeroam, ReplayRecorder recorder)
            : this()
        {
            freeroaming = freeroam;
            this.recorder = recorder;

            // Get level name
            levelName = file.Split('\\')[file.Split('\\').Length - 1].Replace(".srl", "");
            custom = true;

            SimpleAES decryptor = new SimpleAES();
            StreamReader levelReader = new StreamReader(file);
            string[] line;

            // Get level id
            levelID = levelReader.ReadLine();

            // Check if level will have a leaderboard
            canViewLeaderboards = decryptor.DecryptString(levelReader.ReadLine()) == "1";

            // Get level theme
            line = decryptor.DecryptString(levelReader.ReadLine()).Split(' ');
            Theme = FindTheme(line[0]);
            wallSet = new Tileset(Game1.tileSet[(int)Theme], 32, 32, 3, 3);

            // Get room dimensions
            line = decryptor.DecryptString(levelReader.ReadLine()).Split(' ');
            roomWidth = int.Parse(line[0]);
            roomHeight = int.Parse(line[1]);

            // Get goal times
            goals = new int[3];
            line = decryptor.DecryptString(levelReader.ReadLine()).Split(' ');
            for (int i = 0; i < 3; i++)
                goals[i] = int.Parse(line[i]);

            // Get objects and tiles
            while (!levelReader.EndOfStream)
            {
                string s = levelReader.ReadLine();
                if (s.Length > 0)
                {
                    line = decryptor.DecryptString(s).Split(' ');
                    ParseObjectOrTile(line, freeroam);
                }
            }
            levelReader.Dispose();

            BuildTiles();

            // Generate zipline poles
            foreach (ZipLine z in ziplines)
                z.SetPoles(this);

            // Get current record for this level
            FindRecord(decryptor);

            UpdateViewBox(false);
        }
Example #2
0
        public virtual void Update(GameTime gameTime)
        {
            // Reset keys
            if (!Keyboard.GetState().IsKeyDown(Settings.controls["Restart"]))
                rcheck = true;
            if (!Keyboard.GetState().IsKeyDown(Settings.controls["Pause"]))
                pcheck = true;
            if (!Keyboard.GetState().IsKeyDown(Settings.controls["Freeroam"]))
                fcheck = true;
            if (!Keyboard.GetState().IsKeyDown(Keys.L))
                lcheck = true;
            if (!Keyboard.GetState().IsKeyDown(Keys.Up))
                upcheck = true;
            if (!Keyboard.GetState().IsKeyDown(Keys.Down))
                downcheck = true;
            if (!Keyboard.GetState().IsKeyDown(Keys.S))
                scheck = true;
            if (!Keyboard.GetState().IsKeyDown(Keys.O))
                ocheck = true;
            if (!Keyboard.GetState().IsKeyDown(Keys.T))
                tcheck = true;

            // Rate level
            if (Keyboard.GetState().IsKeyDown(Keys.T) && tcheck && Game1.online && custom)
            {
                tcheck = false;
                Paused = true;
                int temp;
                if (int.TryParse(Microsoft.VisualBasic.Interaction.InputBox("Enter a number from 1 to 5, 5 being a great level.", "Rate"), out temp))
                {
                    if (temp > 0 && temp <= 5)
                    {
                        try
                        {
                            WebStuff.SendRating(temp, Game1.userName, levelID);
                        }
                        catch (Exception)
                        {
                            System.Windows.Forms.MessageBox.Show("Rating failed to upload. Please try again later.", "Rating Failed");
                        }
                    }
                    else
                        System.Windows.Forms.MessageBox.Show("Your rating must be a number between 1 and 5");
                }
            }

            // Restart when R is pressed
            if (Keyboard.GetState().IsKeyDown(Settings.controls["Restart"]) && rcheck)
            {
                if (custom)
                    Game1.currentRoom = new Room("Content\\rooms\\" + levelName + ".srl", false, new ReplayRecorder());
                else
                    Game1.currentRoom = new Room(Levels.levels[Levels.Index], false, new ReplayRecorder());
            }
            else if (Keyboard.GetState().IsKeyDown(Settings.controls["Freeroam"]) && fcheck)
            {
                if (custom)
                    Game1.currentRoom = new Room("Content\\rooms\\" + levelName + ".srl", true, new ReplayRecorder());
                else
                    Game1.currentRoom = new Room(Levels.levels[Levels.Index], true, new ReplayRecorder());
            }

            // Pause the game when P is pressed
            if (Keyboard.GetState().IsKeyDown(Settings.controls["Pause"]) && pcheck && !viewingLeaderboards)
            {
                pcheck = false;
                Paused = !Paused;
            }

            // Show leaderboards when L is pressed
            if (Keyboard.GetState().IsKeyDown(Keys.L) && lcheck && (Paused || Finished) && Game1.online && canViewLeaderboards)
            {
                lcheck = false;
                if (!viewingLeaderboards)
                {
                    try
                    {
                        viewingLeaderboards = true;
                        leaderboardData = WebStuff.GetScores(levelID, Game1.userName, leaderboardPage * 10);
                        canScrollDown = leaderboardData.Length == 11;
                    }
                    catch (Exception)
                    {
                        System.Windows.Forms.MessageBox.Show("There was a problem connecting to the leaderboards.", "Connection Error");
                        viewingLeaderboards = false;
                    }
                }
                else
                    viewingLeaderboards = false;
            }

            if (viewingLeaderboards)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Up) && upcheck && leaderboardPage > 0)
                {
                    upcheck = false;
                    try
                    {
                        leaderboardData = WebStuff.GetScores(levelID, Game1.userName, (leaderboardPage - 1) * 10);
                    }
                    catch (Exception)
                    {
                        System.Windows.Forms.MessageBox.Show("There was a problem connecting to the leaderboards.", "Connection Error");
                    }
                    leaderboardPage--;
                    canScrollDown = true;
                }
                else if (Keyboard.GetState().IsKeyDown(Keys.Down) && downcheck && canScrollDown)
                {
                    downcheck = false;
                    try
                    {
                        leaderboardData = WebStuff.GetScores(levelID, Game1.userName, (leaderboardPage + 1) * 10);
                    }
                    catch (Exception)
                    {
                        System.Windows.Forms.MessageBox.Show("There was a problem connecting to the leaderboards.", "Connection Error");
                    }
                    leaderboardPage++;
                    canScrollDown = leaderboardData.Length == 11;
                }
            }

            // Update freeroam cam
            if (freeroaming && !Paused)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Left))
                    viewBox.X -= 8;
                if (Keyboard.GetState().IsKeyDown(Keys.Right))
                    viewBox.X += 8;
                if (Keyboard.GetState().IsKeyDown(Keys.Up))
                    viewBox.Y -= 8;
                if (Keyboard.GetState().IsKeyDown(Keys.Down))
                    viewBox.Y += 8;
            }

            if (!Finished)
            {
                if (!Paused)
                {
                    if (recorder.playing)
                        recorder.PlayFrame();
                    else
                        recorder.RecordFrame();

                    // Update booster animations
                    foreach (Booster b in boosters)
                        b.Update();

                    // Find platforms
                    var plats = from Wall f in walls
                                where f is FloatingPlatform
                                select f as FloatingPlatform;
                    foreach (FloatingPlatform f in plats)
                        f.Update();

                    // Update boxes
                    foreach (Box b in boxes)
                        b.Update();

                    // Update character
                    Runner.Update();

                    // Update rocket launchers
                    foreach (RocketLauncher r in launchers)
                    {
                        if (r.explosion != null)
                            r.explosion.Update();
                        if (r.Update())
                        {
                            r.explosion = new Explosion(new Vector2(r.rocket.hitBox.X, r.rocket.hitBox.Y), Color.OrangeRed);
                            r.pause = 0;
                            r.rocket.position.X = -10000;
                            r.rocket.position.Y = -10000;
                            r.rocket.hitBox.X = -10000;
                            r.rocket.hitBox.Y = -10000;
                        }
                    }

                    // Update flamethrowers
                    foreach (Flamethrower f in flamethrowers)
                        f.Update();

                    // Move viewbox to keep up with character
                    UpdateViewBox(freeroaming);

                    // If the runner can be moved, increment the timer
                    if (Runner.controllable)
                    {
                        time += gameTime.ElapsedGameTime.Milliseconds;
                        if (!Game1.finishedSS && Game1.startTotalTime && !recorder.playing)
                            Game1.totalTime += gameTime.ElapsedGameTime.Milliseconds;
                    }
                }
                else
                {
                    Game1.run.Stop();
                    Game1.slide.Stop();

                    if (Keyboard.GetState().IsKeyDown(Keys.O) && ocheck)
                    {
                        ocheck = false;
                        System.Windows.Forms.OpenFileDialog openFD = new System.Windows.Forms.OpenFileDialog();
                        if (!Directory.Exists("Content\\replays"))
                            Directory.CreateDirectory("Content\\replays");
                        openFD.InitialDirectory = "Content\\replays";
                        openFD.Filter = "Replay Files (*.rpl)|*.rpl";
                        if (openFD.ShowDialog() == System.Windows.Forms.DialogResult.OK && File.Exists(openFD.FileName))
                        {
                            ReplayRecorder rec = new ReplayRecorder(openFD.FileName);
                            if (custom)
                                Game1.currentRoom = new Room("Content\\rooms\\" + levelName + ".srl", false, rec);
                            else
                                Game1.currentRoom = new Room(Levels.levels[Levels.Index], false, rec);
                        }
                    }
                }
            }
            else
            {
                Game1.run.Stop();
                Game1.slide.Stop();

                // Fix glitch where you can restart at the same time you hit the finish and achieve a time of 0 seconds
                if (time == 0)
                {
                    if (custom)
                        Game1.currentRoom = new Room("Content\\rooms\\" + levelName + ".srl", false, new ReplayRecorder());
                    else
                        Game1.currentRoom = new Room(Levels.levels[Levels.Index], false, new ReplayRecorder());
                    return;
                }

                if (write && !recorder.playing)
                {
                    write = false;

                    // Get goal beaten, if any
                    if (goals.Length > 3 && time <= goals[3])
                        goalBeaten = 0;
                    else if (time <= goals[0])
                        goalBeaten = 1;
                    else if (time <= goals[1])
                        goalBeaten = 2;
                    else if (time <= goals[2])
                        goalBeaten = 3;

                    StreamWriter writer;
                    SimpleAES encryptor = new SimpleAES();

                    // If record is beaten, save new record
                    if (time < record || record == -1)
                    {
                        StreamReader reader = new StreamReader("Content\\records.txt");
                        writer = new StreamWriter("Content\\recordstemp.txt", false);
                        bool found = false;

                        // Rewrite records file, but only change current level's time
                        while (!reader.EndOfStream)
                        {
                            string line = encryptor.DecryptString(reader.ReadLine());
                            if (line.Split(' ')[0] == levelName)
                            {
                                found = true;
                                writer.WriteLine(encryptor.EncryptToString(levelName + " " + (custom ? "1" : "0") + " " + time.ToString()));
                            }
                            else
                                writer.WriteLine(encryptor.EncryptToString(line));
                        }
                        if (!found)
                            writer.WriteLine(encryptor.EncryptToString(levelName + " " + (custom ? "1" : "0") + " " + time.ToString()));
                        reader.Dispose();
                        writer.Flush();
                        writer.Dispose();
                        File.Delete("Content\\records.txt");
                        File.Move("Content\\recordstemp.txt", "Content\\records.txt");
                    }

                    if (Levels.Index == Levels.levels.Length - 2)
                        Game1.beatGame = true;

                    if (!Game1.finishedSS && Game1.startTotalTime && Levels.Index == Levels.levels.Length - 2 && (Game1.totalTime < Game1.totalRecord || Game1.totalRecord == -1))
                    {
                        Game1.finishedSS = true;
                        StreamReader reader = new StreamReader("Content\\records.txt");
                        writer = new StreamWriter("Content\\recordstemp.txt", false);
                        bool found = false;

                        // Rewrite records file, but only change current level's time
                        while (!reader.EndOfStream)
                        {
                            string line = encryptor.DecryptString(reader.ReadLine());
                            if (line.Split(' ')[0] == "fullgame" && line.Split(' ')[1] == "0")
                            {
                                found = true;
                                writer.WriteLine(encryptor.EncryptToString("fullgame 0 " + Game1.totalTime.ToString()));
                            }
                            else
                                writer.WriteLine(encryptor.EncryptToString(line));
                        }
                        if (!found)
                            writer.WriteLine(encryptor.EncryptToString("fullgame 0 " + Game1.totalTime.ToString()));
                        reader.Dispose();
                        writer.Flush();
                        writer.Dispose();
                        File.Delete("Content\\records.txt");
                        File.Move("Content\\recordstemp.txt", "Content\\records.txt");
                    }
                }

                if (upload && canViewLeaderboards && !recorder.playing)
                {
                    // Upload score to leaderboard
                    upload = false;
                    if (Game1.online)
                    {
                        try
                        {
                            WebStuff.WriteScore(time, Game1.userName, levelID);
                            if (Game1.finishedSS && !custom)
                            {
                                WebStuff.WriteScore(Game1.totalTime, Game1.userName, "fullgame");
                                if (Game1.totalTime < Game1.totalRecord)
                                    Game1.totalRecord = Game1.totalTime;
                            }
                        }
                        catch (Exception)
                        {
                            System.Windows.Forms.MessageBox.Show("Uploading score to leaderboards failed.", "Leaderboard Error");
                        }
                        if (Game1.beatGame && !custom)
                        {
                            Game1.commRecord = -1;
                            StreamReader r = new StreamReader("Content\\records.txt");
                            SimpleAES enc = new SimpleAES();
                            while (!r.EndOfStream)
                            {
                                string s = enc.DecryptString(r.ReadLine());
                                if (s.Split(' ')[1] == "0")
                                {
                                    if (s.Split(' ')[0] != "Level_26_-_Credits" && s.Split(' ')[0] != "fullgame")
                                    {
                                        if (Game1.commRecord == -1)
                                            Game1.commRecord = 0;
                                        Game1.commRecord += int.Parse(s.Split(' ')[2]);
                                    }
                                }
                            }
                            r.Close();
                            r.Dispose();
                        }
                    }
                }

                if (writeNext && !custom && Levels.Index < Levels.levels.Count() - 1 && !recorder.playing)
                {
                    // Add next level to level select if not already unlocked
                    writeNext = false;
                    bool recordFound = false;
                    StreamReader reader = new StreamReader("Content\\records.txt");
                    SimpleAES encryptor = new SimpleAES();
                    string name = Levels.levels[Levels.Index + 1][0];
                    while (!reader.EndOfStream)
                    {
                        string s = encryptor.DecryptString(reader.ReadLine());
                        if (s.Split(' ')[0] == name)
                        {
                            recordFound = true;
                            break;
                        }
                    }
                    reader.Close();
                    reader.Dispose();
                    if (!recordFound)
                    {
                        StreamWriter writer = new StreamWriter("Content\\records.txt", true);
                        writer.WriteLine(encryptor.EncryptToString(name + " 0 -1"));
                        writer.Flush();
                        writer.Dispose();
                    }
                }

                // Check if they want to see replay
                if (Keyboard.GetState().IsKeyDown(Keys.W))
                {
                    recorder.playing = true;
                    recorder.start = true;
                    if (custom)
                        Game1.currentRoom = new Room("Content\\rooms\\" + levelName + ".srl", false, recorder);
                    else
                        Game1.currentRoom = new Room(Levels.levels[Levels.Index], false, recorder);
                }

                // Check if they want to save replay
                if (Keyboard.GetState().IsKeyDown(Keys.S) && scheck)
                {
                    scheck = false;
                    recorder.Save(levelName);
                    recorderSaved = true;
                }

                // Move to next level when enter is pressed, or back to menu if custom level
                if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                {
                    if (!custom)
                    {
                        if (!recorder.playing || !recorder.loaded)
                        {
                            if (Game1.startTotalTime && Levels.Index == Levels.levels.Length - 2)
                                Game1.startTotalTime = false;
                            Levels.Index++;
                            if (Levels.Index == Levels.levels.Count())
                                Game1.currentRoom = new MainMenu(false);
                            else
                            {
                                while (Levels.Index < Levels.levels.Length && Levels.levels[Levels.Index][0] == "")
                                    Levels.Index++;
                                Game1.currentRoom = new Room(Levels.levels[Levels.Index], true, new ReplayRecorder());
                            }
                        }
                    }
                    else
                        Game1.currentRoom = new LevelSelect(1);
                }
            }
        }
Example #3
0
        public Room(string[] lines, bool freeroam, ReplayRecorder recorder)
            : this()
        {
            freeroaming = freeroam;
            this.recorder = recorder;

            SimpleAES decryptor = new SimpleAES();
            string[] line;

            // Get level name
            levelName = lines[0];
            custom = false;

            // Get level id
            levelID = lines[1];

            // Check if level will have a leaderboard
            canViewLeaderboards = decryptor.DecryptString(lines[2]) == "1";

            // Get level theme
            line = decryptor.DecryptString(lines[3]).Split(' ');
            Theme = FindTheme(line[0]);
            wallSet = new Tileset(Game1.tileSet[(int)Theme], 32, 32, 3, 3);

            // Get room dimensions
            line = decryptor.DecryptString(lines[4]).Split(' ');
            roomWidth = int.Parse(line[0]);
            roomHeight = int.Parse(line[1]);

            // Get goal times
            // Find record
            int rec = -1;
            if (!File.Exists("Content\\records.txt"))
                File.Create("Content\\records.txt");
            StreamReader findLastLevel = new StreamReader("Content\\records.txt");
            while (!findLastLevel.EndOfStream)
            {
                string[] level = decryptor.DecryptString(findLastLevel.ReadLine()).Split(' ');
                if (level[0] == levelName && level[1] == "0")
                {
                    rec = int.Parse(level[2]);
                    break;
                }
            }
            findLastLevel.Close();
            findLastLevel.Dispose();

            // Check if record is a gold time
            bool gotGold = false;
            string findIndex = levelName.Split('_')[1];
            int ii = int.Parse(findIndex) - 1;
            string[] g = decryptor.DecryptString(Levels.levels[ii][5]).Split(' ');
            if (rec != -1 && rec < int.Parse(g[0]))
                gotGold = true;

            // If it is, unlock platinum time
            goals = new int[gotGold ? 4 : 3];
            line = decryptor.DecryptString(lines[5]).Split(' ');
            for (int i = 0; i < goals.Length; i++)
                goals[i] = int.Parse(line[i]);

            // Get objects and tiles
            int index = 6;
            while (index < lines.Length)
            {
                line = decryptor.DecryptString(lines[index]).Split(' ');
                ParseObjectOrTile(line, freeroam);
                index++;
            }
            BuildTiles();

            // Generate zipline poles
            foreach (ZipLine z in ziplines)
                z.SetPoles(this);

            // Check to see if level is already in records file. If not, add it.
            // This unlocks the level in level select, since main levels (levels hard coded into the game)
            // are added from the records file.
            bool recordFound = false;
            StreamReader reader = new StreamReader("Content\\records.txt");
            while (!reader.EndOfStream)
            {
                string s = decryptor.DecryptString(reader.ReadLine());
                if (s.Split(' ')[0] == levelName)
                {
                    record = int.Parse(s.Split(' ')[2]);
                    recordFound = true;
                    break;
                }
            }
            reader.Close();
            reader.Dispose();
            if (!recordFound)
            {
                record = -1;
                StreamWriter writer = new StreamWriter("Content\\records.txt", true);
                writer.WriteLine(decryptor.EncryptToString(levelName + " 0 -1"));
                writer.Flush();
                writer.Dispose();
            }

            UpdateViewBox(false);
        }