otherworldly_simulation/scene/test/test_main.gd
2024-10-30 19:54:45 +08:00

134 lines
3.9 KiB
GDScript

extends Node2D
@export var player: SelfUnit
func _physics_process(delta: float) -> void:
%mouse_finder.position=get_global_mouse_position()
var now_map:map
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_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
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():
print("检测到切换请求")
var door:Door =player.get_door()
change_map(door.map_id)
func get_closest_node(self_node:Node2D,array:Array,target_class):
if array.size()==0:
return null
var length=999999999
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 l<length:
length=l
node=array[i]
return node
func _ready() -> void:
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.