PerduGames
PerduGames

Reputation: 1228

Paint tilemap around the player

In the last days I did it here:

https://github.com/PerduGames/SoftNoise-GDScript-

and now I can generate my "infinite" maps, however I have problems dealing with the generation of only parts of it as the player moves in a 2D scenario in Godot(GDScript).

I'm trying to paint an area around the player in the tilemap. With this function I take the position of the player:

func check_posChunk(var _posChunk, var _posPlayer):

var pos = $"../TileMap".world_to_map(_posPlayer)

for i in range(0, mapSize, 16):
    if pos >= Vector2(i, i) && pos <= Vector2(i + 16, i + 16):
        if pos.x > pos.y:
            _posChunk = Vector2(i, i) - Vector2(32, 48)
        else:
            _posChunk = Vector2(i, i) - Vector2(16, 16)         
        break
return _posChunk

where I store the position in the variable "posChunk" for and I paint here:

func redor(var posPlayer):

posChunk = check_posChunk(posChunk, posPlayer)

for x in range(64):
    for y in range(64):
        $"../TileMap".set_cell(posChunk.x + x, posChunk.y + y, biomes(elevation_array[posChunk.x + x][posChunk.y + y], umidade_array[posChunk.x + x][posChunk.y + y]))

I can paint around the player when x < y, and when x == y, but when x > y, complications occur, due to this here, even though I check the situation in the above if, there are cases where it will not paint as expected:

https://github.com/godotengine/godot/issues/9284

Upvotes: 0

Views: 442

Answers (1)

PerduGames
PerduGames

Reputation: 1228

How to handle the Vector2 comparison correctly?

I was able to find the answer to this case, answered in another forum, comparing Vector2 would not be the best way to do this, using Rect2 (get two Vector2, the first parameter is the position and the second the size) you can check if the player is inside a box, so this code below happens:

https://godotengine.org/qa/17982/how-to-compare-two-rect2?show=17994#c17994

#Verify that the pos that is the player's position 
#is inside the rect_chunk rectangle with the has_point function of Rect2.

var rect_chunk = Rect2(Vector2(i, i), Vector2(16, 16))
if(rect_chunk).has_point(pos)):

Upvotes: 0

Related Questions