Example #1
0
        private Tile CreateNormalTile(Texture2D sheet)
        {
            var tile   = new Tile();
            var sprite = new Sprite {
                Texture2D = sheet, Rectangle = new Rectangle(0, 0, 32, 32)
            };

            tile.Sprite = tile.AddComponent(sprite);

            var animation = new Animation(sheet, new Rectangle[]
            {
                new Rectangle(0, 0, 32, 32),
                new Rectangle(32, 0, 32, 32),
                new Rectangle(64, 0, 32, 32),
                new Rectangle(96, 0, 32, 32),
                new Rectangle(128, 0, 32, 32),
                new Rectangle(160, 0, 32, 32),
            })
            {
                Name  = AnimationDictionary.TileDestroy,
                Speed = 0.5f
            };

            tile.Animator = tile.AddComponent(new Animator {
                Animations = new[] { animation }
            });

            return(tile);
        }
Example #2
0
        private Tile CreateUnknownTile(Texture2D sheet)
        {
            var tile   = new Tile();
            var sprite = new Sprite {
                Texture2D = sheet, Rectangle = new Rectangle(0, 128, 32, 32)
            };

            tile.Sprite = tile.AddComponent(sprite);

            var destroyAnimation = new Animation(sheet, new[]
            {
                new Rectangle(32, 128, 32, 32),
                new Rectangle(64, 128, 32, 32),
                new Rectangle(96, 128, 32, 32),
                new Rectangle(128, 128, 32, 32),
                new Rectangle(160, 128, 32, 32),
                new Rectangle(192, 128, 32, 32),
                new Rectangle(224, 128, 32, 32),
            })
            {
                Name  = AnimationDictionary.TileDestroy,
                Speed = 0.5f
            };

            var touchAnimation = new Animation(sheet, new[]
            {
                new Rectangle(0, 160, 32, 32),
                new Rectangle(0, 160, 32, 32),
                new Rectangle(0, 160, 32, 32),
                new Rectangle(0, 160, 32, 32),
                new Rectangle(32, 160, 32, 32),
            })
            {
                Name  = AnimationDictionary.TileTouch,
                Speed = 0.3f
            };

            tile.Animator = tile.AddComponent(new Animator {
                Animations = new[] { destroyAnimation, touchAnimation }
            });

            return(tile);
        }
        public static void AddComponent( Tile tile,System.Type classType )
        {
            Undo.RecordObject( tile,"Add Component" );

            TileComponent component = tile.AddComponent( classType );
            Undo.RegisterCreatedObjectUndo( component,"Add Component" );

            AssetDatabase.AddObjectToAsset( component,tile );

            EditorUtility.SetDirty( tile );
        }
Example #4
0
        private Tile CreateIndestructibleTile(Texture2D sheet)
        {
            var tile   = new Tile();
            var sprite = new Sprite {
                Texture2D = sheet, Rectangle = new Rectangle(0, 96, 32, 32)
            };

            tile.Sprite = tile.AddComponent(sprite);

            return(tile);
        }
        public static void PasteComponentAsNew( Tile tile )
        {
            if( !IsCopiedComponent() )
            {
                Debug.LogError( "Not copied component." );
                return;
            }

            Undo.RecordObject( tile,"AddComponent" );

            System.Type classType = _CopiedComponent.GetType();
            TileComponent addComponent = tile.AddComponent( classType );

            AssetDatabase.AddObjectToAsset( addComponent,tile );

            EditorUtility.CopySerialized( _CopiedComponent,addComponent );

            tileField.SetValue( addComponent,tile );

            Undo.RegisterCreatedObjectUndo( addComponent,"AddComponent" );

            EditorUtility.SetDirty( tile );
        }
Example #6
0
        private void GenerateMap()
        {
            var map = Main.Map;

            Rand.ReseedRandom();
            Noise n = new Noise(Rand.Range(0, 10000));

            LoadingScreenText = "Generating map... Placing tiles...";

            // First iteration to place tiles.
            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Depth; y++)
                {
                    for (int z = 0; z < map.Height; z++)
                    {
                        const float SCALE  = 0.035f;
                        Vector2     offset = new Vector2(500, 300);
                        float       perlin = n.GetPerlin(x * SCALE + offset.X, y * SCALE + offset.Y, z * SCALE);
                        bool        place  = z == 0 || perlin >= 0.7f;

                        // Prevent floating tiles.
                        var below = map.GetTile(x, y, z - 1);
                        if (z != 0 && (below == null || below.IsType("Water")))
                        {
                            place = false;
                        }

                        if (place)
                        {
                            const float WATER_HEIGHT = 0.5f;
                            const float SAND_HEIGHT  = 0.55f;

                            Tile t;
                            if (z == 0 && perlin < WATER_HEIGHT)
                            {
                                t = Tile.Create("Water");
                            }
                            else if (perlin < SAND_HEIGHT)
                            {
                                t = Tile.Create(Rand.Chance(0.5f) ? "Sand" : "RedSand");
                            }
                            else
                            {
                                t = Tile.Create("Grass");
                            }

                            map.SetTileInternal(x, y, z, t);
                        }
                    }
                }
            }

            LoadingScreenText = "Generating map... Decorating tiles...";

            // Second iteration to place mountains, trees and all that stuff.
            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Depth; y++)
                {
                    for (int z = 0; z < map.Height; z++)
                    {
                        Tile t = map.GetTile(x, y, z);
                        if (t == null)
                        {
                            continue;
                        }
                        if (t.IsType("Water") || t.IsType("Sand"))
                        {
                            continue;
                        }

                        Tile above = map.GetTile(x, y, z + 1);
                        if (above == null)
                        {
                            if (Rand.Chance(0.04f))
                            {
                                t.AddComponent(TileComponent.Create("Mountain"), 0);
                            }
                            else if (Rand.Chance(0.05f))
                            {
                                t.AddComponent(TileComponent.Create("Trees"), 0);
                            }
                            else if (Rand.Chance(0.01f))
                            {
                                t.AddComponent(TileComponent.Create("House"), 0);
                            }
                            else if (Rand.Chance(0.001f))
                            {
                                t.AddComponent(TileComponent.Create("RedHouse"), 0);
                            }
                        }
                    }
                }
            }

            map.SendPlaceMessageToAll();
        }