JHRGB/Map.gd
2023-12-11 09:10:25 +00:00

199 lines
6.9 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=$"../ScreenOverlay/HUD/HBoxContainer/ToolSelect".get_selected()
if tool.name=="Create Player":
spawn_player(pos,vol)
if tool.name=="Create Pool":
var r=int(tool.get_node("Red").text)
var g=int(tool.get_node("Green").text)
var b=int(tool.get_node("Blue").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").text)
var g=int(tool.get_node("Green").text)
var b=int(tool.get_node("Blue").text)
var rad=int(tool.get_node("Radius").text)
var cnt=int(tool.get_node("Count").text)
if r+g+b>0:
spawn_pool_circle(pos,rad,cnt,r,g,b)
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
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 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 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().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)
func _on_new_dialog_confirmed():
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)