def click_agent(self, cell_row_col): """ The user has clicked on a cell - * If there is an agent, select it * If that agent was already selected, then deselect it * If there is no agent selected, and no agent in the cell, create one * If there is an agent selected, and no agent in the cell, move the selected agent to the cell """ # Has the user clicked on an existing agent? agent_idx = self.find_agent_at(cell_row_col) # This is in case we still have a selected agent even though the env has been recreated # with no agents. if (self.selected_agent is not None) and (self.selected_agent > len(self.env.agents)): self.selected_agent = None # Defensive coding below - for cell_row_col to be a tuple, not a numpy array: # numpy array breaks various things when loading the env. if agent_idx is None: # No if self.selected_agent is None: # Create a new agent and select it. agent = EnvAgent(initial_position=tuple(cell_row_col), initial_direction=0, direction=0, target=tuple(cell_row_col), moving=False, ) self.selected_agent = self.env.add_agent(agent) # self.env.set_agent_active(agent) self.view.oRT.update_background() else: # Move the selected agent to this cell agent = self.env.agents[self.selected_agent] agent.initial_position = tuple(cell_row_col) agent.position = tuple(cell_row_col) agent.old_position = tuple(cell_row_col) else: # Yes # Have they clicked on the agent already selected? if self.selected_agent is not None and agent_idx == self.selected_agent: # Yes - deselect the agent self.selected_agent = None else: # No - select the agent self.selected_agent = agent_idx self.redraw()
def click_agent(self, cell_row_col): """ The user has clicked on a cell - * If there is an agent, select it * If that agent was already selected, then deselect it * If there is no agent selected, and no agent in the cell, create one * If there is an agent selected, and no agent in the cell, move the selected agent to the cell """ # Has the user clicked on an existing agent? agent_idx = self.find_agent_at(cell_row_col) if agent_idx is None: # No if self.selected_agent is None: # Create a new agent and select it. agent = EnvAgent(position=cell_row_col, direction=0, target=cell_row_col, moving=False) self.selected_agent = self.env.add_agent(agent) self.view.oRT.update_background() else: # Move the selected agent to this cell agent = self.env.agents[self.selected_agent] agent.position = cell_row_col agent.old_position = cell_row_col else: # Yes # Have they clicked on the agent already selected? if self.selected_agent is not None and agent_idx == self.selected_agent: # Yes - deselect the agent self.selected_agent = None else: # No - select the agent self.selected_agent = agent_idx self.redraw()