2024-12-08 13:00:34 +04:00
|
|
|
class_name World
|
2024-12-06 02:50:32 +04:00
|
|
|
extends Node2D
|
|
|
|
|
2024-12-08 13:00:34 +04:00
|
|
|
const tile_size: int = 16
|
|
|
|
|
2024-12-06 02:50:32 +04:00
|
|
|
# Переменная для хранения ссылки на TileMap
|
2024-12-06 21:03:26 +04:00
|
|
|
@onready var ground: TileMapLayer = $TileMap/ground
|
2024-12-07 14:43:59 +04:00
|
|
|
@onready var tileMap: Node2D = $TileMap
|
2024-12-06 21:03:26 +04:00
|
|
|
|
2024-12-08 13:00:34 +04:00
|
|
|
const SELECTION_SCENE = preload("res://UI/selection/selection.tscn")
|
2024-12-07 20:58:52 +04:00
|
|
|
|
|
|
|
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
|
2024-12-06 02:50:32 +04:00
|
|
|
|
|
|
|
func _ready():
|
2024-12-08 15:05:33 +04:00
|
|
|
Engine.physics_ticks_per_second = 60
|
2024-12-07 20:58:52 +04:00
|
|
|
selection = SELECTION_SCENE.instantiate()
|
|
|
|
selection2 = SELECTION_SCENE.instantiate()
|
|
|
|
add_child(selection)
|
|
|
|
add_child(selection2)
|
|
|
|
change_instrument(instruments.NULL)
|
2024-12-06 02:50:32 +04:00
|
|
|
|
2024-12-07 20:58:52 +04:00
|
|
|
enum instruments {
|
|
|
|
NULL,
|
|
|
|
DESTROY,
|
2024-12-08 03:17:52 +04:00
|
|
|
CONSTRUCT,
|
2024-12-07 20:58:52 +04:00
|
|
|
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
|
2024-12-08 03:17:52 +04:00
|
|
|
if(selected_tile==selected_tile2):
|
|
|
|
selected_tile2 = Vector2i(-128,-128)
|
|
|
|
selected2 = false
|
2024-12-08 13:00:34 +04:00
|
|
|
print(tileMap.is_walkable(selected_tile))
|
2024-12-07 20:58:52 +04:00
|
|
|
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))
|
2024-12-08 03:17:52 +04:00
|
|
|
if(selected_tile!=selected_tile2): selected2 = true
|
|
|
|
else:
|
|
|
|
selected_tile2 = Vector2i(-128,-128)
|
|
|
|
selected2 = false
|
2024-12-07 20:58:52 +04:00
|
|
|
|
|
|
|
if Input.is_action_just_pressed("ui_cancel"):
|
|
|
|
change_instrument(instruments.NULL)
|
|
|
|
|
|
|
|
match selected_instrument:
|
|
|
|
instruments.NULL:
|
|
|
|
pass
|
|
|
|
instruments.DESTROY:
|
|
|
|
if Input.is_action_just_pressed("ui_accept") and selected:
|
|
|
|
tileMap.destroy_building(selected_tile)
|
2024-12-08 03:17:52 +04:00
|
|
|
instruments.CONSTRUCT:
|
|
|
|
if Input.is_action_just_pressed("ui_accept") and selected and selected2:
|
2024-12-07 20:58:52 +04:00
|
|
|
tileMap.place_way(selected_tile, selected_tile2)
|
2024-12-08 03:17:52 +04:00
|
|
|
elif Input.is_action_just_pressed("ui_accept") and selected:
|
|
|
|
tileMap.place_struct(selected_tile, tileMap.StructType.EMPTY_ROOM)
|
2024-12-07 20:58:52 +04:00
|
|
|
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
|
2024-12-06 02:50:32 +04:00
|
|
|
|
2024-12-07 20:58:52 +04:00
|
|
|
match selected_instrument:
|
|
|
|
instruments.NULL:
|
|
|
|
lmb = true
|
|
|
|
rmb = false
|
|
|
|
instruments.DESTROY:
|
|
|
|
lmb = true
|
|
|
|
rmb = false
|
2024-12-08 03:17:52 +04:00
|
|
|
instruments.CONSTRUCT:
|
2024-12-07 20:58:52 +04:00
|
|
|
lmb = true
|
|
|
|
rmb = true
|
|
|
|
instruments.BUILD_ROAD:
|
2024-12-08 03:17:52 +04:00
|
|
|
lmb = false
|
2024-12-07 20:58:52 +04:00
|
|
|
rmb = false
|
|
|
|
|
2024-12-08 15:37:58 +04:00
|
|
|
func _process(delta: float) -> void:
|
|
|
|
if Input.is_action_pressed("ui_right"):
|
|
|
|
$Camera2D.translate(Vector2.RIGHT*320*delta/$Camera2D.zoom)
|
|
|
|
if Input.is_action_pressed("ui_left"):
|
|
|
|
$Camera2D.translate(Vector2.LEFT*320*delta/$Camera2D.zoom)
|
|
|
|
if Input.is_action_pressed("ui_up"):
|
|
|
|
$Camera2D.translate(Vector2.UP*320*delta/$Camera2D.zoom)
|
|
|
|
if Input.is_action_pressed("ui_down"):
|
|
|
|
$Camera2D.translate(Vector2.DOWN*320*delta/$Camera2D.zoom)
|
|
|
|
if Input.is_action_just_pressed("zoom_in"):
|
|
|
|
$Camera2D.zoom+=Vector2.ONE*0.25
|
|
|
|
if Input.is_action_just_pressed("zoom_out"):
|
|
|
|
$Camera2D.zoom-=Vector2.ONE*0.25
|