some refactoring

This commit is contained in:
Victor Turgeon 2023-10-11 02:44:09 -04:00
parent c05a396954
commit a19250777f
6 changed files with 39 additions and 15 deletions

View File

@ -1,13 +1,9 @@
[gd_scene load_steps=6 format=3 uid="uid://dl0d1m1gfcd4o"]
[gd_scene load_steps=5 format=3 uid="uid://dl0d1m1gfcd4o"]
[ext_resource type="PackedScene" uid="uid://f7n8gd1wlv4v" path="res://Scenes/Player/UI/spawnable_button.tscn" id="1_tvnet"]
[ext_resource type="PackedScene" uid="uid://brnx3xyyd0e71" path="res://Scenes/Spawnables/bat_spawnable.tscn" id="2_borla"]
[ext_resource type="Texture2D" uid="uid://ddvr5ltbo8ome" path="res://Assets/Bat/noBKG_BatFlight_strip.png" id="3_5io10"]
[sub_resource type="AtlasTexture" id="AtlasTexture_qf7uu"]
atlas = ExtResource("3_5io10")
region = Rect2(0, 0, 64, 64)
[sub_resource type="AtlasTexture" id="AtlasTexture_if561"]
resource_local_to_scene = true
atlas = ExtResource("3_5io10")
@ -15,7 +11,6 @@ region = Rect2(64, 0, 64, 64)
[node name="BatButton" instance=ExtResource("1_tvnet")]
spawnableScene = ExtResource("2_borla")
spawnableIcon = SubResource("AtlasTexture_qf7uu")
[node name="AnimatedTextureRect" parent="." index="0"]
texture = SubResource("AtlasTexture_if561")

View File

@ -1,6 +1,7 @@
[gd_scene load_steps=2 format=3 uid="uid://bj2kxv2gq5ase"]
[gd_scene load_steps=3 format=3 uid="uid://bj2kxv2gq5ase"]
[ext_resource type="Script" path="res://Scenes/Player/UI/cursor_state.gd" id="1_ghv1b"]
[ext_resource type="Theme" uid="uid://dr1h7to56a2pv" path="res://Themes/base_theme.tres" id="2_4ke2g"]
[node name="CursorState" type="Node2D"]
script = ExtResource("1_ghv1b")
@ -10,6 +11,7 @@ offset_left = 11.5
offset_top = 14.0
offset_right = 81.5
offset_bottom = 40.0
theme = ExtResource("2_4ke2g")
[node name="Image" type="Sprite2D" parent="."]
position = Vector2(11.5, 14)

View File

@ -2,7 +2,6 @@ class_name SpawnableButton
extends Button
@export var spawnableScene : PackedScene = null
@export var spawnableIcon : Texture2D = null
@onready var animationPlayer : AnimationPlayer = $AnimationPlayer

View File

@ -1,8 +1,10 @@
extends Node2D
var currentSpawnableScene : PackedScene = null
var currentRefInstance : TemplateSpawnable = null
var currentStateIcon : Texture2D = null
var spawnedMonsters : Array = []
var allSpawnedMonsters : Dictionary = {}
var currentSpawnedMonsters : Array = []
@onready var menuZone : Control = $Camera2D/CanvasLayer/PlayerUI.menuZone
@onready var enemy : CharacterBody2D = $"../Enemy"
@onready var playableArea : Area2D = $"../PlayableArea"
@ -20,10 +22,13 @@ func _process(delta):
handle_cursor_state()
func add_monster(monster : TemplateSpawnable):
spawnedMonsters.append(monster)
currentSpawnedMonsters.append(monster)
if !allSpawnedMonsters.has(monster.monsterName):
allSpawnedMonsters[monster.monsterName] = 0
allSpawnedMonsters[monster.monsterName] += 1
func remove_monster(monster : TemplateSpawnable):
spawnedMonsters.erase(monster)
currentSpawnedMonsters.erase(monster)
func entered_playable_area():
cursorState.set_cursor_state("", currentStateIcon)
@ -57,9 +62,13 @@ func spawn_current():
var spawnable = currentSpawnableScene.instantiate()
spawnable.global_position = get_global_mouse_position()
$"..".add_child(spawnable)
spawnable.signal_death.connect(monster_death)
add_monster(spawnable)
if enemy != null:
enemy.add_foe(spawnable)
func monster_death(monster):
remove_monster(monster)
func is_in_menu():
return menuZone.get_rect().has_point(get_local_mouse_position())
@ -72,6 +81,12 @@ func _on_player_ui_button_changed(currentButton):
cursorState.set_cursor_state("", null)
return
currentStateIcon = currentButton.spawnableIcon
if currentRefInstance != null:
currentRefInstance.queue_free()
currentRefInstance = null
currentSpawnableScene = currentButton.spawnableScene
currentRefInstance = currentSpawnableScene.instantiate()
currentStateIcon = currentRefInstance.monsterIcon
cursorState.set_cursor_state("", currentStateIcon)

View File

@ -1,8 +1,13 @@
[gd_scene load_steps=3 format=3 uid="uid://brnx3xyyd0e71"]
[gd_scene load_steps=5 format=3 uid="uid://brnx3xyyd0e71"]
[ext_resource type="PackedScene" uid="uid://kkfxguj0lr5a" path="res://Scenes/Spawnables/template_spawnable.tscn" id="1_verf7"]
[ext_resource type="Texture2D" uid="uid://ddvr5ltbo8ome" path="res://Assets/Bat/noBKG_BatFlight_strip.png" id="2_0p8tq"]
[ext_resource type="Script" path="res://Scenes/Spawnables/AIs/ai_bat.gd" id="2_x3ldf"]
[sub_resource type="AtlasTexture" id="AtlasTexture_bnd0j"]
atlas = ExtResource("2_0p8tq")
region = Rect2(0, 0, 64, 64)
[node name="BatSpawnable" instance=ExtResource("1_verf7")]
maxHp = 10
attackSpeed = 50.0
@ -10,6 +15,10 @@ speed = 4000.0
damage = 1
priority = 1
expReward = 50
cost = 100
minSpawnRange = 10.0
monsterName = "Bat"
monsterIcon = SubResource("AtlasTexture_bnd0j")
[node name="AnimatedSprite2D" parent="." index="1"]
frame_progress = 0.482614

View File

@ -12,7 +12,8 @@ const ATTACK_COOLDOWN : float = 100
@export var cost : int = 0
@export var minSpawnRange : float = 0
@export var canAttack : bool = true
@export var monsterName : String = ""
@export var monsterIcon : Texture2D = null
@onready var ai : Node = $AI
@onready var animatedSprite : AnimatedSprite2D = $AnimatedSprite2D
@ -29,6 +30,8 @@ var enemy : CharacterBody2D = null
var currentHp : int = 0
var cooldown : float = 0
signal signal_death(monster : TemplateSpawnable)
func _ready():
enemy = get_node("../Enemy")
currentHp = maxHp
@ -95,6 +98,7 @@ func receive_damage(dmg):
func die():
enemy.remove_foe(self)
signal_death.emit(self)
bodyCollision.disabled = true
damageCollision.disabled = true
spawnableUI.visible = false