From 4e956c74c62a8662e27671b09b57ca95faa31056 Mon Sep 17 00:00:00 2001 From: fheinrich <fheinrich@techfak.uni-bielefeld.de> Date: Wed, 6 Dec 2023 13:20:16 +0100 Subject: [PATCH] Slight refactor, added testing for pickup, cut tomato --- overcooked_simulator/main.py | 4 +- overcooked_simulator/pygame_gui/__init__.py | 0 .../{ => pygame_gui}/images/tomato.png | Bin .../{ => pygame_gui}/images/tomato_cut.png | Bin .../{ => pygame_gui}/pygame_gui.py | 4 +- overcooked_simulator/simulation_runner.py | 2 +- tests/test_start.py | 104 +++++++++++++++++- 7 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 overcooked_simulator/pygame_gui/__init__.py rename overcooked_simulator/{ => pygame_gui}/images/tomato.png (100%) rename overcooked_simulator/{ => pygame_gui}/images/tomato_cut.png (100%) rename overcooked_simulator/{ => pygame_gui}/pygame_gui.py (98%) diff --git a/overcooked_simulator/main.py b/overcooked_simulator/main.py index 48b34e3c..eae75879 100644 --- a/overcooked_simulator/main.py +++ b/overcooked_simulator/main.py @@ -3,12 +3,12 @@ from pathlib import Path from overcooked_simulator.game_items import Tomato from overcooked_simulator.player import Player -from overcooked_simulator.pygame_gui import PyGameGUI +from overcooked_simulator.pygame_gui.pygame_gui import PyGameGUI from overcooked_simulator.simulation_runner import Simulator def main(): - simulator = Simulator(Path("overcooked_simulator/layouts/basic.layout"), 600) + simulator = Simulator(Path("layouts/basic.layout"), 600) simulator.register_player(Player("p1", [100, 200])) simulator.register_player(Player("p2", [200, 100])) diff --git a/overcooked_simulator/pygame_gui/__init__.py b/overcooked_simulator/pygame_gui/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/overcooked_simulator/images/tomato.png b/overcooked_simulator/pygame_gui/images/tomato.png similarity index 100% rename from overcooked_simulator/images/tomato.png rename to overcooked_simulator/pygame_gui/images/tomato.png diff --git a/overcooked_simulator/images/tomato_cut.png b/overcooked_simulator/pygame_gui/images/tomato_cut.png similarity index 100% rename from overcooked_simulator/images/tomato_cut.png rename to overcooked_simulator/pygame_gui/images/tomato_cut.png diff --git a/overcooked_simulator/pygame_gui.py b/overcooked_simulator/pygame_gui/pygame_gui.py similarity index 98% rename from overcooked_simulator/pygame_gui.py rename to overcooked_simulator/pygame_gui/pygame_gui.py index a812d6f5..f33a49b5 100644 --- a/overcooked_simulator/pygame_gui.py +++ b/overcooked_simulator/pygame_gui/pygame_gui.py @@ -165,11 +165,11 @@ class PyGameGUI: if isinstance(item, Tomato): if item.finished: IMAGE = pygame.image.load( - "overcooked_simulator/images/tomato_cut.png" + "overcooked_simulator/pygame_gui/images/tomato_cut.png" ).convert_alpha() # or .convert_alpha() else: IMAGE = pygame.image.load( - "overcooked_simulator/images/tomato.png" + "overcooked_simulator/pygame_gui/images/tomato.png" ).convert_alpha() # or .convert_alpha() rect = IMAGE.get_rect() rect.center = pos diff --git a/overcooked_simulator/simulation_runner.py b/overcooked_simulator/simulation_runner.py index 470330eb..74b5b492 100644 --- a/overcooked_simulator/simulation_runner.py +++ b/overcooked_simulator/simulation_runner.py @@ -63,7 +63,7 @@ class Simulator(Thread): Args: player: The player to be added. """ - print(f"Added player {player.name} to the game.") + # print(f"Added player {player.name} to the game.") self.env.players[player.name] = player diff --git a/tests/test_start.py b/tests/test_start.py index 84ca938c..bae42abb 100644 --- a/tests/test_start.py +++ b/tests/test_start.py @@ -3,7 +3,8 @@ from pathlib import Path import numpy as np -from overcooked_simulator.counters import Counter +from overcooked_simulator.counters import Counter, CuttingBoard +from overcooked_simulator.game_items import Tomato from overcooked_simulator.overcooked_environment import Action from overcooked_simulator.player import Player from overcooked_simulator.simulation_runner import Simulator @@ -46,7 +47,7 @@ def test_simulator_frequency(): self.c += 1 frequency = 2000 - running_time_seconds = 3 + running_time_seconds = 2 sim = Simulator(layouts_folder / "basic.layout", frequency) sim.env = TestEnv() # Overwrite environment with a simple counting env @@ -77,7 +78,7 @@ def test_movement(): expected = start_pos + do_moves_number * (move_direction * player1.move_dist) assert ( - np.linalg.norm(expected - sim.env.players[player_name].pos) == 0 + np.linalg.norm(expected - sim.env.players[player_name].pos) == 0 ), "Should be here?" @@ -111,3 +112,100 @@ def test_collision_detection(): player1 ), "Player collides with world bounds." sim.stop() + + +def test_player_reach(): + sim = Simulator(layouts_folder / "empty.layout", 200) + counter_pos = np.array([100, 100]) + counter = Counter(counter_pos) + sim.env.counters = [counter] + player = Player("p1", np.array([100, 200])) + sim.register_player(player) + assert not player.can_reach(counter), "Player is too far away." + + do_moves_number = 30 + for i in range(do_moves_number): + move_action = Action("p1", "movement", np.array([0, -1])) + sim.enter_action(move_action) + assert player.can_reach(counter), "Player can reach counter?" + + +def test_pickup(): + sim = Simulator(layouts_folder / "empty.layout", 200) + + counter_pos = np.array([100, 100]) + counter = Counter(counter_pos) + counter.occupied_by = Tomato() + sim.env.counters = [counter] + + player = Player("p1", np.array([100, 160])) + sim.register_player(player) + + move_down = Action("p1", "movement", np.array([0, -1])) + move_up = Action("p1", "movement", np.array([0, 1])) + pick = Action("p1", "pickup", "pickup") + + sim.enter_action(move_down) + assert player.can_reach(counter), "Player can reach counter?" + + sim.enter_action(pick) + assert player.holding is not None, "Player should have picked up tomato." + assert isinstance(player.holding, Tomato), "Should be tomato." + + sim.enter_action(move_up) + sim.enter_action(move_up) + sim.enter_action(move_up) + sim.enter_action(move_up) + + sim.enter_action(pick) + assert ( + player.holding is not None + ), "Player should be too far away to put tomato down." + + sim.enter_action(move_down) + sim.enter_action(move_down) + sim.enter_action(move_down) + sim.enter_action(move_down) + + sim.enter_action(pick) + + assert player.holding is None, "Player should have put tomato down." + assert counter.occupied_by is not None and isinstance( + counter.occupied_by, Tomato + ), "Tomato should be here now." + + +def test_processing(): + sim_frequency = 1000 + sim = Simulator(layouts_folder / "empty.layout", sim_frequency) + sim.start() + + counter_pos = np.array([100, 100]) + counter = CuttingBoard(counter_pos) + sim.env.counters = [counter] + + player = Player("p1", np.array([100, 150])) + tomato = Tomato() + player.holding = tomato + sim.register_player(player) + + move = Action("p1", "movement", np.array([0, -1])) + pick = Action("p1", "pickup", "pickup") + + sim.enter_action(move) + sim.enter_action(pick) + + hold_down = Action("p1", "interact", "keydown") + sim.enter_action(hold_down) + + assert not tomato.finished, "Tomato is not finished yet." + + sleep_time = (tomato.steps_needed / sim_frequency) + 0.1 + time.sleep(sleep_time) + + assert tomato.finished, "Tomato should be finished." + + button_up = Action("p1", "interact", "keyup") + sim.enter_action(button_up) + + sim.stop() -- GitLab