Refactored and documented utils
This commit is contained in:
parent
cabc547b51
commit
de06759514
6 changed files with 41 additions and 18 deletions
|
|
@ -1,8 +1,9 @@
|
|||
extends Node2D
|
||||
|
||||
var parent : Node2D
|
||||
@export var center = Vector2.ZERO
|
||||
var angle = 0
|
||||
|
||||
# Quick access to relative cardinal directions
|
||||
var up : Vector2 :
|
||||
get():
|
||||
return global_from_local(Vector2.UP)
|
||||
|
|
@ -17,18 +18,20 @@ var left : Vector2 :
|
|||
return global_from_local(Vector2.LEFT)
|
||||
|
||||
func _ready() -> void:
|
||||
parent = get_parent()
|
||||
align()
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
align()
|
||||
|
||||
func align():
|
||||
angle = -(parent.position - center).angle_to(Vector2.UP)
|
||||
parent.rotation = angle;
|
||||
|
||||
# Aligns the parent to the center
|
||||
func align():
|
||||
angle = -(get_parent().position - center).angle_to(Vector2.UP)
|
||||
get_parent().rotation = angle;
|
||||
|
||||
# Converts directions in local coordinates into global coordinates
|
||||
func global_from_local (_velocity: Vector2) -> Vector2:
|
||||
return _velocity.rotated(angle)
|
||||
|
||||
|
||||
# Converts directions in global coordinates into local coordinates
|
||||
func local_from_global (_velocity: Vector2) -> Vector2:
|
||||
return _velocity.rotated(-angle)
|
||||
|
|
|
|||
|
|
@ -1,43 +1,51 @@
|
|||
class_name EnemyHurtbox extends Area2D
|
||||
|
||||
# HP Management
|
||||
@export var max_hp : int
|
||||
@onready var hp = max_hp
|
||||
@onready var hp = max_hp :
|
||||
set(new_hp):
|
||||
if new_hp > max_hp:
|
||||
new_hp = max_hp
|
||||
if new_hp < 0 :
|
||||
new_hp = 0
|
||||
hp = new_hp
|
||||
if hp == 0:
|
||||
die()
|
||||
@export var flash_duration = 0.2
|
||||
@export var canvasItem : CanvasItem
|
||||
@export var flashColor : Color = Color(1.5, 1.5, 1.5)
|
||||
|
||||
# Instance-Specific Damage Cooldown
|
||||
@export var id_block_time = 0.12
|
||||
var flash_time = 0
|
||||
var blocked_damage_ids : Array[int] = []
|
||||
|
||||
signal damage_taken(damage, dir, id)
|
||||
signal died
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
flash_time = max(flash_time-delta, 0)
|
||||
if(flash_time <= 0 and canvasItem != null):
|
||||
# Reset Hit Flash
|
||||
func _on_flash_ended():
|
||||
if canvasItem != null:
|
||||
canvasItem.modulate = Color(1,1,1)
|
||||
|
||||
func hurt(damage : int, dir : Vector2 = Vector2.ZERO, id : int = -1) -> bool:
|
||||
if not id in blocked_damage_ids:
|
||||
block_id(id)
|
||||
if canvasItem != null: canvasItem.modulate = flashColor
|
||||
flash_time = flash_duration
|
||||
hp = max(hp-damage, 0)
|
||||
$FlashTimer.start(flash_duration)
|
||||
hp -= damage
|
||||
damage_taken.emit(damage, dir, id)
|
||||
if(hp <= 0):
|
||||
die()
|
||||
return true
|
||||
return false
|
||||
|
||||
# When taking damage, prevent damage from that instance for id_block_time.
|
||||
func block_id(id : int):
|
||||
blocked_damage_ids.append(id)
|
||||
await get_tree().create_timer(id_block_time).timeout
|
||||
var id_pos = blocked_damage_ids.find(id)
|
||||
if not id_pos == -1: blocked_damage_ids.remove_at(id_pos)
|
||||
|
||||
|
||||
func die():
|
||||
died.emit()
|
||||
|
||||
func destroy():
|
||||
hurt(9999)
|
||||
hurt(10 * max_hp)
|
||||
|
|
|
|||
|
|
@ -5,3 +5,9 @@
|
|||
[node name="EnemyHurtbox" type="Area2D"]
|
||||
collision_layer = 18
|
||||
script = ExtResource("1_wa58b")
|
||||
|
||||
[node name="FlashTimer" type="Timer" parent="."]
|
||||
wait_time = 0.2
|
||||
one_shot = true
|
||||
|
||||
[connection signal="timeout" from="FlashTimer" to="." method="_on_flash_ended"]
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
class_name Global
|
||||
|
||||
# Returns the next damage instance (and increases by 1)
|
||||
static var next_dmg_id : int = 0 :
|
||||
get:
|
||||
next_dmg_id += 1
|
||||
return next_dmg_id
|
||||
|
||||
# Entrywise modulo
|
||||
static func vec_mod(vec : Vector2, modulus : float):
|
||||
return Vector2(fposmod(vec.x, modulus), fposmod(vec.y, modulus))
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
# Simple area that emits a signal when the player interacts while overlapping
|
||||
extends Area2D
|
||||
|
||||
@export var oneshot = true
|
||||
|
|
|
|||
|
|
@ -2,12 +2,15 @@ class_name Platform extends StaticBody2D
|
|||
var building
|
||||
|
||||
func _ready() -> void:
|
||||
# Choose a platform design at random
|
||||
if(randf() > 0.5):
|
||||
$Sprite2D.visible = true
|
||||
$Sprite2D2.visible = false
|
||||
|
||||
# Stretch platforms depending on their height to prevent gaps in the ceiling
|
||||
func init_at_horizontal_distortion(distortion : float):
|
||||
scale.x *= distortion
|
||||
|
||||
# When a platform is destroyed, destroy the whole building
|
||||
func destroy():
|
||||
building.destroy()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue