コード例 #1
0
ファイル: character.py プロジェクト: ehernandezanda/D3labs
    def move(self, move_vector: (float, float)) -> bool:
        """This function represents the interaction between my character and the environment.

        The speed and terrain part that follows could be simplified in our current case, since all the terrain is
        the same and the speed of our army elements is all equal. But in a more complex situation, where the terrain
        is heterogeneous and we have a mixed army, this code would be able to handle properly the movement in such
        cases

        Returns
        -------

        """
        factor = Map.getTerrainAt(int(self._x), int(self._y)).cross() * self._speed
        m_x = move_vector[0] * factor
        m_y = move_vector[1] * factor
        new_x = self._x + m_x
        new_y = self._y + m_y
        if Map.getMap().width >= new_x >= 0:
            self._x = new_x
        elif Map.getMap().width < new_x:
            self._x = Map.getMap().width
        else:
            self._x = 0
        if Map.getMap().height >= new_y >= 0:
            self._y = new_y
        elif Map.getMap().height < new_y:
            self._y = Map.getMap().height
        else:
            self._y = 0
        return True
コード例 #2
0
    def move(
        self, e_center: (float, float), vectorCenterToCenter: (float, float)
    ) -> bool:
        """This function represents the interaction between my character and the environment.

        The speed and terrain part that follows could be simplified in our current case, since all the terrain is
        the same and the speed of our army elements is all equal. But in a more complex situation, where the terrain
        is heterogeneous and we have a mixed army, this code would be able to handle properly the movement in such
        cases

        Returns
        -------

        """
        factor = Map.getTerrainAt(int(self._x), int(
            self._y)).cross() * self._speed
        Vg = (e_center[0] - self._x, e_center[1] - self._y)
        Mag = sqrt(pow(Vg[0], 2) + pow(Vg[1], 2))
        Vg = (Vg[0] / Mag, Vg[1] / Mag)
        Vt = (Vg[0] + vectorCenterToCenter[0], Vg[1] + vectorCenterToCenter[1])
        mag2 = sqrt(pow(Vt[0], 2) + pow(Vt[1], 2))
        move_vector = (Vt[0] / mag2, Vt[1] / mag2)
        m_x = move_vector[0] * factor
        m_y = move_vector[1] * factor
        new_x = self._x + m_x
        new_y = self._y + m_y
        if Map.getMap().width > new_x >= 0:
            self._x = new_x
        elif Map.getMap().width <= new_x:
            self._x = Map.getMap().width - 1
        else:
            self._x = 0
        if Map.getMap().height > new_y >= 0:
            self._y = new_y
        elif Map.getMap().height <= new_y:
            self._y = Map.getMap().height - 1
        else:
            self._y = 0
        return True