Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node2D
- @onready var sprite = $Sprite2D
- # Called when the node enters the scene tree for the first time.
- func _ready():
- load_map()
- func load_map():
- var mapImage = sprite.get_texture().get_image()
- var color_dict = import_file("res://Map Data/regions.txt")
- var all_colors = []
- for i in color_dict.keys():
- var color = string_to_color(i)
- all_colors.append(color)
- print("ALL COLORS: ", all_colors)
- var pixel_dict = {}
- for i in all_colors:
- pixel_dict[i] = []
- print("PLEASE OH MY GOD PLEASE: ", pixel_dict)
- for y in range(mapImage.get_height()):
- for x in range(mapImage.get_width()):
- var pixel_color = mapImage.get_pixel(x, y)
- if pixel_color != Color(1, 1, 1, 0):
- var valid_color = false
- for i in all_colors:
- if pixel_color.is_equal_approx(i):
- var neighbors = get_neighbors(x, y)
- var border_pixel = false
- for j in neighbors:
- var j_color = mapImage.get_pixel(j[0], j[1])
- if pixel_color.is_equal_approx(j_color) == false:
- border_pixel = true
- if border_pixel:
- print("Yes")
- pixel_dict[pixel_color].append(Vector2(x, y))
- #for key in pixel_dict.keys():
- #create_polygon(pixel_dict[key], key, "country")
- create_polygon([Vector2(58, 693), Vector2(59, 693), Vector2(60, 693), Vector2(57, 694), Vector2(58, 694), Vector2(58, 693)], Color(1, 0, 0, 1), "Country")
- # Called every frame. 'delta' is the elapsed time since the previous frame.
- func _process(delta):
- #print(get_local_mouse_position())
- pass
- func import_file(filepath):
- var file = FileAccess.open(filepath, FileAccess.READ)
- if file != null:
- return JSON.parse_string(file.get_as_text().replace("_", " "))
- else:
- print("Failed to find file lol")
- return null
- func create_polygon(points, color, name):
- var area = Area2D.new()
- var poly = CollisionPolygon2D.new()
- var visible_poly = Polygon2D.new()
- visible_poly.polygon = points
- visible_poly.color = color
- poly.polygon = points
- area.add_child(visible_poly)
- area.add_child(poly)
- #area.position = Vector2(0, -400)
- area.set_name(name)
- $Provinces.add_child(area)
- func get_neighbors(x, y):
- var neighbors = []
- # Loop through all adjacent positions including diagonals
- for i in range(-1, 2): # from -1 to 1
- for j in range(-1, 2): # from -1 to 1
- if i == 0 and j == 0:
- continue # Skip the center pixel itself
- neighbors.append(Vector2(x + i, y + j))
- return neighbors
- func string_to_color(color_string):
- # Remove the parentheses and split the string by commas
- var nums = color_string.replace("(", "").replace(")", "").split(",")
- # Convert the split strings into float values and normalize RGB by 255
- return Color(float(nums[0]) / 255.0, float(nums[1]) / 255.0, float(nums[2]) / 255.0, float(nums[3]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement