Godot Version
4.4.1.stable
Question
I tried to do this with tiles and code, but it got overly complicated over time. I decided to spawn Sprite2D as ‘cargo’ boxes for my game. The idea is for the game to spawn buttons that are then assigned to random cargo of a variety of shapes. Then you have to fit then into the ship’s cargohold but they cannot overlap each other. I was able to figure out how to spawn sprites via gdscript and have them follow the mouse pointer, but I do not know how to spawn the sprite so the mouse pointer is ‘centered’ on a corner of the sprite. And how do I ‘place’ a Sprite2D? And when the Sprite2D is placed, I want to prevent other shapes from overlapping it. I understand Collisionshap2D can prevent the Sprite2D from overlapping each other, but how do I set that if the Sprite2D is being made via code? Also, how can I make the Sprite2D ‘snap’ to the grid of the tilemap? The code below is what I have so far:
extends TileMapLayer
@onready var h_box_container: HBoxContainer = $"../HUD/Panel/HBoxContainer"
@onready var cargo: TileMapLayer = $"."
@onready var placed_cargo: TileMapLayer = $"../Placed_cargo"
var mouse_position: Vector2i
var old_mouse_position: Vector2i
func button_pressed(btn: Button, cargo_selector: int):
Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
var sprite = Sprite2D.new()
sprite.texture = load("res://sprites/L.png")
add_child(sprite)
btn.queue_free()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float):
mouse_position = local_to_map(get_local_mouse_position())
position = get_viewport().get_mouse_position()
if old_mouse_position != mouse_position:
erase_cell(old_mouse_position)
temp_occupied_tiles.clear()
old_mouse_position = mouse_position