206 lines
7.3 KiB
GDScript
206 lines
7.3 KiB
GDScript
extends Node2D
|
|
|
|
var mouse_down_position = Vector2()
|
|
var controlling=null
|
|
|
|
func _process(_delta):
|
|
if controlling and is_instance_valid(controlling):
|
|
$Camera2D.position=controlling.position
|
|
|
|
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 tool=%ToolSelect.get_selected()
|
|
if tool.name=="Create Player":
|
|
spawn_player(pos,vol)
|
|
if tool.name=="Create Pool":
|
|
var r=int(tool.get_node("Red/val").text)
|
|
var g=int(tool.get_node("Green/val").text)
|
|
var b=int(tool.get_node("Blue/val").text)
|
|
if r+g+b>0:
|
|
spawm_pool(pos,r,g,b)
|
|
if tool.name=="Paste":
|
|
var filename="user://saves/"+tool.get_item_text(tool.get_selected_id())
|
|
get_tree().root.get_node("Sim/Map").paste_map(filename,pos)
|
|
if tool.name=="Create Pool Circle":
|
|
var r=int(tool.get_node("Red/val").text)
|
|
var g=int(tool.get_node("Green/val").text)
|
|
var b=int(tool.get_node("Blue/val").text)
|
|
var rad=int(tool.get_node("Radius/val").text)
|
|
var cnt=int(tool.get_node("Count/val").text)
|
|
if r+g+b>0:
|
|
spawn_pool_circle(pos,rad,cnt,r,g,b)
|
|
if tool.name=="Create Player Circle":
|
|
var rad=int(tool.get_node("Radius/val").text)
|
|
var cnt=int(tool.get_node("Count/val").text)
|
|
spawn_player_circle(pos,rad,cnt)
|
|
if tool.name=="Delete":
|
|
var space_state = get_world_2d().direct_space_state
|
|
var query_params = PhysicsPointQueryParameters2D.new()
|
|
query_params.position = pos
|
|
query_params.collide_with_areas = true # Set to false if you only want to check collision with bodies
|
|
query_params.collide_with_bodies = true # Set to false if you only want to check collision with areas
|
|
query_params.collision_mask = 0b1111111111111111 # Adjust as needed
|
|
|
|
var results = space_state.intersect_point(query_params, 32) # Adjust max_results as needed
|
|
for result in results:
|
|
result.collider.queue_free()
|
|
if tool.name=="Control Player":
|
|
var closest_node = null
|
|
var min_distance = INF # Infinite distance initially
|
|
for node in %Map.get_children():
|
|
if node is Player:
|
|
var distance = pos.distance_to(node.global_position)
|
|
if distance < min_distance:
|
|
min_distance = distance
|
|
closest_node = node
|
|
controlling=closest_node
|
|
if tool.name=="Impulse":
|
|
if tool.get_node("Type").selected==0: # linear
|
|
for node in %Map.get_children():
|
|
if node is Player:
|
|
var direction = vol.normalized()
|
|
var distance = pos.distance_to(node.position)
|
|
var dropOff = int(tool.get_node("Attenuation/val").text)
|
|
var power = int(tool.get_node("Power/val").text)
|
|
var magnitude = power * vol.length()
|
|
if dropOff:
|
|
magnitude/= distance * dropOff
|
|
node.apply_impulse(direction * magnitude)
|
|
if tool.get_node("Type").selected==1: # radial
|
|
for node in %Map.get_children():
|
|
if node is Player:
|
|
var direction = pos.direction_to(node.position)
|
|
var distance = pos.distance_to(node.position)
|
|
var dropOff = int(tool.get_node("DropOff").text)
|
|
var power = int(tool.get_node("Power").text)
|
|
var magnitude = power * vol.length()
|
|
if dropOff:
|
|
magnitude/= distance * dropOff
|
|
node.apply_impulse(direction * magnitude)
|
|
|
|
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
|
|
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)
|
|
add_child(new_player)
|
|
|
|
func spawn_pool_circle(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 spawn_player_circle(pos: Vector2, radius, count):
|
|
for n in range(1,count+1):
|
|
var new_player: Player = load("res://player.tscn").instantiate()
|
|
var t=(float(n)/count)*3.141*2
|
|
new_player.position = pos + (Vector2(sin(t),cos(t))*radius)
|
|
#new_player.get_node("CollisionShape2D2").disabled=true;
|
|
add_child(new_player)
|
|
|
|
func paste_map(filename,offset):
|
|
print("paste:", filename)
|
|
if not FileAccess.file_exists(filename):
|
|
return # Error! We don't have a save to load.
|
|
|
|
# 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)
|
|
new_object.position+=offset
|
|
add_child(new_object)
|
|
|
|
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().root.get_node("Sim/Map").get_children()
|
|
for i in save_nodes:
|
|
if i is Player or i is Pool:
|
|
i.collision_layer = 0
|
|
i.collision_mask = 0
|
|
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)
|
|
|
|
func _on_new_dialog_confirmed():
|
|
await get_tree().process_frame
|
|
var save_nodes = get_tree().get_nodes_in_group("Persist")
|
|
for i in save_nodes:
|
|
i.queue_free()
|
|
$Camera2D.position=Vector2(0,0)
|
|
$Camera2D.zoom=Vector2(1,1)
|