otherworldly_simulation/scene/test/test_main.gd

185 lines
5.6 KiB
GDScript3
Raw Permalink Normal View History

2024-10-25 15:41:39 +08:00
extends Node2D
2024-11-01 23:23:07 +08:00
class_name MainScene
2024-10-30 19:54:45 +08:00
@export var player: SelfUnit
2024-10-25 15:41:39 +08:00
2024-11-01 23:23:07 +08:00
2024-10-25 15:41:39 +08:00
func _physics_process(delta: float) -> void:
%mouse_finder.position=get_global_mouse_position()
2024-10-28 19:24:10 +08:00
var now_map:map
2024-11-01 23:23:07 +08:00
var now_e_interacetion_item
2024-10-25 15:41:39 +08:00
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())
2024-11-01 23:23:07 +08:00
#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
2024-10-25 17:45:08 +08:00
if event.is_action_pressed("mouse_left"):
2024-11-01 23:23:07 +08:00
#投放单位,用于测试
2024-10-28 19:24:10 +08:00
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
2024-10-30 19:54:45 +08:00
2024-11-01 23:23:07 +08:00
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
2024-10-25 17:45:08 +08:00
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)
2024-10-25 15:41:39 +08:00
pass
2024-10-30 19:54:45 +08:00
2024-10-25 15:41:39 +08:00
pass # Replace with function body.
2024-11-01 23:23:07 +08:00
#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()
2024-10-25 17:45:08 +08:00
func get_closest_node(self_node:Node2D,array:Array,target_class):
if array.size()==0:
return null
2024-11-01 23:23:07 +08:00
var length=null
2024-10-25 17:45:08 +08:00
var node=null
for i in range(0,array.size()):
print(is_instance_of(i,target_class))
2024-10-26 22:17:29 +08:00
if is_instance_of(array[i],target_class) and array[i]!=self_node:
2024-10-25 17:45:08 +08:00
var l=(array[i].global_position-self_node.global_position).length()
2024-11-01 23:23:07 +08:00
if length==null:
length=l
node=array[i]
elif l<length:
2024-10-25 17:45:08 +08:00
length=l
node=array[i]
return node
2024-10-28 19:24:10 +08:00
func _ready() -> void:
2024-11-01 23:23:07 +08:00
#同步主场景
Global.main_scene=self
2024-10-28 19:24:10 +08:00
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.
2024-10-30 19:54:45 +08:00
func change_map(map_id:String,ind:int=0):
print("map_changed:"+str(map_id)+",map_ind:"+str(ind))
2024-10-28 19:24:10 +08:00
if now_map!=null:
2024-10-30 19:54:45 +08:00
Global.map_dictionary.erase(now_map.map_id)
2024-10-28 19:24:10 +08:00
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()
2024-10-30 19:54:45 +08:00
2024-10-28 19:24:10 +08:00
var new_map_tscn=Database.get_map_data(map_id)
if new_map_tscn==null:
return
2024-11-01 23:23:07 +08:00
2024-10-28 19:24:10 +08:00
var new_map=load(new_map_tscn).instantiate() as map
new_map.map_id=map_id
add_child(new_map)
2024-10-30 19:54:45 +08:00
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)
2024-10-28 19:24:10 +08:00
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.
2024-11-01 23:23:07 +08:00
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)