Procházet zdrojové kódy

An attempt at pathfinding

497009 před 2 roky
rodič
revize
c3ffa188ec

+ 6 - 5
godot_project/Player.tscn

@@ -5,7 +5,7 @@
 [ext_resource path="res://Player_camera.tscn" type="PackedScene" id=3]
 
 [sub_resource type="RectangleShape2D" id=1]
-extents = Vector2( 59.7122, 32 )
+extents = Vector2( 32, 31.1563 )
 
 [node name="Player" type="KinematicBody2D" groups=[
 "player",
@@ -13,11 +13,12 @@ extents = Vector2( 59.7122, 32 )
 scale = Vector2( 0.48, 1 )
 collision_mask = 31
 script = ExtResource( 2 )
+__meta__ = {
+"_edit_group_": true
+}
 jump_speed = -700
 
 [node name="Sprite" type="Sprite" parent="."]
-position = Vector2( -7.15256e-07, 0 )
-scale = Vector2( 1.89919, 1 )
 texture = ExtResource( 1 )
 
 [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
@@ -34,13 +35,13 @@ cast_to = Vector2( 0, -50 )
 collision_mask = 12
 
 [node name="tile_check_ray" type="RayCast2D" parent="."]
-position = Vector2( 59.0834, 32 )
+position = Vector2( 32, 32 )
 enabled = true
 cast_to = Vector2( 0, 10 )
 collision_mask = 8
 
 [node name="tile_check_ray2" type="RayCast2D" parent="."]
-position = Vector2( -59.0834, 32 )
+position = Vector2( -32, 32 )
 enabled = true
 cast_to = Vector2( 0, 10 )
 collision_mask = 8

+ 70 - 0
godot_project/hero_kinematic_body.gd

@@ -0,0 +1,70 @@
+extends KinematicBody2D
+
+#var is_moving = false
+onready var SPEED = 40
+var velocity = Vector2.ZERO
+var point_to_go_to = 0
+
+var path = []
+
+# Add more variables if there are more zones
+var zone_sizes = {1:[Vector2(512, 320), Vector2(-1600, -64), false],
+2:[Vector2(512, 256), Vector2(-1088, -64), false],
+3:[Vector2(320, 256), Vector2(-1600, 320), false],
+4:[Vector2(576, 320), Vector2(-1216, 256), false],
+5:[Vector2(320, 192), Vector2(-512, 384), false],
+6:[Vector2(320, 320), Vector2(-512, -64), false]}
+
+
+func pick_random_point():
+	var position_to_go_to = Vector2.ZERO
+	var needed_zone = 0
+
+	var rng = RandomNumberGenerator.new()
+	rng.randomize()
+	needed_zone = round(rng.randf_range(1, 6))
+
+	while zone_sizes[int(needed_zone)][2] != false:
+		needed_zone = round(rng.randf_range(1, 6))
+
+	position_to_go_to.x = rng.randf_range(0, zone_sizes[int(needed_zone)][0].x)
+	position_to_go_to.y = rng.randf_range(0, zone_sizes[int(needed_zone)][0].y)
+
+	position_to_go_to.x += zone_sizes[int(needed_zone)][1].x
+	position_to_go_to.y += zone_sizes[int(needed_zone)][1].y
+	return position_to_go_to
+
+
+func generate_path(path_to_move_to):
+	print($LevelNavigation)
+	path = $LevelNavigation.get_simple_path(self.global_position, path_to_move_to, false)
+	$Line2D.points = path
+
+
+func navigate():
+	if path.size() > 0:
+		velocity = self.global_position.direction_to(path[1]) * SPEED
+		
+		if self.global_position == path[0]:
+			path.pop_front()
+	
+#	print(path[0])
+#	print(hero.global_position)
+
+
+func move_to_random_point():
+#	is_moving = true
+	velocity = move_and_slide(velocity)
+
+
+func _ready():
+	point_to_go_to = pick_random_point()
+
+
+func _physics_process(delta):
+#	print(velocity)
+	$Line2D.global_position = Vector2.ZERO
+	if $LevelNavigation != null:
+		generate_path(point_to_go_to)
+		navigate()
+		move_to_random_point()

+ 66 - 31
godot_project/hero_node.gd

@@ -1,45 +1,80 @@
 extends Node
 
-onready var hero = get_node("Hero_character/hero_kinematic_body")
-var is_moving = false
+onready var hero = get_node("hero_kinematic_body")
+onready var levelNavigation = get_node("LevelNavigation")
+
+#var is_moving = false
+onready var SPEED = 40
+var velocity = Vector2.ZERO
+var position = null
+
+var path = []
 
 # Add more variables if there are more zones
-var zone_sizes = {1:[Vector2(256, 160), Vector2(0, 0), false],
-2:[Vector2(160, 128), Vector2(0, 192), false],
-3:[Vector2(288, 160), Vector2(192, 160), false],
-4:[Vector2(256, 128), Vector2(256, 0), false],
-5:[Vector2(160, 160), Vector2(544, 0), false],
-6:[Vector2(160, 96), Vector2(544, 224), false]}
+#var zone_sizes = {1:[Vector2(512, 320), Vector2(-1600, -64), false],
+#2:[Vector2(512, 256), Vector2(-1088, -64), false],
+#3:[Vector2(320, 256), Vector2(-1600, 320), false],
+#4:[Vector2(576, 320), Vector2(-1216, 256), false],
+#5:[Vector2(320, 192), Vector2(-512, 384), false],
+#6:[Vector2(320, 320), Vector2(-512, -64), false]}
+#
+#
+#func pick_random_point():
+#	var position_to_go_to = Vector2.ZERO
+#	var needed_zone = 0
+#
+#	var rng = RandomNumberGenerator.new()
+#	rng.randomize()
+#	needed_zone = round(rng.randf_range(1, 6))
+#
+#	while zone_sizes[int(needed_zone)][2] != false:
+#		needed_zone = round(rng.randf_range(1, 6))
+#
+#	position_to_go_to.x = rng.randf_range(0, zone_sizes[int(needed_zone)][0].x)
+#	position_to_go_to.y = rng.randf_range(0, zone_sizes[int(needed_zone)][0].y)
+#
+#	position_to_go_to.x += zone_sizes[int(needed_zone)][1].x
+#	position_to_go_to.y += zone_sizes[int(needed_zone)][1].y
+#
+#	$Line2D.add_point(position_to_go_to)
+#	$Line2D.add_point(position_to_go_to + Vector2(20, 20))
+#	return position_to_go_to
 
 
-func pick_random_point():
-	var position_to_go_to = Vector2.ZERO
-	var needed_zone = 0
-	
-	var rng = RandomNumberGenerator.new()
-	rng.randomize()
-	needed_zone = round(rng.randf_range(1, 6))
-	
-	while zone_sizes[int(needed_zone)][2] != false:
-		needed_zone = round(rng.randf_range(1, 6))
-	
-	position_to_go_to.x = rng.randf_range(0, zone_sizes[int(needed_zone)][0].x)
-	position_to_go_to.y = rng.randf_range(0, zone_sizes[int(needed_zone)][0].y)
-	
-	position_to_go_to.x += zone_sizes[int(needed_zone)][1].x
-	position_to_go_to.y += zone_sizes[int(needed_zone)][1].y
+func generate_path(path_to_move_to):
+	path = levelNavigation.get_simple_path(hero.global_position, path_to_move_to, false)
+	print(path)
+#	$Line2D.points = path
+
+
+func navigate():
+	if path.size() > 0:
+		velocity = hero.global_position.direction_to(path[1]) * SPEED
+		
+		if hero.global_position == path[0]:
+			path.pop_front()
 	
-	return position_to_go_to
+#	print(path[0])
+#	print(hero.global_position)
 
 
 func move_to_random_point():
-	var position = pick_random_point()
-	is_moving = true
+#	is_moving = true
+	velocity = hero.move_and_slide(velocity)
 
 
 func _physics_process(delta):
-	if is_moving == false:
-		move_to_random_point()
-
 	if Input.is_action_just_pressed("left_mouse_button"):
-		pass
+		position = hero.get_global_mouse_position()
+#	print(velocity)
+#	$Line2D.global_position = Vector2.ZERO
+	if hero != null and levelNavigation != null:
+		if position != null:
+#			is_moving = true
+			generate_path(position)
+			print(position)
+			
+		else:
+			navigate()
+	
+	move_to_random_point()

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 57 - 62
godot_project/test_scene.tscn


Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů