From 4597189f157834c80f56b12b701fd2b2a15c2798 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 8 Sep 2024 22:35:06 +0200 Subject: next commit --- Game/States/Build/BuildGrid.tscn | 6 +++ Game/States/Build/BuilderElement.gd | 87 +++++++++++++++++++++++++++++++++++ Game/States/Build/BuilderElement.tscn | 19 ++++++++ Game/States/Build/StateBuild.gd | 50 ++++++++++++++++++++ Game/States/Build/StateBuild.tscn | 11 +++++ Game/States/Build/build_grid.gd | 21 +++++++++ 6 files changed, 194 insertions(+) create mode 100644 Game/States/Build/BuildGrid.tscn create mode 100644 Game/States/Build/BuilderElement.gd create mode 100644 Game/States/Build/BuilderElement.tscn create mode 100644 Game/States/Build/StateBuild.gd create mode 100644 Game/States/Build/StateBuild.tscn create mode 100644 Game/States/Build/build_grid.gd (limited to 'Game/States/Build') diff --git a/Game/States/Build/BuildGrid.tscn b/Game/States/Build/BuildGrid.tscn new file mode 100644 index 0000000..ffb41d7 --- /dev/null +++ b/Game/States/Build/BuildGrid.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://8oq8aa1q4c1h"] + +[ext_resource type="Script" path="res://Game/States/Build/build_grid.gd" id="1_jige0"] + +[node name="BuildGrid" type="Node2D"] +script = ExtResource("1_jige0") diff --git a/Game/States/Build/BuilderElement.gd b/Game/States/Build/BuilderElement.gd new file mode 100644 index 0000000..8315792 --- /dev/null +++ b/Game/States/Build/BuilderElement.gd @@ -0,0 +1,87 @@ +class_name BuilderElement +extends Node2D + + +@export var element: Node2D + +var collision_shape: Shape2D + +var previous_position: Vector2 + +var collision_areas: Array[Area2D] = [] + + +func _ready(): + var element_sprite: Sprite2D = element.get_node("Sprite2D") + $Sprite2D.texture = element_sprite.texture + $Sprite2D.region_enabled = element_sprite.region_enabled + $Sprite2D.region_rect = element_sprite.region_rect + $Sprite2D.scale = element_sprite.scale + $Sprite2D.position = element_sprite.position + + var element_collision_shape = element.get_node("CollisionShape2D").duplicate() as CollisionShape2D + collision_shape = element_collision_shape.shape + + var tile_size = Client.stage.map.tile_set.tile_size + var snapped_size = collision_shape.size.snapped(tile_size) + for x in (snapped_size.x / tile_size.x): + for y in (snapped_size.y / tile_size.y): + var collision_visibility_area_scene = preload("res://UI/CollisionVisibilityArea.tscn") + var collision_visibility_area = collision_visibility_area_scene.instantiate() + collision_visibility_area.position = Vector2( + x * tile_size.x, + y * tile_size.y + ) + collision_visibility_area.set_collision_layer_value(3, true) + collision_visibility_area.set_collision_mask_value(1, true) + collision_visibility_area.set_collision_mask_value(2, true) + collision_visibility_area.set_collision_mask_value(3, true) + collision_visibility_area.colliding_color = Color(1, 0, 0, 0.5) + collision_visibility_area.not_colliding_color = Color(0, 1, 0, 0.25) + + var shape = RectangleShape2D.new() + shape.size = Vector2(tile_size - Vector2i(1, 1)) + var cshape = CollisionShape2D.new() + cshape.shape = shape + cshape.position = tile_size / 2 + + collision_visibility_area.add_child(cshape) + add_child(collision_visibility_area) + + collision_areas.append(collision_visibility_area) + + +func _draw(): + draw_circle( + Client.stage.map.tile_set.tile_size, + 8 + element.attack_range, + Color(1, 1, 1, 0.75), + false, + 1.0 + ) + + +func _process(_delta): + previous_position = global_position + global_position = get_global_mouse_position().snapped(Client.stage.map.tile_set.tile_size) - Vector2(16,16) + + if global_position != previous_position: + queue_redraw() + + +func can_build(): + for area in collision_areas: + if area.get_overlapping_areas().size() > 0: + return false + + if area.get_overlapping_bodies().size() > 0: + return false + + return true + + +func _on_area_entered(_node) -> void: + queue_redraw() + +func _on_area_exited(_node) -> void: + queue_redraw() diff --git a/Game/States/Build/BuilderElement.tscn b/Game/States/Build/BuilderElement.tscn new file mode 100644 index 0000000..ea0a857 --- /dev/null +++ b/Game/States/Build/BuilderElement.tscn @@ -0,0 +1,19 @@ +[gd_scene load_steps=2 format=3 uid="uid://cleoiemwasbm5"] + +[ext_resource type="Script" path="res://Game/States/Build/BuilderElement.gd" id="1_xxacj"] + +[node name="BuilderElement" type="Node2D"] +script = ExtResource("1_xxacj") + +[node name="Sprite2D" type="Sprite2D" parent="."] +texture_filter = 1 +centered = false + +[node name="Area2D" type="Area2D" parent="."] +collision_layer = 4 +collision_mask = 7 + +[connection signal="area_entered" from="Area2D" to="." method="_on_area_entered"] +[connection signal="area_exited" from="Area2D" to="." method="_on_area_exited"] +[connection signal="body_entered" from="Area2D" to="." method="_on_area_entered"] +[connection signal="body_exited" from="Area2D" to="." method="_on_area_exited"] diff --git a/Game/States/Build/StateBuild.gd b/Game/States/Build/StateBuild.gd new file mode 100644 index 0000000..b92020b --- /dev/null +++ b/Game/States/Build/StateBuild.gd @@ -0,0 +1,50 @@ +class_name StateBuild +extends State + + +static var current_builder_element: BuilderElement + + +func _state_enter(): + %BuildGrid.visible = true + var builder_element_scene = preload("res://Game/States/Build/BuilderElement.tscn") + var builder_element = builder_element_scene.instantiate() + + var tower = preload("res://Towers/Tower.tscn").instantiate() as Tower + tower.attack_range = [ + Client.stage.map.tile_set.tile_size.x * 2, + Client.stage.map.tile_set.tile_size.x * 3, + Client.stage.map.tile_set.tile_size.x * 4, + ].pick_random() + + builder_element.element = tower + get_tree().current_scene.add_child(builder_element) + current_builder_element = builder_element + + +func _state_exit(): + %BuildGrid.visible = false + + +func _state_input(event: InputEvent): + if event.is_action_pressed("builder_tower_select"): + get_viewport().set_input_as_handled() + + if current_builder_element.can_build(): + var placed_tower = current_builder_element.element.duplicate() as Tower + Client.place_tower(placed_tower, current_builder_element.global_position) + + placed_tower.selected.connect(func(): + Client.stage.hud.tower.text = "Range: %s - Power: %s - Speed: %s" % [ + placed_tower.attack_range, placed_tower.attack_power, placed_tower.attack_speed + ] + ) + + if not Input.is_action_pressed("builder_tower_place_keep"): + current_builder_element.queue_free() + set_state("StateDefault") + + if event.is_action_pressed("builder_cancel") or event.is_action_pressed("build_mode_start"): + get_viewport().set_input_as_handled() + current_builder_element.queue_free() + set_state("StateDefault") diff --git a/Game/States/Build/StateBuild.tscn b/Game/States/Build/StateBuild.tscn new file mode 100644 index 0000000..84583c2 --- /dev/null +++ b/Game/States/Build/StateBuild.tscn @@ -0,0 +1,11 @@ +[gd_scene load_steps=3 format=3 uid="uid://bo5dp02vlui3l"] + +[ext_resource type="Script" path="res://Game/States/Build/StateBuild.gd" id="1_s0n2d"] +[ext_resource type="PackedScene" uid="uid://8oq8aa1q4c1h" path="res://Game/States/Build/BuildGrid.tscn" id="2_iheti"] + +[node name="StateBuild" type="Node"] +script = ExtResource("1_s0n2d") + +[node name="BuildGrid" parent="." instance=ExtResource("2_iheti")] +unique_name_in_owner = true +visible = false diff --git a/Game/States/Build/build_grid.gd b/Game/States/Build/build_grid.gd new file mode 100644 index 0000000..6fd93aa --- /dev/null +++ b/Game/States/Build/build_grid.gd @@ -0,0 +1,21 @@ +extends Node2D + + +func _draw(): + var area = Client.stage.map.get_used_rect() + var cell_size = Client.stage.map.tile_set.tile_size + var offset = Client.stage.map.position + for row in range(0, area.size.y + 1): + draw_line( + Vector2(offset.x, offset.y + row * cell_size.y), + Vector2(offset.x + area.size.x * cell_size.x, offset.y + row * cell_size.y), + Color(1, 1, 1, 0.25), + 1.0 + ) + for column in range(0, area.size.x + 1): + draw_line( + Vector2(offset.x + column * cell_size.x, offset.y), + Vector2(offset.x + column * cell_size.x, offset.y + area.size.y * cell_size.y), + Color(1, 1, 1, 0.25), + 1.0 + ) -- cgit v1.2.3