From 861677bdf753a81b9b5b0783a47f9c1a3a0208af Mon Sep 17 00:00:00 2001 From: Joshua Haustein Date: Wed, 24 Nov 2021 23:48:43 +0000 Subject: [PATCH] Improved ant fighting --- entities/base_ant.gd | 16 +- entities/base_ant.tscn | 310 ++++++++++++++++++++++++++++++++- entities/coins.gd | 2 +- entities/npc_ant_controller.gd | 33 +++- 4 files changed, 338 insertions(+), 23 deletions(-) diff --git a/entities/base_ant.gd b/entities/base_ant.gd index 8a080e8..98d9fc4 100644 --- a/entities/base_ant.gd +++ b/entities/base_ant.gd @@ -92,14 +92,14 @@ func _process(_delta: float) -> void: set_state(State.WALKING) func _update_animation(dir: Vector2) -> void: - if dir.x > 0: - active_sprite.flip_h = false - - if dir.x < 0: - active_sprite.flip_h = true - - if !is_equal_approx(velocity.length(), 0): - rotation = atan2(velocity.y, velocity.x) + PI/2 + if _current_state == State.ATTACKING: + rotation = atan2(_dir.y, _dir.x) + PI/2 + else: + active_sprite.flip_h = dir.x > 0 + active_sprite.flip_h = dir.x < 0 + if !is_equal_approx(velocity.length(), 0): + rotation = atan2(velocity.y, velocity.x) + PI/2 + if _current_state != State.DEAD or not _death_animation_finished: $AnimationPlayer.play(STATE_NAMES[_current_state]) diff --git a/entities/base_ant.tscn b/entities/base_ant.tscn index 6e30d21..8122a58 100644 --- a/entities/base_ant.tscn +++ b/entities/base_ant.tscn @@ -143,6 +143,54 @@ tracks/10/keys = { "update": 0, "values": [ Color( 1, 1, 1, 1 ) ] } +tracks/11/type = "value" +tracks/11/path = NodePath("Default:position") +tracks/11/interp = 1 +tracks/11/loop_wrap = true +tracks/11/imported = false +tracks/11/enabled = true +tracks/11/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Vector2( 5.96046e-08, 0 ) ] +} +tracks/12/type = "value" +tracks/12/path = NodePath("Miner:position") +tracks/12/interp = 1 +tracks/12/loop_wrap = true +tracks/12/imported = false +tracks/12/enabled = true +tracks/12/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Vector2( 9.83477e-07, -8.04663e-07 ) ] +} +tracks/13/type = "value" +tracks/13/path = NodePath("Miner:rotation_degrees") +tracks/13/interp = 1 +tracks/13/loop_wrap = true +tracks/13/imported = false +tracks/13/enabled = true +tracks/13/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ 0.0 ] +} +tracks/14/type = "value" +tracks/14/path = NodePath("Chief:rotation_degrees") +tracks/14/interp = 1 +tracks/14/loop_wrap = true +tracks/14/imported = false +tracks/14/enabled = true +tracks/14/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ 0.0 ] +} [sub_resource type="Animation" id=45] resource_name = "attacking" @@ -227,10 +275,22 @@ tracks/6/loop_wrap = true tracks/6/imported = false tracks/6/enabled = true tracks/6/keys = { -"times": PoolRealArray( 0, 0.48 ), -"transitions": PoolRealArray( 61.8199, 42.2243 ), -"update": 0, -"values": [ Vector2( 0, 0 ), Vector2( 0, -2 ) ] +"times": PoolRealArray( 0, 0.52, 0.8 ), +"transitions": PoolRealArray( 61.8199, 42.2243, 1 ), +"update": 1, +"values": [ Vector2( 0, 0 ), Vector2( 0, -15 ), Vector2( 0, 0 ) ] +} +tracks/7/type = "value" +tracks/7/path = NodePath("Default:position") +tracks/7/interp = 1 +tracks/7/loop_wrap = true +tracks/7/imported = false +tracks/7/enabled = true +tracks/7/keys = { +"times": PoolRealArray( 0, 0.52, 0.8 ), +"transitions": PoolRealArray( 132.514, 73.5168, 1 ), +"update": 1, +"values": [ Vector2( 5.96046e-08, 0 ), Vector2( 0, -15 ), Vector2( 0, 0 ) ] } [sub_resource type="Animation" id=51] @@ -312,6 +372,54 @@ tracks/4/keys = { "update": 0, "values": [ Color( 1, 1, 1, 1 ), Color( 0.32549, 0.113725, 0.113725, 1 ) ] } +tracks/5/type = "value" +tracks/5/path = NodePath("Default:position") +tracks/5/interp = 1 +tracks/5/loop_wrap = true +tracks/5/imported = false +tracks/5/enabled = true +tracks/5/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Vector2( 5.96046e-08, 0 ) ] +} +tracks/6/type = "value" +tracks/6/path = NodePath("Default:rotation_degrees") +tracks/6/interp = 1 +tracks/6/loop_wrap = true +tracks/6/imported = false +tracks/6/enabled = true +tracks/6/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ 0.0 ] +} +tracks/7/type = "value" +tracks/7/path = NodePath("Chief:position") +tracks/7/interp = 1 +tracks/7/loop_wrap = true +tracks/7/imported = false +tracks/7/enabled = true +tracks/7/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Vector2( 0, 0 ) ] +} +tracks/8/type = "value" +tracks/8/path = NodePath("Chief:rotation_degrees") +tracks/8/interp = 1 +tracks/8/loop_wrap = true +tracks/8/imported = false +tracks/8/enabled = true +tracks/8/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ 0.0 ] +} [sub_resource type="Animation" id=50] resource_name = "hurting" @@ -417,6 +525,102 @@ tracks/3/keys = { "update": 1, "values": [ 0 ] } +tracks/4/type = "value" +tracks/4/path = NodePath("Chief:position") +tracks/4/interp = 1 +tracks/4/loop_wrap = true +tracks/4/imported = false +tracks/4/enabled = true +tracks/4/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Vector2( 0, 0 ) ] +} +tracks/5/type = "value" +tracks/5/path = NodePath("Chief:rotation_degrees") +tracks/5/interp = 1 +tracks/5/loop_wrap = true +tracks/5/imported = false +tracks/5/enabled = true +tracks/5/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ 0.0 ] +} +tracks/6/type = "value" +tracks/6/path = NodePath("Enemy:position") +tracks/6/interp = 1 +tracks/6/loop_wrap = true +tracks/6/imported = false +tracks/6/enabled = true +tracks/6/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Vector2( 0, 0 ) ] +} +tracks/7/type = "value" +tracks/7/path = NodePath("Enemy:rotation_degrees") +tracks/7/interp = 1 +tracks/7/loop_wrap = true +tracks/7/imported = false +tracks/7/enabled = true +tracks/7/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ 0.0 ] +} +tracks/8/type = "value" +tracks/8/path = NodePath("Miner:position") +tracks/8/interp = 1 +tracks/8/loop_wrap = true +tracks/8/imported = false +tracks/8/enabled = true +tracks/8/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Vector2( 9.83477e-07, -8.04663e-07 ) ] +} +tracks/9/type = "value" +tracks/9/path = NodePath("Miner:rotation_degrees") +tracks/9/interp = 1 +tracks/9/loop_wrap = true +tracks/9/imported = false +tracks/9/enabled = true +tracks/9/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ 0.0 ] +} +tracks/10/type = "value" +tracks/10/path = NodePath("Default:position") +tracks/10/interp = 1 +tracks/10/loop_wrap = true +tracks/10/imported = false +tracks/10/enabled = true +tracks/10/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Vector2( 5.96046e-08, 0 ) ] +} +tracks/11/type = "value" +tracks/11/path = NodePath("Default:rotation_degrees") +tracks/11/interp = 1 +tracks/11/loop_wrap = true +tracks/11/imported = false +tracks/11/enabled = true +tracks/11/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ 0.0 ] +} [sub_resource type="Animation" id=46] resource_name = "walking" @@ -470,11 +674,108 @@ tracks/3/keys = { "update": 1, "values": [ 0, 1, 2, 3 ] } +tracks/4/type = "value" +tracks/4/path = NodePath("Default:position") +tracks/4/interp = 1 +tracks/4/loop_wrap = true +tracks/4/imported = false +tracks/4/enabled = true +tracks/4/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Vector2( 5.96046e-08, 0 ) ] +} +tracks/5/type = "value" +tracks/5/path = NodePath("Default:rotation_degrees") +tracks/5/interp = 1 +tracks/5/loop_wrap = true +tracks/5/imported = false +tracks/5/enabled = true +tracks/5/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ 0.0 ] +} +tracks/6/type = "value" +tracks/6/path = NodePath("Miner:position") +tracks/6/interp = 1 +tracks/6/loop_wrap = true +tracks/6/imported = false +tracks/6/enabled = true +tracks/6/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Vector2( 9.83477e-07, -8.04663e-07 ) ] +} +tracks/7/type = "value" +tracks/7/path = NodePath("Miner:rotation_degrees") +tracks/7/interp = 1 +tracks/7/loop_wrap = true +tracks/7/imported = false +tracks/7/enabled = true +tracks/7/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ 0.0 ] +} +tracks/8/type = "value" +tracks/8/path = NodePath("Enemy:position") +tracks/8/interp = 1 +tracks/8/loop_wrap = true +tracks/8/imported = false +tracks/8/enabled = true +tracks/8/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Vector2( 0, 0 ) ] +} +tracks/9/type = "value" +tracks/9/path = NodePath("Enemy:rotation_degrees") +tracks/9/interp = 1 +tracks/9/loop_wrap = true +tracks/9/imported = false +tracks/9/enabled = true +tracks/9/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ 0.0 ] +} +tracks/10/type = "value" +tracks/10/path = NodePath("Chief:position") +tracks/10/interp = 1 +tracks/10/loop_wrap = true +tracks/10/imported = false +tracks/10/enabled = true +tracks/10/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Vector2( 0, 0 ) ] +} +tracks/11/type = "value" +tracks/11/path = NodePath("Chief:rotation_degrees") +tracks/11/interp = 1 +tracks/11/loop_wrap = true +tracks/11/imported = false +tracks/11/enabled = true +tracks/11/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ 0.0 ] +} [node name="base_ant" type="KinematicBody2D"] scale = Vector2( 0.4, 0.4 ) collision_mask = 12 script = ExtResource( 5 ) +hit_points = 1 [node name="CollisionShape2D" type="CollisionShape2D" parent="."] rotation = 1.57254 @@ -522,7 +823,6 @@ anims/walking = SubResource( 46 ) visible = false scale = Vector2( 2, 2 ) texture = ExtResource( 3 ) -offset = Vector2( 0, 0.0650516 ) hframes = 4 [connection signal="body_entered" from="interaction_field" to="." method="_on_interaction_field_body_entered"] diff --git a/entities/coins.gd b/entities/coins.gd index f9d6d92..7739129 100644 --- a/entities/coins.gd +++ b/entities/coins.gd @@ -22,4 +22,4 @@ func collect(): queue_free() return 0 else: - return 1 \ No newline at end of file + return 1 diff --git a/entities/npc_ant_controller.gd b/entities/npc_ant_controller.gd index 09c5dbb..6f8e6d6 100644 --- a/entities/npc_ant_controller.gd +++ b/entities/npc_ant_controller.gd @@ -14,6 +14,7 @@ var _time_since_change := 0.0 var _sensed_pheromones: Array = [] var _resource_load: Dictionary = {'type': null, 'number': 0} var _limit_direction_flip: bool = false +var _enemy_target: BaseAnt = null func _ready(): _dir = _change_dir() @@ -37,9 +38,8 @@ func _physics_process(delta: float) -> void: _dir = _change_dir() set_state(State.WALKING) elif _current_state == State.ATTACKING: - if len(_bodies_in_interaction_field) > 0: - var relative_pos: Vector2 = _bodies_in_interaction_field[0].get_global_position() - get_global_position() - _dir = relative_pos.normalized() + var relative_pos: Vector2 = _enemy_target.get_global_position() - get_global_position() + _dir = relative_pos.normalized() if enable_debug_drawing: update() @@ -127,14 +127,29 @@ func _on_interaction_field_area_entered(area): _dir = -_dir _limit_direction_flip = false +func is_enemy(body): + return ( + not body.is_dead() and + ( + body.is_in_group("enemy_npc_ants") and self.is_in_group("npc_ants") or + (body.is_in_group("npc_ants") or body.is_in_group("player")) and self.is_in_group("enemy_npc_ants") + ) + ) + func _body_entered_interaction_field(body): if _current_state != State.WALKING and _current_state != State.IDLE: return - if ( - body.is_in_group("enemy_npc_ants") and self.is_in_group("npc_ants") or - (body.is_in_group("npc_ants") or body.is_in_group("player")) and self.is_in_group("enemy_npc_ants") - ): - if not body.is_dead(): + if (is_enemy(body)): + if _enemy_target == null: + _enemy_target = body set_state(State.ATTACKING) - print("Set attacking") +func _body_left_interaction_field(body): + if body == _enemy_target: + _enemy_target = null + for other_body in _bodies_in_interaction_field: + if is_enemy(other_body): + _enemy_target = other_body + break + if _current_state == State.ATTACKING and _enemy_target == null: + set_state(State.WALKING)