def ev_keydown(self, event: "tcod.event.KeyDown") -> Optional[Action]: """ Receive key press events and return an 'Action' or None if no valid key was pressed :param event: a key press :return: and 'Action' or None """ # action will default to None or be assigned an 'Action' action: Optional[Action] = None # key holds which key was pressed (doesn't include modifiers like shift or alt key = event.sym # create a 'MovementAction' with the desired direction if key == tcod.event.K_UP: action = MovementAction(dx=0, dy=-1) elif key == tcod.event.K_DOWN: action = MovementAction(dx=0, dy=1) elif key == tcod.event.K_LEFT: action = MovementAction(dx=-1, dy=0) elif key == tcod.event.K_RIGHT: action = MovementAction(dx=1, dy=0) # if the 'escape' key is pressed return an 'EscapeAction' elif key == tcod.event.K_ESCAPE: action = EscapeAction() # no valid key was pressed (returns None) return action
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]: action: Optional[Action] = None key = event.sym if key == tcod.event.K_UP or key == tcod.event.K_KP_8: action = MovementAction(dx=0, dy=-1) elif key == tcod.event.K_DOWN or key == tcod.event.K_KP_2: action = MovementAction(dx=0, dy=1) elif key == tcod.event.K_LEFT or key == tcod.event.K_KP_4: action = MovementAction(dx=-1, dy=0) elif key == tcod.event.K_RIGHT or key == tcod.event.K_KP_6: action = MovementAction(dx=1, dy=0) elif key == tcod.event.K_KP_7: action = MovementAction(dx=-1, dy=-1) elif key == tcod.event.K_KP_9: action = MovementAction(dx=1, dy=-1) elif key == tcod.event.K_KP_1: action = MovementAction(dx=-1, dy=1) elif key == tcod.event.K_KP_3: action = MovementAction(dx=1, dy=1) elif key == tcod.event.K_ESCAPE: action = EscapeAction() # No valid key pressed return action
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]: action: Optional[Action] = None key = event.sym if key == tcod.event.K_KP_8 or tcod.event.KP_w: action = MovementAction(dx=0, dy=-1) elif key == tcod.event.K_KP_2 or tcod.event.KP_x: action = MovementAction(dx=0, dy=1) elif key == tcod.event.K_KP_4 or tcod.event.KP_a: action = MovementAction(dx=-1, dy=0) elif key == tcod.event.K_KP_6 or tcod.event.KP_d: action = MovementAction(dx=1, dy=0) elif key == tcod.event.K_KP_7 or tcod.event.KP_q: action = MovementAction(dx=-1, dy=-1) elif key == tcod.event.K_KP_9 or tcod.event.KP_e: action = MovementAction(dx=1, dy=-1) elif key == tcod.event.K_KP_1 or tcod.event.KP_z: action = MovementAction(dx=-1, dy=1) elif key == tcod.event.K_KP_3 or tcod.event.KP_c: action = MovementAction(dx=1, dy=1) elif key == tcod.event.K_ESCAPE: action = EscapeAction() return action
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]: action: Optional[Action] = None key = event.sym if key == tcod.event.K_UP: action = MovementAction(dx=0, dy=-1) elif key == tcod.event.K_DOWN: action = MovementAction(dx=0, dy=1) elif key == tcod.event.K_LEFT: action = MovementAction(dx=-1, dy=0) elif key == tcod.event.K_RIGHT: action = MovementAction(dx=1, dy=0) elif key == tcod.event.K_ESCAPE: action = EscapeAction() return action
def perform(self) -> None: ''' - If not in the player's vision, wait until next turn. - If the player is right next to the enemy, attack the player. - If the player sees the enemy, but the enemy is too far away to attack, move closer to the player. ''' target = self.engine.player dx = target.x - self.entity.x dy = target.y - self.entity.y distance = max(abs(dx), abs(dy)) # "Chevyshev" distance if self.engine.game_map.visible[self.entity.x, self.entity.y]: if distance <= 1: return MeleeAction(self.entity, dx, dy).perform() # Update the enemy's path to chase after the player self.path = self.get_path_to(target.x, target.y) if self.path: dest_x, dest_y = self.path.pop(0) return MovementAction(self.entity, dest_x - self.entity.x, dest_y - self.entity.y).perform() # Wait until the next turn return WaitAction(self.entity).perform()
def ev_keydown(self, event: tcod.event.KeyDown) -> None: action = None key = event.sym if key == tcod.event.K_UP: action = MovementAction(0, -1) elif key == tcod.event.K_DOWN: action = MovementAction(0, 1) elif key == tcod.event.K_RIGHT: action = MovementAction(1, 0) elif key == tcod.event.K_LEFT: action = MovementAction(-1, 0) elif key == tcod.event.K_ESCAPE: action = EscapeAction() return action
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]: # holds the subclass of action that we assign it to, if none is assigned it will remain set to none action: Optional[Action] = None # holds the key press key = event.sym if key == tcod.event.K_UP: action = MovementAction(dx=0, dy=-1) elif key == tcod.event.K_DOWN: action = MovementAction(dx=0, dy=1) elif key == tcod.event.K_LEFT: action = MovementAction(dx=-1, dy=0) elif key == tcod.event.K_RIGHT: action = MovementAction(dx=1, dy=0) elif key == tcod.event.K_ESCAPE: action = EscapeAction() # No valid key was pressed return action
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]: action: Optional[Action] = None key = event.sym if key == tcod.event.K_UP: action = MovementAction(dx=0, dy=-1) elif key == tcod.event.K_DOWN: action = MovementAction(dx=0, dy=1) elif key == tcod.event.K_LEFT: action = MovementAction(dx=-1, dy=0) elif key == tcod.event.K_RIGHT: action = MovementAction(dx=1, dy=0) # alt+enter # if key.vk == tcod.KEY_ENTER and key.lalt: # return {'fullscreen': True} elif key == tcod.event.K_ESCAPE: action = EscapeAction() # no valid key pressed return action
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]: #action is the variable we use to hold whatever subclass of Action action: Optional[Action] = None key = event.sym #possible keys pressed if key == tcod.event.K_UP: action = MovementAction(dx=0, dy=-1) elif key == tcod.event.K_DOWN: action = MovementAction(dx=0, dy=1) elif key == tcod.event.K_LEFT: action = MovementAction(dx=-1, dy=0) elif key == tcod.event.K_RIGHT: action = MovementAction(dx=1, dy=0) #used to escape the game elif key == tcod.event.K_ESCAPE: action = EscapeAction() #No valid key was pressed return action
def perform(self) -> None: target = self.engine.player dx = target.x - self.entity.x dy = target.y - self.entity.y distance = max(abs(dx), abs(dy)) # Chebyshev distance. if self.engine.game_map.visible[self.entity.x, self.entity.y]: if distance <= 1: return MeleeAction(self.entity, dx, dy).perform() self.path = self.get_path_to(target.x, target.y) if self.path: dest_x, dest_y = self.path.pop(0) return MovementAction(self.entity, dest_x - self.entity.x, dest_y - self.entity.y).perform() return WaitAction(self.entity).perform()
def perform(self) -> None: target = self.engine.player dx = target.x - self.entity.x dy = target.y - self.entity.y distance = max(abs(dx), abs(dy)) # Расстояние Чебышёва (максимум модуля разности компонент n-мерных числовых векторов) if self.engine.game_map.visible[self.entity.x, self.entity.y]: if distance <= 1: return MeleeAction(self.entity, dx, dy).perform() self.path = self.get_path_to(target.x, target.y) if self.path: dest_x, dest_y = self.path.pop(0) return MovementAction( self.entity, dest_x - self.entity.x, dest_y - self.entity.y, ).perform() return WaitAction(self.entity).perform()
def perform(self) -> None: target = self.engine.player dx = target.x - self.entity.x dy = target.y - self.entity.y distance = max(abs(dx), abs(dy)) # Chebyshev distance. # when the player is visible: if near player, attack!, if not, move towards it. if self.engine.game_map.visible[self.entity.x, self.entity.y]: if distance <= 1: return MeleeAction(self.entity, dx, dy).perform() self.path = self.get_path_to(target.x, target.y) # if there is a path, move one step. if self.path: dest_x, dest_y = self.path.pop(0) return MovementAction( self.entity, dest_x - self.entity.x, dest_y - self.entity.y, ).perform() return WaitAction(self.entity).perform()
def take_turn(self, engine: Engine, target: Entity) -> Action: dx = target.x - self.parent.x dy = target.y - self.parent.y distance = math.sqrt(dx**2 + dy**2) dx = int(round(dx / distance)) dy = int(round(dy / distance)) action: Action = WaitAction(engine, self.parent) if engine.game_map.visible[self.parent.x, self.parent.y]: if distance >= 2: destination_x, destination_y = self.parent.get_first_step_towards_destination( target.x, target.y, engine.game_map) action = MovementAction(engine, self.parent, destination_x - self.parent.x, destination_y - self.parent.y) else: action = MeleeAction(engine, self.parent, dx, dy) return action
def perform(self) -> None: """Does what an hostile enemy does: when you see the player, move towards it and attack when in range""" target = self.engine.player dx = target.x - self.entity.x dy = target.y - self.entity.y distance = max(abs(dx), abs(dy)) # Chebyshev distance. if self.engine.game_map.visible[self.entity.x, self.entity.y]: if distance <= 1: return MeleeAction(self.entity, dx, dy).perform() self.path = self.get_path_to(target.x, target.y) if self.path: dest_x, dest_y = self.path.pop(0) return MovementAction( self.entity, dest_x - self.entity.x, dest_y - self.entity.y, ).perform() return WaitAction(self.entity).perform()
def perform(self): target = self.engine.player dir_x = target.x - self.entity.x dir_y = target.y - self.entity.y distance = max( abs(dir_x), abs(dir_y)) # https://en.wikipedia.org/wiki/Chebyshev_distance # Prøve at angribe if self.engine.game_map.visible[self.entity.x, self.entity.y]: if distance <= 1: # Hvis distancen mellem entity og target er 1 eller mindre (de rører hinanden) return MeleeAction(self.entity, dir_x, dir_y).perform() self.path = self.get_path_to(target.x, target.y) if self.path: # Hvis entity har en path dest_x, dest_y = self.path.pop(0) # Fjern den ældste entry return MovementAction( self.entity, dest_x - self.entity.x, dest_y - self.entity.y, ).perform() return WaitAction(self.entity).perform()
def perform(self) -> None: target = self.engine.player dx = target.x - self.entity.x dy = target.y - self.entity.y distance = max( abs(dx), abs(dy)) # Chebyshev distance, max distance over 2d matrix if self.engine.game_map.visible[self.entity.x, self.entity.y]: #if T in field of view # if distance <= 1: # return InitiateAction(self.entity, dx, dy).perform() #T cannot init to @; only @ to T self.path = self.get_path_to(target.x, target.y) if self.path: dest_x, dest_y = self.path.pop(0) return MovementAction( self.entity, dest_x - self.entity.x, dest_y - self.entity.y, ).perform() return WaitAction(self.entity).perform()
def perform(self) -> None: if self.mode == HostileMode.PATROL: # check if we happen to spot the player player_visible = self.is_visible(self.engine.player.x, self.engine.player.y) print( f'Patrolling to waypoint {self.waypoint} now at {(self.entity.x, self.entity.y)}' ) if player_visible: print("SAW PLAYER, HUNTING") self.mode = HostileMode.HUNT else: if self.waypoint == None: # we need to set a new waypoint. self.path = [] tx = 0 ty = 0 while len(self.path) == 0: tx = int(random() * self.engine.game_map.width) ty = int(random() * self.engine.game_map.height) self.path = self.get_path_to(tx, ty) # print(f'setting path: {self.path}') self.waypoint = (tx, ty) if (self.waypoint[0] == self.entity.x) and (self.waypoint[1] == self.entity.y): print("Arrived at destination.") self.waypoint = None elif self.mode == HostileMode.HUNT: return self.perform_hunt() else: print('Unhandled mode.') if self.path: dest_x, dest_y = self.path[0] moving_direction = Facing.get_direction(self.entity.x, self.entity.y, dest_x, dest_y) # print(f'checking facing: {(dest_x, dest_y)} direction {moving_direction} current facing {self.entity.facing}') if (moving_direction == self.entity.facing): # now pop it. self.path.pop(0) return MovementAction( self.entity, dest_x - self.entity.x, dest_y - self.entity.y, ).perform() else: # print(f'returning rotate action to {moving_direction}') return RotateAction(self.entity, moving_direction).perform() return WaitAction(self.entity).perform()