Skip to content

Commit

Permalink
One Shot: Pathfinding on Tilemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Bradfield committed Jun 5, 2017
1 parent fadddbd commit 0e8326e
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 0 deletions.
1 change: 1 addition & 0 deletions One_Shots/nav/attributions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Art from Kenney.nl
10 changes: 10 additions & 0 deletions One_Shots/nav/engine.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[application]

name="Pathfinding with Tilemaps"
main_scene="res://main.tscn"
icon="res://icon.png"

[input]

l_click=[mbutton(0, 1)]
r_click=[mbutton(0, 2)]
Binary file added One_Shots/nav/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions One_Shots/nav/icon.png.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gen_mipmaps=false
31 changes: 31 additions & 0 deletions One_Shots/nav/main.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
extends Node

signal map_update

var mob = preload("res://mob.tscn")

onready var start_pos = get_node("start_pos").get_pos()
onready var end_pos = get_node("end_pos").get_pos()
onready var nav = get_node("nav")
onready var map = get_node("nav/map")

func _ready():
set_process_input(true)

func _input(event):
if event.type == InputEvent.MOUSE_BUTTON and event.pressed:
var tile = map.world_to_map(event.pos)
if event.button_index == 1:
map.set_cell(tile.x, tile.y, 1)
elif event.button_index == 2:
map.set_cell(tile.x, tile.y, 0)
if event.type == InputEvent.MOUSE_BUTTON and not event.pressed:
emit_signal("map_update")

func _on_mob_timer_timeout():
var m = mob.instance()
add_child(m)
m.set_pos(start_pos)
m.goal = end_pos
m.nav = nav
connect("map_update", m, "update_path")
66 changes: 66 additions & 0 deletions One_Shots/nav/main.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[gd_scene load_steps=4 format=1]

[ext_resource path="res://main.gd" type="Script" id=1]
[ext_resource path="res://tileset_grass_dirt.tres" type="TileSet" id=2]
[ext_resource path="res://towerDefense_tilesheet.tex" type="Texture" id=3]

[node name="main" type="Node"]

script/script = ExtResource( 1 )

[node name="nav" type="Navigation2D" parent="."]

[node name="map" type="TileMap" parent="nav"]

mode = 0
tile_set = ExtResource( 2 )
cell/size = Vector2( 64, 64 )
cell/quadrant_size = 16
cell/custom_transform = Matrix32( 1, 0, 0, 1, 0, 0 )
cell/half_offset = 2
cell/tile_origin = 0
cell/y_sort = false
collision/use_kinematic = false
collision/friction = 1.0
collision/bounce = 0.0
collision/layers = 1
collision/mask = 1
occluder/light_mask = 1
tile_data = IntArray( 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 1, 12, 1, 13, 1, 14, 1, 15, 1, 65536, 1, 65537, 0, 65538, 0, 65539, 0, 65540, 1, 65541, 0, 65542, 0, 65543, 0, 65544, 0, 65545, 0, 65546, 0, 65547, 0, 65548, 1, 65549, 0, 65550, 0, 65551, 1, 131072, 1, 131073, 0, 131074, 0, 131075, 0, 131076, 1, 131077, 0, 131078, 0, 131079, 0, 131080, 0, 131081, 0, 131082, 0, 131083, 0, 131084, 1, 131085, 0, 131086, 0, 131087, 1, 196608, 1, 196609, 0, 196610, 0, 196611, 0, 196612, 1, 196613, 0, 196614, 0, 196615, 0, 196616, 0, 196617, 0, 196618, 0, 196619, 0, 196620, 1, 196621, 1, 196622, 1, 196623, 1, 262144, 1, 262145, 0, 262146, 0, 262147, 0, 262148, 1, 262149, 1, 262150, 1, 262151, 1, 262152, 1, 262153, 1, 262154, 0, 262155, 0, 262156, 1, 262157, 0, 262158, 0, 262159, 1, 327680, 1, 327681, 0, 327682, 0, 327683, 0, 327684, 1, 327685, 0, 327686, 0, 327687, 0, 327688, 0, 327689, 1, 327690, 0, 327691, 0, 327692, 1, 327693, 0, 327694, 0, 327695, 1, 393216, 1, 393217, 0, 393218, 0, 393219, 0, 393220, 1, 393221, 0, 393222, 0, 393223, 0, 393224, 0, 393225, 1, 393226, 0, 393227, 0, 393228, 1, 393229, 0, 393230, 0, 393231, 1, 458752, 1, 458753, 1, 458754, 1, 458755, 1, 458756, 1, 458757, 0, 458758, 0, 458759, 0, 458760, 0, 458761, 1, 458762, 1, 458763, 1, 458764, 1, 458765, 1, 458766, 1, 458767, 1, 524288, 1, 524289, 0, 524290, 0, 524291, 0, 524292, 1, 524293, 1, 524294, 1, 524295, 1, 524296, 1, 524297, 1, 524298, 0, 524299, 0, 524300, 0, 524301, 0, 524302, 0, 524303, 0, 589824, 1, 589825, 0, 589826, 0, 589827, 0, 589828, 0, 589829, 0, 589830, 0, 589831, 0, 589832, 0, 589833, 0, 589834, 0, 589835, 0, 589836, 0, 589837, 0, 589838, 0, 589839, 0 )

[node name="start_pos" type="Position2D" parent="."]

transform/pos = Vector2( 29.915, 568.605 )
__meta__ = {
"_edit_group_": true
}

[node name="sprite" type="Sprite" parent="start_pos"]

texture = ExtResource( 3 )
region = true
region_rect = Rect2( 832, 768, 64, 64 )

[node name="end_pos" type="Position2D" parent="."]

transform/pos = Vector2( 994.048, 33.4137 )
__meta__ = {
"_edit_group_": true
}

[node name="sprite" type="Sprite" parent="end_pos"]

texture = ExtResource( 3 )
region = true
region_rect = Rect2( 704, 768, 64, 64 )

[node name="mob_timer" type="Timer" parent="."]

process_mode = 1
wait_time = 0.5
one_shot = false
autostart = true

[connection signal="timeout" from="mob_timer" to="." method="_on_mob_timer_timeout"]


28 changes: 28 additions & 0 deletions One_Shots/nav/mob.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
extends Sprite

var speed = 150
var nav = null setget set_nav
var path = []
var goal = Vector2()

func _ready():
set_fixed_process(true)

func set_nav(new_nav):
nav = new_nav
update_path()

func update_path():
path = nav.get_simple_path(get_pos(), goal, false)
if path.size() == 0:
queue_free()

func _fixed_process(delta):
if path.size() > 1:
var d = get_pos().distance_to(path[0])
if d > 2:
set_pos(get_pos().linear_interpolate(path[0], (speed * delta)/d))
else:
path.remove(0)
else:
queue_free()
14 changes: 14 additions & 0 deletions One_Shots/nav/mob.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[gd_scene load_steps=3 format=1]

[ext_resource path="res://towerDefense_tilesheet.tex" type="Texture" id=1]
[ext_resource path="res://mob.gd" type="Script" id=2]

[node name="mob" type="Sprite"]

transform/pos = Vector2( 193.173, 280.237 )
texture = ExtResource( 1 )
region = true
region_rect = Rect2( 976.49, 652.633, 32.5, 39 )
script/script = ExtResource( 2 )


32 changes: 32 additions & 0 deletions One_Shots/nav/tileset_grass_dirt.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[gd_resource type="TileSet" load_steps=3 format=1]

[ext_resource path="res://towerDefense_tilesheet.tex" type="Texture" id=1]

[sub_resource type="NavigationPolygon" id=1]

vertices = Vector2Array( 32, 32, -32, 32, -32, -32, 32, -32 )
polygons = [ IntArray( 0, 1, 2, 3 ) ]
outlines = [ Vector2Array( 32, -32, -32, -32, -32, 32, 32, 32 ) ]

[resource]

0/name = "Sprite 4"
0/texture = ExtResource( 1 )
0/tex_offset = Vector2( 0, 0 )
0/modulate = Color( 1, 1, 1, 1 )
0/region = Rect2( 64, 64, 64, 64 )
0/occluder_offset = Vector2( 32, 32 )
0/navigation_offset = Vector2( 32, 32 )
0/shape_offset = Vector2( 0, 0 )
0/shapes = [ ]
1/name = "Sprite 14"
1/texture = ExtResource( 1 )
1/tex_offset = Vector2( 0, 0 )
1/modulate = Color( 1, 1, 1, 1 )
1/region = Rect2( 256, 128, 64, 64 )
1/occluder_offset = Vector2( 32, 32 )
1/navigation_offset = Vector2( 32, 32 )
1/navigation = SubResource( 1 )
1/shape_offset = Vector2( 0, 0 )
1/shapes = [ ]

35 changes: 35 additions & 0 deletions One_Shots/nav/tileset_grass_dirt.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[gd_scene load_steps=3 format=1]

[ext_resource path="res://towerDefense_tilesheet.tex" type="Texture" id=1]

[sub_resource type="NavigationPolygon" id=16]

vertices = Vector2Array( 32, 32, -32, 32, -32, -32, 32, -32 )
polygons = [ IntArray( 0, 1, 2, 3 ) ]
outlines = [ Vector2Array( 32, -32, -32, -32, -32, 32, 32, 32 ) ]

[node name="tileset_grass_dirt" type="Node2D"]

[node name="Sprite 4" type="Sprite" parent="."]

transform/pos = Vector2( 64, 64 )
texture = ExtResource( 1 )
region = true
region_rect = Rect2( 64, 64, 64, 64 )

[node name="Sprite 14" type="Sprite" parent="."]

transform/pos = Vector2( 160, 64 )
texture = ExtResource( 1 )
region = true
region_rect = Rect2( 256, 128, 64, 64 )
__meta__ = {
"_edit_group_": true
}

[node name="NavigationPolygonInstance" type="NavigationPolygonInstance" parent="Sprite 14"]

navpoly = SubResource( 16 )
enabled = true


Binary file added One_Shots/nav/towerDefense_tilesheet.pbm
Binary file not shown.
Binary file added One_Shots/nav/towerDefense_tilesheet.tex
Binary file not shown.

0 comments on commit 0e8326e

Please sign in to comment.