119 lines
3.6 KiB
GDScript
119 lines
3.6 KiB
GDScript
extends Node2D
|
|
|
|
# Переменная для хранения ссылки на TileMap
|
|
@onready var ground: TileMapLayer = $TileMap/ground
|
|
@onready var tileMap: Node2D = $TileMap
|
|
|
|
const SELECTION_SCENE = preload("res://selection.tscn")
|
|
|
|
func _on_null_pressed() -> void:
|
|
change_instrument(instruments.NULL)
|
|
|
|
func _on_demolish_pressed() -> void:
|
|
change_instrument(instruments.DESTROY)
|
|
|
|
func _on_construct_pressed() -> void:
|
|
change_instrument(instruments.CONSTRUCT)
|
|
|
|
func _on_build_pressed() -> void:
|
|
change_instrument(instruments.BUILD_ROAD)
|
|
|
|
var selected_instrument: instruments
|
|
var selected_tile: Vector2i
|
|
var selected_tile2: Vector2i
|
|
var selected: bool
|
|
var selected2: bool
|
|
var selection: Node
|
|
var selection2: Node
|
|
var lmb: bool
|
|
var rmb: bool
|
|
|
|
func _ready():
|
|
selection = SELECTION_SCENE.instantiate()
|
|
selection2 = SELECTION_SCENE.instantiate()
|
|
add_child(selection)
|
|
add_child(selection2)
|
|
change_instrument(instruments.NULL)
|
|
|
|
enum instruments {
|
|
NULL,
|
|
DESTROY,
|
|
CONSTRUCT,
|
|
BUILD_ROAD
|
|
}
|
|
|
|
func select_tile(pos: Vector2i) -> Vector2i:
|
|
var toplayer: TileMapLayer = tileMap.get_toplayer(pos)
|
|
var tiledata: TileData = toplayer.get_cell_tile_data(pos)
|
|
if tiledata == null: return pos
|
|
|
|
while tiledata.get_custom_data("center_direction") != Vector2i.ZERO:
|
|
pos+=tiledata.get_custom_data("center_direction")
|
|
tiledata = toplayer.get_cell_tile_data(pos)
|
|
return pos
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if Input.is_action_just_released("left_mouse") and lmb:
|
|
selected_tile = select_tile(ground.local_to_map(get_global_mouse_position()-tileMap.global_position))
|
|
selected = true
|
|
if(selected_tile==selected_tile2):
|
|
selected_tile2 = Vector2i(-128,-128)
|
|
selected2 = false
|
|
if Input.is_action_just_released("right_mouse") and rmb:
|
|
selected_tile2 = select_tile(ground.local_to_map(get_global_mouse_position()-tileMap.global_position))
|
|
if(selected_tile!=selected_tile2): selected2 = true
|
|
else:
|
|
selected_tile2 = Vector2i(-128,-128)
|
|
selected2 = false
|
|
|
|
if Input.is_action_just_pressed("ui_cancel"):
|
|
change_instrument(instruments.NULL)
|
|
if Input.is_action_just_pressed("ui_right"):
|
|
change_instrument(selected_instrument+1)
|
|
if Input.is_action_just_pressed("ui_left"):
|
|
change_instrument(selected_instrument-1)
|
|
|
|
match selected_instrument:
|
|
instruments.NULL:
|
|
pass
|
|
instruments.DESTROY:
|
|
if Input.is_action_just_pressed("ui_accept") and selected:
|
|
tileMap.destroy_building(selected_tile)
|
|
instruments.CONSTRUCT:
|
|
if Input.is_action_just_pressed("ui_accept") and selected and selected2:
|
|
tileMap.place_way(selected_tile, selected_tile2)
|
|
elif Input.is_action_just_pressed("ui_accept") and selected:
|
|
tileMap.place_struct(selected_tile, tileMap.StructType.EMPTY_ROOM)
|
|
instruments.BUILD_ROAD:
|
|
selected_tile = ground.local_to_map(get_global_mouse_position()-tileMap.global_position)
|
|
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
|
|
tileMap.place_road(selected_tile)
|
|
elif Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
|
|
tileMap.erase_road(selected_tile)
|
|
selection.offset = ground.map_to_local(selected_tile)+tileMap.global_position
|
|
selection2.offset = ground.map_to_local(selected_tile2)+tileMap.global_position
|
|
|
|
func change_instrument(new: instruments):
|
|
selected_tile = Vector2i(-128, -128)
|
|
selected_tile2 = Vector2i(-128, -128)
|
|
selected = false
|
|
selected2 = false
|
|
selected_instrument = new
|
|
|
|
match selected_instrument:
|
|
instruments.NULL:
|
|
lmb = true
|
|
rmb = false
|
|
instruments.DESTROY:
|
|
lmb = true
|
|
rmb = false
|
|
instruments.CONSTRUCT:
|
|
lmb = true
|
|
rmb = true
|
|
instruments.BUILD_ROAD:
|
|
lmb = false
|
|
rmb = false
|
|
|
|
func _input(event):
|
|
pass
|