コード例 #1
0
        //Spawns an AIShip object in a random position
        public void SpawnAI()
        {
            double xc = Random.NextDouble() * 5000.0;
            double yc = Random.NextDouble() * 5000.0;

            if (!NearOtherObject(xc, yc))
            {
                AIShip aI = new AIShip(xc, yc, this);
                AIShips.Add(aI);
                GameObjects.Add(aI);
                UpdateAnimationEvent(this, new AnimationEventArgs(false, AnimationType.AI, AIShips.Count, 0, 0));
            }
        }
コード例 #2
0
        //This method reads a file, deserializes it into a Game, and returns the Game object.
        public static Game Load(string filename, bool isCheatMode)
        {
            string json;

            try
            {
                using (StreamReader sr = new StreamReader(File.OpenRead(filename)))
                {
                    json = sr.ReadToEnd();
                }
            }
            catch
            {
                throw new ArgumentException("The file does not exist");
            }
            string version = JsonUtils.ExtractValue(json, "version");

            if (version != '"' + Version + '"')
            {
                throw new FormatException("Wrong version of saved game file: " + version);
            }
            var game = new Game(isCheatMode);

            game.StableWells   = new List <Well>();
            game.UnstableWells = new List <Well>();
            game.Orbs          = new List <Orb>();


            game.Username                      = JsonUtils.ExtractValue(json, "username");
            game.Ticks                         = Convert.ToInt32(JsonUtils.ExtractValue(json, "ticks"));
            game.Player                        = GameObject.FromJsonFactory <Ship>(JsonUtils.ExtractValue(json, "humanplayer"));
            game.Points                        = game.Player.Points;
            game.Player.ParentGame             = game;
            game.Player.GamePowerup.ParentGame = game;

            foreach (string s in JsonUtils.GetObjectsInArray(JsonUtils.ExtractValue(json, "stablegravitywells")))
            {
                game.StableWells.Add(GameObject.FromJsonFactory <Well>(s));
            }
            foreach (string s in JsonUtils.GetObjectsInArray(JsonUtils.ExtractValue(json, "unstablegravitywells")))
            {
                Well w = GameObject.FromJsonFactory <Well>(s);
                w.ShockWave.ParentGame = game;
                game.UnstableWells.Add(w);
            }
            foreach (string s in JsonUtils.GetObjectsInArray(JsonUtils.ExtractValue(json, "orbs")))
            {
                game.Orbs.Add(GameObject.FromJsonFactory <Orb>(s));
            }
            foreach (string s in JsonUtils.GetObjectsInArray(JsonUtils.ExtractValue(json, "aiplayers")))
            {
                AIShip aIShip = GameObject.FromJsonFactory <AIShip>(s);
                aIShip.ParentGame = game;
                game.AIShips.Add(aIShip);
                aIShip.GamePowerup.ParentGame = game;
                aIShip.InitializeTargets();
            }
            game.GameObjects.AddRange(game.Orbs);
            game.GameObjects.AddRange(game.UnstableWells);
            game.GameObjects.AddRange(game.StableWells);
            game.GameObjects.Add(game.Player);
            return(game);
        }