Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var selection = []
- var selection_shape
- var selection_shape_query
- var selection_origin
- var selection_rect = Rect2()
- var space_state
- func _ready():
- space_state = get_world_2d().direct_space_state
- selection_shape = RectangleShape2D.new()
- selection_shape_query = Physics2DShapeQueryParameters.new()
- selection_shape_query.set_shape(selection_shape)
- selection_shape_query.collide_with_bodies = false
- selection_shape_query.collide_with_areas = true
- selection_shape_query.collision_layer = 4
- func _unhandled_input(event):
- if event is InputEventMouseMotion:
- if Input.is_action_pressed("mouse_left"):
- update_select_area()
- if event.is_action_pressed("mouse_left"):
- update_selection([])
- $SelectVisual.visible = true
- selection_origin = get_global_mouse_position()
- update_select_area()
- if event.is_action_released("mouse_left"):
- $SelectVisual.visible = false
- func update_select_area():
- var mouse_pos = get_global_mouse_position()
- # set our selection_rect, the human readable selection area
- selection_rect.position = Vector2(min(mouse_pos.x, selection_origin.x), min(mouse_pos.y, selection_origin.y))
- selection_rect.end = Vector2(max(mouse_pos.x, selection_origin.x), max(mouse_pos.y, selection_origin.y))
- # set visuals
- $SelectVisual.rect_position = selection_rect.position
- $SelectVisual.rect_size = selection_rect.size
- # set our shape
- selection_shape.extents = selection_rect.size / 2
- selection_shape_query.transform.origin = selection_rect.position + selection_rect.size / 2
- # query the space_state about what our shape is overlapping. it's set to only detect unit hitboxes in the ready function
- var new_selection = space_state.intersect_shape(selection_shape_query)
- # trims the query return to what info we need
- for i in range(len(new_selection)):
- new_selection[i] = new_selection[i]["collider"].owner
- update_selection(new_selection)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement