Compare commits
8 Commits
first_bran
...
xonkaede
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b4c9ef899 | ||
| 62cf438b0d | |||
| 81054b3572 | |||
| 1a29a449b0 | |||
| b5eadea9c0 | |||
| 5b7f1a5ee4 | |||
| 2c207b94fb | |||
| 77973c54c6 |
7
UI/selection/selection.tscn
Normal file
7
UI/selection/selection.tscn
Normal file
@@ -0,0 +1,7 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://dyvpxcmnnre1i"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://dq0at84j2c0av" path="res://assets/selection.png" id="1_1haqg"]
|
||||
|
||||
[node name="Selection" type="Sprite2D"]
|
||||
z_index = 1024
|
||||
texture = ExtResource("1_1haqg")
|
||||
BIN
assets/moonbase1.png
Normal file
BIN
assets/moonbase1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
34
assets/moonbase1.png.import
Normal file
34
assets/moonbase1.png.import
Normal file
@@ -0,0 +1,34 @@
|
||||
[remap]
|
||||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://dqkc6jqfc6bxp"
|
||||
path="res://.godot/imported/moonbase1.png-3dab277b7e316bd64644cfdb9a2e6cd4.ctex"
|
||||
metadata={
|
||||
"vram_texture": false
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://assets/moonbase1.png"
|
||||
dest_files=["res://.godot/imported/moonbase1.png-3dab277b7e316bd64644cfdb9a2e6cd4.ctex"]
|
||||
|
||||
[params]
|
||||
|
||||
compress/mode=0
|
||||
compress/high_quality=false
|
||||
compress/lossy_quality=0.7
|
||||
compress/hdr_compression=1
|
||||
compress/normal_map=0
|
||||
compress/channel_pack=0
|
||||
mipmaps/generate=false
|
||||
mipmaps/limit=-1
|
||||
roughness/mode=0
|
||||
roughness/src_normal=""
|
||||
process/fix_alpha_border=true
|
||||
process/premult_alpha=false
|
||||
process/normal_map_invert_y=false
|
||||
process/hdr_as_srgb=false
|
||||
process/hdr_clamp_exposure=false
|
||||
process/size_limit=0
|
||||
detect_3d/compress_to=1
|
||||
BIN
assets/moonnauts.png
Normal file
BIN
assets/moonnauts.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 937 B |
34
assets/moonnauts.png.import
Normal file
34
assets/moonnauts.png.import
Normal file
@@ -0,0 +1,34 @@
|
||||
[remap]
|
||||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://bhpnk3olpquth"
|
||||
path="res://.godot/imported/moonnauts.png-9888b6b5f38f2c677f48207439089193.ctex"
|
||||
metadata={
|
||||
"vram_texture": false
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://assets/moonnauts.png"
|
||||
dest_files=["res://.godot/imported/moonnauts.png-9888b6b5f38f2c677f48207439089193.ctex"]
|
||||
|
||||
[params]
|
||||
|
||||
compress/mode=0
|
||||
compress/high_quality=false
|
||||
compress/lossy_quality=0.7
|
||||
compress/hdr_compression=1
|
||||
compress/normal_map=0
|
||||
compress/channel_pack=0
|
||||
mipmaps/generate=false
|
||||
mipmaps/limit=-1
|
||||
roughness/mode=0
|
||||
roughness/src_normal=""
|
||||
process/fix_alpha_border=true
|
||||
process/premult_alpha=false
|
||||
process/normal_map_invert_y=false
|
||||
process/hdr_as_srgb=false
|
||||
process/hdr_clamp_exposure=false
|
||||
process/size_limit=0
|
||||
detect_3d/compress_to=1
|
||||
BIN
assets/selection.png
Normal file
BIN
assets/selection.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 186 B |
34
assets/selection.png.import
Normal file
34
assets/selection.png.import
Normal file
@@ -0,0 +1,34 @@
|
||||
[remap]
|
||||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://dq0at84j2c0av"
|
||||
path="res://.godot/imported/selection.png-fb5449805f2ef00ca05e408b4d47d602.ctex"
|
||||
metadata={
|
||||
"vram_texture": false
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://assets/selection.png"
|
||||
dest_files=["res://.godot/imported/selection.png-fb5449805f2ef00ca05e408b4d47d602.ctex"]
|
||||
|
||||
[params]
|
||||
|
||||
compress/mode=0
|
||||
compress/high_quality=false
|
||||
compress/lossy_quality=0.7
|
||||
compress/hdr_compression=1
|
||||
compress/normal_map=0
|
||||
compress/channel_pack=0
|
||||
mipmaps/generate=false
|
||||
mipmaps/limit=-1
|
||||
roughness/mode=0
|
||||
roughness/src_normal=""
|
||||
process/fix_alpha_border=true
|
||||
process/premult_alpha=false
|
||||
process/normal_map_invert_y=false
|
||||
process/hdr_as_srgb=false
|
||||
process/hdr_clamp_exposure=false
|
||||
process/size_limit=0
|
||||
detect_3d/compress_to=1
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 24 KiB |
30
npc/npc.gd
Normal file
30
npc/npc.gd
Normal file
@@ -0,0 +1,30 @@
|
||||
class_name NPC
|
||||
extends Node2D
|
||||
|
||||
var id_path: Array[Vector2i]
|
||||
|
||||
func is_walking() -> bool:
|
||||
return not id_path.is_empty()
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
randomize()
|
||||
$Sprite2D.set_nation(randi()%$Sprite2D.Nations.max)
|
||||
set_spacesuit(false)
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _physics_process(delta: float) -> void:
|
||||
if id_path.is_empty(): return
|
||||
|
||||
var target_pos = Vector2(id_path.front()*World.tile_size)
|
||||
|
||||
global_position = global_position.move_toward(target_pos, 1)
|
||||
|
||||
if global_position == target_pos:
|
||||
id_path.pop_front()
|
||||
|
||||
func set_spacesuit(suit: bool) -> void:
|
||||
$Sprite2D.set_spacesuit(suit)
|
||||
|
||||
19
npc/npc.tscn
Normal file
19
npc/npc.tscn
Normal file
@@ -0,0 +1,19 @@
|
||||
[gd_scene load_steps=5 format=3 uid="uid://cxueg5xm4buqk"]
|
||||
|
||||
[ext_resource type="Script" path="res://npc/npc.gd" id="1_m82ir"]
|
||||
[ext_resource type="Texture2D" uid="uid://bhpnk3olpquth" path="res://assets/moonnauts.png" id="1_vgeae"]
|
||||
[ext_resource type="Script" path="res://npc/sprite_2d.gd" id="2_jtl2q"]
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_2v0ml"]
|
||||
atlas = ExtResource("1_vgeae")
|
||||
region = Rect2(0, 0, 16, 16)
|
||||
|
||||
[node name="npc" type="Node2D"]
|
||||
z_index = 32
|
||||
z_as_relative = false
|
||||
script = ExtResource("1_m82ir")
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
texture = SubResource("AtlasTexture_2v0ml")
|
||||
centered = false
|
||||
script = ExtResource("2_jtl2q")
|
||||
27
npc/sprite_2d.gd
Normal file
27
npc/sprite_2d.gd
Normal file
@@ -0,0 +1,27 @@
|
||||
extends Sprite2D
|
||||
|
||||
enum Nations {
|
||||
ru = 0,
|
||||
arab = 1,
|
||||
chi = 2,
|
||||
max,
|
||||
}
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func set_spacesuit(suit: bool) -> void:
|
||||
var atlas: AtlasTexture = texture
|
||||
if suit: atlas.region.position.y = 0
|
||||
else: atlas.region.position.y = 16
|
||||
|
||||
|
||||
func set_nation(nation: Nations) -> void:
|
||||
var atlas: AtlasTexture = texture
|
||||
atlas.region.position.x = 16*nation
|
||||
@@ -11,7 +11,7 @@ config_version=5
|
||||
[application]
|
||||
|
||||
config/name="TestProject"
|
||||
run/main_scene="res://world.tscn"
|
||||
run/main_scene="res://world/world.tscn"
|
||||
config/features=PackedStringArray("4.3", "GL Compatibility")
|
||||
config/icon="res://assets/icon.svg"
|
||||
|
||||
@@ -26,7 +26,30 @@ window/stretch/aspect="keep_width"
|
||||
|
||||
project/assembly_name="TestProject"
|
||||
|
||||
[editor_plugins]
|
||||
|
||||
enabled=PackedStringArray()
|
||||
|
||||
[input]
|
||||
|
||||
left_click={
|
||||
"deadzone": 0.5,
|
||||
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null)
|
||||
]
|
||||
}
|
||||
left_mouse={
|
||||
"deadzone": 0.5,
|
||||
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(92, 19),"global_position":Vector2(101, 65),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null)
|
||||
]
|
||||
}
|
||||
right_mouse={
|
||||
"deadzone": 0.5,
|
||||
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":2,"position":Vector2(277, 13),"global_position":Vector2(286, 59),"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null)
|
||||
]
|
||||
}
|
||||
|
||||
[rendering]
|
||||
|
||||
textures/canvas_textures/default_texture_filter=0
|
||||
renderer/rendering_method="gl_compatibility"
|
||||
renderer/rendering_method.mobile="gl_compatibility"
|
||||
|
||||
76
tile_map.gd
76
tile_map.gd
@@ -1,76 +0,0 @@
|
||||
extends Node2D
|
||||
|
||||
enum StructType {
|
||||
EMPTY_ROOM, # 0. Пустой модуль
|
||||
RESIDENTIAL_ROOM, # 1. Жилой Модуль
|
||||
LABORATORY_ROOM, # 2. Лабораторный Модуль
|
||||
ENERGY_ROOM, # 3. Энергетический Модуль
|
||||
FARM_ROOM, # 4. Фермерский Модуль
|
||||
LIFE_SUPPORT_ROOM, # 5. Модуль Жизнеобеспечения
|
||||
CONTROL_ROOM, # 6. Модуль Управления
|
||||
OBSERVATORY_ROOM, # 7. Модуль Обсерватории
|
||||
MEDICAL_ROOM, # 8. Медицинский Модуль
|
||||
GEOLOGICAL_ROOM, # 9. Геологический Модуль
|
||||
RESOURCE_EXTRACTION_ROOM, # 10. Модуль Добычи и Переработки Ресурсов
|
||||
COMMUNICATIONS_ROOM # 11. Модуль Связи и Навигации
|
||||
}
|
||||
|
||||
enum struct_fields {
|
||||
SOURCE_ID,
|
||||
SIZE,
|
||||
POS_ATLAS,
|
||||
LAYER
|
||||
}
|
||||
|
||||
@onready var structs = {
|
||||
StructType.EMPTY_ROOM: {
|
||||
struct_fields.SOURCE_ID: 1,
|
||||
struct_fields.SIZE: Vector2i(5,5),
|
||||
struct_fields.POS_ATLAS: Vector2i(0,0),
|
||||
struct_fields.LAYER: $buildings/buildings0
|
||||
}
|
||||
}
|
||||
|
||||
var layers: Array[TileMapLayer]
|
||||
var layers_dict: Dictionary
|
||||
|
||||
func _list_childrens(node: Node2D):
|
||||
for child in node.get_children():
|
||||
if child is TileMapLayer:
|
||||
layers.append(child)
|
||||
layers_dict.get_or_add(child.name, child)
|
||||
elif child is Node2D:
|
||||
_list_childrens(child)
|
||||
pass
|
||||
|
||||
func _ready() -> void:
|
||||
layers = []
|
||||
_list_childrens(self)
|
||||
|
||||
func get_maxZ(tile_pos: Vector2i) -> int:
|
||||
for layer in layers:
|
||||
if layer.get_cell_source_id(tile_pos) != -1:
|
||||
return layer.z_index
|
||||
return -1
|
||||
|
||||
func place_struct(pos: Vector2i, type: StructType):
|
||||
#Is place free check
|
||||
var struct_size = structs[type][struct_fields.SIZE]
|
||||
var layer = structs[type][struct_fields.LAYER]
|
||||
var source_id = structs[type][struct_fields.SOURCE_ID]
|
||||
var pos_atlas = structs[type][struct_fields.POS_ATLAS]
|
||||
|
||||
var half_size = Vector2i(struct_size.x / 2, struct_size.y / 2)
|
||||
|
||||
for x in range(struct_size.x):
|
||||
for y in range(struct_size.y):
|
||||
var offset = Vector2i(x, y)
|
||||
var tile_pos = pos+offset-half_size
|
||||
if get_maxZ(tile_pos) >= layer.z_index:
|
||||
return
|
||||
|
||||
for x in range(struct_size.x):
|
||||
for y in range(struct_size.y):
|
||||
var offset = Vector2i(x, y)
|
||||
var tile_pos = pos+offset-half_size
|
||||
layer.set_cell(tile_pos, source_id, Vector2i(x,y)+pos_atlas)
|
||||
26
world.gd
26
world.gd
@@ -1,26 +0,0 @@
|
||||
extends Node2D
|
||||
|
||||
# Переменная для хранения ссылки на TileMap
|
||||
@onready var buildings: TileMapLayer = $TileMap/buildings/buildings0
|
||||
@onready var roads: TileMapLayer = $TileMap/roads
|
||||
@onready var ground: TileMapLayer = $TileMap/ground
|
||||
|
||||
var is_drawing_road: bool = false # Флаг для отслеживания зажатия кнопки мыши
|
||||
var is_drawing_mountian: bool = false # Флаг для отслеживания зажатия кнопки мыши
|
||||
|
||||
func _ready():
|
||||
pass
|
||||
|
||||
func _input(event):
|
||||
if event is InputEventMouseButton:
|
||||
# Проверяем нажатие или отпускание левой кнопки мыши
|
||||
if event.button_index == MOUSE_BUTTON_LEFT:
|
||||
is_drawing_road = event.pressed # Устанавливаем флаг, если кнопка нажата
|
||||
elif event.button_index == MOUSE_BUTTON_RIGHT:
|
||||
$TileMap.place_struct(ground.local_to_map(get_global_mouse_position()-$TileMap.global_position), $TileMap.StructType.EMPTY_ROOM)
|
||||
|
||||
if event is InputEventMouseMotion and is_drawing_road:
|
||||
# Получаем позицию мыши в мировых координатах
|
||||
var current_tile:Vector2 = ground.local_to_map(get_global_mouse_position()-$TileMap.global_position)
|
||||
if buildings.get_cell_source_id(current_tile) == -1:
|
||||
roads.set_cells_terrain_connect([current_tile], 0, 2, true)
|
||||
38
world/npcs.gd
Normal file
38
world/npcs.gd
Normal file
@@ -0,0 +1,38 @@
|
||||
extends Node2D
|
||||
|
||||
const NPC_SCENE = preload("res://npc/npc.tscn")
|
||||
|
||||
var npcs: Array[NPC]
|
||||
|
||||
|
||||
func add_npc(pos: Vector2i):
|
||||
var npc: NPC = NPC_SCENE.instantiate()
|
||||
npc.translate(pos*World.tile_size)
|
||||
npcs.append(npc)
|
||||
add_child(npc)
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
randomize()
|
||||
add_npc(Vector2i(11, 8))
|
||||
add_npc(Vector2i(9, 9))
|
||||
add_npc(Vector2i(12, 11))
|
||||
|
||||
|
||||
|
||||
|
||||
func set_npc_target(npc: NPC, target: Vector2i):
|
||||
var id_path = $"../TileMap".astar_grid.get_id_path(
|
||||
$"../TileMap/ground".local_to_map(npc.global_position+$"../TileMap/ground".global_position),
|
||||
target
|
||||
).slice(1)
|
||||
|
||||
if not id_path.is_empty():
|
||||
npc.id_path = id_path
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _physics_process(delta: float) -> void:
|
||||
for npc in npcs:
|
||||
if not npc.is_walking():
|
||||
set_npc_target(npc, Vector2i(randi()%64,randi()%64))
|
||||
pass
|
||||
246
world/tile_map.gd
Normal file
246
world/tile_map.gd
Normal file
@@ -0,0 +1,246 @@
|
||||
class_name MyTileMap
|
||||
extends Node2D
|
||||
|
||||
enum StructType {
|
||||
EMPTY_ROOM, # 0. Пустой модуль
|
||||
RESIDENTIAL_ROOM, # 1. Жилой Модуль
|
||||
LABORATORY_ROOM, # 2. Лабораторный Модуль
|
||||
ENERGY_ROOM, # 3. Энергетический Модуль
|
||||
FARM_ROOM, # 4. Фермерский Модуль
|
||||
LIFE_SUPPORT_ROOM, # 5. Модуль Жизнеобеспечения
|
||||
CONTROL_ROOM, # 6. Модуль Управления
|
||||
OBSERVATORY_ROOM, # 7. Модуль Обсерватории
|
||||
MEDICAL_ROOM, # 8. Медицинский Модуль
|
||||
GEOLOGICAL_ROOM, # 9. Геологический Модуль
|
||||
RESOURCE_EXTRACTION_ROOM, # 10. Модуль Добычи и Переработки Ресурсов
|
||||
COMMUNICATIONS_ROOM, # 11. Модуль Связи и Навигации_
|
||||
|
||||
V_WAY, H_WAY, WAY_UP, WAY_DOWN, WAY_LEFT, WAY_RIGHT,
|
||||
}
|
||||
|
||||
enum struct_fields {
|
||||
SOURCE_ID,
|
||||
SIZE,
|
||||
POS_ATLAS,
|
||||
LAYER
|
||||
}
|
||||
|
||||
@onready var structs = {
|
||||
StructType.EMPTY_ROOM: {
|
||||
struct_fields.SOURCE_ID: 1,
|
||||
struct_fields.SIZE: Vector2i(5,5),
|
||||
struct_fields.POS_ATLAS: Vector2i(0,0),
|
||||
struct_fields.LAYER: $buildings/buildings0
|
||||
},
|
||||
StructType.V_WAY: {
|
||||
struct_fields.SOURCE_ID: 1,
|
||||
struct_fields.SIZE: Vector2i(3,1),
|
||||
struct_fields.POS_ATLAS: Vector2i(0,6),
|
||||
struct_fields.LAYER: $buildings/buildings1
|
||||
},
|
||||
StructType.H_WAY: {
|
||||
struct_fields.SOURCE_ID: 1,
|
||||
struct_fields.SIZE: Vector2i(1,3),
|
||||
struct_fields.POS_ATLAS: Vector2i(4,5),
|
||||
struct_fields.LAYER: $buildings/buildings1
|
||||
},
|
||||
StructType.WAY_UP: {
|
||||
struct_fields.SOURCE_ID: 1,
|
||||
struct_fields.SIZE: Vector2i(3,1),
|
||||
struct_fields.POS_ATLAS: Vector2i(0,5),
|
||||
struct_fields.LAYER: $buildings/buildings1
|
||||
},
|
||||
StructType.WAY_DOWN: {
|
||||
struct_fields.SOURCE_ID: 1,
|
||||
struct_fields.SIZE: Vector2i(3,1),
|
||||
struct_fields.POS_ATLAS: Vector2i(0,7),
|
||||
struct_fields.LAYER: $buildings/buildings1
|
||||
},
|
||||
StructType.WAY_LEFT: {
|
||||
struct_fields.SOURCE_ID: 1,
|
||||
struct_fields.SIZE: Vector2i(1,3),
|
||||
struct_fields.POS_ATLAS: Vector2i(3,5),
|
||||
struct_fields.LAYER: $buildings/buildings1
|
||||
},
|
||||
StructType.WAY_RIGHT: {
|
||||
struct_fields.SOURCE_ID: 1,
|
||||
struct_fields.SIZE: Vector2i(1,3),
|
||||
struct_fields.POS_ATLAS: Vector2i(5,5),
|
||||
struct_fields.LAYER: $buildings/buildings1
|
||||
},
|
||||
}
|
||||
|
||||
var layers: Array[TileMapLayer]
|
||||
var layers_dict: Dictionary
|
||||
var astar_grid: AStarGrid2D
|
||||
|
||||
func _list_childrens(node: Node2D):
|
||||
for child in node.get_children():
|
||||
if child is TileMapLayer:
|
||||
layers.append(child)
|
||||
layers_dict.get_or_add(child.name, child)
|
||||
elif child is Node2D:
|
||||
_list_childrens(child)
|
||||
pass
|
||||
|
||||
func _ready() -> void:
|
||||
layers = []
|
||||
_list_childrens(self)
|
||||
astar_grid = AStarGrid2D.new()
|
||||
astar_grid.region = layers_dict["ground"].get_used_rect()
|
||||
astar_grid.cell_size = Vector2i(16, 16)
|
||||
astar_grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
|
||||
astar_grid.update()
|
||||
|
||||
for x in $ground.get_used_rect().size.x:
|
||||
for y in $ground.get_used_rect().size.y:
|
||||
var tile_pos = Vector2i(x,y)
|
||||
update_astar_tile(tile_pos)
|
||||
|
||||
|
||||
|
||||
func get_maxZ(tile_pos: Vector2i) -> int:
|
||||
var max_z: int = -1024
|
||||
for layer: TileMapLayer in layers:
|
||||
if layer.get_cell_source_id(tile_pos) != -1 and layer.z_index > max_z:
|
||||
max_z = layer.z_index
|
||||
return max_z
|
||||
|
||||
func get_toplayer(pos: Vector2i) -> TileMapLayer:
|
||||
var toplayer: TileMapLayer = null
|
||||
for layer: TileMapLayer in layers:
|
||||
if layer.get_cell_tile_data(pos) != null:
|
||||
if toplayer == null: toplayer = layer
|
||||
elif layer.z_index > toplayer.z_index: toplayer = layer
|
||||
return toplayer
|
||||
|
||||
func place_road(pos: Vector2i) -> bool:
|
||||
var layer: TileMapLayer = layers_dict["roads"]
|
||||
if get_maxZ(pos) >= layer.z_index:
|
||||
return false
|
||||
layer.set_cells_terrain_connect([pos], 0, 2, true)
|
||||
update_astar_tile(pos)
|
||||
return true
|
||||
func erase_road(pos: Vector2i) -> bool:
|
||||
var layer: TileMapLayer = layers_dict["roads"]
|
||||
layer.erase_cell(pos)
|
||||
update_astar_tile(pos)
|
||||
return true
|
||||
|
||||
func place_way(pos1: Vector2i, pos2: Vector2i) -> bool:
|
||||
if pos1.x == pos2.x and pos1.y == pos2.y: return false
|
||||
if pos1.x != pos2.x and pos1.y != pos2.y: return false
|
||||
|
||||
var tiledata1: TileData = layers_dict["buildings0"].get_cell_tile_data(pos1)
|
||||
var tiledata2: TileData = layers_dict["buildings0"].get_cell_tile_data(pos2)
|
||||
|
||||
if tiledata1 == null or tiledata2 == null: return false
|
||||
|
||||
if not (tiledata1.get_custom_data("is_center") and tiledata1.get_custom_data("struct_name")=="EMPTY_ROOM"): return false
|
||||
if not (tiledata2.get_custom_data("is_center") and tiledata2.get_custom_data("struct_name")=="EMPTY_ROOM"): return false
|
||||
|
||||
#Check place is free
|
||||
if pos1.x != pos2.x:
|
||||
if abs(pos1.x-pos2.x)<=5: return false
|
||||
for x in range(min(pos1.x,pos2.x)+2, max(pos1.x,pos2.x)-1):
|
||||
if get_maxZ(Vector2i(x, pos1.y)) >= structs[StructType.H_WAY][struct_fields.LAYER].z_index:
|
||||
return false
|
||||
if pos1.y != pos2.y:
|
||||
if abs(pos1.y-pos2.y)<=5: return false
|
||||
for y in range(min(pos1.y,pos2.y)+2, max(pos1.y,pos2.y)-1):
|
||||
if get_maxZ(Vector2i(pos1.x, y)) >= structs[StructType.V_WAY][struct_fields.LAYER].z_index:
|
||||
return false
|
||||
|
||||
|
||||
if pos1.x != pos2.x:
|
||||
place_struct(Vector2i(min(pos1.x,pos2.x)+2, pos1.y), StructType.WAY_LEFT)
|
||||
place_struct(Vector2i(max(pos1.x,pos2.x)-2, pos1.y), StructType.WAY_RIGHT)
|
||||
for x in range(min(pos1.x,pos2.x)+3, max(pos1.x,pos2.x)-2):
|
||||
place_struct(Vector2i(x, pos1.y), StructType.H_WAY)
|
||||
if pos1.y != pos2.y:
|
||||
place_struct(Vector2i(pos1.x, min(pos1.y,pos2.y)+2), StructType.WAY_UP)
|
||||
place_struct(Vector2i(pos1.x, max(pos1.y,pos2.y)-2), StructType.WAY_DOWN)
|
||||
for y in range(min(pos1.y,pos2.y)+3, max(pos1.y,pos2.y)-2):
|
||||
place_struct(Vector2i(pos1.x, y), StructType.V_WAY)
|
||||
return true
|
||||
|
||||
func destroy_building(pos: Vector2i, only_ways: bool = false):
|
||||
for layer in layers:
|
||||
var tiledata: TileData = layer.get_cell_tile_data(pos)
|
||||
if tiledata != null and tiledata.get_custom_data("is_center"):
|
||||
if tiledata.get_custom_data("struct_name") == "H_WAY":
|
||||
remove_struct(pos)
|
||||
destroy_building(pos+Vector2i.LEFT)
|
||||
destroy_building(pos+Vector2i.RIGHT)
|
||||
elif tiledata.get_custom_data("struct_name") == "V_WAY":
|
||||
remove_struct(pos)
|
||||
destroy_building(pos+Vector2i.UP)
|
||||
destroy_building(pos+Vector2i.DOWN)
|
||||
elif not only_ways and tiledata.get_custom_data("struct_name") == "EMPTY_ROOM":
|
||||
remove_struct(pos)
|
||||
destroy_building(pos+Vector2i.LEFT*3, true)
|
||||
destroy_building(pos+Vector2i.RIGHT*3, true)
|
||||
destroy_building(pos+Vector2i.UP*3, true)
|
||||
destroy_building(pos+Vector2i.DOWN*3, true)
|
||||
elif not only_ways:
|
||||
remove_struct(pos)
|
||||
return
|
||||
|
||||
func remove_struct(pos: Vector2i):
|
||||
for layer in layers:
|
||||
var tiledata: TileData = layer.get_cell_tile_data(pos)
|
||||
if tiledata != null and tiledata.get_custom_data("is_center"):
|
||||
var struct_size = tiledata.get_custom_data("struct_size")
|
||||
var half_size = Vector2i(struct_size.x / 2, struct_size.y / 2)
|
||||
for x in range(struct_size.x):
|
||||
for y in range(struct_size.y):
|
||||
var offset = Vector2i(x, y)
|
||||
var tile_pos = pos+offset-half_size
|
||||
layer.erase_cell(tile_pos)
|
||||
update_astar_tile(tile_pos)
|
||||
return
|
||||
|
||||
func place_struct(pos: Vector2i, type: StructType) -> bool:
|
||||
if not structs.has(type):
|
||||
return false
|
||||
var struct_size = structs[type][struct_fields.SIZE]
|
||||
var layer = structs[type][struct_fields.LAYER]
|
||||
var source_id = structs[type][struct_fields.SOURCE_ID]
|
||||
var pos_atlas = structs[type][struct_fields.POS_ATLAS]
|
||||
|
||||
var half_size = Vector2i(struct_size.x / 2, struct_size.y / 2)
|
||||
|
||||
#Is place free check
|
||||
for x in range(struct_size.x):
|
||||
for y in range(struct_size.y):
|
||||
var offset = Vector2i(x, y)
|
||||
var tile_pos = pos+offset-half_size
|
||||
if get_maxZ(tile_pos) >= layer.z_index:
|
||||
return false
|
||||
|
||||
for x in range(struct_size.x):
|
||||
for y in range(struct_size.y):
|
||||
var offset = Vector2i(x, y)
|
||||
var tile_pos = pos+offset-half_size
|
||||
layer.set_cell(tile_pos, source_id, Vector2i(x,y)+pos_atlas)
|
||||
update_astar_tile(tile_pos)
|
||||
return true
|
||||
|
||||
func is_walkable(pos: Vector2i) -> bool:
|
||||
var state: bool = true
|
||||
for layer in layers:
|
||||
var tiledata: TileData = layer.get_cell_tile_data(pos)
|
||||
if tiledata != null and not tiledata.get_custom_data("walkable"):
|
||||
state = false
|
||||
break
|
||||
return state
|
||||
|
||||
func weight(pos: Vector2i) -> int:
|
||||
var layer = get_toplayer(pos)
|
||||
var tiledata: TileData = layer.get_cell_tile_data(pos)
|
||||
if tiledata != null: return tiledata.get_custom_data("astar_weight")
|
||||
else: return 5
|
||||
|
||||
func update_astar_tile(pos: Vector2i):
|
||||
astar_grid.set_point_solid(pos, not is_walkable(pos))
|
||||
astar_grid.set_point_weight_scale(pos, weight(pos))
|
||||
110
world/world.gd
Normal file
110
world/world.gd
Normal file
@@ -0,0 +1,110 @@
|
||||
class_name World
|
||||
extends Node2D
|
||||
|
||||
const tile_size: int = 16
|
||||
|
||||
# Переменная для хранения ссылки на TileMap
|
||||
@onready var ground: TileMapLayer = $TileMap/ground
|
||||
@onready var tileMap: Node2D = $TileMap
|
||||
|
||||
const SELECTION_SCENE = preload("res://UI/selection/selection.tscn")
|
||||
|
||||
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
|
||||
print(tileMap.is_walkable(selected_tile))
|
||||
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
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user