Created
May 19, 2016 18:46
-
-
Save ruimgoncalves/209e13a4ae5dac5b11c1941ef6ea5a9d to your computer and use it in GitHub Desktop.
Edge Screen Arrow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Sprite | |
export(NodePath) var originPath | |
export(NodePath) var targetPath setget setTarget | |
var t = 1 | |
func _ready(): | |
canActivateProcess() | |
func _process(delta): | |
t += delta | |
var target = get_node(targetPath) | |
var vpRect = get_viewport_rect() | |
var toCamera = get_viewport_transform().affine_inverse() | |
var wRect = toCamera.xform(vpRect) | |
var bbox = getBoundingBox(target) | |
if wRect.intersects(bbox): | |
if is_visible(): | |
hide() | |
else: | |
if is_hidden(): | |
show() | |
var oPos | |
if !originPath.is_empty(): # use the origin nodes position | |
oPos = get_node(originPath).get_global_pos() | |
else: # use the camera center position | |
oPos = toCamera * (vpRect.size / 2) | |
var tPos = target.get_global_pos() | |
var ang_op = oPos.angle_to_point(tPos) | |
#set_pos(toCamera * (vpRect.size / 2)) | |
var mySize = get_item_rect() | |
var halfSize = max(mySize.size.x, mySize.size.y) | |
var pRectEdge = segmentIntersectsRect(wRect.grow(-halfSize / 1.5), oPos, tPos) | |
if pRectEdge != null: | |
set_pos(pRectEdge) | |
set_rot(ang_op) | |
func setTarget(val): | |
targetPath = val | |
canActivateProcess(); | |
func canActivateProcess(): | |
var canActivate = (originPath != null && targetPath != null) | |
set_process(canActivate) | |
func getBoundingBox(node, centered = true): | |
var size = node.get_item_rect().size * node.get_scale() | |
var pos = node.get_global_pos() | |
if centered: | |
pos -= size / 2 | |
return Rect2(pos, size) | |
func segmentIntersectsRect(rect, p1, p2): | |
var topLeft = rect.pos | |
var topRight = Vector2(rect.end.x, rect.pos.y) | |
var bottomLeft = Vector2(rect.pos.x, rect.end.y) | |
var bottomRight = Vector2(rect.end.x, rect.end.y) | |
var top = Geometry.segment_intersects_segment_2d(topLeft, topRight, p1, p2) | |
if top != null: | |
return top | |
var bottom = Geometry.segment_intersects_segment_2d(bottomLeft, bottomRight, p1, p2) | |
if bottom != null: | |
return bottom | |
var left = Geometry.segment_intersects_segment_2d(topLeft, bottomLeft, p1, p2) | |
if left != null: | |
return left | |
var right = Geometry.segment_intersects_segment_2d(topRight, bottomRight, p1, p2) | |
if right != null: | |
return right | |
return null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment