Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 3.31 KB | Source Code | 0 0
  1. extends Node2D
  2.  
  3. # Viewport size.
  4. var viewport_size : Vector2 = Vector2.ZERO
  5.  
  6. # Camera zoom.
  7. var camera_zoom : Vector2 = Vector2.ZERO
  8.  
  9. # Boundary size.
  10. var boundary_size : Vector2 = Vector2.ZERO
  11.  
  12. # Boundary buffer.
  13. var boundary_buffer : int = 0
  14.  
  15.  
  16. # Civilian scene.
  17. export (PackedScene) var civilian_scene
  18.  
  19. # Ready.
  20. func _ready():
  21.     # Seed random numbers.
  22.     randomize()
  23.    
  24.     # Store the viewport size.
  25.     viewport_size = get_viewport_rect().size
  26.    
  27.     # Store the camera zoom.
  28.     camera_zoom = $Player/Camera2D.zoom
  29.    
  30.     # Calculate boundary size.
  31.     boundary_size.x = int((viewport_size.x * camera_zoom.x + boundary_buffer) / $SpaceTiles.cell_size.x)
  32.     boundary_size.y = int((viewport_size.y * camera_zoom.y + boundary_buffer) / $SpaceTiles.cell_size.y)
  33.  
  34.  
  35. # Process.
  36. func _process(delta):
  37.  
  38.     # Draw the boundary. Pass through player position.
  39.     draw_boundary_tiles($SpaceTiles.world_to_map($Player.position))
  40.    
  41. # Draw the boundary tiles.
  42. func draw_boundary_tiles(player_position):
  43.    
  44.     # Get starting boundary point.
  45.     var bound_start_cell = Vector2(int(player_position.x - boundary_size.x),
  46.                                    int(player_position.y - boundary_size.y))
  47.    
  48.     # Get ending boundary point.
  49.     var bound_end_cell = Vector2(int(player_position.x + boundary_size.x),
  50.                                  int(player_position.y + boundary_size.y))
  51.    
  52.     # TODO: Prioritise boundary drawing based on current direction of player.
  53.     #       Use $Spaceship.rotation / rotation_degrees
  54.     #       i.e. cut off loop when a drawn tile is encountered
  55.    
  56.     # Draw from start to end.
  57.     for y in range(bound_start_cell.y, bound_end_cell.y):
  58.         for x in range(bound_start_cell.x, bound_end_cell.x):
  59.             if $SpaceTiles.get_cell(x, y) == TileMap.INVALID_CELL:
  60.                 set_cell($SpaceTiles, x, y, 0)
  61.  
  62. # Set a given cell on the given tilemap.
  63. func set_cell(tilemap, x, y, id):
  64.     tilemap.set_cell(x, y, id, false, false, false, get_subtile_with_priority(id, tilemap))
  65.  
  66. # Generate a tile adhering to priority of the given tilemap.
  67. func get_subtile_with_priority(id, tilemap: TileMap):
  68.     var tiles = tilemap.tile_set
  69.     var rect = tilemap.tile_set.tile_get_region(id)
  70.     var size_x = rect.size.x / tiles.autotile_get_size(id).x
  71.     var size_y = rect.size.y / tiles.autotile_get_size(id).y
  72.     var tile_array = []
  73.     for x in range(size_x):
  74.         for y in range(size_y):
  75.             var priority = tiles.autotile_get_subtile_priority(id, Vector2(x ,y))
  76.             for p in priority:
  77.                 tile_array.append(Vector2(x,y))
  78.  
  79.     return tile_array[randi() % tile_array.size()]
  80.  
  81. # Spawn a civilian.
  82. func _on_CivilianTimer_timeout():
  83.     # Instance a civilian.
  84.     var civilian = civilian_scene.instance()
  85.    
  86.     # Choose a random location to spawn.
  87.     var civilian_spawn_location = get_node("/root/Space/Player/Camera2D/NPCPath/NPCSpawnLocation")
  88.     civilian_spawn_location.offset = randi()
  89.    
  90.     # Set direction perpendicular to spawn location.
  91.     var direction = civilian_spawn_location.rotation + (PI / 2)
  92.    
  93.     # Add randomness to direction.
  94.     direction += rand_range(-PI / 4, PI / 4)
  95.     civilian.rotation = direction
  96.    
  97.     # Set the location.
  98.     civilian.position = civilian_spawn_location.position
  99.    
  100.     # Choose velocity for civilian.
  101.     var velocity = Vector2(rand_range(150.0, 250.0), 0.0)
  102.     civilian.set_linear_velocity(velocity.rotated(direction))
  103.    
  104.     # Spawn civilian by adding to scene.
  105.     add_child(civilian)
  106.    
  107.     # Start timer again.
  108.     $CivilianTimer.start()
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement