From 3cf7e8c73aee9e1ebe3fa6fd187a65427ef7d62a Mon Sep 17 00:00:00 2001 From: Christopho Date: Sat, 28 Jul 2018 21:47:33 +0200 Subject: [PATCH] Fix enemy:on_restarted() called twice Closes #1179. It was called both when the map started and when the map opening transition finished. --- changelog.txt | 1 + src/entities/Enemy.cpp | 4 --- tests/CMakeLists.txt | 1 + .../bugs/1179_on_restarted_called_twice.lua | 14 ++++++++ .../1179_enemy_on_restarted_called_twice.dat | 36 +++++++++++++++++++ .../1179_enemy_on_restarted_called_twice.lua | 10 ++++++ tests/testing_quest/data/project_db.dat | 2 ++ 7 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 tests/testing_quest/data/enemies/bugs/1179_on_restarted_called_twice.lua create mode 100644 tests/testing_quest/data/maps/bugs/1179_enemy_on_restarted_called_twice.dat create mode 100644 tests/testing_quest/data/maps/bugs/1179_enemy_on_restarted_called_twice.lua diff --git a/changelog.txt b/changelog.txt index 7a3bd4d73..6da9f4c63 100644 --- a/changelog.txt +++ b/changelog.txt @@ -32,6 +32,7 @@ Engine changes * Fix hero shield sprite directions in animation sword (#1185). * Fix facing entity randomly chosen when the hero faces multiple ones (#1042). * Fix blocks not falling into water or lava (#1214). +* Fix enemy:on_restarted() called twice (#1179). * Fix removed entity still drawn during scrolling transition (#1193). * Fix sprite parameters order in custom entity collision callback (#1162). * Fix sol.file.exists() returning false for directories. diff --git a/src/entities/Enemy.cpp b/src/entities/Enemy.cpp index c5bcbe1ca..3e5828ee0 100644 --- a/src/entities/Enemy.cpp +++ b/src/entities/Enemy.cpp @@ -212,10 +212,6 @@ void Enemy::notify_created() { for (const SpritePtr& sprite: get_sprites()) { sprite->set_current_direction(initial_direction); } - - if (is_enabled()) { - restart(); - } } /** diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e0e308b36..4514dcfd9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -75,6 +75,7 @@ set(lua_test_maps "bugs/1162_custom_entity_collision_wrong_order" "bugs/1163_circular_movement_get_direction4" "bugs/1171_sword_knowledge_on_ability_used" + "bugs/1179_enemy_on_restarted_called_twice" "bugs/1181_hurt_enemy_after_custom_attack" "bugs/1193_entity_drawn_after_removed/map_1" "bugs/1195_enemy_dying_animation_never_finishes" diff --git a/tests/testing_quest/data/enemies/bugs/1179_on_restarted_called_twice.lua b/tests/testing_quest/data/enemies/bugs/1179_on_restarted_called_twice.lua new file mode 100644 index 000000000..58559d8c6 --- /dev/null +++ b/tests/testing_quest/data/enemies/bugs/1179_on_restarted_called_twice.lua @@ -0,0 +1,14 @@ + +local enemy = ... +local game = enemy:get_game() + +local count = 0 + +function enemy:on_restarted() + + count = count + 1 +end + +function enemy:get_on_restarted_count() + return count +end diff --git a/tests/testing_quest/data/maps/bugs/1179_enemy_on_restarted_called_twice.dat b/tests/testing_quest/data/maps/bugs/1179_enemy_on_restarted_called_twice.dat new file mode 100644 index 000000000..e03ea9fd8 --- /dev/null +++ b/tests/testing_quest/data/maps/bugs/1179_enemy_on_restarted_called_twice.dat @@ -0,0 +1,36 @@ +properties{ + x = 0, + y = 0, + width = 320, + height = 240, + min_layer = 0, + max_layer = 2, + tileset = "castle", +} + +tile{ + layer = 0, + x = 0, + y = 0, + width = 320, + height = 240, + pattern = "3", +} + +enemy{ + name = "enemy", + layer = 0, + x = 24, + y = 61, + direction = 0, + breed = "bugs/1179_on_restarted_called_twice", +} + +destination{ + name = "destination", + layer = 0, + x = 24, + y = 29, + direction = 1, +} + diff --git a/tests/testing_quest/data/maps/bugs/1179_enemy_on_restarted_called_twice.lua b/tests/testing_quest/data/maps/bugs/1179_enemy_on_restarted_called_twice.lua new file mode 100644 index 000000000..27cf13818 --- /dev/null +++ b/tests/testing_quest/data/maps/bugs/1179_enemy_on_restarted_called_twice.lua @@ -0,0 +1,10 @@ +local map = ... +local game = map:get_game() + +function map:on_opening_transition_finished() + + sol.timer.start(map, 100, function() + assert_equal(enemy:get_on_restarted_count(), 1) + sol.main.exit() + end) +end diff --git a/tests/testing_quest/data/project_db.dat b/tests/testing_quest/data/project_db.dat index 7512004b1..072dc43a4 100644 --- a/tests/testing_quest/data/project_db.dat +++ b/tests/testing_quest/data/project_db.dat @@ -13,6 +13,7 @@ map{ id = "bugs/1158_entity_timer_suspended", description = "#1158: timer:set_su map{ id = "bugs/1162_custom_entity_collision_wrong_order", description = "#1162: Wrong sprite order in custom entity collision callback" } map{ id = "bugs/1163_circular_movement_get_direction4", description = "#1163: circular_movement:get_direction4() not working" } map{ id = "bugs/1171_sword_knowledge_on_ability_used", description = "#1171: Fix item:on_ability_used() not called with sword knowledge" } +map{ id = "bugs/1179_enemy_on_restarted_called_twice", description = "#1179: enemy:on_restarted() is called twice" } map{ id = "bugs/1181_hurt_enemy_after_custom_attack", description = "#1181: enemy:hurt() does not work after a custom attack" } map{ id = "bugs/1193_entity_drawn_after_removed/map_1", description = "#1193: Entity still drawn after removed" } map{ id = "bugs/1193_entity_drawn_after_removed/map_2", description = "Destination map" } @@ -278,6 +279,7 @@ item{ id = "item_no_dialog_sprite", description = "Item with no dialog and a spr item{ id = "non_saved_item", description = "Non-saved item" } item{ id = "sword", description = "Sword" } +enemy{ id = "bugs/1179_on_restarted_called_twice", description = "#1179: enemy:on_restarted() is called twice" } enemy{ id = "slime_green", description = "Green Slime" } enemy{ id = "test_enemy", description = "Test enemy" } enemy{ id = "test_flying_enemy", description = "Flying enemy" }