94 lines
2.4 KiB
GDScript
94 lines
2.4 KiB
GDScript
class_name Vec2
|
|
extends Node
|
|
|
|
static func get_surrounding_cells(post,range = 1,is_self = false):
|
|
var arr = []
|
|
if is_self:
|
|
arr.append(post)
|
|
for x in range:
|
|
for y in range:
|
|
arr.append_array([post + Vector2i(x+1,y),
|
|
post + Vector2i((x+1)*-1,y),
|
|
post + Vector2i(x,y+1),
|
|
post + Vector2i(x,(y+1)*-1),
|
|
|
|
post + Vector2i((x+1),y+1),
|
|
post + Vector2i((x+1)*-1,y+1),
|
|
|
|
post + Vector2i((x+1)*-1,(y+1)*-1),
|
|
post + Vector2i((x+1),(y+1)*-1),
|
|
])
|
|
|
|
return arr
|
|
static func vec_to_str(vec):
|
|
return str(vec.x) + "_" + str(vec.y)
|
|
|
|
static func str_to_vec(str):
|
|
var arr = str.rsplit("_")
|
|
return Vector2(int(arr[0]),int(arr[1]))
|
|
static func str_to_veci(str):
|
|
var arr = str.rsplit("_")
|
|
return Vector2i(int(arr[0]),int(arr[1]))
|
|
|
|
static func strArr_to_vecArr(strArr):
|
|
var vecArr = PackedVector2Array()
|
|
for str in strArr:
|
|
vecArr.append(str_to_vec(str))
|
|
return vecArr
|
|
|
|
static func has_post(start,end,post):
|
|
if start.x < end.x and start.y < end.y:
|
|
if (start.x ) <= post.x:
|
|
if (start.y ) <= post.y:
|
|
if (end.x ) >= post.x:
|
|
if (end.y ) >= post.y:
|
|
return true
|
|
if start.x < end.x and start.y > end.y:
|
|
if start.x <= post.x:
|
|
if end.y <= post.y:
|
|
if end.x >= post.x:
|
|
if start.y <= post.x:
|
|
return true
|
|
if start.x > end.x and start.y < end.y:
|
|
if end.x <= post.x:
|
|
if start.y <= post.y:
|
|
if start.x >= post.x:
|
|
if end.y > post.y:
|
|
return true
|
|
if start.x > end.x and start.y > end.y:
|
|
if end.x <= post.x:
|
|
if end.y <= post.y:
|
|
if (start.x ) <= post.x:
|
|
if (start.y ) <= post.y:
|
|
return true
|
|
return false
|
|
static func has_node_post_and(start,end,post,size):
|
|
var ret = true
|
|
#if !has_post(start,end,post + size/2):
|
|
#ret = false
|
|
if !has_post(start,end,post):
|
|
ret = false
|
|
if !has_post(start,end,post + size):
|
|
ret = false
|
|
if !has_post(start,end,Vector2(post.x + size.x,post.y)):
|
|
ret = false
|
|
if !has_post(start,end,Vector2(post.x,post.y + size.y)):
|
|
ret = false
|
|
return ret
|
|
static func has_node_post(start,end,post,size,is_mouse = false):
|
|
if start.distance_to(post) > (Vector2(0,0).distance_to(size) ):
|
|
return false
|
|
if is_mouse and has_post(start,end,Global.scence.get_global_mouse_position()):
|
|
return true
|
|
if has_post(start,end,post + size/2):
|
|
return true
|
|
if has_post(start,end,post):
|
|
return true
|
|
if has_post(start,end,post + size):
|
|
return true
|
|
if has_post(start,end,Vector2(post.x + size.x,post.y)):
|
|
return true
|
|
if has_post(start,end,Vector2(post.x,post.y + size.y)):
|
|
return true
|
|
return false
|