+1 vote

i want it to snap to cell/ grid. cell size = 96x96
how should i do it?

onready var objec_for_fill = preload("res://objectforcell.tscn")

onready var sprite_node = get_parent().get_node("Sprite")

func areAllTilesInRectOfType(globalRect: Rect2, tileType: int) -> bool:
    var firstCellCoords: Vector2 = world_to_map(globalRect.position)
    var lastCellCoords: Vector2 = world_to_map(globalRect.end)
    for x in range(firstCellCoords.x,lastCellCoords.x + 1):
        for y in range(firstCellCoords.y,lastCellCoords.y + 1):
            if get_cell(x, y) != tileType:
                return false
    return true    

func _unhandled_input(event: InputEvent) -> void:

    sprite_node.global_position = get_global_mouse_position()
    if event.is_action_pressed("Lclick"):
        var spriteGlobalRect: Rect2 = sprite_node.global_transform.xform(sprite_node.get_rect())
        var tileTypeToCheckFor: int = 7
        if areAllTilesInRectOfType(spriteGlobalRect, tileTypeToCheckFor) :
            var test_obj = objec_for_fill.instance()
            test_obj.global_position = spriteGlobalRect.position + spriteGlobalRect.size/2

            print("obj pos",test_obj.position, "mouse pos", spriteGlobalRect.position)
in Engine by (419 points)

1 Answer

+3 votes

just put this

var mouse_pos = world_to_map(get_global_mouse_position())
sprite_node.global_position = map_to_world(mouse_pos)
by (419 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 Frequently asked questions and 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 [email protected] with your username.