challenge-editor/scene/map.gd

179 lines
5.5 KiB
GDScript

extends Control
@onready var date: Label = $TextureRect/date
@onready var time: Label = $TextureRect/time
@onready var weather: Label = $TextureRect/weather
@onready var pos: Label = $TextureRect/pos
@onready var h_box_container: HBoxContainer = $ScrollContainer/HBoxContainer
@onready var scroll_container: ScrollContainer = $ScrollContainer
@onready var map_0
@onready var map_1
@onready var map_2
@onready var map_now
@onready var map_add_pos: MarginContainer = $ScrollContainer/HBoxContainer/map_add_pos
@onready var pointer: Label = $ScrollContainer/HBoxContainer/map_add_pos/pointer_map/pointer
var map_tween:Array=[null,null]
func recreate_map_tween():
for i in map_tween.size():
if map_tween[i]==null:
map_tween[i]=get_tree().create_tween()
else:
map_tween[i].stop()
map_tween[i].kill()
map_tween[i]=get_tree().create_tween()
func show_map_animation(map_from,map_next):
recreate_map_tween()
map_from.modulate.a=1
map_next.modulate.a=1
var map=[map_0,map_1,map_2]
for i in map:
if not i in [map_from,map_next]:
i.hide()
i.modulate.a=1
else:
i.show()
map_tween[0].tween_property(map_from,"modulate:a",0,0.1)
map_tween[0].tween_callback(map_from.hide)
map_tween[1].tween_property(map_next,"modulate:a",1,0.1)
#map_from.hide()
#map_next.show()
pass
signal scene_change(scene_id:String)
var scale_rag:float=1:
set(val):
if val>scale_index_1 and val<scale_index_2 and (scale_rag<scale_index_1 or scale_rag>scale_index_2):
show_map_animation(map_now,map_1)
map_now=map_1
elif val<=scale_index_1 and scale_rag>scale_index_1:
show_map_animation(map_now,map_0)
map_now=map_0
elif val>scale_index_2 and scale_rag<scale_index_2:
show_map_animation(map_now,map_2)
map_now=map_2
resize_map(scale_rag,val,get_global_mouse_position(),init_size)
scale_rag=val
pass
var scale_limit_min:float=1
var scale_limit_max:float=2
var scale_index_1:float=1.2
var scale_index_2:float=1.5
var map_data:Dictionary
func set_map(id:String):
set_process(true)
scale_rag=1
map_data=Database.get_map_data(id)
var map_index:Array=map_data["index"]
for i in map_add_pos.get_children():
if i is MapIndex:
i.queue_free()
var tscn=[load(map_index[0]).instantiate(),load(map_index[1]).instantiate(),load(map_index[2]).instantiate()]
for i in tscn:
map_add_pos.add_child(i)
i.click.connect(click)
map_0=tscn[0]
map_1=tscn[1]
map_2=tscn[2]
map_1.hide()
map_2.hide()
map_0.show()
map_now=map_0
update_pointer()
var scale_index:Array=map_data["scale_index"]
var scale_limit:Array=map_data["scale_limit"]
scale_limit_min=scale_limit[0]
scale_limit_max=scale_limit[1]
scale_index_1=scale_index[0]
scale_index_2=scale_index[1]
#var _init_size=map_data["init_size"]
#init_size=Vector2(_init_size[0],_init_size[1])
pass
func update_time():
var time_dic:Dictionary=Global.get_time_dictionary()
date.text=str(time_dic["year"])+"/"+str(time_dic["month"])+"/"+str(time_dic["day"])
time.text=str(time_dic["hour"])+":"+"%02d"%[time_dic["minute"]]
func resize_map(before_rag:float,rag:float,mouse_pos:Vector2,init:Vector2):
var l=rag/before_rag
var now_pos=Vector2(scroll_container.scroll_horizontal,scroll_container.scroll_vertical)+mouse_pos
now_pos*=l
var result=now_pos-mouse_pos
h_box_container.custom_minimum_size=init*rag
scroll_container.set_deferred("scroll_horizontal", result.x)
scroll_container.set_deferred("scroll_vertical", result.y)
func update_pointer():
var now_scene_id=Global.get_now_scene()
var pos=map_now.get_scene_global_pos(now_scene_id)-Vector2(pointer.size.x/2,pointer.size.y)
pointer.global_position=pos
func click(scene_id:String):
if scene_id==Global.get_now_scene():
return
scene_change.emit(scene_id)
var is_success:bool=Global.move_scene(scene_id,map_now.get_scene_distance(Global.get_now_scene(),scene_id))
if is_success:
self.hide()
self.set_process(false)
@onready var init_size:Vector2=get_viewport().size
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
set_map("map_01")
update_time()
get_window().size_changed.connect(resize_map.bindv([scale_rag,scale_rag,get_global_mouse_position(),init_size]))
Global.time_changed.connect(update_time)
h_box_container.custom_minimum_size=init_size
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
init_size=get_viewport().size
update_pointer()
pass
var keep_tween:Tween
func _gui_input(event: InputEvent) -> void:
if event is InputEventMouseMotion and Input.is_action_pressed("mouse_left"):
var relative:Vector2=event.relative
scroll_container.scroll_horizontal-=relative.x
scroll_container.scroll_vertical-=relative.y
if Input.is_action_pressed("mouse_up"):
if keep_tween==null:
keep_tween=get_tree().create_tween()
else:
keep_tween.stop()
keep_tween.kill()
keep_tween=get_tree().create_tween()
keep_tween.tween_property(self,"scale_rag",clamp(scale_rag+0.2,scale_limit_min,scale_limit_max),0.1)
if Input.is_action_just_pressed("mouse_down"):
if keep_tween==null:
keep_tween=get_tree().create_tween()
else:
keep_tween.stop()
keep_tween.kill()
keep_tween=get_tree().create_tween()
keep_tween.tween_property(self,"scale_rag",clamp(scale_rag-0.2,scale_limit_min,scale_limit_max),0.1)
func _on_back_pressed() -> void:
set_process(false)
self.hide()
pass # Replace with function body.
func _on_visibility_changed() -> void:
#当地图开始显示的时候
if visible:
var tween=create_tween()
tween.tween_property(self,"scale_rag",2,0.1)
pass