131 lines
4.0 KiB
GDScript
131 lines
4.0 KiB
GDScript
@tool
|
|
class_name Pool
|
|
extends StaticBody2D
|
|
|
|
@export var mana_r: int = 10
|
|
@export var mana_g: int = 10
|
|
@export var mana_b: int = 10
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready():
|
|
update()
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(_delta):
|
|
if $CollisionShape2D2.disabled:
|
|
# Prepare the shape query parameters
|
|
var query_parameters = PhysicsShapeQueryParameters2D.new()
|
|
var collision_shape = $CollisionShape2D2.shape
|
|
query_parameters.set_shape(collision_shape)
|
|
query_parameters.set_transform(Transform2D(0, position))
|
|
query_parameters.set_collision_mask(1) #1=players
|
|
|
|
# Get the Physics2DDirectSpaceState for collision checking
|
|
var space_state = get_world_2d().direct_space_state
|
|
|
|
# Check for collision
|
|
var collision_results = space_state.intersect_shape(query_parameters)
|
|
if collision_results.size() == 0:
|
|
$CollisionShape2D2.disabled=false
|
|
update()
|
|
|
|
func pool_coll_check():
|
|
var query_parameters = PhysicsShapeQueryParameters2D.new()
|
|
var collision_shape = $CollisionShape2D2.shape
|
|
query_parameters.set_shape(collision_shape)
|
|
query_parameters.set_transform(Transform2D(0, scale, 0, position))
|
|
query_parameters.set_collision_mask(2) #2=pools
|
|
|
|
# Get the Physics2DDirectSpaceState for collision checking
|
|
var space_state = get_world_2d().direct_space_state
|
|
|
|
# Check for collision
|
|
var collision_results = space_state.intersect_shape(query_parameters)
|
|
for result in collision_results:
|
|
if result.collider is Pool and result.collider != self:
|
|
if is_fully_overlapping(self, result.collider):
|
|
merge_with(result.collider)
|
|
|
|
func is_fully_overlapping(pool1, pool2):
|
|
var radius1 = pool1.scale[0]*pool1.get_node("CollisionShape2D2").scale[0]*pool1.get_node("CollisionShape2D2").shape.radius
|
|
var radius2 = pool2.scale[0]*pool2.get_node("CollisionShape2D2").scale[0]*pool1.get_node("CollisionShape2D2").shape.radius
|
|
var center_distance = pool1.position.distance_to(pool2.position)
|
|
|
|
# Check if one circle is completely inside the other
|
|
return center_distance <= abs(radius1 - radius2)
|
|
|
|
func merge_with(p: Pool):
|
|
# Calculate RGB sums for both pools
|
|
var sum_rgb_self = mana_r + mana_g + mana_b
|
|
var sum_rgb_other = p.mana_r + p.mana_g + p.mana_b
|
|
|
|
# Avoid division by zero
|
|
var total_sum = sum_rgb_self + sum_rgb_other
|
|
if total_sum == 0:
|
|
return
|
|
|
|
# Calculate weighted positions
|
|
var weighted_position_self = position * sum_rgb_self
|
|
var weighted_position_other = p.position * sum_rgb_other
|
|
|
|
# Compute new position as weighted average
|
|
position = (weighted_position_self + weighted_position_other) / total_sum
|
|
|
|
# Merge mana values
|
|
mana_r += p.mana_r
|
|
mana_g += p.mana_g
|
|
mana_b += p.mana_b
|
|
p.mana_r = 0
|
|
p.mana_g = 0
|
|
p.mana_b = 0
|
|
|
|
#update()
|
|
#p.update()
|
|
|
|
func update():
|
|
var max_value: float = max(mana_r, mana_g, mana_b)
|
|
if max_value==0:
|
|
self.queue_free()
|
|
return;
|
|
var normalized_r = mana_r / max_value
|
|
var normalized_g = mana_g / max_value
|
|
var normalized_b = mana_b / max_value
|
|
|
|
self.modulate = Color(normalized_r, normalized_g, normalized_b)
|
|
var s=sqrt((mana_r+mana_g+mana_b)/1000.0)
|
|
self.scale=Vector2(s,s)
|
|
self.queue_redraw() # Request to redraw the node
|
|
pool_coll_check()
|
|
|
|
func decay():
|
|
var new_player = load("res://player.tscn").instantiate()
|
|
new_player.apply_impulse(Vector2.from_angle(randf_range(0, 2 * PI)) * 100)
|
|
new_player.position = position
|
|
new_player.exchange_with(self)
|
|
new_player.get_node("CollisionShape2D").disabled=true
|
|
get_tree().root.add_child(new_player)
|
|
|
|
func save_data():
|
|
var data = {
|
|
"filename" : get_scene_file_path(),
|
|
"pos_x" : position.x,
|
|
"pos_y" : position.y,
|
|
"r" : mana_r,
|
|
"g" : mana_g,
|
|
"b" : mana_b,
|
|
}
|
|
return data
|
|
|
|
func load_data(data):
|
|
position=Vector2(data["pos_x"],data["pos_y"])
|
|
mana_r=data["r"]
|
|
mana_g=data["g"]
|
|
mana_b=data["b"]
|
|
|
|
func _on_decay_timer_timeout():
|
|
if not get_tree().root.get_node("Sim/ScreenOverlay/SettingsWindow/VBoxContainer/Decay").is_pressed():
|
|
return
|
|
if (mana_r+mana_g+mana_b)>randf_range(0,99999):
|
|
decay()
|
|
#$DecayTimer.wait_time=
|