185 lines
5.6 KiB
GDScript
185 lines
5.6 KiB
GDScript
extends Node2D
|
|
class_name MainScene
|
|
@export var player: SelfUnit
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
%mouse_finder.position=get_global_mouse_position()
|
|
|
|
var now_map:map
|
|
var now_e_interacetion_item
|
|
func _on_control_gui_input(event: InputEvent) -> void:
|
|
if event.is_action_pressed("mouse_right"):
|
|
if player:
|
|
player.sent_message("move",player.get_global_mouse_position())
|
|
#if event is InputEventMouseMotion:
|
|
#var item=%mouse_finder.get_overlapping_areas()
|
|
#if item.size()!=0:
|
|
#var target=get_closest_node(player,item,EInteractionItem)
|
|
#if target is EInteractionItem:
|
|
#if now_e_interacetion_item!=target:
|
|
#if is_instance_valid(now_e_interacetion_item):
|
|
#now_e_interacetion_item._exit_highlight()
|
|
#if is_instance_valid(target):
|
|
#target._on_highlight()
|
|
#now_e_interacetion_item=target
|
|
#else:
|
|
#if now_e_interacetion_item!=null and is_instance_valid(now_e_interacetion_item):
|
|
#now_e_interacetion_item._exit_highlight()
|
|
#now_e_interacetion_item=null
|
|
if event.is_action_pressed("mouse_left"):
|
|
#投放单位,用于测试
|
|
if %put_check.button_pressed:
|
|
var id=%LineEdit.text
|
|
var type=selected_item
|
|
var data=Database.get_unit_data(type,id)
|
|
var new_cb=load("res://scene/test/other_character.tscn").instantiate()
|
|
new_cb.unit_data=Database.get_unit_data(type,id)
|
|
new_cb.global_position=get_global_mouse_position()
|
|
%CB_add_pos.add_child(new_cb)
|
|
new_cb.second_timer_time_out()
|
|
return
|
|
|
|
if now_e_interacetion_item!=null:
|
|
if player!=null and is_instance_valid(player) and player.is_unit_instance_in_touch_area(now_e_interacetion_item):
|
|
now_e_interacetion_item.E_pressed()
|
|
return
|
|
var arr=%mouse_finder.get_overlapping_bodies()
|
|
print(arr)
|
|
if arr.size()==0:
|
|
return
|
|
var target=get_closest_node(player,arr,Unit)
|
|
print(target)
|
|
if target!=null:
|
|
|
|
player.sent_message("attack",target)
|
|
pass
|
|
|
|
pass # Replace with function body.
|
|
#func _input(event: InputEvent) -> void:
|
|
#print(event)
|
|
#
|
|
#if event.is_action_pressed("e") and player!=null and player.is_door_availible() and $CanvasLayer/Control.has_focus():
|
|
#print("检测到切换请求")
|
|
#var e=player.get_e_interaction_item()
|
|
#if e is EInteractionItem:
|
|
#e.E_pressed()
|
|
|
|
func get_closest_node(self_node:Node2D,array:Array,target_class):
|
|
if array.size()==0:
|
|
return null
|
|
var length=null
|
|
var node=null
|
|
for i in range(0,array.size()):
|
|
print(is_instance_of(i,target_class))
|
|
if is_instance_of(array[i],target_class) and array[i]!=self_node:
|
|
var l=(array[i].global_position-self_node.global_position).length()
|
|
if length==null:
|
|
length=l
|
|
node=array[i]
|
|
elif l<length:
|
|
length=l
|
|
node=array[i]
|
|
return node
|
|
|
|
func _ready() -> void:
|
|
#同步主场景
|
|
Global.main_scene=self
|
|
var all_types=Database.get_all_unit_type()
|
|
if all_types.size()>0:
|
|
selected_item=all_types[0]
|
|
%type.get_popup().clear()
|
|
for i in all_types:
|
|
%type.get_popup().add_item(i)
|
|
|
|
%type.get_popup().index_pressed.connect(pop_up_selected)
|
|
change_map("map_0")
|
|
var selected_item:String:
|
|
set(val):
|
|
selected_item=val
|
|
%type.text=val
|
|
func pop_up_selected(index:int):
|
|
selected_item=%type.get_popup().get_item_text(index)
|
|
|
|
|
|
func _on_cacul_num_number_timeout() -> void:
|
|
%num.text="场上单位数量:"+str(Global.unit_instance_dic.keys().size())
|
|
pass # Replace with function body.
|
|
|
|
func change_map(map_id:String,ind:int=0):
|
|
print("map_changed:"+str(map_id)+",map_ind:"+str(ind))
|
|
if now_map!=null:
|
|
Global.map_dictionary.erase(now_map.map_id)
|
|
now_map.queue_free()
|
|
for i in %CB_add_pos.get_children():
|
|
if i is UnitOther:
|
|
Global.delete_unit_instance(i.get_unit_id())
|
|
Global.add_unit_id_save_map(i.unit_data)
|
|
|
|
i.queue_free()
|
|
|
|
var new_map_tscn=Database.get_map_data(map_id)
|
|
if new_map_tscn==null:
|
|
return
|
|
|
|
var new_map=load(new_map_tscn).instantiate() as map
|
|
new_map.map_id=map_id
|
|
add_child(new_map)
|
|
var new_player= preload("res://scene/test/character.tscn").instantiate()
|
|
if player!=null:
|
|
player.queue_free()
|
|
player=new_player
|
|
new_player.global_position=new_map.get_mark_postion(ind)
|
|
%CB_add_pos.add_child(new_player)
|
|
new_map.z_index-=1
|
|
now_map=new_map
|
|
if not Global.is_map_initlized(map_id):
|
|
var characte_arr=new_map.pre_character_data
|
|
for i in characte_arr:
|
|
var new_unit=preload("res://scene/test/other_character.tscn").instantiate()
|
|
new_unit.unit_data=Database.get_unit_data(i["unit_data_from_id"],i["unit_id"])
|
|
new_unit.unit_data["type"]=i["type"]
|
|
new_unit.global_position=i["position"]
|
|
%CB_add_pos.add_child(new_unit)
|
|
new_unit.second_timer_time_out()
|
|
else:
|
|
var map_character_data=Global.get_map_data(map_id)
|
|
for i in map_character_data.values():
|
|
var new_unit=preload("res://scene/test/other_character.tscn").instantiate()
|
|
new_unit.unit_data=i
|
|
new_unit.global_position=Vector2(i["position"][0],i["position"][1])
|
|
%CB_add_pos.add_child(new_unit)
|
|
pass
|
|
pass
|
|
|
|
|
|
func _on_change_scene_0_pressed() -> void:
|
|
change_map("map_0")
|
|
pass # Replace with function body.
|
|
|
|
|
|
func _on_change_scene_1_pressed() -> void:
|
|
change_map("map_1")
|
|
pass # Replace with function body.
|
|
|
|
|
|
func _on_mouse_finder_area_entered(area: Area2D) -> void:
|
|
if area is EInteractionItem:
|
|
if now_e_interacetion_item!=null and is_instance_valid(now_e_interacetion_item):
|
|
now_e_interacetion_item._exit_highlight()
|
|
now_e_interacetion_item=area
|
|
area._on_highlight()
|
|
pass # Replace with function body.
|
|
|
|
|
|
func _on_mouse_finder_area_exited(area: Area2D) -> void:
|
|
if area==now_e_interacetion_item:
|
|
now_e_interacetion_item._exit_highlight()
|
|
now_e_interacetion_item=null
|
|
pass # Replace with function body.
|
|
|
|
func show_trader_board(item_array:Array[BagItem],bind:Unit=null):
|
|
%trader_board.open(item_array,bind)
|