102 lines
3.4 KiB
GDScript
102 lines
3.4 KiB
GDScript
@tool
|
|
class_name Helper
|
|
|
|
|
|
const CONFIRM_POPUP : PackedScene = preload("uid://cxn3x8dq8vawa")
|
|
|
|
|
|
const SCENE_DIR_PATTERN : String = "res://\\w+/(Scenes|Map \\d+)/$"
|
|
const SCENE_FILE_PATTERN : String = "([^/]*)\\.tscn$"
|
|
const RESOURCE_FILE_PATTERN : String = "([^/]*)\\.tres$"
|
|
|
|
const TOWER_DIR : String = "res://Towers/Scenes/"
|
|
const ENEMY_DIR : String = "res://Enemies/Scenes/"
|
|
const LEVEL_DIR : String = "res://Levels/"
|
|
|
|
static var towers : Dictionary
|
|
static var enmies : Dictionary
|
|
|
|
enum RESOURCE_TYPE { TOWER, ENEMY, LEVEL }
|
|
|
|
## [color=crimson][b]WARNING :[/b] Work only with the [enum RESOURCE_TYPE] [param TOWER][/color]
|
|
static func preloadSceneInGlobal(property: Dictionary, type: RESOURCE_TYPE) -> void:
|
|
assert(type == RESOURCE_TYPE.TOWER, "[color=crimson][b]WARNING :[/b] Work only with the [enum RESOURCE_TYPE] [param TOWER][/color]")
|
|
|
|
var files : Array[String] = getPackedScenesPaths(type)
|
|
|
|
for file in files:
|
|
var scene = load(file)
|
|
if scene:
|
|
property.set(EnhancedResource.getPackedSceneProperty(scene, "type"), scene)
|
|
|
|
property.sort()
|
|
|
|
|
|
static func getPackedScenesPaths(type: RESOURCE_TYPE) -> Array[String]:
|
|
return getFileFromDir(getDirFromType(type), SCENE_FILE_PATTERN, SCENE_DIR_PATTERN)
|
|
|
|
|
|
static func getResourcesPaths(type: RESOURCE_TYPE) -> Array[String]:
|
|
if type == RESOURCE_TYPE.LEVEL:
|
|
return getFileFromDir(getDirFromType(type), RESOURCE_FILE_PATTERN, LEVEL_DIR)
|
|
return []
|
|
|
|
|
|
static func getDirFromType(type: RESOURCE_TYPE) -> String:
|
|
match type:
|
|
RESOURCE_TYPE.TOWER: return TOWER_DIR
|
|
RESOURCE_TYPE.ENEMY: return ENEMY_DIR
|
|
RESOURCE_TYPE.LEVEL: return LEVEL_DIR
|
|
_: return ""
|
|
|
|
|
|
static func getFileFromDir(path : String, regexPattern : String, pathRegexPattern : String = "") -> Array[String]:
|
|
var files : Array[String]
|
|
var regex : RegEx = RegEx.create_from_string(regexPattern)
|
|
var pathRegex : RegEx = RegEx.create_from_string(pathRegexPattern)
|
|
var dir : DirAccess = DirAccess.open(path)
|
|
|
|
if pathRegexPattern:
|
|
for subDir in dir.get_directories():
|
|
subDir = path + subDir + "/"
|
|
if pathRegex.search(subDir):
|
|
files.append_array(getFileFromDir(subDir, regexPattern, pathRegexPattern))
|
|
|
|
for file in dir.get_files():
|
|
if regex.search(file) && pathRegex.search(path):
|
|
files.append(path + file)
|
|
|
|
return files
|
|
|
|
|
|
static func showConfirmPopup(
|
|
text : String,
|
|
nodeToAppend : Node,
|
|
confirmCallback : Callable,
|
|
cancelCallback : Callable = func(): pass
|
|
) -> void:
|
|
var confirmPopup : ConfirmPopup = CONFIRM_POPUP.instantiate()
|
|
nodeToAppend.add_child(confirmPopup)
|
|
nodeToAppend.get_tree().paused = true
|
|
confirmPopup.label.text = text
|
|
confirmPopup.confirmed.connect(func(): confirmCallback.call(); nodeToAppend.get_tree().paused = false)
|
|
confirmPopup.canceled.connect(func(): cancelCallback.call(); nodeToAppend.get_tree().paused = false)
|
|
|
|
enum POSITION { TOP, CENTER, DOWN }
|
|
static func getHitBoxLocation(body : CollisionObject3D, position : POSITION) -> float:
|
|
if body is GameTile:
|
|
return .2
|
|
|
|
if body.has_node("CollisionShape3D"):
|
|
var shape : Shape3D = body.shape_owner_get_shape(0, 0)
|
|
var transform : Transform3D = body.shape_owner_get_transform(0)
|
|
if position == POSITION.CENTER:
|
|
return transform.origin.y
|
|
else:
|
|
var factor : int = 1 if POSITION.TOP == position else -1
|
|
if shape is CapsuleShape3D:
|
|
return factor * (shape.height + shape.radius) + transform.origin.y
|
|
if shape is SphereShape3D:
|
|
return factor * shape.radius + transform.origin.y
|
|
|
|
return 0
|