Exemple #1
0
        public void AddComponentWithExistingDefine()
        {
            Game game = new Game("Test Game");

            Using use = new Using() { File = Path.GetFileName(typeof(TransformComponent).Assembly.Location) };
            game.AddUsing(use);

            Define define = new Define(TransformComponentShort, TransformComponentType);
            use.AddDefine(define);

            Entity entity = new Entity() { Name = "entity" };

            Component component = new Component(game.GetPlugin(TransformComponentShort));
            entity.AddComponent(component);
            game.AddPrototype(entity);

            Assert.AreEqual(1, game.Usings.Count);
            Assert.AreEqual(1, game.Usings.Single().Defines.Count(x => x.Name == TransformComponentShort && x.Class == TransformComponentType));
            Assert.AreEqual(TransformComponentShort, component.Type);
        }
Exemple #2
0
        public void AddComponentWithoutExistingDefine()
        {
            bool firedPluginUsed = false;
            bool firedDefineAdded = false;

            Game game = new Game("Test Game");
            game.AddHandler<PluginUsed>(n => firedPluginUsed = true);
            game.AddHandler<DefineAdded>(n => firedDefineAdded = true);

            Entity entity = new Entity() { Name = "entity" };
            game.AddPrototype(entity);

            Component component = new Component(game.GetPlugin(TransformComponentType));
            entity.AddComponent(component);

            Assert.IsTrue(firedPluginUsed);
            Assert.IsTrue(firedDefineAdded);
            Assert.AreEqual(1, game.Usings.Count);
            Assert.AreEqual(1, game.Usings.Single().Defines.Count(x => x.Name == TransformComponentShort && x.Class == TransformComponentType));
            Assert.AreEqual(TransformComponentShort, component.Type);
        }
        // Multiple inheritance tests
        private static Game CreateTestGame()
        {
            var game = new Game("Test Game");

            var prototypeA0 = new Entity() { Name = "prototypeA0" };
            game.AddPrototype(prototypeA0);
            var prototypeB0 = new Entity() { Name = "prototypeB0" };
            game.AddPrototype(prototypeB0);
            var prototypeC0 = new Entity() { Name = "prototypeC0" };
            game.AddPrototype(prototypeC0);

            var prototypeA1 = new Entity() { Name = "prototypeA1" };
            prototypeA1.AddPrototype(prototypeA0);
            game.AddPrototype(prototypeA1);

            var prototypeB1 = new Entity() { Name = "prototypeB1" };
            prototypeB1.AddPrototype(prototypeB0);
            prototypeB1.AddPrototype(prototypeC0);
            game.AddPrototype(prototypeB1);

            var scene = new Scene("Test Scene");
            game.FirstScene = scene;
            var testEntity = new Entity() { Name = "testEntity" };
            scene.AddEntity(testEntity);
            testEntity.AddPrototype(prototypeA1);
            testEntity.AddPrototype(prototypeB1);

            return game;
        }
        public GameViewModel(Game game)
        {
            this.game = game;

            game.PropertyChanged += Game_PropertyChanged;

            Prototypes = new ComputedObservableCollection<Entity, EntityViewModel>(game.Prototypes, (prototype) => new EntityViewModel(prototype));
            Scenes = new ComputedObservableCollection<Scene, SceneViewModel>(game.Scenes, (scene) => new SceneViewModel(scene));

            AddPrototypeCommand = new DelegateCommand(null,
                (parameter) =>
                {
                    Entity prototype = new Entity();

                    DialogService.ShowDialog(DialogService.Constants.EntityDialog, prototype,
                        (result) =>
                        {
                            if (result == true)
                            {
                                game.AddPrototype(prototype);
                            }
                        }
                    );
                }
            );

            RemovePrototypeCommand = new DelegateCommand(null,
                (parameter) =>
                {
                    Entity prototype = parameter as Entity;
                    game.RemovePrototype(prototype);
                }
            );

            AddSceneCommand = new DelegateCommand(null,
                (parameter) =>
                {
                    Scene scene = game.CreateScene();

                    DialogService.ShowDialog(DialogService.Constants.SceneDialog, scene,
                        (result) =>
                        {
                            if (result == true)
                            {
                                game.AddScene(scene);
                            }
                        }
                    );
                }
            );

            RemoveSceneCommand = new DelegateCommand(null,
                (parameter) =>
                {
                    Scene scene = parameter as Scene;
                    game.RemoveScene(scene);
                }
            );

            AddAttributeCommand = new DelegateCommand(null,
                (parameter) =>
                {
                    game.CreateAttribute();
                }
            );

            RemoveAttributeCommand = new DelegateCommand(null,
                (parameter) =>
                {
                    Attribute attribute = parameter as Attribute;
                    game.RemoveAttribute(attribute);
                }
            );

            AddAssetCommand = new DelegateCommand(null,
                (parameter) =>
                {
                    // TODO: File Chooser
                    Asset asset = new Asset("An Asset");
                    game.AddAsset(asset);
                }
            );

            RemoveAssetCommand = new DelegateCommand(null,
                (parameter) =>
                {
                    Asset asset = parameter as Asset;
                    game.RemoveAsset(asset);
                }
            );

            RemoveItemCommand = new DelegateCommand(null,
                (parameter) =>
                {
                    Entity prototype = parameter as Entity;
                    if (null != prototype)
                    {
                        game.RemovePrototype(prototype);
                    }
                    else
                    {
                        Scene scene = parameter as Scene;
                        if (null != scene)
                        {
                            game.RemoveScene(scene);
                        }
                    }
                }
            );
        }
        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;
        }
Exemple #6
0
        public void AddPrototype()
        {
            bool collectionChanged = false;

            Game game = new Game("Test Game");
            game.Prototypes.CollectionChanged += (o, e) => collectionChanged = true;

            Entity entity = new Entity() { Name = "TestPrototype" };
            game.AddPrototype(entity);

            Assert.IsTrue(collectionChanged);
            Assert.AreEqual(game.Prototypes.Count(), 1);
            Assert.AreEqual(game.Prototypes.First().Name, "TestPrototype");
        }
Exemple #7
0
        public void RemovePrototype()
        {
            int eventsFired = 0;

            Game game = new Game("Test Game");
            game.Prototypes.CollectionChanged += (o, e) => eventsFired++;

            Entity entity = new Entity() { Name = "TestPrototype" };
            game.AddPrototype(entity);
            game.RemovePrototype(entity);

            Assert.AreEqual(2, eventsFired);
            Assert.AreEqual(game.Prototypes.Count(), 0);
        }
Exemple #8
0
        public void PrototypeMustHaveName()
        {
            bool collectionChanged = false;

            Game game = new Game("Test Game");
            game.Prototypes.CollectionChanged += (o, e) => collectionChanged = true;

            game.AddPrototype(new Entity());

            Assert.IsFalse(collectionChanged);
            Assert.AreEqual(0, game.Prototypes.Count);
        }
Exemple #9
0
        public void CannotAddDuplicatePrototypeName()
        {
            int eventsFired = 0;

            Game game = new Game("Test Game");
            game.Prototypes.CollectionChanged += (o, e) => eventsFired++;

            game.AddPrototype(new Entity() { Name = "prototype" });
            game.AddPrototype(new Entity() { Name = "prototype" });

            Assert.AreEqual(1, eventsFired);
            Assert.AreEqual(1, game.Prototypes.Count(x => x.Name == "prototype" ));
        }
        public void Game_RemoveItem()
        {
            var game = new Game("Test Game");
            var scene = new Scene("Test Scene");
            game.AddScene(scene);

            CommandHelper.TestUndoableCommand(
                () => Assert.AreEqual(1, game.Scenes.Count),
                () => game.RemoveItemCommand.Execute(scene),
                () => Assert.AreEqual(0, game.Scenes.Count)
            );

            var prototype = new Entity() { Name = "Prototype" };
            game.AddPrototype(prototype);

            CommandHelper.TestUndoableCommand(
                () => Assert.AreEqual(1, game.Prototypes.Count),
                () => game.RemoveItemCommand.Execute(prototype),
                () => Assert.AreEqual(0, game.Prototypes.Count)
            );
        }
        public void CannotRenameToDuplicateNameInSameScope()
        {
            Game game = new Game("Test Game");
            game.AddPrototype(new Entity() { Name = "TestEntity" });

            Scene scene = new Scene("Test Scene");

            Entity entity = new Entity() { Name = "TestEntity2" };
            scene.AddEntity(entity);

            game.AddScene(scene);

            entity.Name = "TestEntity";

            Assert.AreEqual(1, scene.Entities.Count);
            Assert.AreEqual("TestEntity2", entity.Name);
        }