extends Node2D var mouse_down_position = Vector2() func _unhandled_input(event): if event is InputEventMouseButton: if event.button_index == MOUSE_BUTTON_LEFT: if event.pressed: # Record the position where the mouse was pressed down mouse_down_position = get_global_mouse_position() else: # Handle mouse button release var pos = get_global_mouse_position() var vol = pos - mouse_down_position spawn(mouse_down_position,vol) func spawn(pos,vol): var r=int($"../ScreenOverlay/HUD/HBoxContainer/Colors/Red".text) var g=int($"../ScreenOverlay/HUD/HBoxContainer/Colors/Green".text) var b=int($"../ScreenOverlay/HUD/HBoxContainer/Colors/Blue".text) if r+g+b>0: spawm_pool(pos,r,g,b) else: spawn_player(pos,vol) func spawm_pool(pos,red,green,blue): var new_pool: Pool = load("res://pool.tscn").instantiate() new_pool.position = pos new_pool.mana_r=red new_pool.mana_g=green new_pool.mana_b=blue new_pool.get_node("CollisionShape2D2").disabled=true; #new_pool.get_node("StaticBody2D/CollisionShape2D2").set_deferred("disabled", true) get_tree().root.get_node("Sim/Map").call_deferred("add_child",new_pool) func spawn_player(pos,vol): var new_player = load("res://player.tscn").instantiate() new_player.position = pos new_player.apply_impulse(vol) # Prepare the shape query parameters var query_parameters = PhysicsShapeQueryParameters2D.new() var collision_shape = new_player.get_node("CollisionShape2D").shape query_parameters.set_shape(collision_shape) query_parameters.set_transform(Transform2D(0, pos)) query_parameters.set_collision_mask(1) # 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: # No collision, safe to add child add_child(new_player) else: for collision in collision_results: #print if collision.collider.get_parent() is Pool: new_player.queue_free() collision.collider.get_parent()._on_decay_timer_timeout() return var marker = Sprite2D.new() marker.position = new_player.position marker.scale=Vector2(0.1,0.1) marker.texture = load("res://images/crosshair.png") add_child(marker) new_player.queue_free() func _ready(): poolcircle(Vector2(0,0),200,32,100,0,0) poolcircle(Vector2(600,0),200,32,0,100,0) poolcircle(Vector2(1200,0),200,32,0,0,100) poolcircle(Vector2(0,600),200,32,0,50,50) poolcircle(Vector2(600,600),200,32,50,0,50) poolcircle(Vector2(1200,600),200,32,50,50,0) #poolcircle(Vector2(600,200),2000,100,1000,1000,1000) func poolcircle(pos: Vector2, radius, count, r,g,b): for n in range(1,count+1): var new_pool: Pool = load("res://pool.tscn").instantiate() var t=(float(n)/count)*3.141*2 new_pool.position = pos + (Vector2(sin(t),cos(t))*radius) new_pool.mana_r=r new_pool.mana_g=g new_pool.mana_b=b new_pool.get_node("CollisionShape2D2").disabled=true; add_child(new_pool) func load_map(filename): print("load:", filename) if not FileAccess.file_exists(filename): return # Error! We don't have a save to load. # deleting saveable objects. var save_nodes = get_tree().get_nodes_in_group("Persist") for i in save_nodes: i.queue_free() # Load the file line by line and process var save_game = FileAccess.open(filename, FileAccess.READ) while save_game.get_position() < save_game.get_length(): var json_string = save_game.get_line() # Creates the helper class to interact with JSON var json = JSON.new() # Check if there is any error while parsing the JSON string, skip in case of failure var parse_result = json.parse(json_string) if not parse_result == OK: print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line()) continue # Get the data from the JSON object var node_data = json.get_data() # Firstly, we need to create the object and add it to the tree and set its position. var new_object = load(node_data["filename"]).instantiate() # Check the node has a load function. if !new_object.has_method("load_data"): print("persistent node '%s' is missing a load_data() function, skipped" % new_object.name) new_object.queue_free() continue new_object.load_data(node_data) add_child(new_object)