コード例 #1
0
        /// <summary>
        /// Generates the javascript code for the game described in the specified model.
        /// </summary>
        /// <param name="model">The model that describes the game.</param>
        public string GenerateGameCode(GameModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            UpdateModules(model.Units);
            UpdateModules(model);

            var unitModules = new ModuleCollection();

            foreach (var unit in model.Units)
            {
                unitModules = new ModuleCollection(unitModules.Union(unit.Modules, Module.Comparer));
            }

            string background       = model.Background.GenerateCode();
            string modules          = unitModules.GenerateUnitModulesCode() + "\n" + model.Modules.GenerateGameModulesCode();
            string unitConstructors = GenerateUnitConstructors();
            string units            = unitConstructors + model.Units.GenerateCollectionCode((unit) => unit.GenerateCode());
            string bindings         = model.KeyBindings.GenerateCollectionCode((binding) => binding.GenerateCode());

            string game = Regex.Replace(gameTemplate, BackgroundPlaceholder, background, RegexOptions.None);

            game = Regex.Replace(game, ModulesPlaceholder, modules, RegexOptions.None);
            game = Regex.Replace(game, UnitsPlaceholder, units, RegexOptions.None);
            game = Regex.Replace(game, KeyBindingsPlaceholder, bindings, RegexOptions.None);

            return(game);
        }
コード例 #2
0
        /// <summary>
        /// Constructs a new <see cref="JsGenerator"/> using the specified <see cref="ModuleCollection"/>.
        /// </summary>
        /// <param name="modules"></param>
        public JsGenerator(ModuleCollection modules)
        {
            if (modules == null)
            {
                throw new ArgumentNullException("modules");
            }

            gameTemplate  = PathFinder.ReadEmbeddedFile(PathFinder.GameTemplateFile);
            presetModules = modules.ToDictionary(x => x.Name);
        }