115 lines
3.4 KiB
GDScript
115 lines
3.4 KiB
GDScript
class_name Player
|
|
extends Node2D
|
|
|
|
var carrying_r=0
|
|
var carrying_g=0
|
|
var carrying_b=0
|
|
|
|
func _process(_delta):
|
|
if $Body2D/CollisionShape2D.disabled:
|
|
# Prepare the shape query parameters
|
|
var query_parameters = PhysicsShapeQueryParameters2D.new()
|
|
var collision_shape = $Body2D/CollisionShape2D.shape
|
|
query_parameters.set_shape(collision_shape)
|
|
query_parameters.set_transform(Transform2D(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)
|
|
if collision_results.size() == 0:
|
|
$Body2D/CollisionShape2D.disabled=false
|
|
|
|
#func _physics_process(delta):
|
|
# if Input.is_action_pressed('move_up'):
|
|
# velocity.y -= 1
|
|
# if Input.is_action_pressed('move_down'):
|
|
# velocity.y += 1
|
|
# if Input.is_action_pressed('move_left'):
|
|
# velocity.x -= 1
|
|
# if Input.is_action_pressed('move_right'):
|
|
# velocity.x += 1
|
|
# if Input.is_action_just_pressed('take'):
|
|
# drop()
|
|
#
|
|
# var collision_info = move_and_collide(velocity * delta)
|
|
# if collision_info:
|
|
# velocity = velocity.bounce(collision_info.get_normal())
|
|
# var collider=collision_info.get_collider();
|
|
# if collider.get_parent() is Pool:
|
|
# exchange_with(collider.get_parent())
|
|
# collider.get_parent().update()
|
|
# if collider.get_parent() is Player:
|
|
# drop()
|
|
# update()
|
|
|
|
func drop():
|
|
if not is_carrying():
|
|
return
|
|
var new_pool: Pool = load("res://pool.tscn").instantiate()
|
|
new_pool.position = $Body2D.position
|
|
new_pool.mana_r=carrying_r
|
|
new_pool.mana_g=carrying_g
|
|
new_pool.mana_b=carrying_b
|
|
new_pool.get_node("StaticBody2D/CollisionShape2D2").disabled=true;
|
|
#new_pool.get_node("StaticBody2D/CollisionShape2D2").set_deferred("disabled", true)
|
|
carrying_r=0
|
|
carrying_g=0
|
|
carrying_b=0
|
|
get_tree().root.get_node("Sim").call_deferred("add_child",new_pool)
|
|
update()
|
|
|
|
func doubledrop(player: Player):
|
|
if not is_carrying() or not player.is_carrying():
|
|
return
|
|
var new_pool: Pool = load("res://pool.tscn").instantiate()
|
|
new_pool.position = ($Body2D.position + player.get_node("Body2D").position ) /2
|
|
new_pool.mana_r=carrying_r+player.carrying_r
|
|
new_pool.mana_g=carrying_g+player.carrying_g
|
|
new_pool.mana_b=carrying_b+player.carrying_b
|
|
new_pool.get_node("StaticBody2D/CollisionShape2D2").disabled=true;
|
|
carrying_r=0
|
|
carrying_g=0
|
|
carrying_b=0
|
|
player.carrying_r=0
|
|
player.carrying_g=0
|
|
player.carrying_b=0
|
|
get_tree().root.get_node("Sim").call_deferred("add_child",new_pool)
|
|
update()
|
|
|
|
func exchange_with(pool: Pool):
|
|
if is_carrying():
|
|
pool.mana_r+=carrying_r
|
|
pool.mana_g+=carrying_g
|
|
pool.mana_b+=carrying_b
|
|
carrying_r=0
|
|
carrying_g=0
|
|
carrying_b=0
|
|
#position += velocity.normalized() #move away
|
|
else:
|
|
if pool.mana_r>0 and pool.mana_r >= pool.mana_g and pool.mana_r >= pool.mana_b:
|
|
pool.mana_r -= 1
|
|
carrying_r=1
|
|
elif pool.mana_g>0 and pool.mana_g >= pool.mana_r and pool.mana_g >= pool.mana_b:
|
|
pool.mana_g -= 1
|
|
carrying_g=1
|
|
elif pool.mana_b>0:
|
|
pool.mana_b -= 1
|
|
carrying_b=1
|
|
update()
|
|
|
|
func update():
|
|
var max_value = max(carrying_r, carrying_g, carrying_b)
|
|
if max_value==0:
|
|
self.modulate = Color(1, 1, 1, 1)
|
|
return
|
|
var normalized_r = carrying_r / max_value
|
|
var normalized_g = carrying_g / max_value
|
|
var normalized_b = carrying_b / max_value
|
|
self.modulate = Color(normalized_r, normalized_g, normalized_b)
|
|
|
|
func is_carrying():
|
|
return (carrying_r+carrying_g+carrying_b)>0
|