Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node2D
- # Viewport size.
- var viewport_size : Vector2 = Vector2.ZERO
- # Camera zoom.
- var camera_zoom : Vector2 = Vector2.ZERO
- # Boundary size.
- var boundary_size : Vector2 = Vector2.ZERO
- # Boundary buffer.
- var boundary_buffer : int = 0
- # Civilian scene.
- export (PackedScene) var civilian_scene
- # Ready.
- func _ready():
- # Seed random numbers.
- randomize()
- # Store the viewport size.
- viewport_size = get_viewport_rect().size
- # Store the camera zoom.
- camera_zoom = $Player/Camera2D.zoom
- # Calculate boundary size.
- boundary_size.x = int((viewport_size.x * camera_zoom.x + boundary_buffer) / $SpaceTiles.cell_size.x)
- boundary_size.y = int((viewport_size.y * camera_zoom.y + boundary_buffer) / $SpaceTiles.cell_size.y)
- # Process.
- func _process(delta):
- # Draw the boundary. Pass through player position.
- draw_boundary_tiles($SpaceTiles.world_to_map($Player.position))
- # Draw the boundary tiles.
- func draw_boundary_tiles(player_position):
- # Get starting boundary point.
- var bound_start_cell = Vector2(int(player_position.x - boundary_size.x),
- int(player_position.y - boundary_size.y))
- # Get ending boundary point.
- var bound_end_cell = Vector2(int(player_position.x + boundary_size.x),
- int(player_position.y + boundary_size.y))
- # TODO: Prioritise boundary drawing based on current direction of player.
- # Use $Spaceship.rotation / rotation_degrees
- # i.e. cut off loop when a drawn tile is encountered
- # Draw from start to end.
- for y in range(bound_start_cell.y, bound_end_cell.y):
- for x in range(bound_start_cell.x, bound_end_cell.x):
- if $SpaceTiles.get_cell(x, y) == TileMap.INVALID_CELL:
- set_cell($SpaceTiles, x, y, 0)
- # Set a given cell on the given tilemap.
- func set_cell(tilemap, x, y, id):
- tilemap.set_cell(x, y, id, false, false, false, get_subtile_with_priority(id, tilemap))
- # Generate a tile adhering to priority of the given tilemap.
- func get_subtile_with_priority(id, tilemap: TileMap):
- var tiles = tilemap.tile_set
- var rect = tilemap.tile_set.tile_get_region(id)
- var size_x = rect.size.x / tiles.autotile_get_size(id).x
- var size_y = rect.size.y / tiles.autotile_get_size(id).y
- var tile_array = []
- for x in range(size_x):
- for y in range(size_y):
- var priority = tiles.autotile_get_subtile_priority(id, Vector2(x ,y))
- for p in priority:
- tile_array.append(Vector2(x,y))
- return tile_array[randi() % tile_array.size()]
- # Spawn a civilian.
- func _on_CivilianTimer_timeout():
- # Instance a civilian.
- var civilian = civilian_scene.instance()
- # Choose a random location to spawn.
- var civilian_spawn_location = get_node("/root/Space/Player/Camera2D/NPCPath/NPCSpawnLocation")
- civilian_spawn_location.offset = randi()
- # Set direction perpendicular to spawn location.
- var direction = civilian_spawn_location.rotation + (PI / 2)
- # Add randomness to direction.
- direction += rand_range(-PI / 4, PI / 4)
- civilian.rotation = direction
- # Set the location.
- civilian.position = civilian_spawn_location.position
- # Choose velocity for civilian.
- var velocity = Vector2(rand_range(150.0, 250.0), 0.0)
- civilian.set_linear_velocity(velocity.rotated(direction))
- # Spawn civilian by adding to scene.
- add_child(civilian)
- # Start timer again.
- $CivilianTimer.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement