コード例 #1
0
        public Game LoadGame()
        {
            Parser parser = new Parser(grammar);
            src = File.ReadAllText(FileName.FullName);
            ParseTree parseTree = parser.Parse(src, FileName.FullName);

            if (parseTree.HasErrors())
            {
                throw new StorageException(BuildErrorMessage(parseTree.ParserMessages, FileName.Name));
            }

            root = parseTree.Root;
            var nameProperty = GetProperties(root).First(x => x.Item1 == "Name");
            var name = getStrVal((ParseTreeNode)nameProperty.Item2);
            game = new Game(new Value(name).GetStringValue());
            AddDefaultUsings(game);
            IEnumerable<ParseTreeNode> usings = grammar.GetOfType(root, grammar.Uses);

            foreach(ParseTreeNode node in usings)
            {
                Using use = CreateUsing(node);
                game.AddUsing(use);
            }

            foreach (Tuple<string, object> attribute in GetProperties(root))
            {
                ParseTreeNode attributeNode = (ParseTreeNode)attribute.Item2;
                if (attribute.Item1 != "Name")
                {
                    game.AddAttribute(new Attribute(attribute.Item1) { Value = new Value(getStrVal(attributeNode)) });
                }
            }

            foreach (ParseTreeNode prototype in grammar.GetOfType(root, grammar.Prototype))
            {
                Entity entity = CreateEntity(prototype, null, true);
                game.AddPrototype(entity);
            }

            foreach (ParseTreeNode serviceNode in grammar.GetOfType(root, grammar.Service))
            {
                Service service = CreateService(serviceNode);
                game.AddService(service);
            }

            foreach (ParseTreeNode sceneNode in grammar.GetOfType(root, grammar.Scene))
            {
                Scene scene = CreateScene(sceneNode);
                game.AddScene(scene);
            }

            var firstScene = game.GetAttribute("FirstScene");
            game.FirstScene = game.GetScene(firstScene.Value.GetStringValue());
            game.RemoveAttribute(firstScene);

            return game;
        }
コード例 #2
0
ファイル: Workspace.cs プロジェクト: kinectitude/kinectitude
        private Workspace()
        {
            Plugins = new ObservableCollection<Plugin>();

            Services = new FilteredObservableCollection<Plugin>(Plugins, p => p.Type == PluginType.Service);
            Managers = new FilteredObservableCollection<Plugin>(Plugins, p => p.Type == PluginType.Manager);
            Components = new FilteredObservableCollection<Plugin>(Plugins, p => p.Type == PluginType.Component);

            Events = new ObservableCollection<StatementFactory>();
            Actions = new ObservableCollection<StatementFactory>();
            Statements = new ObservableCollection<StatementFactory>();

            NewProjectCommand = new DelegateCommand(null, p =>
            {
                var create = true;

                if (null != Project && CommandHistory.HasUnsavedChanges)
                {
                    DialogService.Warn(Messages.NewProject, Messages.UnsavedMessage, MessageBoxButton.YesNoCancel, r =>
                    {
                        if (r == MessageBoxResult.Yes)
                        {
                            SaveProject();
                        }
                        else if (r == MessageBoxResult.Cancel)
                        {
                            create = false;
                        }
                    });
                }

                if (create)
                {
                    Game game = new Game("Untitled Game");
                    KglGameStorage.AddDefaultUsings(game);

                    var service = new Service(GetPlugin(typeof(RenderService)));
                    service.SetProperty("Width", new Value(800, true));
                    service.SetProperty("Height", new Value(600, true));
                    game.AddService(service);

                    var scene = new Scene("Scene1");

                    var manager = new Manager(GetPlugin(typeof(RenderManager)));
                    scene.AddManager(manager);

                    game.AddScene(scene);
                    game.FirstScene = scene;

                    Project project = new Project();
                    project.Game = game;

                    DialogService.ShowDialog<ProjectDialog>(project, (result) =>
                    {
                        if (result == true)
                        {
                            ProjectStorage.CreateProject(project);
                            Project = project;
                        }
                    });
                }
            });

            LoadProjectCommand = new DelegateCommand(null, p =>
            {
                var load = true;

                if (null != Project && CommandHistory.HasUnsavedChanges)
                {
                    DialogService.Warn(Messages.LoadProject, Messages.UnsavedMessage, MessageBoxButton.YesNoCancel, r =>
                    {
                        if (r == MessageBoxResult.Yes)
                        {
                            SaveProject();
                        }
                        else if (r == MessageBoxResult.Cancel)
                        {
                            load = false;
                        }
                    });
                }

                if (load)
                {
                    DialogService.ShowLoadDialog((result, fileName) =>
                    {
                        if (result == true)
                        {
                            try
                            {
                                LoadProject(fileName);
                            }
                            catch (EditorException e)
                            {
                                DialogService.Warn(Messages.FailedToLoad, e.Message, MessageBoxButton.OK);
                            }
                        }
                    });
                }
            });

            SaveProjectCommand = new DelegateCommand(p => null != project, p =>
            {
                if (null == Project.Title)
                {
                    DialogService.ShowSaveDialog(
                        (result, fileName) =>
                        {
                            if (result == true)
                            {
                                Project.Title = fileName;
                            }
                        }
                    );
                }

                if (null != Project.Title)
                {
                    SaveProject();
                }
            });

            RevertProjectCommand = new DelegateCommand(p => null != Project, p => RevertProject());

            Assembly core = typeof(Kinectitude.Core.Base.Component).Assembly;
            RegisterPlugins(core);

            DirectoryInfo path = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, PluginDirectory));
            if (path.Exists)
            {
                FileInfo[] files = path.GetFiles("*.dll");
                foreach (FileInfo file in files)
                {
                    Assembly asm = Assembly.LoadFrom(file.FullName);
                    RegisterPlugins(asm);
                }
            }

            CommandHistory = new CommandHistory();
            DialogService = new DialogService();
        }