Skip to content

Commit

Permalink
Toggle signals on click
Browse files Browse the repository at this point in the history
  • Loading branch information
Godsmith committed Sep 15, 2023
1 parent a8f7dfa commit 9626b6d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
18 changes: 17 additions & 1 deletion tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ def test_creating_signal_creates_two_signal_blocks(self, game: Game):
.-.h.-.
""",
)
game.grid.create_signals_at_click_position(60, 15)
game.grid.toggle_signals_at_click_position(60, 15)
# Sort the blocks so that the test becomes predictable
blocks = sorted(
game.signal_controller._signal_blocks, key=lambda b: sorted(b.positions)[0]
Expand All @@ -870,6 +870,22 @@ def test_clicking_grid_in_signal_mode_creates_signal(self, game: Game):
game.on_left_click(61, 1)
assert len(game.grid.signals) == 2

def test_clicking_signal_in_signal_mode_removes_signal(self, game: Game):
create_objects(
game.grid,
"""
.h.
""",
)
game.gui.disable()
game.gui.mode = Mode.SIGNAL

assert len(game.grid.signals) == 2

game.on_left_click(45, 15)

assert len(game.grid.signals) == 0

def test_clicking_grid_in_signal_mode_when_no_rail_does_nothing(self, game: Game):
"""For code coverage."""
game.gui.disable()
Expand Down
2 changes: 1 addition & 1 deletion tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _create_rails(grid: Grid, lines: list[str]):
if character in "-h|v/\\":
grid.create_rail({Rail(x1, y1, x2, y2)})
if character in "hv":
grid.create_signals_at_grid_position(abs(x1 + x2) / 2, abs(y1 + y2) / 2)
grid.toggle_signals_at_grid_position(abs(x1 + x2) / 2, abs(y1 + y2) / 2)


def _remove_offset(lines: Iterable[str]):
Expand Down
2 changes: 1 addition & 1 deletion trainfinity2/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def on_left_click(self, x, y):
x, y
)
self.drawer.handle_events(
self.grid.create_signals_at_click_position(world_x_float, world_y_float)
self.grid.toggle_signals_at_click_position(world_x_float, world_y_float)
)
elif self.gui.mode == Mode.DESTROY:
self.grid.remove_rail(Vec2(world_x, world_y))
Expand Down
17 changes: 11 additions & 6 deletions trainfinity2/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,22 +392,27 @@ def distance_to_rail(rail: Rail, x: float, y: float):
closest_rail, distance = sorted(rails_and_distances, key=lambda x: x[1])[0]
return closest_rail if distance < 1 else None

def create_signals_at_click_position(
def toggle_signals_at_click_position(
self, world_x: float, world_y: float
) -> Sequence[Event]:
# Transpose half a box since rail coordinates are in the bottom left
# of each grid cell while they are visible in the middle
x = world_x - 0.5
y = world_y - 0.5
return self.create_signals_at_grid_position(x, y)
return self.toggle_signals_at_grid_position(x, y)

def create_signals_at_grid_position(self, x: float, y: float) -> Sequence[Event]:
def toggle_signals_at_grid_position(self, x: float, y: float) -> Sequence[Event]:
events = []
if rail := self._closest_rail(x, y):
for position in rail.positions:
signal = Signal(position, rail)
self.signals[(position, rail)] = signal
events.append(CreateEvent(signal))
signal = self.signals.get((position, rail))
if signal:
del self.signals[(position, rail)]
events.append(DestroyEvent(signal))
else:
signal = Signal(position, rail)
self.signals[(position, rail)] = signal
events.append(CreateEvent(signal))
self._signal_controller.create_signal_blocks(self, list(self.signals.values()))
return events

Expand Down

0 comments on commit 9626b6d

Please sign in to comment.