From 347abe64bf84e6f04d70c2b16f9557fdf1f176b2 Mon Sep 17 00:00:00 2001 From: tonydero Date: Tue, 15 Oct 2024 15:04:40 -0600 Subject: [PATCH] transition to physics-based movement to enable collisions, customize health bar appearance --- scenes/enemy.tscn | 41 ++++++++++++++++++++++-------------- scenes/npc.tscn | 20 ++++++++---------- scripts/NPC.gd | 2 +- scripts/NPCEnemy.gd | 14 +++++++++++- scripts/test_scroll_scene.gd | 39 +++++++++++++--------------------- 5 files changed, 63 insertions(+), 53 deletions(-) diff --git a/scenes/enemy.tscn b/scenes/enemy.tscn index 2598876..ca20120 100644 --- a/scenes/enemy.tscn +++ b/scenes/enemy.tscn @@ -1,24 +1,32 @@ -[gd_scene load_steps=4 format=3 uid="uid://55qfhbliodhn"] +[gd_scene load_steps=6 format=3 uid="uid://55qfhbliodhn"] [ext_resource type="Script" path="res://scripts/NPCEnemy.gd" id="1_ec4cy"] [ext_resource type="Texture2D" uid="uid://b10c1776j6j60" path="res://assets/icon.svg" id="2_twdr5"] -[sub_resource type="RectangleShape2D" id="RectangleShape2D_tv6kf"] -size = Vector2(196, 175) +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_yg7tn"] +bg_color = Color(0.623529, 0, 0, 1) +border_color = Color(0.491583, 0.491583, 0.491583, 1) -[node name="NPCEnemy" type="Node2D"] +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_enag8"] +bg_color = Color(0.207843, 0, 0, 1) +border_color = Color(0.491583, 0.491583, 0.491583, 1) + +[sub_resource type="Theme" id="Theme_fx206"] +ProgressBar/styles/background = SubResource("StyleBoxFlat_yg7tn") +ProgressBar/styles/fill = SubResource("StyleBoxFlat_enag8") + +[node name="NPCEnemy" type="CharacterBody2D"] script = ExtResource("1_ec4cy") -[node name="CharacterBody2D" type="CharacterBody2D" parent="."] +[node name="CollisionShape2D" type="CollisionPolygon2D" parent="."] +position = Vector2(-100, -86) +polygon = PackedVector2Array(0, 0, 100, -43, 200, 0, 100, 43) -[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"] -position = Vector2(0, -11.5) -shape = SubResource("RectangleShape2D_tv6kf") - -[node name="Sprite2D" type="Sprite2D" parent="CharacterBody2D"] +[node name="Sprite2D" type="Sprite2D" parent="."] texture = ExtResource("2_twdr5") -[node name="MarginContainer" type="MarginContainer" parent="CharacterBody2D/Sprite2D"] +[node name="MarginContainer" type="MarginContainer" parent="Sprite2D"] +z_index = 2 anchors_preset = 15 anchor_right = 1.0 anchor_bottom = 1.0 @@ -29,29 +37,30 @@ offset_bottom = -57.0 grow_horizontal = 2 grow_vertical = 2 -[node name="Nameplate" type="VBoxContainer" parent="CharacterBody2D/Sprite2D/MarginContainer"] +[node name="Nameplate" type="VBoxContainer" parent="Sprite2D/MarginContainer"] layout_mode = 2 -[node name="Healthbar" type="ProgressBar" parent="CharacterBody2D/Sprite2D/MarginContainer/Nameplate"] +[node name="Healthbar" type="ProgressBar" parent="Sprite2D/MarginContainer/Nameplate"] unique_name_in_owner = true custom_minimum_size = Vector2(0, 16) layout_mode = 2 +theme = SubResource("Theme_fx206") max_value = 10.0 step = 1.0 value = 10.0 show_percentage = false -[node name="TitleBox" type="HBoxContainer" parent="CharacterBody2D/Sprite2D/MarginContainer/Nameplate"] +[node name="TitleBox" type="HBoxContainer" parent="Sprite2D/MarginContainer/Nameplate"] layout_mode = 2 -[node name="TierLabel" type="Label" parent="CharacterBody2D/Sprite2D/MarginContainer/Nameplate/TitleBox"] +[node name="TierLabel" type="Label" parent="Sprite2D/MarginContainer/Nameplate/TitleBox"] layout_mode = 2 size_flags_horizontal = 4 size_flags_vertical = 0 text = "Tier " -[node name="NPCNameLabel" type="Label" parent="CharacterBody2D/Sprite2D/MarginContainer/Nameplate/TitleBox"] +[node name="NPCNameLabel" type="Label" parent="Sprite2D/MarginContainer/Nameplate/TitleBox"] unique_name_in_owner = true layout_mode = 2 size_flags_horizontal = 4 diff --git a/scenes/npc.tscn b/scenes/npc.tscn index 8c6eb71..12549d6 100644 --- a/scenes/npc.tscn +++ b/scenes/npc.tscn @@ -6,19 +6,17 @@ [sub_resource type="RectangleShape2D" id="RectangleShape2D_tv6kf"] size = Vector2(196, 175) -[node name="NPC" type="Node2D"] +[node name="NPC" type="CharacterBody2D"] script = ExtResource("1_m6htj") -[node name="CharacterBody2D" type="CharacterBody2D" parent="."] - -[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"] +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] position = Vector2(0, -11.5) shape = SubResource("RectangleShape2D_tv6kf") -[node name="Sprite2D" type="Sprite2D" parent="CharacterBody2D"] +[node name="Sprite2D" type="Sprite2D" parent="."] texture = ExtResource("2_yarwk") -[node name="MarginContainer" type="MarginContainer" parent="CharacterBody2D/Sprite2D"] +[node name="MarginContainer" type="MarginContainer" parent="Sprite2D"] anchors_preset = 15 anchor_right = 1.0 anchor_bottom = 1.0 @@ -29,10 +27,10 @@ offset_bottom = -57.0 grow_horizontal = 2 grow_vertical = 2 -[node name="Nameplate" type="VBoxContainer" parent="CharacterBody2D/Sprite2D/MarginContainer"] +[node name="Nameplate" type="VBoxContainer" parent="Sprite2D/MarginContainer"] layout_mode = 2 -[node name="Healthbar" type="ProgressBar" parent="CharacterBody2D/Sprite2D/MarginContainer/Nameplate"] +[node name="Healthbar" type="ProgressBar" parent="Sprite2D/MarginContainer/Nameplate"] unique_name_in_owner = true custom_minimum_size = Vector2(0, 16) layout_mode = 2 @@ -41,17 +39,17 @@ step = 1.0 value = 10.0 show_percentage = false -[node name="TitleBox" type="HBoxContainer" parent="CharacterBody2D/Sprite2D/MarginContainer/Nameplate"] +[node name="TitleBox" type="HBoxContainer" parent="Sprite2D/MarginContainer/Nameplate"] layout_mode = 2 -[node name="TierLabel" type="Label" parent="CharacterBody2D/Sprite2D/MarginContainer/Nameplate/TitleBox"] +[node name="TierLabel" type="Label" parent="Sprite2D/MarginContainer/Nameplate/TitleBox"] layout_mode = 2 size_flags_horizontal = 4 size_flags_vertical = 0 text = "Tier " -[node name="NPCNameLabel" type="Label" parent="CharacterBody2D/Sprite2D/MarginContainer/Nameplate/TitleBox"] +[node name="NPCNameLabel" type="Label" parent="Sprite2D/MarginContainer/Nameplate/TitleBox"] unique_name_in_owner = true layout_mode = 2 size_flags_horizontal = 4 diff --git a/scripts/NPC.gd b/scripts/NPC.gd index 082d126..10c992c 100644 --- a/scripts/NPC.gd +++ b/scripts/NPC.gd @@ -1,5 +1,5 @@ class_name NPC -extends Node2D +extends CharacterBody2D enum npcDifficulties { MINION, NORMAL, ELITE, BOSS, ELITEBOSS, BBEG } diff --git a/scripts/NPCEnemy.gd b/scripts/NPCEnemy.gd index d2f9d77..a294a90 100644 --- a/scripts/NPCEnemy.gd +++ b/scripts/NPCEnemy.gd @@ -2,12 +2,16 @@ class_name NPCEnemy extends NPC -enum questTypes { NONE, FEW_KILL, MANY_KILL, ONE_KILL } +enum questTypes {NONE, FEW_KILL, MANY_KILL, ONE_KILL} signal enemy_died(npcDifficulty: npcDifficulties, questType: questTypes) @export var questType: questTypes = questTypes.NONE +var target +var speed := 300 +var prevCollisions := 0 + func _init() -> void: self.npcDifficulty = npcDifficulties.NORMAL @@ -32,3 +36,11 @@ func _process(_delta: float) -> void: enemy_died.emit(self.npcDifficulty, self.questType) # delete node self.queue_free() + +func _physics_process(_delta): + velocity = position.direction_to(target) * speed + # look_at(target) + # FIXME want enemy to stop only when touching something, to stop vibrating + if position.distance_to(target) > 10 and get_slide_collision_count() == prevCollisions: + move_and_slide() + prevCollisions = get_slide_collision_count() diff --git a/scripts/test_scroll_scene.gd b/scripts/test_scroll_scene.gd index 9e2392e..a983716 100644 --- a/scripts/test_scroll_scene.gd +++ b/scripts/test_scroll_scene.gd @@ -19,12 +19,24 @@ func _ready() -> void: $MarginContainer/Menus/MenuButtons/DebugMenu.hide() $SpawnTimer.start(spawnTimerValue) _spawn_enemies(5) + _spawn_enemies(5) + + +func _process(delta: float) -> void: + playerPos = Vector2(middleX, get_viewport_rect().size.y*(0.75)) + if questsComplete: + $SpawnTimer.stop() + if _get_enemies().is_empty(): + print("Area Complete!") + get_tree().quit() + func _create_enemy(): var enemy = load("res://scenes/enemy.tscn").instantiate() enemy.scale = Vector2.ONE*0.5 enemy.position = Vector2(randi_range(0, get_viewport_rect().size.x), -50) - #enemy.get_node("%NPCNameLabel").text = "Test" + enemy.speed *= randf_range(0.5, 2.0) + enemy.target = playerPos return enemy @@ -44,32 +56,11 @@ func _spawn_enemies(maxEnemies: int) -> void: for i in range(numEnemies): var enemy = _create_enemy() %SpawnArea.add_child(enemy) - var randEnemyMoveSpeed := randf_range(0.5, 2.0)*enemyMoveSpeed - create_tween().tween_property(enemy, "position", playerPos, randEnemyMoveSpeed) - - -func _process(_delta: float) -> void: - #var closestEnemy = _get_closest_enemy() - deltaCount += 1 - #if numEnemies != 0: - #print(playerPos.distance_squared_to(closestEnemy.position)) - # change movement test, not sure it's working right... - #create_tween().tween_property(enemies[randi_range(0, numEnemies-1)], "position", Vector2(100, 100), 1.0) - #if deltaCount % 200 == 0: - #%SpawnArea.remove_child(closestEnemy) - # change to signal - if questsComplete: - $SpawnTimer.stop() - if _get_enemies().is_empty(): - print("Area Complete!") + # var randEnemyMoveSpeed := randf_range(0.5, 2.0)*enemyMoveSpeed + # create_tween().tween_property(enemy, "position", playerPos, randEnemyMoveSpeed) func _on_button_test_pressed() -> void: - # remove all existing enemies from scene and array - #for enemy in enemies: - #remove_child(enemy) - #enemies = [] - questsComplete = true