1 Star 0 Fork 0

kevinlights/rimworldripoff

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Pathfinding.gd 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
@tool
extends Node2D
@onready var terrain = $"../Terrain"
var astar_grid = AStarGrid2D.new()
@export var start : Vector2i
@export var end : Vector2i
@export var calculate : bool
var path = []
# Called when the node enters the scene tree for the first time.
func _ready():
InitPathfinding()
pass
func _draw():
print("redrawing")
if len(path) > 0:
for i in range(len(path) - 1):
draw_line(path[i], path[i+1], Color.PURPLE)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if calculate:
calculate = false
InitPathfinding()
RequestPath(start, end)
func RequestPath(start: Vector2i, end: Vector2i):
path = astar_grid.get_point_path(start, end)
for i in range(len(path)):
path[i] += Vector2(terrain.rendering_quadrant_size/2, terrain.rendering_quadrant_size/2)
queue_redraw()
return path
func InitPathfinding():
astar_grid.region = Rect2i(0, 0, terrain.mapWidth, terrain.mapHeight)
astar_grid.cell_size = Vector2(16, 16)
astar_grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES
astar_grid.update()
for x in range(terrain.mapWidth):
for y in range(terrain.mapHeight):
if GetTerrainDifficulty(Vector2i(x,y)) == -1:
astar_grid.set_point_solid(Vector2i(x,y))
else:
astar_grid.set_point_weight_scale(Vector2i(x,y), GetTerrainDifficulty(Vector2i(x, y)))
func GetTerrainDifficulty(coords : Vector2i):
var layer = 0
var source_id = terrain.get_cell_source_id(layer, coords, false)
var source: TileSetAtlasSource = terrain.tile_set.get_source(source_id)
var atlas_coords = terrain.get_cell_atlas_coords(layer, coords, false)
var tile_data = source.get_tile_data(atlas_coords, 0)
return tile_data.get_custom_data("walk_difficulty")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kevinlights/rimworldripoff.git
git@gitee.com:kevinlights/rimworldripoff.git
kevinlights
rimworldripoff
rimworldripoff
master

搜索帮助