2d战棋寻路

This commit is contained in:
TsubakiLoL 2024-11-26 21:10:14 +08:00
parent 09de53f523
commit d2ff7beb2d
19 changed files with 477 additions and 1 deletions

View File

@ -1021,7 +1021,7 @@ func explore_round(character_data:Dictionary,round_dic:Dictionary,choice:String)
return false return false
#探索鉴定中判定是否结束的函数 #探索鉴定中判定是否结束的函数
func explore_is_finish(character_data:Dictionary,round_dic:Dictionary): func explore_is_finish(character_data:Dictionary,round_dic:Dictionary):
if round_dic["right_cost"]==0 ||round_dic.has("is_finish") && round_dic["is_finish"]: if round_dic["right_cost"]==0 ||round_dic.has("is_finish") && round_dic["is_finish"]:
return true return true
else: else:
return false return false

0
export_presets.cfg Normal file
View File

View File

@ -58,8 +58,32 @@ mouse_right={
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":2,"position":Vector2(242, 15),"global_position":Vector2(251, 61),"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null) "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":2,"position":Vector2(242, 15),"global_position":Vector2(251, 61),"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null)
] ]
} }
w={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"location":0,"echo":false,"script":null)
]
}
s={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null)
]
}
a={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null)
]
}
d={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null)
]
}
[layer_names] [layer_names]
2d_physics/layer_1="队伍0" 2d_physics/layer_1="队伍0"
2d_physics/layer_2="队伍1" 2d_physics/layer_2="队伍1"
[rendering]
textures/vram_compression/import_etc2_astc=true

Binary file not shown.

After

Width:  |  Height:  |  Size: 221 B

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://wld1fbwto47r"
path="res://.godot/imported/black.png-2ea109517b87d40b76ece4571bbc1773.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://res/ui/ui_033_2d_fight/icon_color_64x64/black.png"
dest_files=["res://.godot/imported/black.png-2ea109517b87d40b76ece4571bbc1773.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 B

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://chl3m4p51lsop"
path="res://.godot/imported/blue.png-f1d798c1750095af5cb97caa45e246b9.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://res/ui/ui_033_2d_fight/icon_color_64x64/blue.png"
dest_files=["res://.godot/imported/blue.png-f1d798c1750095af5cb97caa45e246b9.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 B

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://t2v5bekhf6gw"
path="res://.godot/imported/green.png-4b663a6369183fd8a83d12a35152d1aa.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://res/ui/ui_033_2d_fight/icon_color_64x64/green.png"
dest_files=["res://.godot/imported/green.png-4b663a6369183fd8a83d12a35152d1aa.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 B

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://dkecwuix7ykv4"
path="res://.godot/imported/red.png-06affee2752e276f5718457858c8774f.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://res/ui/ui_033_2d_fight/icon_color_64x64/red.png"
dest_files=["res://.godot/imported/red.png-06affee2752e276f5718457858c8774f.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

Binary file not shown.

After

Width:  |  Height:  |  Size: 221 B

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://bi25ys30tbg5s"
path="res://.godot/imported/white.png-5098444c65e8f586c54e185f6781d969.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://res/ui/ui_033_2d_fight/icon_color_64x64/white.png"
dest_files=["res://.godot/imported/white.png-5098444c65e8f586c54e185f6781d969.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 B

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://cq0wc2267qtc4"
path="res://.godot/imported/yellow.png-5768b9102bd9b8a848ab80965160346e.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://res/ui/ui_033_2d_fight/icon_color_64x64/yellow.png"
dest_files=["res://.godot/imported/yellow.png-5768b9102bd9b8a848ab80965160346e.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

142
scene/_2d_fight/2d_fight.gd Normal file
View File

@ -0,0 +1,142 @@
extends Node2D
#网格寻路系统
var astar:AStarGrid2D=AStarGrid2D.new()
##当前使用的tilemap的范围开始
@export var tile_map_area_from:Vector2i
##当前使用的tilemap的范围结束
@export var tile_map_area_to:Vector2i
##color block的颜色指示
enum ColorBlockSetColor{
BLACK=0,
BLUE=1,
GREEN=2,
RED=3,
WHITE=4,
YELLOW=5
}
#镜头部分
#镜头速度
var camera_speed:float=1000
#镜头模式
enum CameraMode{
#wsad自由移动
FREE=0,
#镜头放大,并且进行一个平滑位移
FOCUS=1
}
##相机模式
@export var camera_mode:CameraMode=CameraMode.FREE
##相机基础缩放乘数
@export var camera_base_zoom_scale:Vector2
##当前聚焦的相机对象
@export var camera_focusmode_focus_target:Node2D:
set(val):
if camera_mode==CameraMode.FOCUS and val!=camera_focusmode_focus_target:
if camera_focus_mode_focus_tween!=null:
camera_focus_mode_focus_tween.kill()
camera_focus_mode_focus_tween=create_tween()
camera_focus_mode_focus_tween.tween_property(%main_camera,"globla_position",val.global_position,camera_focus_mode_focus_time)
camera_focus_mode_focus_tween.set_parallel()
camera_focus_mode_focus_tween.tween_property(%main_camera,"zoom",camera_base_zoom_scale*camera_focus_mode_focus_scale,camera_focus_mode_focus_time)
camera_focusmode_focus_target=val
pass
#聚焦模式下相机的tween补间动画
var camera_focus_mode_focus_tween
#相机聚焦所用时间
var camera_focus_mode_focus_time:float=0.2
#相机聚焦倍率
var camera_focus_mode_focus_scale:float=1.5
#测试部分
#测试的角色移动力
var test_character_move_distance:int=5
func update_moveable():
for i in get_children():
if i is Label:
i.queue_free()
%color_block.clear()
var character_crood:Vector2i=%walkable.local_to_map(%test_character.global_position)
for i in range(tile_map_area_from.x,tile_map_area_to.x):
for j in range(tile_map_area_from.y,tile_map_area_to.y):
var crood=Vector2i(i,j)
var res=is_move_able(character_crood,crood,test_character_move_distance)
if res[0]:
%color_block.set_cell(crood,ColorBlockSetColor.GREEN,Vector2i(0,0))
else:
%color_block.set_cell(crood,ColorBlockSetColor.RED,Vector2i(0,0))
var new_label=Label.new()
new_label.text=str(res[1])
new_label.global_position=%walkable.map_to_local(crood)
add_child(new_label)
#区域是否可达
func is_move_able(from:Vector2i,to:Vector2i,move_distance:int):
var res=get_walkable_path(from,to)
if not res[0]:
return [false,0,null]
var distance=res[1]
if move_distance>=distance:
return [true,res[1],res[2]]
else:
return [false,0,null]
func _ready() -> void:
astar.default_compute_heuristic=AStarGrid2D.HEURISTIC_MANHATTAN
astar.default_estimate_heuristic=AStarGrid2D.HEURISTIC_MANHATTAN
astar.diagonal_mode=AStarGrid2D.DIAGONAL_MODE_NEVER
astar.region=Rect2i(tile_map_area_from,tile_map_area_to-tile_map_area_from)
astar.update()
update_astar_from_walkable_map()
update_moveable()
#从可行走tilemap图中同步到astar寻路中
func update_astar_from_walkable_map():
for i in range(tile_map_area_from.x,tile_map_area_to.x):
for j in range(tile_map_area_from.y,tile_map_area_to.y):
var crood=Vector2i(i,j)
var tile_data=%walkable.get_cell_tile_data(crood)
if tile_data !=null and tile_data.get_custom_data("walkable"):
astar.set_point_solid(crood,false)
else:
astar.set_point_solid(crood,true)
astar.update()
#获取两点之间的最短可达路径返回一个数组0下标未是否可达1下标为距离2下标为途径点数组
func get_walkable_path(from:Vector2i,to:Vector2i)->Array:
#如果不在要求的tilemap范围内则返回不可达
if to.x<tile_map_area_from.x or to.x>tile_map_area_to.x or to.y<tile_map_area_from.y or to.y>tile_map_area_to.y:
return [false,0,null]
var arr=astar.get_id_path(from,to)
if arr==null or (arr is Array and arr.size()<=0):
return [false,0,null]
return [true,arr.size()-1,arr]
pass
func _process(delta: float) -> void:
match camera_mode:
CameraMode.FREE:
var input=Input.get_vector("a","d","w","s").normalized()
%main_camera.position+=input*delta*camera_speed
CameraMode.FOCUS:
pass
if Input.is_action_just_pressed("mouse_left"):
print("click")
var from_crood=%walkable.local_to_map(%test_character.global_position)
var crood=%walkable.local_to_map(get_global_mouse_position())
var res=is_move_able(from_crood,crood,test_character_move_distance)
print(res)
if res[0]:
%test_character.global_position=%walkable.map_to_local(crood)
update_moveable()

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,4 @@
[gd_scene format=3 uid="uid://nydfou5oj7ue"]
[node name="TileMap" type="TileMap"]
format = 2

9
scene/new_fight.tscn Normal file
View File

@ -0,0 +1,9 @@
[gd_scene format=3 uid="uid://bcpmvsblobwhr"]
[node name="Control" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2