Last active
October 18, 2020 06:33
-
-
Save sairam4123/c340e8f7420062bf81b3efc325709f5c to your computer and use it in GitHub Desktop.
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 GridMap | |
enum Roads {ROAD_END = 9, ROAD_STARIGHT = 15, ROAD_CURVE = 11, ROAD_EMPTY = 12, ROAD_3_WAY = 13, ROAD_4_WAY = 14} | |
const RAY_LENGTH = 1000 | |
var send_ray = false | |
var event_position = Vector2(0, 0) | |
var camera = null | |
var last_road: Vector3 = Vector3() | |
func _ready(): | |
camera = get_node("../../Camera") | |
# set_cell_item(10, 0, 10, 13, get_orientation_from_degrees(null, 0)) | |
# set_cell_item(13, 0, 13, 13, get_orientation_from_degrees(null, 90)) | |
# set_cell_item(16, 0, 16, 13, get_orientation_from_degrees(null, 180)) | |
# set_cell_item(20, 0, 20, 13, get_orientation_from_degrees(null, 270)) | |
func get_item_to_remove(vec: Vector3) -> int: | |
var relative_tiles = PoolVector3Array([ | |
Vector3(vec.x+1, vec.y, vec.z+1), | |
Vector3(vec.x-1, vec.y, vec.z-1), | |
Vector3(vec.x-1, vec.y, vec.z+1), | |
Vector3(vec.x+1, vec.y, vec.z-1), | |
Vector3(vec.x+1, vec.y, vec.z), | |
Vector3(vec.x-1, vec.y, vec.z), | |
Vector3(vec.x, vec.y, vec.z+1), | |
Vector3(vec.x, vec.y, vec.z-1) | |
]) | |
for tile in relative_tiles: | |
var direction = tile - vec | |
if ((get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_END) or (get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_STARIGHT)) and direction.x: | |
for relative_tile_curve in relative_tiles: | |
var direction_curve = relative_tile_curve - vec | |
if get_cell_item(relative_tile_curve.x, relative_tile_curve.y, relative_tile_curve.z) == Roads.ROAD_3_WAY and direction_curve.z: | |
print("something") | |
set_cell_item(relative_tile_curve.x, relative_tile_curve.y, relative_tile_curve.z, Roads.ROAD_4_WAY) | |
if get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_STARIGHT and direction.x: | |
set_cell_item(tile.x, tile.y, tile.z, Roads.ROAD_END, get_orientation_from_degrees(null, (180 if not direction.x == -1 else 360))) | |
if get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_STARIGHT and direction.z: | |
set_cell_item(tile.x, tile.y, tile.z, Roads.ROAD_END, get_orientation_from_degrees(null, (90 if not direction.z == -1 else 270))) | |
return -1 | |
func get_orientation_from_degrees(vec=null, deg=null): | |
if !vec: | |
vec = Vector3(0, 1, 0) | |
var myQuaternion = Quat(vec, deg2rad(deg)) | |
var cell_item_orientation = Basis(myQuaternion).get_orthogonal_index() | |
return cell_item_orientation | |
func get_item_to_place(vec: Vector3) -> Array: | |
# print(relative_tiles) | |
var array = [] | |
var relative_tiles = PoolVector3Array([ | |
Vector3(vec.x, vec.y, vec.z+2), | |
Vector3(vec.x-2, vec.y, vec.z), | |
Vector3(vec.x, vec.y, vec.z-2), | |
Vector3(vec.x+2, vec.y, vec.z), | |
Vector3(vec.x+1, vec.y, vec.z), | |
Vector3(vec.x-1, vec.y, vec.z), | |
Vector3(vec.x, vec.y, vec.z+1), | |
Vector3(vec.x, vec.y, vec.z-1) | |
]) | |
for tile in relative_tiles: | |
array.append(get_cell_item(tile.x, tile.y, tile.z) == -1) | |
if Lib.all(array): | |
return [Roads.ROAD_EMPTY, 0] | |
for tile in relative_tiles: | |
var direction = tile - vec | |
# 4-way | |
if ((get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_END) or (get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_STARIGHT)) and direction.x: | |
for relative_tile_curve in relative_tiles: | |
var direction_curve = relative_tile_curve - vec | |
if get_cell_item(relative_tile_curve.x, relative_tile_curve.y, relative_tile_curve.z) == Roads.ROAD_3_WAY and direction_curve.z: | |
print("something") | |
set_cell_item(relative_tile_curve.x, relative_tile_curve.y, relative_tile_curve.z, Roads.ROAD_4_WAY) | |
return [Roads.ROAD_END, 90] | |
# 3-way | |
if ((get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_END) or (get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_STARIGHT)) and direction.x: | |
for relative_tile_curve in relative_tiles: | |
var direction_curve = relative_tile_curve - vec | |
if get_cell_item(relative_tile_curve.x, relative_tile_curve.y, relative_tile_curve.z) == Roads.ROAD_CURVE and direction_curve.z: | |
var degrees_to_rotate = 0 | |
if direction.x == 1 and direction.z == -1: | |
degrees_to_rotate = 360 | |
elif direction.x == 1 and direction.z == 1: | |
degrees_to_rotate = 180 | |
elif direction.x == -1 and direction.z == 1: | |
degrees_to_rotate = 180 | |
elif direction.x == -1 and direction.z == -1: | |
degrees_to_rotate = 360 | |
set_cell_item(relative_tile_curve.x, relative_tile_curve.y, relative_tile_curve.z, Roads.ROAD_3_WAY, get_orientation_from_degrees(null, degrees_to_rotate)) | |
return [Roads.ROAD_END, (270 if relative_tile_curve.z == 1 else 90)] | |
if get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_END and direction.z: | |
for relative_tile_curve in relative_tiles: | |
var direction_curve = relative_tile_curve - vec | |
if get_cell_item(relative_tile_curve.x, relative_tile_curve.y, relative_tile_curve.z) == Roads.ROAD_CURVE and direction_curve.x: | |
var degrees_to_rotate = 0 | |
if direction.x == 1 and direction.z == -1: | |
degrees_to_rotate = 270 | |
elif direction.x == 1 and direction.z == 1: | |
degrees_to_rotate = 90 | |
elif direction.x == -1 and direction.z == 1: | |
degrees_to_rotate = 90 | |
elif direction.x == -1 and direction.z == -1: | |
degrees_to_rotate = 270 | |
set_cell_item(relative_tile_curve.x, relative_tile_curve.y, relative_tile_curve.z, Roads.ROAD_3_WAY, get_orientation_from_degrees(null, degrees_to_rotate)) | |
return [Roads.ROAD_END, (360 if direction_curve.x == -1 else 180)] | |
# curved roads | |
if get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_STARIGHT and direction.x: | |
for tile_direction in relative_tiles: | |
var direction_in = tile_direction - vec | |
if get_cell_item(tile_direction.x, tile_direction.y, tile_direction.z) == Roads.ROAD_END and direction_in.z: | |
var degrees_to_rotate = 0 | |
if direction.x == 1 and direction.z == 1: | |
degrees_to_rotate = 90 | |
elif direction.x == 1 and direction.z == -1: | |
degrees_to_rotate = 0 | |
elif direction.x == -1 and direction.z == 1: | |
degrees_to_rotate = 180 | |
elif direction.x == -1 and direction.z == -1: | |
degrees_to_rotate = 270 | |
set_cell_item(tile_direction.x, tile_direction.y, tile_direction.z, Roads.ROAD_CURVE, get_orientation_from_degrees(null, degrees_to_rotate)) | |
return [Roads.ROAD_END, (90 if direction_in.z == 1 else 270)] | |
if get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_STARIGHT and direction.z: | |
for tile_direction in relative_tiles: | |
var direction_in = tile_direction - vec | |
if get_cell_item(tile_direction.x, tile_direction.y, tile_direction.z) == Roads.ROAD_END and direction_in.x: | |
var degrees_to_rotate = 0 | |
if direction.x == 1 and direction.z == 1: | |
degrees_to_rotate = 270 | |
elif direction.x == 1 and direction.z == -1: | |
degrees_to_rotate = 180 | |
elif direction.x == -1 and direction.z == 1: | |
degrees_to_rotate = 360 | |
elif direction.x == -1 and direction.z == -1: | |
degrees_to_rotate = 90 | |
set_cell_item(tile_direction.x, tile_direction.y, tile_direction.z, Roads.ROAD_CURVE, get_orientation_from_degrees(null, degrees_to_rotate)) | |
return [Roads.ROAD_END, (360 if direction_in.x == -1 else 180)] | |
# staright roads | |
if get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_EMPTY and direction.x: | |
set_cell_item(tile.x, tile.y, tile.z, Roads.ROAD_END, get_orientation_from_degrees(null, (180 if direction.x == -1 else 360))) | |
# print(direction) | |
return [Roads.ROAD_END, (180 if not direction.x == -1 else 360)] | |
if get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_END and direction.x: | |
# print(direction) | |
set_cell_item(tile.x, tile.y, tile.z, Roads.ROAD_STARIGHT, get_orientation_from_degrees(null, (180 if direction.x == -1 else 360))) | |
# print(direction) | |
return [Roads.ROAD_END, (180 if not direction.x == -1 else 360)] | |
if get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_EMPTY and direction.z: | |
set_cell_item(tile.x, tile.y, tile.z, Roads.ROAD_END, get_orientation_from_degrees(null, (90 if direction.z == -1 else 270))) | |
# print(direction) | |
return [Roads.ROAD_END, (90 if not direction.z == -1 else 270)] | |
if get_cell_item(tile.x, tile.y, tile.z) == Roads.ROAD_END and direction.z: | |
set_cell_item(tile.x, tile.y, tile.z, Roads.ROAD_STARIGHT, get_orientation_from_degrees(null, (90 if direction.z == -1 else 270))) | |
# print(direction) | |
return [Roads.ROAD_END, (90 if not direction.z == -1 else 270)] | |
# | |
# var direction = last_road - vec | |
# print("direction", direction) | |
# print(180 * direction.x) | |
# if get_cell_item(last_road.x, last_road.y, last_road.z) == 12: | |
# set_cell_item(last_road.x, last_road.y, last_road.z, 9, get_orientation_from_degrees(null, 0)) | |
# return [9, 180 * direction.x] | |
# if get_cell_item(last_road.x, last_road.y, last_road.z) == 9: | |
# set_cell_item(last_road.x, last_road.y, last_road.z, 15, get_orientation_from_degrees(null, 180 * direction.x)) | |
# return [9, 180 * direction.x] | |
return [15, 180] | |
# return [15, 90] | |
func _physics_process(_delta): | |
if (send_ray == true): | |
send_ray = false | |
var from = camera.project_ray_origin(event_position) | |
var to = from + camera.project_ray_normal(event_position) * RAY_LENGTH | |
var space_state = get_world().get_direct_space_state() | |
var result = space_state.intersect_ray(from, to, [self, camera]) | |
if (result != null and not result.empty()): | |
# print ("collided with: ", result["collider"].name, " at position: ", world_to_map(result["position"]), " in Terrain.") | |
var global_pos = result["position"] | |
var position1 = world_to_map(global_pos) | |
position1.y = 0 | |
print(global_pos, " before") | |
global_pos = result["position"] | |
print(global_pos, " after") | |
if get_cell_item(position1.x, position1.y,position1.z) == -1: | |
if $"../Trees".get_cell_item(global_pos.x, position1.y, global_pos.z) != -1: | |
$"../Trees".set_cell_item(global_pos.x, position1.y, global_pos.z, -1) | |
if $"../Rocks".get_cell_item(global_pos.x, position1.y, global_pos.z) != -1: | |
$"../Rocks".set_cell_item(global_pos.x, position1.y, global_pos.z, -1) | |
var item_to_place_with_orientation = get_item_to_place(position1) | |
set_cell_item(position1.x, position1.y, position1.z, item_to_place_with_orientation[0], get_orientation_from_degrees(null, item_to_place_with_orientation[1])) | |
else: | |
var item_to_remove = get_item_to_remove(position1) | |
set_cell_item(position1.x, position1.y, position1.z, item_to_remove) | |
func _input(event): | |
if (event is InputEventMouseButton): | |
if (event.pressed == true and event.button_index == 1): | |
send_ray = true | |
event_position = event.position | |
#func _ready(): | |
# pass | |
# | |
##func _input(event): | |
## if event is InputEventMouseButton and Input.is_action_pressed("left_click"): | |
## print(event.position) | |
## set_cell_item(event.position.x, 0.5, event.position.y, 15) | |
# | |
#const RAY_LENGTH = 1 | |
# | |
##func _input(event): | |
## if event is InputEventMouseButton and event.pressed and event.button_index == 1: | |
## var camera: Camera = $"../../Camera" | |
## var from = camera.project_ray_origin(event.position) | |
## var to = from + camera.project_ray_normal(event.position) * ray_length | |
## var result = $"../../Terrain".get_world().direct_space_state.intersect_ray(from, to, [self]) | |
### print(result) | |
## if typeof(result["collider"]) == typeof($"../../Terrain"): | |
## print(result) | |
## if result["position"]: | |
## set_cell_item(result["position"].x, 0.5, result["position"].z, 15) | |
## | |
## print(camera.project_ray_normal(event.position)*ray_length) | |
## set_cell_item(to.x, 0.5, to.z, 15) | |
# | |
# | |
#var i = 0 | |
# | |
#func _physics_process(delta): | |
# if Input.is_action_pressed("left_click"): | |
## print(world_to_map(get_viewport().get_mouse_position())) | |
# var from = $'../../Camera'.project_ray_origin(get_viewport().get_mouse_position()) | |
# var to = from + $"../../Camera".project_ray_normal(get_viewport().get_mouse_position()) * RAY_LENGTH | |
# var space_state = get_world().get_direct_space_state() | |
# var result = space_state.intersect_ray(from, to) | |
# print(result) | |
## if typeof(result["collider"]) == typeof($"../../Terrain"): | |
## print(result["position"]*100) | |
## if result["position"]: | |
## print("test") | |
## print(i) | |
### if get_cell_item(result["position"].x*10) | |
## set_cell_item(result["position"].x*1000, 0.5, result["position"].z, 15) | |
## i += 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment