+2 votes

Hello,
I have made a tile map autotiler and the texture gets repetitive when grouped together so I was wondering if there is a way to randomly rotate each tile at either 0, 90, 180, 270 degrees through code (gdscript).

Thanks in advance for any help!

in Engine by (16 points)

1 Answer

0 votes

you can use the three bool (flipX, flipY, and Blah) parameters in set_cell to setup any tile orientation

func _setRandomTile(map: TileMap, x: int, y: int, id: int, flipX: bool = false, flipY: bool = false, rot90: bool = false) -> void:
    map.set_cell(x, y, id, flipX, flipY, rot90, _randomTile(id))

func _randomTile(id: int) -> Vector2:
    var p := Vector2.ZERO
    var s := _tileSet.tile_get_region(id).size / _tileSet.autotile_get_size(id)
    var total := 0
    for y in range(s.y):
        for x in range(s.x):
            total += _tileSet.autotile_get_subtile_priority(id, Vector2(x, y))
    var selected := Random.next(total)
    var current := 0
    for y in range(s.y):
        for x in range(s.x):
            p = Vector2(x, y)
            current += _tileSet.autotile_get_subtile_priority(id, p)
            if current > selected:
                return p
    return p

func _setBackRandom(x: int, y: int, tile: int, flipX := false, flipY := false, rot90 := false) -> void:
    _setRandomTile(_back, x, y, tile, flipX, flipY, rot90)

func setFloor(x: int, y: int, wonky := false) -> void:
    var id
    match theme:
        0: id = Tile.Theme0Floor
        1: id = Tile.Theme1Floor
        2: id = Tile.Theme2Floor
        3: id = Tile.Theme3Floor
    var flipX := Random.nextBool() if wonky else false
    var flipY := Random.nextBool() if wonky else false
    var rot90 := Random.nextBool() if wonky else false
    _setBackRandom(x, y, id, flipX, flipY, rot90)

https://gotm.io/henry-software/pixelleveltest/

my wall tiles flip horizontally and sometimes (if wonky) my floor tiles flip and rotate in all directions creating new patterns

by (1,665 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.