Godot Version
4.4.stable
Question
I have been able to make an AnimatedSprite2D that changes animation speed based on wind speed. I tried to write my code to where it places this animated sprite onto the tilemaplayer. However, another error happened as a result. The ‘preview’ version of the wind turbine shows up hovering over the mouse AND at the center of the map. When I left click to place the building, the ‘preview’ version of the tile behaves the way I intended, but the ‘built’ wind turbines are static. I have no idea why this is, but I suspect there is only a small change in code needed. The link below is the video showing what I am experiencing:
Code below:
extends TileMapLayer
@onready var terrain: TileMapLayer = $"../Terrain"
@onready var buildings: TileMapLayer = $"../Buildings"
@onready var wind_turbine: AnimatedSprite2D = $Wind_turbine
var width: float = Globals.width
var height: float = Globals.height
var source_id: int
var build_status: bool = false
var building_vector: Vector2i
var mouse_position: Vector2i
var old_mouse_position: Vector2i
var alt_id: int = 0
var solar_qty: int = 0
var wind_qty: int = 0
func _ready():
#Ensures no building is shown on the mouse pointer on startup
source_id = -1
#Makes the preview version of the tile the default setting
modulate.a = 0.5
func _on_thermal_solar_pressed():
build_status = true
source_id = 0
building_vector = Vector2i(5, 13)
func _on_wind_turbine_pressed():
build_status = true
source_id = 0
building_vector = Vector2i(0, 4)
wind_turbine.visible = true
func _on_demolish_pressed():
build_status = true
source_id = -1
func _input(event: InputEvent):
#Creates preview version of building sprite, hovers on mouse position
if event is InputEventMouseMotion and build_status == true:
#Gets the new coordinates of mouse pointer when mouse moves
mouse_position = local_to_map(get_local_mouse_position())
#Erases tile in old position, so the building preview 'follows' the mouse
if old_mouse_position != mouse_position:
erase_cell(old_mouse_position)
#Places the building tile onto the grid and stores the new tile coordinates
set_cell(mouse_position, source_id, building_vector)
old_mouse_position = mouse_position
#Indicates if tile is occupied
if buildings.get_cell_atlas_coords(mouse_position) != Vector2i(-1, -1):
modulate = Color.RED
else:
make_transparent()
if Input.is_action_pressed("click"):
#Prevents building on an occupied space
if buildings.get_cell_atlas_coords(mouse_position) != Vector2i(-1, -1):
make_transparent()
#Places building on Buildings tilemaplayer
elif build_status == true and source_id != -1:
if building_vector == Vector2i(0, 4):
buildings.set_cell(mouse_position, 3, Vector2i.ZERO, 1)
else:
buildings.set_cell(mouse_position, source_id, building_vector)
erase_cell(mouse_position)
build_status = false
match building_vector:
Vector2i(5, 13): solar_qty += 1
Vector2i(0, 4): wind_qty += 1
print("Solar qty: " + str(solar_qty))
print("Wind qty: " + str(wind_qty))
#Erases atlas coordinates at tile
if build_status == true and source_id == -1:
match buildings.get_cell_atlas_coords(mouse_position):
Vector2i(5, 13): solar_qty -= 1
Vector2i.ZERO: wind_qty -= 1
buildings.erase_cell(mouse_position)
erase_cell(mouse_position)
if Input.is_action_pressed("right_click"):
build_status = false
erase_cell(old_mouse_position)
func make_transparent():
modulate = Color.TRANSPARENT
modulate.a = 0.5