Created
December 31, 2015 02:18
-
-
Save vinod8990/a403e30c1ad15394ac80 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
tool | |
extends Polygon2D | |
export(Texture) var top_left_texture = null | |
export(Texture) var top_center_texture = null | |
export(Texture) var top_right_texture = null | |
export(Texture) var right_center_texture = null | |
export(Texture) var left_center_texture = null | |
export(Texture) var bottom_left_texture = null | |
export(Texture) var bottom_center_texture = null | |
export(Texture) var bottom_right_texture = null | |
var _all_v = [] | |
func draw_level(): | |
set_process(true) | |
func _process(delta): | |
update() | |
func _draw(): | |
print("start") | |
_all_v = Array(get_polygon()) | |
var top_v = [] | |
var left_v = [] | |
var bottom_v = [] | |
var right_v = [] | |
for i in range(_all_v.size()): | |
var v1 = _all_v[i] | |
var v2 = _get_next_vertex(v1) | |
var tangent = (v1-v2).tangent() | |
var rot = rad2deg(atan2(tangent.x,tangent.y)) | |
if(rot>-45 and rot<=45): | |
if(top_v.find(v1)==-1): | |
top_v.append(v1) | |
elif(rot<-45 and rot>=-135): | |
if(right_v.find(v1)==-1): | |
right_v.append(v1) | |
elif(rot<-135 or rot>135): | |
if(bottom_v.find(v1)==-1): | |
bottom_v.append(v1) | |
elif(rot>45 and rot<=135): | |
if(left_v.find(v1)==-1): | |
left_v.append(v1) | |
draw_edges(right_v,right_center_texture) | |
draw_edges(left_v,left_center_texture) | |
draw_edges(bottom_v,bottom_center_texture) | |
draw_edges(top_v,top_center_texture) | |
for i in range(_all_v.size()): | |
var v = _all_v[i] | |
var p_v = _get_prev_vertex(v) | |
var n_v = _get_next_vertex(v) | |
var b_t = (p_v - v).tangent() | |
var f_t = (v - n_v).tangent() | |
var b_a = atan2(b_t.x,b_t.y) | |
var f_a = atan2(f_t.x,f_t.y) | |
if(top_v.find(v)>-1 and top_v.find(p_v)==-1): | |
print("tl_corner") | |
var o = (n_v - v).normalized()*-top_left_texture.get_width() | |
_draw_tex(v+o,Vector2(1,1),f_a,top_left_texture) | |
if(top_v.find(v)==-1 and top_v.find(p_v)>-1): | |
print("tr_corner") | |
_draw_tex(v,Vector2(1,1),b_a,top_right_texture) | |
if(bottom_v.find(v)>-1 and bottom_v.find(p_v)==-1): | |
print("bl_corner") | |
_draw_tex(v,Vector2(1,1),f_a,bottom_left_texture) | |
if(bottom_v.find(v)==-1 and bottom_v.find(p_v)>-1): | |
print("br_corner") | |
var o = (p_v - v).normalized()*top_left_texture.get_width() | |
_draw_tex(v+o,Vector2(1,1),b_a,bottom_right_texture) | |
VisualServer.canvas_item_add_set_transform(get_canvas_item(),Matrix32()) | |
func draw_edges(edges,texture): | |
for i in range(edges.size()): | |
var v1 = edges[i] | |
var v2 = _get_next_vertex(v1) | |
var distance = v1.distance_to(v2) | |
var count = distance/texture.get_width()#check how many will fit | |
var real_count = round(count) | |
var scale = 1 + (count - real_count) / real_count #scale up or down to perfectly fit to the last point | |
var tangent = (v1-v2).tangent() | |
var rot = atan2(tangent.x,tangent.y)#get the normal rotation angle | |
for i in range(0,real_count): | |
var x = v1.x + (v2-v1).normalized().x*i*texture.get_width()*scale | |
var y = v1.y +(v2-v1).normalized().y*i*texture.get_height()*scale | |
_draw_tex(Vector2(x,y),Vector2(scale,1),rot,texture) | |
func _draw_tex(pos,scale,rot,tex): | |
var temp_sprite = Sprite.new()#find another way to set the matrix correctly | |
temp_sprite.set_pos(pos) | |
temp_sprite.set_scale(scale) | |
temp_sprite.set_rot(rot) | |
var m = temp_sprite.get_global_transform() | |
VisualServer.canvas_item_add_set_transform(get_canvas_item(),m) | |
temp_sprite.queue_free() | |
temp_sprite = null | |
draw_texture(tex,Vector2(),Color("#ffffff")) | |
func _get_next_vertex(v): | |
var index = _all_v.find(v) | |
index += 1 | |
if(index >= _all_v.size()): | |
index = 0 | |
return _all_v[index] | |
func _get_prev_vertex(v): | |
var index = _all_v.find(v) | |
index -= 1 | |
if(index < 0): | |
index = _all_v.size()-1 | |
return _all_v[index] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Godot Developer
Could you please Give me an project