Advertisement
FaresFilms

Untitled

Apr 28th, 2024
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node2D
  2.  
  3. @onready var sprite = $Sprite2D
  4. # Called when the node enters the scene tree for the first time.
  5. func _ready():
  6.     load_map()
  7.  
  8. func load_map():
  9.     var mapImage = sprite.get_texture().get_image()
  10.     var color_dict = import_file("res://Map Data/regions.txt")
  11.     var all_colors = []
  12.     for i in color_dict.keys():
  13.         var color = string_to_color(i)
  14.         all_colors.append(color)
  15.         print("ALL COLORS: ", all_colors)
  16.     var pixel_dict = {}
  17.     for i in all_colors:
  18.         pixel_dict[i] = []
  19.     print("PLEASE OH MY GOD PLEASE: ", pixel_dict)
  20.    
  21.    
  22.     for y in range(mapImage.get_height()):
  23.         for x in range(mapImage.get_width()):
  24.             var pixel_color = mapImage.get_pixel(x, y)
  25.             if pixel_color != Color(1, 1, 1, 0):
  26.                 var valid_color = false
  27.                 for i in all_colors:
  28.                     if pixel_color.is_equal_approx(i):
  29.                         var neighbors = get_neighbors(x, y)
  30.                         var border_pixel = false
  31.                         for j in neighbors:
  32.                             var j_color = mapImage.get_pixel(j[0], j[1])
  33.                             if pixel_color.is_equal_approx(j_color) == false:
  34.                                 border_pixel = true
  35.                         if border_pixel:
  36.                             print("Yes")
  37.                             pixel_dict[pixel_color].append(Vector2(x, y))
  38.  
  39.                        
  40.     #for key in pixel_dict.keys():
  41.         #create_polygon(pixel_dict[key], key, "country")
  42.     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")
  43.    
  44.  
  45. # Called every frame. 'delta' is the elapsed time since the previous frame.
  46. func _process(delta):
  47.     #print(get_local_mouse_position())
  48.     pass
  49.  
  50. func import_file(filepath):
  51.     var file = FileAccess.open(filepath, FileAccess.READ)
  52.     if file != null:
  53.         return JSON.parse_string(file.get_as_text().replace("_", " "))
  54.     else:
  55.         print("Failed to find file lol")
  56.         return null
  57.  
  58. func create_polygon(points, color, name):
  59.     var area = Area2D.new()
  60.     var poly = CollisionPolygon2D.new()
  61.     var visible_poly = Polygon2D.new()
  62.     visible_poly.polygon = points
  63.     visible_poly.color = color
  64.     poly.polygon = points
  65.  
  66.    
  67.    
  68.     area.add_child(visible_poly)
  69.     area.add_child(poly)
  70.     #area.position = Vector2(0, -400)
  71.     area.set_name(name)
  72.     $Provinces.add_child(area)
  73.  
  74.  
  75.  
  76.  
  77. func get_neighbors(x, y):
  78.     var neighbors = []
  79.     # Loop through all adjacent positions including diagonals
  80.     for i in range(-1, 2):  # from -1 to 1
  81.         for j in range(-1, 2):  # from -1 to 1
  82.             if i == 0 and j == 0:
  83.                 continue  # Skip the center pixel itself
  84.             neighbors.append(Vector2(x + i, y + j))
  85.     return neighbors
  86.  
  87. func string_to_color(color_string):
  88.     # Remove the parentheses and split the string by commas
  89.     var nums = color_string.replace("(", "").replace(")", "").split(",")
  90.     # Convert the split strings into float values and normalize RGB by 255
  91.     return Color(float(nums[0]) / 255.0, float(nums[1]) / 255.0, float(nums[2]) / 255.0, float(nums[3]))
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement