Skip to content

Commit

Permalink
added knight navigation nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ProfAndreaPollini committed Jul 28, 2022
1 parent e24efb3 commit 11c136a
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 5 deletions.
42 changes: 40 additions & 2 deletions GameScreen.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=22 format=2]
[gd_scene load_steps=26 format=2]

[ext_resource path="res://GameScreen.gd" type="Script" id=1]
[ext_resource path="res://assets/0x72_16x16DungeonTileset.v4.png" type="Texture" id=2]
Expand All @@ -10,6 +10,8 @@
[ext_resource path="res://assets/Structures.png" type="Texture" id=8]
[ext_resource path="res://lib/door/Door.tscn" type="PackedScene" id=9]
[ext_resource path="res://lib/toggle/ToggleElement.tscn" type="PackedScene" id=10]
[ext_resource path="res://lib/knight/Knight.tscn" type="PackedScene" id=11]
[ext_resource path="res://NavigationPolygonInstance.gd" type="Script" id=12]

[sub_resource type="ConvexPolygonShape2D" id=11]
points = PoolVector2Array( 16, 16, 0, 16, 0, 16, 16, 16 )
Expand Down Expand Up @@ -210,18 +212,23 @@ points = PoolVector2Array( 0, 0, 32, 0, 32, 32, 0, 32 )
5/shapes = [ ]
5/z_index = 0

[sub_resource type="NavigationPolygon" id=22]

[sub_resource type="NavigationPolygon" id=23]

[node name="GameScreen" type="Node"]
script = ExtResource( 1 )

[node name="Level" type="Node" parent="."]

[node name="Level0" type="Node" parent="Level"]
[node name="Level0" type="Node2D" parent="Level"]

[node name="TileMap" type="TileMap" parent="Level/Level0"]
tile_set = SubResource( 20 )
cell_size = Vector2( 16, 16 )
show_collision = true
collision_layer = 32
bake_navigation = true
format = 1
tile_data = PoolIntArray( -65536, 0, 196608, -65535, 0, 196608, -65534, 0, 196608, -65533, 0, 196608, -65532, 0, 196608, -65531, 0, 196608, -65530, 0, 196608, -65527, 0, 196608, -65526, 0, 196608, -65525, 0, 196608, -65524, 0, 196608, -65523, 0, 196608, -65522, 0, 196608, -65521, 0, 196608, -65520, 0, 196608, 7, 0, 6, 8, 0, 6, 65538, 0, 2, 65539, 0, 2, 65540, 0, 2, 65541, 0, 2, 65542, 0, 2, 65543, 0, 6, 65544, 0, 6, 65545, 0, 2, 65546, 0, 2, 65547, 0, 2, 65548, 0, 2, 65549, 0, 2, 65550, 0, 2, 65551, 0, 2, 65552, 0, 2, 65553, 0, 2, 65554, 0, 2, 65555, 0, 2, 131074, 0, 65536, 131075, 0, 65536, 131076, 1, 3, 131077, 0, 65536, 131078, 0, 262149, 131079, 0, 6, 131080, 0, 6, 131081, 0, 65536, 131082, 1, 5, 131083, 0, 65536, 131084, 5, 0, 131085, 0, 65536, 131086, 0, 65536, 131087, 0, 65539, 131088, 0, 65536, 131089, 0, 65536, 131090, 0, 65536, 131091, 0, 65536, 196610, 0, 65536, 196611, 0, 131072, 196612, 0, 131072, 196613, 0, 131072, 196614, 0, 327685, 196615, 0, 131072, 196616, 0, 131072, 196617, 0, 131072, 196618, 0, 131072, 196619, 0, 131072, 196620, 0, 131072, 196621, 0, 131072, 196622, 0, 131072, 196623, 0, 131075, 196624, 0, 131072, 196625, 0, 131072, 196626, 0, 131072, 196627, 0, 65536, 262146, 0, 65536, 262147, 0, 196610, 262148, 0, 196610, 262149, 0, 196610, 262150, 0, 196610, 262151, 0, 196610, 262152, 0, 196610, 262153, 0, 196610, 262154, 0, 196610, 262155, 0, 196610, 262156, 0, 196610, 262157, 0, 196610, 262158, 0, 196610, 262159, 0, 196610, 262160, 0, 196610, 262161, 0, 196610, 262162, 0, 196610, 262163, 0, 65536, 327682, 0, 65536, 327683, 0, 196610, 327684, 0, 196610, 327685, 0, 196610, 327686, 0, 196610, 327687, 0, 196610, 327688, 0, 196610, 327689, 0, 196610, 327690, 0, 196610, 327691, 0, 196610, 327692, 0, 196610, 327693, 0, 196610, 327694, 0, 196610, 327695, 0, 196610, 327696, 0, 196610, 327697, 0, 196610, 327698, 0, 196610, 327699, 0, 65536, 393218, 0, 65536, 393219, 0, 196610, 393220, 0, 196610, 393221, 0, 196610, 393222, 0, 196610, 393223, 0, 393216, 393224, 0, 393217, 393225, 0, 393217, 393226, 0, 393217, 393227, 0, 393217, 393228, 0, 393219, 393229, 0, 196610, 393230, 0, 196610, 393231, 0, 196610, 393232, 0, 196610, 393233, 0, 196610, 393234, 0, 196610, 393235, 0, 65536, 458754, 0, 65536, 458755, 0, 196610, 458756, 0, 196610, 458757, 0, 196610, 458758, 0, 196610, 458759, 0, 458752, 458760, 0, 458756, 458761, 0, 458756, 458762, 0, 458756, 458763, 0, 458756, 458764, 0, 458755, 458765, 0, 196610, 458766, 0, 196610, 458767, 0, 196610, 458768, 0, 196610, 458769, 0, 196610, 458770, 0, 196610, 458771, 0, 65536, 524290, 0, 65536, 524291, 0, 196610, 524292, 0, 196610, 524293, 0, 196610, 524294, 0, 196610, 524295, 0, 524288, 524296, 0, 524289, 524297, 0, 524289, 524298, 0, 524289, 524299, 0, 524289, 524300, 0, 458755, 524301, 0, 196610, 524302, 0, 196610, 524303, 0, 196610, 524304, 0, 196610, 524305, 0, 196610, 524306, 0, 196610, 524307, 0, 65536, 589826, 0, 65536, 589827, 0, 196610, 589828, 0, 196610, 589829, 0, 196610, 589830, 0, 196610, 589831, 0, 196610, 589832, 0, 196610, 589833, 0, 196610, 589834, 0, 196610, 589835, 0, 196610, 589836, 0, 458755, 589837, 0, 196610, 589838, 0, 196610, 589839, 0, 196610, 589840, 0, 196610, 589841, 0, 196610, 589842, 0, 196610, 589843, 0, 65536, 655362, 0, 65536, 655363, 0, 196610, 655364, 0, 196610, 655365, 0, 196610, 655366, 0, 196610, 655367, 0, 196610, 655368, 0, 196610, 655369, 0, 196610, 655370, 0, 196610, 655371, 0, 196610, 655372, 0, 196610, 655373, 0, 196610, 655374, 0, 196610, 655375, 0, 196610, 655376, 0, 196610, 655377, 0, 196610, 655378, 0, 196610, 655379, 0, 65536, 720898, 0, 65536, 720899, 0, 196610, 720900, 0, 196610, 720901, 0, 196610, 720902, 0, 196610, 720903, 0, 196610, 720904, 0, 196610, 720905, 0, 196610, 720906, 0, 196610, 720907, 0, 196610, 720908, 0, 196610, 720909, 0, 196610, 720910, 0, 196610, 720911, 0, 196610, 720912, 0, 196610, 720913, 0, 196610, 720914, 0, 196610, 720915, 0, 65536, 786434, 0, 65536, 786435, 0, 196610, 786436, 0, 196610, 786437, 0, 196610, 786438, 0, 196610, 786439, 0, 196610, 786440, 0, 196610, 786441, 0, 196610, 786442, 0, 196610, 786443, 0, 196610, 786444, 0, 196610, 786445, 0, 196610, 786446, 0, 196610, 786447, 0, 196610, 786448, 0, 196610, 786449, 0, 196610, 786450, 0, 196610, 786451, 0, 65536, 851970, 0, 65536, 851971, 0, 65536, 851972, 0, 65536, 851973, 0, 65536, 851974, 0, 65536, 851975, 0, 65536, 851976, 0, 65536, 851977, 0, 65536, 851978, 0, 65536, 851979, 0, 65536, 851980, 0, 65536, 851981, 0, 65536, 851982, 0, 65536, 851983, 0, 65536, 851984, 0, 65536, 851985, 0, 65536, 851986, 0, 65536, 851987, 0, 65536 )

Expand Down Expand Up @@ -253,4 +260,35 @@ player_path = NodePath("../Player")
[node name="ToggleElement" parent="Level/Level0" instance=ExtResource( 10 )]
position = Vector2( 184, 72 )

[node name="Node2D" type="Node2D" parent="Level/Level0"]

[node name="NavigationPolygonInstance" type="NavigationPolygonInstance" parent="Level/Level0/Node2D"]
navpoly = SubResource( 22 )

[node name="Knight" parent="Level/Level0" instance=ExtResource( 11 )]
position = Vector2( 272, 184 )

[node name="Knight2" parent="Level/Level0" instance=ExtResource( 11 )]
position = Vector2( 288, 168 )

[node name="Knight3" parent="Level/Level0" instance=ExtResource( 11 )]
position = Vector2( 288, 168 )

[node name="Knight4" parent="Level/Level0" instance=ExtResource( 11 )]
position = Vector2( 272, 168 )

[node name="Knight5" parent="Level/Level0" instance=ExtResource( 11 )]
position = Vector2( 264, 192 )

[node name="Knight6" parent="Level/Level0" instance=ExtResource( 11 )]
position = Vector2( 288, 184 )

[node name="NavigationPolygonInstance" type="NavigationPolygonInstance" parent="Level/Level0"]
navpoly = SubResource( 23 )
script = ExtResource( 12 )

[node name="Polygon2D" type="Polygon2D" parent="Level/Level0/NavigationPolygonInstance"]
color = Color( 1, 1, 1, 0.109804 )
polygon = PoolVector2Array( 304, 208, 304, 48, 48, 48, 48, 208, 128, 208, 128, 96, 240, 104, 240, 208 )

[node name="Overlays" type="Node" parent="."]
22 changes: 22 additions & 0 deletions NavigationPolygonInstance.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extends NavigationPolygonInstance


# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
func _ready():
var p = NavigationPolygon.new()
p.add_outline($Polygon2D.polygon)
p.make_polygons_from_outlines()

navpoly = p




# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
30 changes: 30 additions & 0 deletions lib/knight/Knight.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
extends KinematicBody2D


var target : Vector2

# Called when the node enters the scene tree for the first time.
func _ready():
$NavigationAgent2D.set_target_location(global_position)


#func _physics_process(delta):

func _input(event):
if Input.is_mouse_button_pressed(BUTTON_RIGHT):
print("new path")
target = get_global_mouse_position()
$NavigationAgent2D.set_target_location(target)
print($NavigationAgent2D.get_nav_path())
$Line2D.points = $NavigationAgent2D.get_nav_path()


func _physics_process(delta):
if target:
var next_position =$NavigationAgent2D.get_next_location()
#print(next_position,global_position)
var v = global_position.direction_to(next_position) * 20
$NavigationAgent2D.set_velocity(v)

func _on_NavigationAgent2D_velocity_computed(safe_velocity):
move_and_slide(safe_velocity)
27 changes: 24 additions & 3 deletions lib/knight/Knight.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=18 format=2]
[gd_scene load_steps=21 format=2]

[ext_resource path="res://assets/Fantasy RPG NPCs - Individuel Frames/Knight - Standard/Knight_Idle_3.png" type="Texture" id=1]
[ext_resource path="res://assets/Fantasy RPG NPCs - Individuel Frames/Knight - Standard/Knight_Idle_2.png" type="Texture" id=2]
Expand All @@ -8,6 +8,8 @@
[ext_resource path="res://assets/Fantasy RPG NPCs - Individuel Frames/Knight - Standard/Knight_Walk_1.png" type="Texture" id=6]
[ext_resource path="res://assets/Fantasy RPG NPCs - Individuel Frames/Knight - Standard/Knight_Walk_2.png" type="Texture" id=7]
[ext_resource path="res://assets/Fantasy RPG NPCs - Individuel Frames/Knight - Standard/Knight_Walk_3.png" type="Texture" id=8]
[ext_resource path="res://lib/knight/SimpleMovement.gd" type="Script" id=9]
[ext_resource path="res://lib/knight/Knight.gd" type="Script" id=10]

[sub_resource type="SpriteFrames" id=1]
animations = [ {
Expand Down Expand Up @@ -111,7 +113,11 @@ states/walk/position = Vector2( 116, 50 )

[sub_resource type="AnimationNodeStateMachinePlayback" id=9]

[node name="Knight" type="Node2D"]
[sub_resource type="RectangleShape2D" id=10]
extents = Vector2( 2, 3.5 )

[node name="Knight" type="KinematicBody2D"]
script = ExtResource( 10 )

[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
scale = Vector2( 0.5, 0.4 )
Expand All @@ -132,4 +138,19 @@ anim_player = NodePath("../AnimationPlayer")
active = true
process_mode = 0
parameters/playback = SubResource( 9 )
parameters/walk/blend_position = -0.956822
parameters/walk/blend_position = -0.924007

[node name="SimpleMovement" type="Node" parent="."]
script = ExtResource( 9 )

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( 0, -3.5 )
shape = SubResource( 10 )

[node name="NavigationAgent2D" type="NavigationAgent2D" parent="."]
target_desired_distance = 2.12
avoidance_enabled = true

[node name="Line2D" type="Line2D" parent="."]

[connection signal="velocity_computed" from="NavigationAgent2D" to="." method="_on_NavigationAgent2D_velocity_computed"]
25 changes: 25 additions & 0 deletions lib/knight/SimpleMovement.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
extends Node

export(float) var velocity := 1.0

export (NodePath) var entity_path
onready var entity = get_node(entity_path) as KinematicBody2D

enum Direction {NO_MOVE, UP,DOWN,LEFT,RIGHT}

func move(direction):
var v = Vector2.ZERO
match direction:
Direction.UP:
v = Vector2.UP
Direction.DOWN:
v = Vector2.DOWN
Direction.LEFT:
v = Vector2.LEFT
Direction.RIGHT:
v = Vector2.RIGHT
_ :
pass

entity.move_and_slide(velocity*v)

2 changes: 2 additions & 0 deletions lib/player/Player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ _data = [ Vector2( 0, 0.344318 ), 0.0, 0.0, 0, 0, Vector2( 0.333333, 0.635227 ),

[node name="Player" type="KinematicBody2D"]
position = Vector2( 128, 137 )
scale = Vector2( 0.5, 0.5 )
collision_mask = 48
script = ExtResource( 1 )

Expand Down Expand Up @@ -589,6 +590,7 @@ position = Vector2( -11.3785, 5.77687 )
[node name="CPUParticles2D" type="CPUParticles2D" parent="."]
material = SubResource( 82 )
position = Vector2( 4, 17 )
emitting = false
one_shot = true
explosiveness = 0.8
randomness = 1.0
Expand Down

0 comments on commit 11c136a

Please sign in to comment.