Exemple #1
0
        // receives a string and returns a default infantry object based off the string
        public static Infantry MakeInfantry(string type)
        {
            Infantry i = new Infantry();

            switch (type)
            {
            case "b":
                i.imageID     = 0;
                i.health      = 20;
                i.rewardMoney = 10;
                i.speed       = 3.125; // decide speed for advance type, replace 4 with desired speed
                i.rewardScore = 2;
                i.type        = "binfantry";
                break;

            case "a":
                i.imageID     = 4;
                i.health      = 50;
                i.rewardMoney = 25;
                i.speed       = 2.5; // decide speed for advance type, replace 4 with desired speed
                i.rewardScore = 5;
                i.type        = "ainfantry";
                break;
            }
            i.posX         = Map.coords[0].x;
            i.posY         = Map.coords[0].y;
            i.pathProgress = 0;
            return(i);
        }
Exemple #2
0
        // deserializes a string and converts it into an object with the specified values within that string.
        public override object Deserialize(string info)
        {
            string[] aInfo = info.Split(',');
            string   s     = aInfo[0] == "ainfantry" ? "a" : "b";
            Infantry i     = MakeInfantry(s);

            i.posX         = Convert.ToDouble(aInfo[1]);
            i.posY         = Convert.ToDouble(aInfo[2]);
            i.health       = Convert.ToDouble(aInfo[3]);
            i.pathProgress = Convert.ToInt32(aInfo[4]);
            return(i);
        }
        public void LoadGame_ValidInput_ValidClass()
        {
            Game g = new Game(0, false, null, null, 1);

            Game.map       = new Map(0);
            g.currentWave  = 5;
            g.waveProgress = 3;
            g.score        = 700;
            Game.money     = 200;
            g.isWaveOver   = false;
            g.spawner      = new Spawner(null);
            Infantry i1 = Infantry.MakeInfantry("b");

            i1.posX         = 2;
            i1.posY         = 4;
            i1.health       = 13;
            i1.pathProgress = 3;
            Vehicle v1 = Vehicle.MakeVehicle("b");

            v1.posX         = 6;
            v1.posY         = 2;
            v1.health       = 10;
            v1.pathProgress = 2;
            Spawner.enemies.Add(i1);
            Spawner.enemies.Add(v1);
            g.currentEnemies.Add(i1);
            g.currentEnemies.Add(v1);
            Mortar m1 = Mortar.MakeMortar(2, 3, 0);
            Stun   s1 = Stun.MakeStun(7, 5, 0);
            Tesla  t1 = Tesla.MakeTesla(3, 5, 0);

            g.currentTurrets = new List <Turret>();
            g.currentTurrets.Add(m1);
            g.currentTurrets.Add(s1);
            g.currentTurrets.Add(t1);
            Game loadedGame = Game.LoadGame("SavedGame.txt", null, null);

            // all should be working
            Assert.AreEqual(g.currentEnemies.Count, loadedGame.currentEnemies.Count);
            Assert.AreEqual(8, Game.lives);
            Assert.AreEqual(g.currentWave, loadedGame.currentWave);
            Assert.AreEqual(200, Game.money);
            Assert.AreEqual(g.score, loadedGame.score);
            Assert.AreEqual(g.waveProgress, loadedGame.waveProgress);
            Assert.AreEqual(g.difficulty, loadedGame.difficulty);
            Assert.AreEqual(g.isWaveOver, loadedGame.isWaveOver);
            Assert.AreEqual(g.currentTurrets.Count, loadedGame.currentTurrets.Count);
        }
        public void SaveGame_ValidInput_ValidString()
        {
            Game g = new Game(0, false, null, null, 1);

            g.currentWave  = 5;
            g.waveProgress = 3;
            Game.money     = 200;
            g.score        = 700;
            g.isWaveOver   = false;
            Game.lives     = 8;
            Game.map       = new Map(0);
            Mortar m1 = Mortar.MakeMortar(2, 3, 0);
            Stun   s1 = Stun.MakeStun(7, 5, 0);
            Tesla  t1 = Tesla.MakeTesla(3, 5, 0);

            g.currentTurrets = new List <Turret>();
            g.currentTurrets.Add(m1);
            g.currentTurrets.Add(s1);
            g.currentTurrets.Add(t1);
            g.spawner = new Spawner(null);
            Infantry i1 = Infantry.MakeInfantry("b");

            i1.posX         = 2;
            i1.posY         = 4;
            i1.health       = 13;
            i1.pathProgress = 3;
            Vehicle v1 = Vehicle.MakeVehicle("b");

            v1.posX         = 6;
            v1.posY         = 2;
            v1.health       = 10;
            v1.pathProgress = 2;
            g.currentEnemies.Add(i1);
            g.currentEnemies.Add(v1);
            Spawner.enemies.Add(i1);
            Spawner.enemies.Add(v1);
            g.SaveGame("SavedGame1.txt");
            StreamReader reader         = new StreamReader("SavedGame1.txt");
            string       SerializedGame = reader.ReadToEnd();

            Assert.AreEqual(SerializedGame, "NG\r\n0,5,3,700,200,20,false,1\r\nENEMIES\r\nbinfantry,2,4,13,3\r\nbvehicle,6,2,10,2\r\nENDENEMIES\r\nTURRETS\r\nmortar,2,3\r\nstun,7,5\r\ntesla,3,5\r\nENDTURRETS\r\nEND\r\n");
            reader.Close();
        }
Exemple #5
0
        // returns an enemy object and decrements the count
        public static Enemy GenerateEnemy()
        {
            Console.WriteLine("Generating enemy");
            Enemy e = null;

            if (count[0] > 0)
            {
                switch (types[0])
                {
                case "bI":
                    enemies.Add(Infantry.MakeInfantry("b"));
                    e = enemies[enemies.Count - 1];
                    count[0]--;
                    break;

                case "aI":
                    enemies.Add(Infantry.MakeInfantry("a"));
                    e = enemies[enemies.Count - 1];
                    count[0]--;
                    break;

                case "bV":
                    enemies.Add(Vehicle.MakeVehicle("b"));
                    e = enemies[enemies.Count - 1];
                    count[0]--;
                    break;

                case "aV":
                    enemies.Add(Vehicle.MakeVehicle("b"));
                    e = enemies[enemies.Count - 1];
                    count[0]--;
                    break;

                case "bA":
                    enemies.Add(Aircraft.MakeAircraft("b"));
                    e = enemies[enemies.Count - 1];
                    count[0]--;
                    break;

                case "gB":
                    enemies.Add(Boss.MakeBoss("g"));
                    e = enemies[enemies.Count - 1];
                    count[0]--;
                    break;
                }
            }
            else if (count[1] > 0)
            {
                switch (types[1])
                {
                case "aI":
                    enemies.Add(Infantry.MakeInfantry("a"));
                    e = enemies[enemies.Count - 1];
                    count[1]--;
                    break;

                case "aV":
                    enemies.Add(Vehicle.MakeVehicle("a"));
                    e = enemies[enemies.Count - 1];
                    count[1]--;
                    break;

                case "aA":
                    enemies.Add(Aircraft.MakeAircraft("a"));
                    e = enemies[enemies.Count - 1];
                    count[1]--;
                    break;

                case "aB":
                    enemies.Add(Boss.MakeBoss("a"));
                    e = enemies[enemies.Count - 1];
                    count[1]--;
                    break;
                }
            }
            else
            {
                Console.WriteLine("no enemy created");
                return(null);
            }
            Console.WriteLine(e.type + " was created");
            return(e);
        }
Exemple #6
0
        // loads a game that is saved in the file named "filename" and returns that game
        public static Game LoadGame(string fileName, Action <Enemy> add, Action <Enemy, bool> remove)
        {
            using (StreamReader reader = new StreamReader(fileName))
            {
                string begin   = reader.ReadLine(); // read the first line and check for a New Game or NG
                Game   newGame = new Game(0, true, add, remove, 0);
                if (begin == "NG")
                {
                    string[] gameInfo = reader.ReadLine().Split(','); // grab the game state information

                    map = new Map(Convert.ToInt32(gameInfo[0]));      // create a new map based on the mapid
                    newGame.currentWave  = Convert.ToInt32(gameInfo[1]);
                    newGame.waveProgress = Convert.ToInt32(gameInfo[2]);
                    newGame.score        = Convert.ToInt32(gameInfo[3]);
                    Game.money           = Convert.ToInt32(gameInfo[4]);
                    newGame.difficulty   = Convert.ToInt32(gameInfo[5]);     // need to change/fix this so that it is a difficulty instead of a int
                    if (gameInfo[6] == "false")
                    {
                        newGame.isWaveOver = false;
                    }
                    else
                    {
                        newGame.isWaveOver = true;
                    }
                    Game.lives = Convert.ToInt32(gameInfo[7]);
                    if (gameInfo[8] == "false")
                    {
                        cheatMode = false;
                    }                                                  // cheatmode is static btw
                    Spawner.count = new int[2] {
                        Convert.ToInt32(gameInfo[9]), Convert.ToInt32(gameInfo[10])
                    };
                    Spawner.types = new string[2] {
                        gameInfo[11], gameInfo[12]
                    };
                    while (true)
                    {
                        string section = reader.ReadLine(); // read the section header
                        if (section.Trim() == "END")
                        {
                            break;
                        }                                          // if its END we're done

                        if (section.Trim() == "ENEMIES")
                        {
                            while (true)
                            {
                                string line = reader.ReadLine(); // read a line
                                if (line.Trim() == "ENDENEMIES")
                                {
                                    break;
                                }                                           // see if we're at the end yet break if we are
                                string[] ele = line.Split(',');

                                switch (ele[0])                 // grab the type and call methods based on it
                                {
                                case "gboss":
                                    Boss bG = Boss.MakeBoss("g");
                                    bG = bG.Deserialize(line) as Boss;
                                    Spawner.enemies.Add(bG);
                                    break;

                                case "aboss":
                                    Boss bA = Boss.MakeBoss("a");
                                    bA = bA.Deserialize(line) as Boss;
                                    Spawner.enemies.Add(bA);
                                    break;

                                case "baircraft":
                                    Aircraft a = Aircraft.MakeAircraft("b");
                                    a = a.Deserialize(line) as Aircraft;
                                    Spawner.enemies.Add(a);
                                    break;

                                case "aaircraft":
                                    Aircraft aA = Aircraft.MakeAircraft("a");
                                    aA = aA.Deserialize(line) as Aircraft;
                                    Spawner.enemies.Add(aA);
                                    break;

                                case "binfantry":
                                    Infantry i = Infantry.MakeInfantry("b");
                                    i = i.Deserialize(line) as Infantry;
                                    Spawner.enemies.Add(i);
                                    break;

                                case "ainfantry":
                                    Infantry iA = Infantry.MakeInfantry("a");
                                    iA = iA.Deserialize(line) as Infantry;
                                    Spawner.enemies.Add(iA);
                                    break;

                                case "bvehicle":
                                    Vehicle v = Vehicle.MakeVehicle("b");
                                    v = v.Deserialize(line) as Vehicle;
                                    Spawner.enemies.Add(v);
                                    break;

                                case "avehicle":
                                    Vehicle vA = Vehicle.MakeVehicle("a");
                                    vA = vA.Deserialize(line) as Vehicle;
                                    Spawner.enemies.Add(vA);
                                    break;

                                default:
                                    Debug.WriteLine("The enemy type was not correct.");
                                    break;
                                }
                            }
                        }
                        else if (section.Trim() == "TURRETS")
                        {
                            while (true)
                            {
                                string lineT = reader.ReadLine(); // read a line
                                if (lineT.Trim() == "ENDTURRETS")
                                {
                                    break;
                                }                                // see if we're at the end yet break if we are
                                string[] eleT = lineT.Split(',');
                                switch (eleT[0])                 // grab the type and call methods based on it
                                {
                                case "flak":
                                    Flak f = Flak.MakeFlak(0, 0, 0);
                                    f.Deserialize(lineT);
                                    newGame.currentTurrets.Add(f);
                                    break;

                                case "laser":
                                    Laser l = Laser.MakeLaser(0, 0, 0);
                                    l.Deserialize(lineT);
                                    newGame.currentTurrets.Add(l);
                                    break;

                                case "machinegun":
                                    MachineGun m = MachineGun.MakeMachineGun(0, 0, 0);
                                    m.Deserialize(lineT);
                                    newGame.currentTurrets.Add(m);
                                    break;

                                case "mortar":
                                    Mortar mo = Mortar.MakeMortar(0, 0, 0);
                                    mo.Deserialize(lineT);
                                    newGame.currentTurrets.Add(mo);
                                    break;

                                case "stun":
                                    Stun s = Stun.MakeStun(0, 0, 0);
                                    s.Deserialize(lineT);
                                    newGame.currentTurrets.Add(s);
                                    break;

                                case "tesla":
                                    Tesla t = Tesla.MakeTesla(0, 0, 0);
                                    t.Deserialize(lineT);
                                    newGame.currentTurrets.Add(t);
                                    break;

                                default:
                                    Debug.WriteLine("The turret type was not correct.");
                                    break;
                                }
                            }
                        }
                    }
                }
                for (int i = 0; i < Spawner.enemies.Count; ++i)
                {
                    newGame.addEnemy(Spawner.enemies[i]);
                }
                for (int i = 0; i < newGame.currentTurrets.Count; ++i)
                {
                    for (int i2 = 0; i2 < newGame.currentTurrets[i].upgradeLvl - 1; ++i2)
                    {
                        newGame.currentTurrets[i2].Upgrade();
                    }
                }
                return(newGame);
            }
        }