Initial commit.

This commit is contained in:
2024-05-20 16:31:11 +02:00
commit 4b5d01c2a7
27 changed files with 1135 additions and 0 deletions

2
demo/.gitattributes vendored Normal file
View File

@@ -0,0 +1,2 @@
# Normalize EOL for all files that Git considers text files.
* text=auto eol=lf

5
demo/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
# Godot 4+ specific ignores
.godot/
# Misc
*.tmp

View File

@@ -0,0 +1,13 @@
[configuration]
entry_symbol = "my_astar_ext_init"
compatibility_minimum = "4.2"
[libraries]
macos.debug = "res://bin/libMultilevelAStarExt.macos.template_debug.framework"
macos.release = "res://bin/libMultilevelAStarExt.macos.template_release.framework"
windows.debug.x86_64 = "res://bin/libMultilevelAStarExt.windows.template_debug.x86_64.dll"
windows.release.x86_64 = "res://bin/libMultilevelAStarExt.windows.template_release.x86_64.dll"
linux.debug.x86_64 = "res://bin/libMultilevelAStarExt.linux.template_debug.x86_64.so"
linux.release.x86_64 = "res://bin/libMultilevelAStarExt.linux.template_release.x86_64.so"

View File

@@ -0,0 +1,63 @@
extends RefCounted
class_name MultilevelAStar
# tile custom data "type":
# 0 => blocked
# 1(+) => can move to same number
# -1(-) => can move to same, one larger or one smaller as long as they're both negative
# => can move to and from its absolute value and one larger than its absolute value
# private variables start with an _ as per the GDScript style guide
var _used_rect: Rect2i
var _astar: MultilevelAStarEx
func _init(map: TileMap):
_used_rect = map.get_used_rect()
# assert stuff here because the extension's assertions just crash without a message
assert(_used_rect.get_area() > 0)
_astar = MultilevelAStarEx.new()
_astar.init(_used_rect)
for layer in map.get_layers_count():
var cells := map.get_used_cells(layer)
for cell in cells:
var tile_data := map.get_cell_tile_data(layer, cell)
if tile_data != null:
var type = tile_data.get_custom_data("type")
assert(type is int)
_astar.set_terrain(cell, type)
func is_unit(point: Vector2i) -> bool:
assert(_used_rect.has_point(point))
return _astar.get_unit(point)
func set_unit(point: Vector2i, solid: bool = true) -> void:
assert(_used_rect.has_point(point))
_astar.set_unit(point, solid)
func get_terrain(point: Vector2i) -> MultilevelAStarEx.TerrainType:
assert(_used_rect.has_point(point))
return _astar.get_terrain(point)
func set_terrain(point: Vector2i, type: MultilevelAStarEx.TerrainType) -> void:
assert(_used_rect.has_point(point))
_astar.set_terrain(point, type)
func find_path(from: Vector2i, to: Vector2i, return_closest: bool = false) -> Array[Vector2i]:
assert(_used_rect.has_point(from))
assert(_used_rect.has_point(to))
var res = _astar.find_path(from, to, return_closest) # vrne Variant: null ali Array[Vector2i]
if res != null:
return res
else:
return []

1
demo/icon.svg Normal file
View File

@@ -0,0 +1 @@
<svg height="128" width="128" xmlns="http://www.w3.org/2000/svg"><rect x="2" y="2" width="124" height="124" rx="14" fill="#363d52" stroke="#212532" stroke-width="4"/><g transform="scale(.101) translate(122 122)"><g fill="#fff"><path d="M105 673v33q407 354 814 0v-33z"/><path d="m105 673 152 14q12 1 15 14l4 67 132 10 8-61q2-11 15-15h162q13 4 15 15l8 61 132-10 4-67q3-13 15-14l152-14V427q30-39 56-81-35-59-83-108-43 20-82 47-40-37-88-64 7-51 8-102-59-28-123-42-26 43-46 89-49-7-98 0-20-46-46-89-64 14-123 42 1 51 8 102-48 27-88 64-39-27-82-47-48 49-83 108 26 42 56 81zm0 33v39c0 276 813 276 814 0v-39l-134 12-5 69q-2 10-14 13l-162 11q-12 0-16-11l-10-65H446l-10 65q-4 11-16 11l-162-11q-12-3-14-13l-5-69z" fill="#478cbf"/><path d="M483 600c0 34 58 34 58 0v-86c0-34-58-34-58 0z"/><circle cx="725" cy="526" r="90"/><circle cx="299" cy="526" r="90"/></g><g fill="#414042"><circle cx="307" cy="532" r="60"/><circle cx="717" cy="532" r="60"/></g></g></svg>

After

Width:  |  Height:  |  Size: 949 B

37
demo/icon.svg.import Normal file
View File

@@ -0,0 +1,37 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://cpupav84hxj1u"
path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://icon.svg"
dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.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
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false

74
demo/main.gd Normal file
View File

@@ -0,0 +1,74 @@
extends Node2D
const CELL_SIZE: int = 32
@onready var map: TileMap = $TileMap
@onready var marker_from: Node2D = $MarkerFrom
@onready var marker_to: Node2D = $MarkerTo
@onready var path_line: Line2D = $PathLine
@onready var check_box: CheckBox = $CheckBox
var astar: MultilevelAStar
var used_rect: Rect2i
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
# mapa začne na -1, -1. tukaj premaknem da se vidi vse na mapi
# (saj verjetno obstaja boljši način ma ga ne poznam - mogoče kakšna kamera)
position = Vector2i.ONE * CELL_SIZE
used_rect = map.get_used_rect()
print(used_rect)
astar = MultilevelAStar.new(map)
mark_units()
marker_from.modulate = Color.html("#ff0000ff")
marker_to.modulate = Color.html("#00ff00ff")
marker_from.position = Vector2i(-1, -1) * CELL_SIZE
marker_to.position = (used_rect.position + used_rect.size - Vector2i.ONE) * CELL_SIZE
find_path()
func mark_units() -> void:
for unit in $Units.get_tree().get_nodes_in_group("unit"):
astar.set_unit(unit.position / CELL_SIZE)
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
if event.pressed:
var cell := map.local_to_map(map.get_local_mouse_position())
if map.get_used_rect().has_point(cell):
if event.button_index == 1:
marker_to.position = Vector2(cell.x * CELL_SIZE, cell.y * CELL_SIZE)
find_path();
elif event.button_index == 2:
marker_from.position = Vector2(cell.x * CELL_SIZE, cell.y * CELL_SIZE)
find_path();
func find_path() -> void:
path_line.clear_points()
var from: Vector2i = Vector2i(marker_from.position / CELL_SIZE)
var to: Vector2i = Vector2i(marker_to.position / CELL_SIZE)
var arr = astar.find_path(from, to, check_box.button_pressed) # vrne Variant: null ali Array
#print(arr)
if arr.size() > 0:
# add new path
path_line.add_point(from * CELL_SIZE + Vector2i(16, 16))
for vec in arr:
#print("%s, %s" % [vec.x, vec.y])
path_line.add_point(vec * CELL_SIZE + Vector2i(16, 16))
func _on_check_box_pressed() -> void:
find_path()

84
demo/main.tscn Normal file

File diff suppressed because one or more lines are too long

BIN
demo/markers/marker.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 251 B

View File

@@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://ydnd8d7pbpct"
path="res://.godot/imported/marker.png-302bb6fffff2bb75ddac5872ea46d55a.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://markers/marker.png"
dest_files=["res://.godot/imported/marker.png-302bb6fffff2bb75ddac5872ea46d55a.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

9
demo/markers/marker.tscn Normal file
View File

@@ -0,0 +1,9 @@
[gd_scene load_steps=2 format=3 uid="uid://cnrhrho63n4n2"]
[ext_resource type="Texture2D" uid="uid://ydnd8d7pbpct" path="res://markers/marker.png" id="1_mefdn"]
[node name="Marker" type="Node2D"]
[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("1_mefdn")
offset = Vector2(16, 16)

BIN
demo/markers/unit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 B

View File

@@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://b6j8es5qgtv05"
path="res://.godot/imported/unit.png-1fe68df64273b07deeeaa2e1953638b5.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://markers/unit.png"
dest_files=["res://.godot/imported/unit.png-1fe68df64273b07deeeaa2e1953638b5.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

9
demo/markers/unit.tscn Normal file
View File

@@ -0,0 +1,9 @@
[gd_scene load_steps=2 format=3 uid="uid://cj34xrleexqf6"]
[ext_resource type="Texture2D" uid="uid://b6j8es5qgtv05" path="res://markers/unit.png" id="1_qutcq"]
[node name="Unit" type="Node2D" groups=["unit"]]
[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(16, 16)
texture = ExtResource("1_qutcq")

20
demo/project.godot Normal file
View File

@@ -0,0 +1,20 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters
config_version=5
[application]
config/name="MultilevelAStarTest"
run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.2", "Forward Plus")
config/icon="res://icon.svg"
[display]
window/stretch/mode="canvas_items"

BIN
demo/tiles/socerb.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://dh20x6lnm6ly8"
path="res://.godot/imported/socerb.png-80780b522a349cb35f220b47d0cdac2c.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://tiles/socerb.png"
dest_files=["res://.godot/imported/socerb.png-80780b522a349cb35f220b47d0cdac2c.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

204
demo/tiles/socerb.tres Normal file
View File

@@ -0,0 +1,204 @@
[gd_resource type="TileSet" load_steps=3 format=3 uid="uid://dsc2e3ohtmv01"]
[ext_resource type="Texture2D" uid="uid://dh20x6lnm6ly8" path="res://tiles/socerb.png" id="1_e53on"]
[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_dokfe"]
texture = ExtResource("1_e53on")
texture_region_size = Vector2i(32, 32)
0:0/0 = 0
0:0/0/custom_data_0 = 1
1:0/0 = 0
1:0/0/custom_data_0 = -1
2:0/0 = 0
2:0/0/custom_data_0 = -1
3:0/0 = 0
3:0/0/custom_data_0 = -1
4:0/0 = 0
4:0/0/custom_data_0 = -2
8:0/0 = 0
9:0/0 = 0
10:0/0 = 0
11:0/0 = 0
12:0/0 = 0
13:0/0 = 0
0:1/0 = 0
0:1/0/custom_data_0 = 1
1:1/0 = 0
1:1/0/custom_data_0 = 1
2:1/0 = 0
3:1/0 = 0
4:1/0 = 0
5:1/0 = 0
6:1/0 = 0
7:1/0 = 0
8:1/0 = 0
9:1/0 = 0
9:1/0/custom_data_0 = 3
10:1/0 = 0
10:1/0/custom_data_0 = 3
11:1/0 = 0
11:1/0/custom_data_0 = 3
12:1/0 = 0
12:1/0/custom_data_0 = 3
13:1/0 = 0
14:1/0 = 0
0:2/0 = 0
0:2/0/custom_data_0 = 1
1:2/0 = 0
1:2/0/custom_data_0 = 1
2:2/0 = 0
3:2/0 = 0
4:2/0 = 0
5:2/0 = 0
6:2/0 = 0
7:2/0 = 0
8:2/0 = 0
8:2/0/custom_data_0 = 3
9:2/0 = 0
9:2/0/custom_data_0 = 3
10:2/0 = 0
10:2/0/custom_data_0 = 3
11:2/0 = 0
11:2/0/custom_data_0 = 3
12:2/0 = 0
12:2/0/custom_data_0 = 3
13:2/0 = 0
13:2/0/custom_data_0 = 3
14:2/0 = 0
0:3/0 = 0
0:3/0/custom_data_0 = 1
1:3/0 = 0
1:3/0/custom_data_0 = 1
2:3/0 = 0
3:3/0 = 0
3:3/0/custom_data_0 = 1
4:3/0 = 0
4:3/0/custom_data_0 = 1
5:3/0 = 0
5:3/0/custom_data_0 = 1
6:3/0 = 0
7:3/0 = 0
8:3/0 = 0
8:3/0/custom_data_0 = 3
9:3/0 = 0
9:3/0/custom_data_0 = 3
10:3/0 = 0
10:3/0/custom_data_0 = 3
11:3/0 = 0
11:3/0/custom_data_0 = 3
12:3/0 = 0
12:3/0/custom_data_0 = 3
13:3/0 = 0
13:3/0/custom_data_0 = 3
14:3/0 = 0
1:4/0 = 0
2:4/0 = 0
3:4/0 = 0
4:4/0 = 0
5:4/0 = 0
5:4/0/custom_data_0 = 1
6:4/0 = 0
7:4/0 = 0
8:4/0 = 0
8:4/0/custom_data_0 = 3
9:4/0 = 0
9:4/0/custom_data_0 = 3
10:4/0 = 0
10:4/0/custom_data_0 = 3
11:4/0 = 0
11:4/0/custom_data_0 = 3
12:4/0 = 0
12:4/0/custom_data_0 = 3
13:4/0 = 0
13:4/0/custom_data_0 = 3
14:4/0 = 0
2:5/0 = 0
3:5/0 = 0
4:5/0 = 0
5:5/0 = 0
6:5/0 = 0
7:5/0 = 0
8:5/0 = 0
8:5/0/custom_data_0 = 3
9:5/0 = 0
9:5/0/custom_data_0 = 3
10:5/0 = 0
10:5/0/custom_data_0 = 3
11:5/0 = 0
11:5/0/custom_data_0 = 3
12:5/0 = 0
12:5/0/custom_data_0 = 3
13:5/0 = 0
13:5/0/custom_data_0 = 3
14:5/0 = 0
2:6/0 = 0
3:6/0 = 0
4:6/0 = 0
5:6/0 = 0
6:6/0 = 0
7:6/0 = 0
8:6/0 = 0
9:6/0 = 0
9:6/0/custom_data_0 = 3
10:6/0 = 0
10:6/0/custom_data_0 = 3
11:6/0 = 0
11:6/0/custom_data_0 = 3
12:6/0 = 0
12:6/0/custom_data_0 = 3
13:6/0 = 0
14:6/0 = 0
2:7/0 = 0
3:7/0 = 0
3:7/0/custom_data_0 = 2
4:7/0 = 0
4:7/0/custom_data_0 = 2
5:7/0 = 0
5:7/0/custom_data_0 = 2
6:7/0 = 0
8:7/0 = 0
9:7/0 = 0
10:7/0 = 0
11:7/0 = 0
12:7/0 = 0
13:7/0 = 0
2:8/0 = 0
3:8/0 = 0
3:8/0/custom_data_0 = 2
4:8/0 = 0
4:8/0/custom_data_0 = 2
5:8/0 = 0
5:8/0/custom_data_0 = 2
6:8/0 = 0
2:9/0 = 0
3:9/0 = 0
3:9/0/custom_data_0 = 2
4:9/0 = 0
4:9/0/custom_data_0 = 2
5:9/0 = 0
5:9/0/custom_data_0 = 2
6:9/0 = 0
2:10/0 = 0
3:10/0 = 0
4:10/0 = 0
5:10/0 = 0
6:10/0 = 0
0:11/0 = 0
0:12/0 = 0
1:12/0 = 0
2:12/0 = 0
0:13/0 = 0
1:13/0 = 0
2:13/0 = 0
0:14/0 = 0
1:14/0 = 0
2:14/0 = 0
0:15/0 = 0
1:15/0 = 0
2:15/0 = 0
[resource]
tile_size = Vector2i(32, 32)
custom_data_layer_0/name = "type"
custom_data_layer_0/type = 2
sources/0 = SubResource("TileSetAtlasSource_dokfe")