updated npc instances to new Enemy class, added initial damage and death signal emission

This commit is contained in:
tonydero 2024-10-15 08:14:02 -06:00
parent 7d79eab28a
commit fab3fe56c1
9 changed files with 188 additions and 96 deletions

14
scenes/ability_ph.tscn Normal file
View File

@ -0,0 +1,14 @@
[gd_scene load_steps=3 format=3 uid="uid://dvwo26vsk46tl"]
[ext_resource type="Script" path="res://scripts/AbilityPH.gd" id="1_6jbus"]
[ext_resource type="Texture2D" uid="uid://b10c1776j6j60" path="res://assets/icon.svg" id="1_eoss7"]
[node name="AbilityPH" type="TextureRect"]
scale = Vector2(0.5, 0.5)
texture = ExtResource("1_eoss7")
script = ExtResource("1_6jbus")
[node name="IdleCooldown" type="Timer" parent="."]
autostart = true
[connection signal="timeout" from="IdleCooldown" to="." method="_on_idle_cooldown_timeout"]

View File

@ -1,13 +1,13 @@
[gd_scene load_steps=4 format=3 uid="uid://dkmnvye1y33d7"]
[ext_resource type="Script" path="res://scripts/Character.gd" id="1_teyfy"]
[ext_resource type="Texture2D" uid="uid://b10c1776j6j60" path="res://assets/icon.svg" id="2_6hwax"]
[ext_resource type="Script" path="res://scripts/character.gd" id="2_bft53"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_tv6kf"]
size = Vector2(196, 175)
[node name="Character" type="Node2D"]
script = ExtResource("2_bft53")
script = ExtResource("1_teyfy")
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]

59
scenes/enemy.tscn Normal file
View File

@ -0,0 +1,59 @@
[gd_scene load_steps=4 format=3 uid="uid://55qfhbliodhn"]
[ext_resource type="Script" path="res://scripts/Enemy.gd" id="1_4u60d"]
[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)
[node name="NPC" type="Node2D"]
script = ExtResource("1_4u60d")
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
position = Vector2(0, -11.5)
shape = SubResource("RectangleShape2D_tv6kf")
[node name="Sprite2D" type="Sprite2D" parent="CharacterBody2D"]
texture = ExtResource("2_twdr5")
[node name="MarginContainer" type="MarginContainer" parent="CharacterBody2D/Sprite2D"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = -91.0
offset_top = -94.0
offset_right = -36.0
offset_bottom = -57.0
grow_horizontal = 2
grow_vertical = 2
[node name="Nameplate" type="VBoxContainer" parent="CharacterBody2D/Sprite2D/MarginContainer"]
layout_mode = 2
[node name="Healthbar" type="ProgressBar" parent="CharacterBody2D/Sprite2D/MarginContainer/Nameplate"]
unique_name_in_owner = true
custom_minimum_size = Vector2(0, 16)
layout_mode = 2
max_value = 10.0
step = 1.0
value = 10.0
show_percentage = false
[node name="TitleBox" type="HBoxContainer" parent="CharacterBody2D/Sprite2D/MarginContainer/Nameplate"]
layout_mode = 2
[node name="TierLabel" type="Label" parent="CharacterBody2D/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"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 0
text = "Name"

View File

@ -33,6 +33,7 @@ grow_vertical = 2
layout_mode = 2
[node name="Healthbar" type="ProgressBar" parent="CharacterBody2D/Sprite2D/MarginContainer/Nameplate"]
unique_name_in_owner = true
custom_minimum_size = Vector2(0, 16)
layout_mode = 2
max_value = 10.0

View File

@ -1,6 +1,7 @@
[gd_scene load_steps=2 format=3 uid="uid://0h3dpe6fuhe8"]
[gd_scene load_steps=3 format=3 uid="uid://0h3dpe6fuhe8"]
[ext_resource type="Script" path="res://scripts/test_scroll_scene.gd" id="1_gc1kb"]
[ext_resource type="PackedScene" uid="uid://dvwo26vsk46tl" path="res://scenes/ability_ph.tscn" id="2_heria"]
[node name="TestInstanceScene" type="Control"]
layout_mode = 3
@ -11,6 +12,7 @@ grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_gc1kb")
debug = true
spawnTimerValue = 20.0
[node name="SpawnTimer" type="Timer" parent="."]
@ -33,61 +35,69 @@ theme_override_constants/margin_top = 10
theme_override_constants/margin_right = 10
theme_override_constants/margin_bottom = 10
[node name="Menus" type="VBoxContainer" parent="MarginContainer"]
[node name="BottomUI" type="VBoxContainer" parent="MarginContainer"]
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 8
[node name="MenuButtons" type="HBoxContainer" parent="MarginContainer/Menus"]
[node name="AbilityBar" type="HBoxContainer" parent="MarginContainer/BottomUI"]
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 8
[node name="MainMenuButton" type="Button" parent="MarginContainer/Menus/MenuButtons"]
[node name="AbilityPH" parent="MarginContainer/BottomUI/AbilityBar" instance=ExtResource("2_heria")]
layout_mode = 2
[node name="MenuButtons" type="HBoxContainer" parent="MarginContainer/BottomUI"]
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 8
[node name="MainMenuButton" type="Button" parent="MarginContainer/BottomUI/MenuButtons"]
layout_mode = 2
text = "Menu"
[node name="MapMenuButton" type="Button" parent="MarginContainer/Menus/MenuButtons"]
[node name="MapMenuButton" type="Button" parent="MarginContainer/BottomUI/MenuButtons"]
layout_mode = 2
text = "Map"
[node name="SkillsMenuButton" type="Button" parent="MarginContainer/Menus/MenuButtons"]
[node name="SkillsMenuButton" type="Button" parent="MarginContainer/BottomUI/MenuButtons"]
layout_mode = 2
text = "Skills"
[node name="InvMenuButton" type="Button" parent="MarginContainer/Menus/MenuButtons"]
[node name="InvMenuButton" type="Button" parent="MarginContainer/BottomUI/MenuButtons"]
layout_mode = 2
text = "Inventory"
[node name="LogMenuButton" type="Button" parent="MarginContainer/Menus/MenuButtons"]
[node name="LogMenuButton" type="Button" parent="MarginContainer/BottomUI/MenuButtons"]
layout_mode = 2
text = "Log"
[node name="CraftMenuButton" type="Button" parent="MarginContainer/Menus/MenuButtons"]
[node name="CraftMenuButton" type="Button" parent="MarginContainer/BottomUI/MenuButtons"]
layout_mode = 2
text = "Crafting"
[node name="DebugMenu" type="HBoxContainer" parent="MarginContainer/Menus/MenuButtons"]
[node name="DebugMenu" type="HBoxContainer" parent="MarginContainer/BottomUI/MenuButtons"]
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 8
[node name="ButtonTest" type="Button" parent="MarginContainer/Menus/MenuButtons/DebugMenu"]
[node name="ButtonTest" type="Button" parent="MarginContainer/BottomUI/MenuButtons/DebugMenu"]
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 8
text = "Complete Quest"
[node name="TouchScreenButton" type="TouchScreenButton" parent="MarginContainer/Menus/MenuButtons/DebugMenu/ButtonTest"]
[node name="TouchScreenButton" type="TouchScreenButton" parent="MarginContainer/BottomUI/MenuButtons/DebugMenu/ButtonTest"]
[node name="ButtonExit" type="Button" parent="MarginContainer/Menus/MenuButtons/DebugMenu"]
[node name="ButtonExit" type="Button" parent="MarginContainer/BottomUI/MenuButtons/DebugMenu"]
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 8
text = "Exit"
[node name="TouchScreenButton" type="TouchScreenButton" parent="MarginContainer/Menus/MenuButtons/DebugMenu/ButtonExit"]
[node name="TouchScreenButton" type="TouchScreenButton" parent="MarginContainer/BottomUI/MenuButtons/DebugMenu/ButtonExit"]
[connection signal="timeout" from="SpawnTimer" to="." method="_on_spawn_timer_timeout"]
[connection signal="pressed" from="MarginContainer/Menus/MenuButtons/DebugMenu/ButtonTest" to="." method="_on_button_test_pressed"]
[connection signal="pressed" from="MarginContainer/Menus/MenuButtons/DebugMenu/ButtonExit" to="." method="_on_button_exit_pressed"]
[connection signal="pressed" from="MarginContainer/BottomUI/MenuButtons/DebugMenu/ButtonTest" to="." method="_on_button_test_pressed"]
[connection signal="pressed" from="MarginContainer/BottomUI/MenuButtons/DebugMenu/ButtonExit" to="." method="_on_button_exit_pressed"]

View File

@ -1,5 +1,54 @@
# placerholder test ability
class_name Ability
extends Sprite2D
extends TextureRect
# basis for the 6 abilities
var middleX
var playerPos
@export var attack_range := 0
@export var attack_type := "single_target"
func _ready() -> void:
middleX = get_viewport_rect().size.x/2
playerPos = Vector2(middleX, get_viewport_rect().size.y*(0.75))
func _get_enemies() -> Array:
var enemies := get_tree().get_nodes_in_group("enemies")
return enemies
func _get_num_enemies() -> int:
var enemies := get_tree().get_nodes_in_group("enemies")
return enemies.size()
func _get_distance_to_player(enemy: Node) -> float:
return enemy.position.distance_squared_to(playerPos)
func _get_target(enemies) -> Node:
# var numEnemies := _get_num_enemies()
var enemyDists
var closestEnemy
# get array of difficulties using map
# get list of distances by mapping distance to each node in enemies
enemyDists = enemies.map(_get_distance_to_player)
# get closest enemy by finding index with min dist
closestEnemy = enemies[enemyDists.find(enemyDists.min())]
return closestEnemy
func _deal_idle_damage(enemiesList) -> void:
var damage = 200
var target = _get_target(enemiesList)
target.damageTaken += damage
func _on_idle_cooldown_timeout() -> void:
var enemiesList = _get_enemies()
if not enemiesList.is_empty():
_deal_idle_damage(enemiesList)

View File

@ -2,33 +2,11 @@ class_name Enemy
extends NPC
enum npcDifficulties { MINION, NORMAL, ELITE, BOSS, ELITEBOSS, BBEG }
enum npcTiers { I, II, III, IV, V, VI, VII, VIII, IX, X }
enum questTypes { NONE, FEW_KILL, MANY_KILL, ONE_KILL }
@export var npcDifficulty: npcDifficulties
@export var npcTier: npcTiers
signal enemy_died(npcDifficulty: npcDifficulties, questType: questTypes)
func _random_mod_health() -> void:
randomize()
# noise factor
var noise_factor = randf_range(7, 10)
self.maxHealth *= noise_factor
# difficulty factor
match self.npcDifficulty:
npcDifficulties.MINION:
self.maxHealth /= 2
npcDifficulties.ELITE:
self.maxHealth *= 2
npcDifficulties.BOSS:
self.maxHealth *= 4
npcDifficulties.ELITEBOSS:
self.maxHealth *= 8
npcDifficulties.BBEG:
self.maxHealth *= 16
# npcTier factor
self.maxHealth *= exp(self.npcTier)
# fun factor (just additional factor to tweak)
self.maxHealth *= 10
@export var questType: questTypes = questTypes.NONE
func _init() -> void:
@ -40,11 +18,17 @@ func _ready() -> void:
# add to enemies
add_to_group("enemies")
# modify max health value based on the various factors
# set health
_random_mod_health()
# set health bar
%Healthbar.max_value = maxHealth
%Healthbar.value = maxHealth
func _process(delta: float) -> void:
# if damageTaken > maxHealth:
# self.free()?
# send signal that I died to be counted, etc.
pass
func _process(_delta: float) -> void:
%Healthbar.value = maxHealth - damageTaken
if damageTaken >= maxHealth:
# send signal that I died to be counted, etc.
enemy_died.emit(self.npcDifficulty, self.questType)
# delete node
self.queue_free()

View File

@ -1,10 +1,15 @@
class_name NPC
extends Character
extends Node2D
enum npcDifficulties { MINION, NORMAL, ELITE, BOSS, ELITEBOSS, BBEG }
enum npcTiers { I, II, III, IV, V, VI, VII, VIII, IX, X }
signal npc_died(npcTier: npcTiers)
@export var charName := "Character"
@export var maxHealth := 10
@export var damageTaken := 0
@export var npcDifficulty: npcDifficulties
@export var npcTier: npcTiers
@ -37,14 +42,17 @@ func _init() -> void:
func _ready() -> void:
# add to enemies
add_to_group("enemies")
# modify max health value based on the various factors
_random_mod_health()
func _process(delta: float) -> void:
# if damageTaken > maxHealth:
# self.free()?
# send signal that I died to be counted, etc.
pass
# set health bar
%Healthbar.max_value = maxHealth
%Healthbar.value = maxHealth
func _process(_delta: float) -> void:
%Healthbar.value = maxHealth - damageTaken
if damageTaken >= maxHealth:
# send signal that I died to be counted, gen loot, etc.
npc_died.emit(self.npcTier)
# delete node
self.free()

View File

@ -18,9 +18,10 @@ func _ready() -> void:
if not debug:
$MarginContainer/Menus/MenuButtons/DebugMenu.hide()
$SpawnTimer.start(spawnTimerValue)
_spawn_enemies(5)
func _create_enemy():
var enemy = load("res://scenes/npc.tscn").instantiate()
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"
@ -37,37 +38,6 @@ func _get_num_enemies() -> int:
return len(enemies)
func _get_distance_to_player(enemy: Node) -> float:
return enemy.position.distance_squared_to(playerPos)
func _get_target() -> Node:
# arrays of enemies by difficulty
var enemies := _get_enemies()
var numEnemies := _get_num_enemies()
#var highestDifficulty
var enemyDists
var closestEnemy
# get array of difficulties using map
if not enemies.is_empty():
# get index of largest non-zero val in numEnemies array, ignoring minions
#highestDifficulty = numEnemies.find(0, 1) - 1
enemyDists = enemies.map(_get_distance_to_player)
closestEnemy = enemies[enemyDists.find(enemyDists.min())]
#var closestDist = INF
#for enemy in enemies:
#var enemyPos = enemy.position
#var enemyDist = playerPos.distance_squared_to(enemyPos)
#if enemyDist < closestDist:
#closestEnemy = enemy
print(enemies)
#print(highestDifficulty)
print(enemyDists)
print(closestEnemy)
return closestEnemy
func _spawn_enemies(maxEnemies: int) -> void:
#await get_tree().create_timer(time).timeout
var numEnemies = randi_range(1, maxEnemies)
@ -78,7 +48,7 @@ func _spawn_enemies(maxEnemies: int) -> void:
create_tween().tween_property(enemy, "position", playerPos, randEnemyMoveSpeed)
func _process(delta: float) -> void:
func _process(_delta: float) -> void:
#var closestEnemy = _get_closest_enemy()
deltaCount += 1
#if numEnemies != 0:
@ -87,9 +57,6 @@ func _process(delta: float) -> void:
#create_tween().tween_property(enemies[randi_range(0, numEnemies-1)], "position", Vector2(100, 100), 1.0)
#if deltaCount % 200 == 0:
#%SpawnArea.remove_child(closestEnemy)
if not _get_enemies().is_empty() and deltaCount % 100 == 0:
var targetEnemy = _get_target()
$SpawnArea.remove_child(targetEnemy)
# change to signal
if questsComplete:
$SpawnTimer.stop()