Change how map loading works
This commit is contained in:
@@ -10,6 +10,17 @@ signal tile_hovered(coords: Vector2i)
|
||||
const TILE_SIZE := 48.0
|
||||
const SOURCE_ID: int = 0
|
||||
|
||||
var _pending_layout: String
|
||||
var _pending_units: Array[Dictionary] = []
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
if _pending_layout:
|
||||
_apply_layout(_pending_layout)
|
||||
for entry in _pending_units:
|
||||
_apply_deploy(entry.unit, entry.coords)
|
||||
_pending_units.clear()
|
||||
|
||||
|
||||
func snap_to_grid(pos: Vector2) -> Vector2:
|
||||
return Vector2(floorf(pos.x / TILE_SIZE), floorf(pos.y / TILE_SIZE)) * TILE_SIZE
|
||||
@@ -31,6 +42,37 @@ func draw_floor(coords: Vector2i) -> void:
|
||||
func draw_custom(coords: Vector2i, tile_coords: Vector2i) -> void:
|
||||
tile_map.set_cell(coords, SOURCE_ID, tile_coords)
|
||||
|
||||
func load_map(layout: String) -> void:
|
||||
if is_node_ready():
|
||||
_apply_layout(layout)
|
||||
else:
|
||||
_pending_layout = layout
|
||||
|
||||
|
||||
func deploy_unit(unit: Unit, coords: Vector2i) -> void:
|
||||
if is_node_ready():
|
||||
_apply_deploy(unit, coords)
|
||||
else:
|
||||
_pending_units.append({unit = unit, coords = coords})
|
||||
|
||||
|
||||
func _apply_layout(layout: String) -> void:
|
||||
var rows := layout.split("\n")
|
||||
for y in rows.size():
|
||||
for x in rows[y].length():
|
||||
var coords := Vector2i(x, y)
|
||||
match rows[y][x]:
|
||||
"#":
|
||||
draw_wall(coords)
|
||||
".":
|
||||
draw_floor(coords)
|
||||
|
||||
|
||||
func _apply_deploy(unit: Unit, coords: Vector2i) -> void:
|
||||
unit.position = coords_to_world(coords)
|
||||
add_child(unit)
|
||||
|
||||
|
||||
func is_wall(coords: Vector2i) -> bool:
|
||||
return tile_map.get_cell_atlas_coords(coords) == tile_set.wall_tile_coords
|
||||
|
||||
|
||||
@@ -72,9 +72,8 @@ func _select_unit(unit: Unit) -> void:
|
||||
|
||||
func _get_unit_at(world_pos: Vector2) -> Unit:
|
||||
var snapped := dl_map.snap_to_grid(world_pos)
|
||||
for child in get_parent().get_children():
|
||||
if child is Unit:
|
||||
var unit_snapped := dl_map.snap_to_grid(child.global_position)
|
||||
if unit_snapped == snapped:
|
||||
return child
|
||||
for unit: Unit in get_tree().get_nodes_in_group("units"):
|
||||
var unit_snapped := dl_map.snap_to_grid(unit.global_position)
|
||||
if unit_snapped == snapped:
|
||||
return unit
|
||||
return null
|
||||
|
||||
@@ -2,13 +2,9 @@
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://cy7r0udfcsqbn" path="res://prefabs/combat_ui.tscn" id="1_5jbmu"]
|
||||
[ext_resource type="PackedScene" uid="uid://dkhyh5ce4iuk3" path="res://prefabs/combat_map.tscn" id="1_7abyo"]
|
||||
[ext_resource type="Script" uid="uid://c8xb86ty7rduf" path="res://scripts/test_map_generator.gd" id="2_ekcfv"]
|
||||
[ext_resource type="Script" uid="uid://csdcbi2gtwrly" path="res://scripts/camera_controller.gd" id="3_cam"]
|
||||
[ext_resource type="Script" uid="uid://dfojm3n0em4ef" path="res://nodes/player_controller.gd" id="4_s5ga2"]
|
||||
[ext_resource type="AudioStream" uid="uid://dsikulned64qt" path="res://assets/music/combat_bgm_01.OGG" id="6_0yobm"]
|
||||
[ext_resource type="PackedScene" uid="uid://b6a7nlnf58mc4" path="res://prefabs/unit.tscn" id="6_rfoto"]
|
||||
[ext_resource type="Resource" uid="uid://dufi2h00j5vrq" path="res://resources/allegiance_types/player_allegiance.tres" id="7_0wg56"]
|
||||
[ext_resource type="Resource" uid="uid://cuc7kkknpsr1g" path="res://resources/allegiance_types/enemy_allegiance.tres" id="8_w105o"]
|
||||
|
||||
[node name="CombatTest" type="Node2D" unique_id=855645983]
|
||||
|
||||
@@ -24,12 +20,6 @@ dl_map = NodePath("../CombatMap")
|
||||
zoom = Vector2(1.5, 1.5)
|
||||
script = ExtResource("3_cam")
|
||||
|
||||
[node name="TestMapGenerator" type="Node" parent="." unique_id=833658301 node_paths=PackedStringArray("dl_map")]
|
||||
script = ExtResource("2_ekcfv")
|
||||
dl_map = NodePath("../CombatMap")
|
||||
unit_template = ExtResource("6_rfoto")
|
||||
player_allegiance = ExtResource("7_0wg56")
|
||||
enemy_allegiance = ExtResource("8_w105o")
|
||||
|
||||
[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="." unique_id=1057500234]
|
||||
stream = ExtResource("6_0yobm")
|
||||
|
||||
@@ -7,9 +7,45 @@
|
||||
resource_name = "StartButton"
|
||||
script/source = "extends Button
|
||||
|
||||
const COMBAT_SCENE = preload(\"res://scenes/combat_test.tscn\")
|
||||
const UNIT_SCENE = preload(\"res://prefabs/unit.tscn\")
|
||||
const PLAYER_ALLEGIANCE = preload(\"res://resources/allegiance_types/player_allegiance.tres\")
|
||||
const ENEMY_ALLEGIANCE = preload(\"res://resources/allegiance_types/enemy_allegiance.tres\")
|
||||
|
||||
const MAP_LAYOUT := \"\"\"\\
|
||||
#####
|
||||
#...#
|
||||
#...#
|
||||
#...#
|
||||
#####\"\"\"
|
||||
|
||||
func _pressed() -> void:
|
||||
get_parent().queue_free()
|
||||
get_tree().change_scene_to_file(\"res://scenes/combat_test.tscn\")
|
||||
var combat_instance := COMBAT_SCENE.instantiate()
|
||||
var combat_map: CombatMap = combat_instance.find_child(\"CombatMap\")
|
||||
|
||||
combat_map.load_map(MAP_LAYOUT)
|
||||
|
||||
var player_unit: Unit = UNIT_SCENE.instantiate()
|
||||
player_unit.stat_template = UnitStats.new(50)
|
||||
player_unit.info_template = UnitInfo.new()
|
||||
player_unit.info_template.name = \"Putit\"
|
||||
player_unit.allegiance_template = PLAYER_ALLEGIANCE
|
||||
combat_map.deploy_unit(player_unit, Vector2i(2, 2))
|
||||
|
||||
var enemy_unit: Unit = UNIT_SCENE.instantiate()
|
||||
enemy_unit.stat_template = UnitStats.new(50)
|
||||
enemy_unit.info_template = UnitInfo.new()
|
||||
enemy_unit.info_template.name = \"Putit\"
|
||||
enemy_unit.allegiance_template = ENEMY_ALLEGIANCE
|
||||
combat_map.deploy_unit(enemy_unit, Vector2i(2, 1))
|
||||
|
||||
var tree := get_tree()
|
||||
var root := tree.root
|
||||
var current_scene := tree.current_scene
|
||||
root.remove_child(current_scene)
|
||||
current_scene.queue_free()
|
||||
root.add_child(combat_instance)
|
||||
tree.current_scene = combat_instance
|
||||
"
|
||||
|
||||
[sub_resource type="GDScript" id="GDScript_ekxnf"]
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
extends Node
|
||||
|
||||
const GRID_SIZE := 5
|
||||
|
||||
@export var dl_map: CombatMap
|
||||
@export var unit_template: PackedScene
|
||||
@export var player_allegiance: UnitAllegiance
|
||||
@export var enemy_allegiance: UnitAllegiance
|
||||
|
||||
func _ready() -> void:
|
||||
for x in GRID_SIZE:
|
||||
for y in GRID_SIZE:
|
||||
var pos := Vector2i(x, y)
|
||||
var is_edge := x == 0 or x == GRID_SIZE - 1 or y == 0 or y == GRID_SIZE - 1
|
||||
if is_edge:
|
||||
dl_map.draw_wall(pos)
|
||||
else:
|
||||
dl_map.draw_floor(pos)
|
||||
|
||||
# Create a putit at the center belonging to the player
|
||||
var center := Vector2i(GRID_SIZE / 2, GRID_SIZE / 2)
|
||||
var unit: Unit = unit_template.instantiate()
|
||||
unit.stat_template = UnitStats.new(50)
|
||||
unit.info_template = UnitInfo.new()
|
||||
unit.allegiance_template = player_allegiance
|
||||
unit.info_template.name = "Putit"
|
||||
unit.position = dl_map.coords_to_world(center)
|
||||
get_parent().add_child.call_deferred(unit)
|
||||
|
||||
# Create a putit at one above the center belonging to the enemy
|
||||
var center_enemy := Vector2i(GRID_SIZE / 2, (GRID_SIZE / 2) - 1)
|
||||
var enemy_unit: Unit = unit_template.instantiate()
|
||||
enemy_unit.stat_template = UnitStats.new(50)
|
||||
enemy_unit.info_template = UnitInfo.new()
|
||||
enemy_unit.allegiance_template = enemy_allegiance
|
||||
enemy_unit.info_template.name = "Putit"
|
||||
enemy_unit.position = dl_map.coords_to_world(center_enemy)
|
||||
get_parent().add_child.call_deferred(enemy_unit)
|
||||
@@ -1 +0,0 @@
|
||||
uid://c8xb86ty7rduf
|
||||
Reference in New Issue
Block a user