Browse Source

working on physics, fixing death crash bug

galo 1 year ago
parent
commit
149eba555a
5 changed files with 36 additions and 18 deletions
  1. 13 9
      Enemy.gd
  2. 16 3
      Enemy.tscn
  3. 5 5
      main.tscn
  4. 1 0
      playerphysics.gd
  5. 1 1
      playerphysics.tscn

+ 13 - 9
Enemy.gd

@@ -14,13 +14,6 @@ var motion = Vector2()
 var target
 onready var reaction_time = int(rand_range(20,60))
 
-func _ready():
-#	var array = get_node('../../players').get_children()
-	var array = get_tree().get_nodes_in_group('players')
-	array.shuffle()
-	if array.size() > 0:
-		target = array[0]
-
 var dir_buffer = 0
 
 var tick = 0
@@ -33,5 +26,16 @@ func _physics_process(delta):
 		if target == null: return
 		dir_buffer += 1
 		if fmod(dir_buffer,reaction_time) == 0:
-			if is_instance_valid(target): motion = (target.global_position - global_position).normalized() * speed
-		position += motion*delta*tickrate
+			if is_instance_valid(target): motion.x = (target.global_position - global_position).normalized().x * speed
+		motion.y += 15
+		if motion.y > 0 and is_on_floor(): motion.y = 0
+		move_and_slide(motion*tickrate,Vector2.UP)
+
+func _on_Area2D_body_entered(body):
+	print(body)
+	if body.is_in_group('players'):
+		var array = get_tree().get_nodes_in_group('players')
+		array.shuffle()
+		if array.size() > 0:
+			target = array[0]
+	pass # Replace with function body.

+ 16 - 3
Enemy.tscn

@@ -1,13 +1,16 @@
-[gd_scene load_steps=4 format=2]
+[gd_scene load_steps=5 format=2]
 
 [ext_resource path="res://Enemy.gd" type="Script" id=1]
 [ext_resource path="res://damage_enemy.tscn" type="PackedScene" id=2]
 
 [sub_resource type="RectangleShape2D" id=1]
 
+[sub_resource type="RectangleShape2D" id=2]
+extents = Vector2( 180, 100 )
+
 [node name="Enemy" type="KinematicBody2D" groups=["enemy"]]
-collision_layer = 4
-collision_mask = 0
+collision_layer = 12
+collision_mask = 8
 script = ExtResource( 1 )
 
 [node name="damage_enemy" parent="." instance=ExtResource( 2 )]
@@ -17,4 +20,14 @@ damage = -10
 [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
 shape = SubResource( 1 )
 
+[node name="Area2D" type="Area2D" parent="."]
+collision_layer = 0
+collision_mask = 2
+
+[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
+visible = false
+shape = SubResource( 2 )
+
+[connection signal="body_entered" from="Area2D" to="." method="_on_Area2D_body_entered"]
+
 [editable path="damage_enemy"]

File diff suppressed because it is too large
+ 5 - 5
main.tscn


+ 1 - 0
playerphysics.gd

@@ -47,6 +47,7 @@ func _physics_process(delta):
 	if motion.x != 0: sidebuffer = motion.x > 0
 	motion.y += 15
 	if motion.y < 0 and is_on_ceiling(): motion.y = 0
+	if motion.y > 0 and is_on_floor(): motion.y = 0
 	aux_bullet -= 1
 	match input2:
 		"ui_attack":

+ 1 - 1
playerphysics.tscn

@@ -6,7 +6,7 @@
 
 [node name="KinematicBody2D" type="KinematicBody2D" groups=["players"]]
 position = Vector2( 320, 180 )
-collision_layer = 2
+collision_layer = 10
 collision_mask = 0
 script = ExtResource( 1 )
 

Some files were not shown because too many files changed in this diff