This repository has been archived on 2020-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
odyssey-old/Scenes/UI/SpaceMapInside.gd

158 lines
5.6 KiB
GDScript3
Raw Permalink Normal View History

2020-07-10 00:09:54 +00:00
extends Control
2020-07-24 11:49:22 +00:00
export var cell_size = 150
2020-07-10 00:09:54 +00:00
export var bgzoom = 50
const BORDER_WIDTH = 4
const BORDER_LENGTH = 30
const RADAR_EFFECT_DELAY = 1
2020-07-22 15:34:04 +00:00
const HALF_BORDER = BORDER_LENGTH/2
2020-07-24 11:49:22 +00:00
const MIN_ZOOM = 30
const ZOOM_STEP = 10
const MAX_ZOOM = 400
2020-07-10 00:09:54 +00:00
export(Texture) var background
export(Font) var font
var dragging = false
var origin = Vector2.ZERO
2020-07-10 00:09:54 +00:00
var last_origin = origin
var last_mouse_pos = Vector2.ZERO
var set_position = true
var radar_next_remaining = RADAR_EFFECT_DELAY
2020-07-24 11:49:22 +00:00
var offset = Vector2.ZERO
var text_two_lines = false
onready var scene = $"/root/scene"
onready var speed_text = $"../HBoxContainer/HBoxContainer/CurrentSpeed"
onready var dir_text = $"../HBoxContainer/HBoxContainer/CurrentAngle"
2020-07-28 07:54:04 +00:00
onready var popup = $"../../.."
var last_pos = []
func _ready():
font = font.duplicate()
func _physics_process(delta):
radar_next_remaining -= delta
if radar_next_remaining < 0:
2020-07-24 11:49:22 +00:00
var current_position = (scene.world.map.current_ship_position + scene.world.map.current_ship_subpos)
last_pos.append(current_position)
if last_pos.size() > 20:
last_pos.pop_front()
radar_next_remaining = RADAR_EFFECT_DELAY
if not visible:
return
speed_text.text = "%.0f u/s" % (scene.world.map.current_ship_speed * 10000)
dir_text.text = "%.0f deg" % rad2deg(scene.world.map.current_ship_direction)
update()
2020-07-10 00:09:54 +00:00
func _draw():
var win_size = get_global_rect().size
2020-07-24 11:49:22 +00:00
var current_position = (scene.world.map.current_ship_position + scene.world.map.current_ship_subpos) * cell_size
if set_position:
2020-07-24 11:49:22 +00:00
origin = current_position - win_size/2
last_origin = origin
if win_size.x > 0:
set_position = false
2020-07-10 00:09:54 +00:00
var cols = int(ceil(win_size.x/cell_size))
var rows = int(ceil(win_size.y/cell_size))
var xoffset = float(int(origin.x) % cell_size)
var yoffset = float(int(origin.y) % cell_size)
draw_texture_rect_region(background, Rect2(Vector2.ZERO, win_size), Rect2(origin*bgzoom, win_size*bgzoom), Color(0.5,0.5,0.5,1))
2020-07-10 00:09:54 +00:00
for i in range(0, cols+1):
draw_line(Vector2(i * cell_size-xoffset, 0), Vector2(i * cell_size-xoffset, win_size.y), Color.white * 0.5)
2020-07-24 11:49:22 +00:00
for i in range(0, rows+1):
draw_line(Vector2(0, i * cell_size-yoffset), Vector2(win_size.x, i * cell_size-yoffset), Color.white * 0.5)
2020-07-10 00:09:54 +00:00
for x in range(-1, cols+1):
for y in range(-1, rows+1):
var real_x = x + int(origin.x/cell_size)
var real_y = y + int(origin.y/cell_size)
var sector = Coordinates.as_string_parts(Vector2(real_x, real_y))
if text_two_lines:
draw_string(font, Vector2(x * cell_size + 6 - xoffset, y * cell_size + 20 - yoffset), sector[0], Color(1,1,1,0.5))
draw_string(font, Vector2(x * cell_size + 6 - xoffset, y * cell_size + 20 - yoffset + font.size + 2), sector[1], Color(1,1,1,0.5))
else:
draw_string(font, Vector2(x * cell_size + 6 - xoffset, y * cell_size + 20 - yoffset), sector[0] + sector[1], Color(1,1,1,0.5))
var viewport = Rect2(origin, win_size)
var point_count = last_pos.size()
for pos_index in range(0, point_count):
2020-07-24 11:49:22 +00:00
draw_circle(last_pos[pos_index] * cell_size - origin, 2, Color(1, 0, 0, pos_index*1.0/point_count))
draw_target(viewport, current_position, Color.red)
2020-07-16 12:37:21 +00:00
# Debug directions:
#draw_line(current_position - viewport.position, current_position - viewport.position + Vector2.RIGHT.rotated(scene.world.map.ship_direction) * 100, Color.green)
#draw_line(current_position - viewport.position, current_position - viewport.position + Vector2.RIGHT.rotated(scene.world.map.current_ship_direction) * 100, Color.yellow)
var current_target = scene.world.map.current_ship_target
if current_target != null:
2020-07-24 11:49:22 +00:00
var current_target_adj = current_target * cell_size
draw_target(viewport, current_target_adj, Color.green)
draw_line(current_position - origin, current_target_adj - origin, Color.darkcyan)
func draw_target(viewport: Rect2, position: Vector2, color: Color):
# Check if target is in range
if viewport.has_point(position):
draw_circle(position - viewport.position, 5, color)
else:
# Draw line pointing to target
var relative_pos = position - viewport.position
var clamped = Vector2(clamp(relative_pos.x, 0, viewport.size.x), clamp(relative_pos.y, 0, viewport.size.y))
if relative_pos.x < 0:
2020-07-22 15:34:04 +00:00
draw_line(Vector2(0, clamped.y-HALF_BORDER), Vector2(0, clamped.y+HALF_BORDER), color, BORDER_WIDTH)
elif relative_pos.x > viewport.size.x:
2020-07-22 15:34:04 +00:00
draw_line(Vector2(viewport.size.x, clamped.y-HALF_BORDER), Vector2(viewport.size.x, clamped.y+HALF_BORDER), color, BORDER_WIDTH)
if relative_pos.y < 0:
2020-07-22 15:34:04 +00:00
draw_line(Vector2(clamped.x-HALF_BORDER, 0), Vector2(clamped.x+HALF_BORDER, 0), color, BORDER_WIDTH)
elif relative_pos.y > viewport.size.y:
2020-07-22 15:34:04 +00:00
draw_line(Vector2(clamped.x-HALF_BORDER, viewport.size.y), Vector2(clamped.x+HALF_BORDER, viewport.size.y), color, BORDER_WIDTH)
2020-07-10 00:09:54 +00:00
func _input(event):
2020-07-24 11:49:22 +00:00
if not visible:
return
if event.is_action_pressed("ui_zoomin"):
if cell_size < MAX_ZOOM:
_recalc_offset(ZOOM_STEP)
cell_size += ZOOM_STEP
update_font()
2020-07-24 11:49:22 +00:00
update()
return
if event.is_action_pressed("ui_zoomout"):
if cell_size > MIN_ZOOM:
_recalc_offset(-ZOOM_STEP)
cell_size -= ZOOM_STEP
update_font()
2020-07-24 11:49:22 +00:00
update()
return
2020-07-10 00:09:54 +00:00
if event is InputEventMouseButton:
dragging = event.pressed
last_origin = origin
last_mouse_pos = event.position
update()
2020-07-24 11:49:22 +00:00
return
if event is InputEventMouseMotion:
2020-07-10 00:09:54 +00:00
if dragging:
origin = last_origin - (event.position - last_mouse_pos)
2020-07-10 00:09:54 +00:00
update()
2020-07-24 11:49:22 +00:00
return
func update_font():
text_two_lines = cell_size < 70
if cell_size < 100:
font.size = 11
else:
font.size = 14
2020-07-24 11:49:22 +00:00
func _recalc_offset(mult: float):
var mouse = get_local_mouse_position()
origin = ((origin + mouse) / cell_size * (cell_size + mult)) - mouse
2020-07-10 00:09:54 +00:00
func _reset_position():
set_position = true
2020-07-28 07:54:04 +00:00
func _on_hide():
popup.visible = false