diff --git a/systems/maps_manager.gd b/systems/maps_manager.gd index cfce293..30b2740 100644 --- a/systems/maps_manager.gd +++ b/systems/maps_manager.gd @@ -15,7 +15,7 @@ ## Manage environment settings and terrain maps within a scene. extends Node -## The default [Environment] used by the current scene in case the [Terrain3D] +## The default [Environment] used by the current scene in case the [Terrain3D] ## node does not have a [WorldEnvironment] as one of its children. var environment := Environment.new() @@ -43,11 +43,11 @@ func get_player_spawn() -> Node3D: if current_map == null: push_error("MapsManager.current_map is null") return null - + if not current_map.has_node("PlayerSpawns"): push_warning("PlayerSpawns node not found in MapsManager.current_map") return null - + var player_spawns : Node = current_map.get_node("PlayerSpawns") var spawn_count : int = player_spawns.get_child_count() if spawn_count == 0: diff --git a/tests/test_flag.gd b/tests/test_flag.gd new file mode 100644 index 0000000..78a87e7 --- /dev/null +++ b/tests/test_flag.gd @@ -0,0 +1,125 @@ +# This file is part of open-fpsz. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +extends GutTest + +var FLAG : PackedScene = preload("res://entities/flag/flag.tscn") +var PLAYER : PackedScene = preload("res://entities/player/player.tscn") + +var _subject : Flag + +func before_each() -> void: + _subject = FLAG.instantiate() + watch_signals(_subject) + add_child_autofree(_subject) + +func _is_mesh_visible() -> bool: + var mesh : Node3D = _subject.find_child("Mesh") + return mesh.is_visible() + +func test_that_flag_is_on_stand_by_default() -> void: + assert_eq(_subject.flag_state, Flag.FlagState.FLAG_STATE_ON_STAND) + +func test_that_mesh_is_visible_by_default() -> void: + assert_true(_is_mesh_visible()) + +func test_that_flag_on_stand_can_be_grabbed() -> void: + _subject.flag_state = Flag.FlagState.FLAG_STATE_ON_STAND + assert_true(_subject.can_be_grabbed()) + +func test_that_dropped_flag_can_be_grabbed() -> void: + _subject.flag_state = Flag.FlagState.FLAG_STATE_DROPPED + assert_true(_subject.can_be_grabbed()) + +func test_that_taken_flag_cannot_be_grabbed() -> void: + _subject.flag_state = Flag.FlagState.FLAG_STATE_TAKEN + assert_false(_subject.can_be_grabbed()) + +func test_that_on_stand_flag_grab_emits_grab_signal_and_changes_state() -> void: + _subject.flag_state = Flag.FlagState.FLAG_STATE_ON_STAND + var player : Player = PLAYER.instantiate() + _subject.grab(player) + assert_signal_emitted_with_parameters(_subject, 'grabbed', [player]) + assert_eq(_subject.flag_state, Flag.FlagState.FLAG_STATE_TAKEN) + assert_eq(_subject.last_carrier, player) + player.free() + +func test_that_dropped_flag_grab_emits_grab_signal_and_changes_state() -> void: + _subject.flag_state = Flag.FlagState.FLAG_STATE_DROPPED + var player : Player = PLAYER.instantiate() + _subject.grab(player) + assert_signal_emitted_with_parameters(_subject, 'grabbed', [player]) + assert_eq(_subject.flag_state, Flag.FlagState.FLAG_STATE_TAKEN) + assert_eq(_subject.last_carrier, player) + player.free() + +func test_that_taken_flag_grab_does_not_emit_grab_signal_and_keeps_state() -> void: + _subject.flag_state = Flag.FlagState.FLAG_STATE_TAKEN + _subject.grab(null) # doesn't matter that it's null in this case + assert_signal_not_emitted(_subject, 'grabbed') + assert_eq(_subject.flag_state, Flag.FlagState.FLAG_STATE_TAKEN) + assert_null(_subject.last_carrier) + +func test_that_flag_grab_hides_mesh() -> void: + var player : Player = PLAYER.instantiate() + _subject.grab(player) + assert_false(_is_mesh_visible()) + player.free() + +func test_that_taken_flag_drop_emits_signal_and_changes_state() -> void: + var player : Player = PLAYER.instantiate() + _subject.grab(player) + _subject.drop(player) + assert_signal_emitted_with_parameters(_subject, 'dropped', [player]) + assert_eq(_subject.flag_state, Flag.FlagState.FLAG_STATE_DROPPED) + player.free() + +func test_that_on_stand_flag_drop_does_not_emit_signals_and_keeps_state() -> void: + var player : Player = PLAYER.instantiate() + _subject.drop(player) + assert_signal_not_emitted(_subject, 'dropped') + assert_eq(_subject.flag_state, Flag.FlagState.FLAG_STATE_ON_STAND) + player.free() + +func test_that_flag_drop_shows_mesh() -> void: + var player : Player = PLAYER.instantiate() + _subject.grab(player) + _subject.drop(player) + assert_true(_is_mesh_visible()) + player.free() + +func test_that_impossible_regrab_does_not_emit_regrabbed_signal() -> void: + var player : Player = PLAYER.instantiate() + _subject.grab(player) + _subject.grab(player) + assert_signal_not_emitted(_subject, 'regrabbed') + player.free() + +func test_that_regrab_by_same_player_emits_regrabbed_signal() -> void: + var player : Player = PLAYER.instantiate() + _subject.grab(player) + _subject.drop(player) + _subject.grab(player) + assert_signal_emitted_with_parameters(_subject, 'regrabbed', [player]) + player.free() + +func test_that_regrab_by_different_player_does_not_emit_regrabbed_signal() -> void: + var player : Player = PLAYER.instantiate() + var different_player : Player = PLAYER.instantiate() + _subject.grab(player) + _subject.drop(player) + _subject.grab(different_player) + assert_signal_not_emitted(_subject, 'regrabbed') + player.free() + different_player.free() diff --git a/tests/test_health_component.gd b/tests/test_health_component.gd index 34287f9..8a3e2e0 100644 --- a/tests/test_health_component.gd +++ b/tests/test_health_component.gd @@ -27,10 +27,10 @@ func before_each() -> void: participant.player_id = TEST_PLAYER_ID participant.team_id = TEST_TEAM_ID _subject.match_participant_component = participant - add_child(_subject) + add_child_autofree(_subject) func after_each() -> void: - _subject.free() + _subject.match_participant_component.free() func test_that_it_has_max_health_when_ready() -> void: assert_eq(_subject.health, _subject.max_health) diff --git a/tests/test_scoreboard.gd b/tests/test_scoreboard.gd index 4c6819d..8bd62ab 100644 --- a/tests/test_scoreboard.gd +++ b/tests/test_scoreboard.gd @@ -20,10 +20,7 @@ var _subject : Scoreboard func before_each() -> void: _subject = SCOREBOARD.instantiate() - add_child(_subject) - -func after_each() -> void: - _subject.free() + add_child_autofree(_subject) func test_that_new_scoreboard_is_empty() -> void: assert_eq(_subject._entries, {})