コード例 #1
0
        private void learnSnap()
        {
            if (tileA.ObjectType.TileData.GetFaceType(tileFaceA) == null)
            {
                tileA.ObjectType.TileData.SetFaceType(tileFaceA, new TileFaceType(Guid.NewGuid()));
            }

            if (tileB.ObjectType.TileData.GetFaceType(tileFaceB) == null)
            {
                tileB.ObjectType.TileData.SetFaceType(tileFaceB, new TileFaceType(Guid.NewGuid()));
            }

            if (getTypeA().GetRoot() == getTypeB().GetRoot())
            {
                return;
            }

            TileFaceType newRoot = new TileFaceType(Guid.NewGuid());

            newRoot.Name = "AutoGen";

            getTypeA().GetRoot().FlipWinding ^= winding;

            getTypeA().GetRoot().SetParent(newRoot);
            getTypeB().GetRoot().SetParent(newRoot);

            Quaternion rotation;
            Vector3    translation;
            Vector3    scale;

            ghost.WorldMatrix.Decompose(out scale, out rotation, out translation);
            tileA.Rotation = rotation;
            tileA.Position = translation;
        }
コード例 #2
0
 private SnapType getSnapType(TileFaceType faceType)
 {
     if (!typesMap.ContainsKey(faceType.GetRoot()))
     {
         typesMap.Add(faceType.GetRoot(), new SnapType("TileFaceType: " + randomNbGenerator.Next(10) + faceType.Name));
     }
     return(typesMap[faceType.GetRoot()]);
 }
コード例 #3
0
        public SnapPoint GetPoint(TileData data, TileFace tileFace, TileFaceType faceType, bool winding)
        {
            var point = new SnapPoint();

            point.Position = Math.Abs(Vector3.Dot((data.Dimensions * 0.5f), getFaceNormal(tileFace))) * getFaceNormal(tileFace);
            if (faceType != null)
            {
                point.SnapType = getSnapType(faceType);
            }
            point.Normal           = getFaceNormal(tileFace);
            point.Up               = getFaceUp(tileFace);
            point.ClockwiseWinding = winding;
            return(point);
        }
コード例 #4
0
        public void TestSerializeTileData()
        {
            TileData     data = new TileData(Guid.NewGuid());
            TileFaceType type = new TileFaceType(Guid.NewGuid());

            type.FlipWinding = true;
            TileFaceType root = new TileFaceType(Guid.NewGuid());

            type.SetParent(root);

            data.Dimensions = new Vector3(1, 2, 3);
            data.SetFaceType(TileFace.Front, type);

            OBJParser.ObjImporter importer = new OBJParser.ObjImporter();
            var c = new OBJToRAMMeshConverter(new RAMTextureFactory());

            importer.AddMaterialFileStream("WallInnerCorner.mtl", new FileStream(TWDir.GameData.CreateSubdirectory("Core\\TileEngine") + "/TileSet001/WallInnerCorner.mtl", FileMode.Open));
            importer.ImportObjFile(TWDir.GameData.CreateSubdirectory("Core\\TileEngine") + "/TileSet001/WallInnerCorner.obj");
            meshWallInnerCorner = c.CreateMesh(importer);

            data.Mesh = meshWallInnerCorner;

            var simpleMeshFactory = new SimpleMeshFactory();

            simpleMeshFactory.AddMesh(meshWallInnerCorner);

            DiskTileDataFactory diskTileDataFactory = new DiskTileDataFactory(simpleMeshFactory,
                                                                              new SimpleTileFaceTypeFactory());

            FileStream stream = File.OpenWrite(TWDir.Test.CreateSubdirectory("TileEngine").FullName + "\\TestTileData.xml");

            diskTileDataFactory.SerializeTileData(data, stream);

            stream.Close();

            FileStream readStream =
                File.OpenRead(TWDir.Test.CreateSubdirectory("TileEngine").FullName + "\\TestTileData.xml");

            TileData readData = diskTileDataFactory.DeserializeTileData(readStream);

            readStream.Close();
        }
コード例 #5
0
 public void SetParent(TileFaceType value)
 {
     parent = value;
 }
コード例 #6
0
        public void TestSnapTo()
        {
            XNAGame game    = new XNAGame();
            var     snapper = new Snapper();

            snapper.AddSnapper(new SnapperPointPoint());
            var builder = new TileSnapInformationBuilder();

            var tileData1 = new TileData(Guid.NewGuid());

            tileData1.Dimensions = new Vector3(1, 2, 3);
            var tileData2 = new TileData(Guid.NewGuid());

            tileData2.Dimensions = new Vector3(2, 2, 5);

            var faceSnapType1 = new TileFaceType(Guid.NewGuid())
            {
                Name = "type1"
            };
            var faceSnapType2 = new TileFaceType(Guid.NewGuid())
            {
                Name = "type2"
            };

            tileData1.SetFaceType(TileFace.Front, faceSnapType1);
            tileData1.SetFaceType(TileFace.Right, faceSnapType2);

            tileData2.SetFaceType(TileFace.Front, faceSnapType1);
            tileData2.SetFaceType(TileFace.Back, faceSnapType1);

            var list = new List <ISnappableWorldTarget>();

            var worldTarget1 = new SimpleSnappableWorldTarget()
            {
                TileData        = tileData1,
                SnapInformation = builder.CreateFromTile(tileData1),
                Transformation  = new Transformation(Vector3.One, Quaternion.Identity, new Vector3(2, 1, 3))
            };
            var worldTarget2 = new SimpleSnappableWorldTarget()
            {
                TileData        = tileData2,
                SnapInformation = builder.CreateFromTile(tileData2),
                Transformation  = new Transformation(Vector3.One, Quaternion.Identity, new Vector3(-2, 1, 1))
            };

            list.Add(worldTarget1);
            list.Add(worldTarget2);

            var tileData1SnapInformation = builder.CreateFromTile(tileData1);

            var transformations = snapper.SnapTo(builder.CreateFromTile(tileData1), list);

            var colorTypeMap = new Dictionary <SnapType, Color>();

            colorTypeMap.Add(worldTarget1.SnapInformation.SnapList[0].SnapType, Color.Red);
            colorTypeMap.Add(worldTarget1.SnapInformation.SnapList[1].SnapType, Color.Green);

            game.DrawEvent += delegate
            {
                for (int i = 0; i < list.Count; i++)
                {
                    var iTarget = (SimpleSnappableWorldTarget)list[i];
                    renderTile(game, iTarget.Transformation.CreateMatrix(), iTarget.TileData, Color.White);

                    renderSnapInformation(game, iTarget.Transformation.CreateMatrix(), iTarget.SnapInformation, colorTypeMap);
                }

                for (int i = 0; i < transformations.Count; i++)
                {
                    var iTransformation = transformations[i];
                    renderTile(game, iTransformation.CreateMatrix(), tileData1, Color.Yellow);
                    renderSnapInformation(game, iTransformation.CreateMatrix(), tileData1SnapInformation, colorTypeMap);
                }
            };

            game.Run();
        }
コード例 #7
0
 public void SetFaceType(TileFace face, TileFaceType value)
 {
     FaceTypes[(int)face - 1] = value;
 }
コード例 #8
0
        public void TestSerializeWorld()
        {
            var game = new XNAGame();

            var texturePool           = new TexturePool();
            var meshpartPool          = new MeshPartPool();
            var vertexDeclarationPool = new VertexDeclarationPool();

            vertexDeclarationPool.SetVertexElements <TangentVertex>(TangentVertex.VertexElements);
            var renderer = new SimpleMeshRenderer(texturePool, meshpartPool, vertexDeclarationPool);

            TileData     data = new TileData(Guid.NewGuid());
            TileFaceType type = new TileFaceType(Guid.NewGuid());

            type.FlipWinding = true;
            TileFaceType root = new TileFaceType(Guid.NewGuid());

            type.SetParent(root);

            data.Dimensions = new Vector3(1, 2, 3);
            data.SetFaceType(TileFace.Front, type);

            OBJParser.ObjImporter importer = new OBJParser.ObjImporter();
            var c = new OBJToRAMMeshConverter(new RAMTextureFactory());

            importer.AddMaterialFileStream("WallInnerCorner.mtl", new FileStream(TWDir.GameData.CreateSubdirectory("Core\\TileEngine") + "/TileSet001/WallInnerCorner.mtl", FileMode.Open));
            importer.ImportObjFile(TWDir.GameData.CreateSubdirectory("Core\\TileEngine") + "/TileSet001/WallInnerCorner.obj");
            meshWallInnerCorner = c.CreateMesh(importer);

            data.Mesh = meshWallInnerCorner;

            var builder = new TileSnapInformationBuilder();

            var world   = new World();
            var objType = new WorldObjectType(data.Mesh, Guid.NewGuid(), builder);

            objType.TileData = data;

            var obj = world.CreateNewWorldObject(game, objType, renderer);

            obj.Position = Vector3.One * 3;



            var simpleMeshFactory = new SimpleMeshFactory();

            simpleMeshFactory.AddMesh(meshWallInnerCorner);

            DiskTileDataFactory diskTileDataFactory = new DiskTileDataFactory(simpleMeshFactory,
                                                                              new SimpleTileFaceTypeFactory());

            diskTileDataFactory.AddTileData(data);


            var typeFactory = new SimpleWorldObjectTypeFactory();



            var serializer = new WorldSerializer(simpleMeshFactory, diskTileDataFactory, game, renderer, typeFactory, builder);


            FileStream stream = File.OpenWrite(TWDir.Test.CreateSubdirectory("TileEngine").FullName + "\\TestWorld.xml");

            serializer.SerializeWorld(world, stream);

            stream.Close();

            FileStream readStream =
                File.OpenRead(TWDir.Test.CreateSubdirectory("TileEngine").FullName + "\\TestWorld.xml");

            var readWorld = new World();

            serializer.DeserializeWorld(readWorld, readStream);

            readStream.Close();
        }
コード例 #9
0
        public void TestAddDeleteMeshToFromWorld()
        {
            XNAGame game = new XNAGame();

            OBJParser.ObjImporter importer = new OBJParser.ObjImporter();
            var c = new OBJToRAMMeshConverter(new RAMTextureFactory());

            importer.AddMaterialFileStream("WallInnerCorner.mtl", new FileStream(TWDir.GameData.CreateSubdirectory("Core\\TileEngine") + "/TileSet001/WallInnerCorner.mtl", FileMode.Open));
            importer.ImportObjFile(TWDir.GameData.CreateSubdirectory("Core\\TileEngine") + "/TileSet001/WallInnerCorner.obj");
            var meshWallInnerCorner = c.CreateMesh(importer);

            importer.AddMaterialFileStream("WallStraight.mtl", new FileStream(TWDir.GameData.CreateSubdirectory("Core\\TileEngine") + "/TileSet001/WallStraight.mtl", FileMode.Open));
            importer.ImportObjFile(TWDir.GameData.CreateSubdirectory("Core\\TileEngine") + "/TileSet001/WallStraight.obj");
            var meshWallStraight = c.CreateMesh(importer);

            importer.AddMaterialFileStream("WallOuterCorner.mtl", new FileStream(TWDir.GameData.CreateSubdirectory("Core\\TileEngine") + "/TileSet001/WallOuterCorner.mtl", FileMode.Open));
            importer.ImportObjFile(TWDir.GameData.CreateSubdirectory("Core\\TileEngine") + "/TileSet001/WallOuterCorner.obj");
            var meshWallOuterCorner = c.CreateMesh(importer);

            var texturePool           = new TexturePool();
            var meshpartPool          = new MeshPartPool();
            var vertexDeclarationPool = new VertexDeclarationPool();

            var renderer = new SimpleMeshRenderer(texturePool, meshpartPool, vertexDeclarationPool);

            vertexDeclarationPool.SetVertexElements <TangentVertex>(TangentVertex.VertexElements);

            World world = new World();
            TileSnapInformationBuilder builder = new TileSnapInformationBuilder();

            WorldObjectPlaceTool placeTool = new WorldObjectPlaceTool(game, world, renderer, builder, new SimpleMeshFactory(), new SimpleTileFaceTypeFactory());
            WorldObjectType      type1     = new WorldObjectType(meshWallInnerCorner, Guid.NewGuid(), builder);
            WorldObjectType      type2     = new WorldObjectType(meshWallStraight, Guid.NewGuid(), builder);
            WorldObjectType      type3     = new WorldObjectType(meshWallOuterCorner, Guid.NewGuid(), builder);

            var tileDataInnerCorner = new TileData(Guid.NewGuid());
            var tileDataStraight    = new TileData(Guid.NewGuid());
            var tileDataOuterCorner = new TileData(Guid.NewGuid());

            tileDataInnerCorner.Dimensions = type1.BoundingBox.Max - type1.BoundingBox.Min;
            tileDataStraight.Dimensions    = type2.BoundingBox.Max - type2.BoundingBox.Min;
            tileDataOuterCorner.Dimensions = type1.BoundingBox.Max - type1.BoundingBox.Min;

            var faceSnapType1 = new TileFaceType(Guid.NewGuid())
            {
                Name = "type1"
            };

            //var faceSnapType2 = new TileFaceType() { Name = "type2" };

            tileDataInnerCorner.SetFaceType(TileFace.Front, faceSnapType1);
            tileDataInnerCorner.SetLocalWinding(TileFace.Front, true);

            tileDataInnerCorner.SetFaceType(TileFace.Left, faceSnapType1);
            tileDataInnerCorner.SetLocalWinding(TileFace.Left, true);

            tileDataStraight.SetFaceType(TileFace.Back, faceSnapType1);
            tileDataStraight.SetLocalWinding(TileFace.Back, false);

            tileDataStraight.SetFaceType(TileFace.Front, faceSnapType1);
            tileDataStraight.SetLocalWinding(TileFace.Front, true);

            tileDataOuterCorner.SetFaceType(TileFace.Front, faceSnapType1);
            tileDataOuterCorner.SetLocalWinding(TileFace.Front, true);

            tileDataOuterCorner.SetFaceType(TileFace.Right, faceSnapType1);
            tileDataOuterCorner.SetLocalWinding(TileFace.Right, true);


            type1.TileData = tileDataInnerCorner;
            type2.TileData = tileDataStraight;
            type3.TileData = tileDataOuterCorner;


            type1.SnapInformation = builder.CreateFromTile(tileDataInnerCorner);
            type2.SnapInformation = builder.CreateFromTile(tileDataStraight);
            type3.SnapInformation = builder.CreateFromTile(tileDataOuterCorner);

            List <WorldObjectType> typeList = new List <WorldObjectType>();

            typeList.Add(type1);
            typeList.Add(type2);
            typeList.Add(type3);

            WorldObjectMoveTool moveTool = new WorldObjectMoveTool(game, world, builder, renderer);

            game.AddXNAObject(moveTool);
            game.AddXNAObject(placeTool);

            game.IsFixedTimeStep = false;
            game.DrawFps         = true;
            game.AddXNAObject(texturePool);
            game.AddXNAObject(meshpartPool);
            game.AddXNAObject(vertexDeclarationPool);
            game.AddXNAObject(renderer);

            EditorGrid grid;

            grid               = new EditorGrid();
            grid.Size          = new Vector2(100, 100);
            grid.Interval      = 1;
            grid.MajorInterval = 10;

            bool mouseEnabled = false;

            game.UpdateEvent += delegate
            {
                if (placeTool.Enabled)
                {
                    moveTool.Enabled = false;

                    if (placeTool.ObjectsPlacedSinceEnabled() == 1)
                    {
                        placeTool.Enabled = false;
                    }
                }
                else
                {
                    moveTool.Enabled = true;
                }

                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.NumPad1))
                {
                    placeTool.PlaceType = typeList[0];
                    placeTool.Enabled   = true;
                }
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.NumPad2))
                {
                    placeTool.PlaceType = typeList[1];
                    placeTool.Enabled   = true;
                }
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.NumPad3))
                {
                    placeTool.PlaceType = typeList[2];
                    placeTool.Enabled   = true;
                }

                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.LeftAlt))
                {
                    mouseEnabled = !mouseEnabled;
                }
                if (mouseEnabled)
                {
                    game.Mouse.CursorEnabled     = true;
                    game.IsMouseVisible          = true;
                    game.SpectaterCamera.Enabled = false;
                }
                else
                {
                    game.Mouse.CursorEnabled     = false;
                    game.IsMouseVisible          = false;
                    game.SpectaterCamera.Enabled = true;
                    //activeWorldObject = null;
                }
            };
            game.DrawEvent += delegate
            {
                grid.Render(game);
            };

            game.Run();
        }