def whatIsInTheWay(self, origin: Vector2, direction: Vector2) -> {}: ''' Return a dict[x,y] = mapEntity between a position and a direction vector(:class:`.Vector2`) Exemple:: objects = world.whatIsInTheWay(origin, MathUtils.getDirectionVector(origin, toPosition)) ''' obstacle = {} length = int(math.sqrt(direction.x**2 + direction.y**2)) if length == 0: return obstacle unit_direction = Vector2(direction.x / length, direction.y / length) if math.sqrt(unit_direction.x**2 + unit_direction.y**2) != 1: raise Exception("Non possible path", unit_direction) for i in range(1, length - 1): currentPos = Vector2(origin.x + unit_direction.x * i, origin.y + unit_direction.y * i) obj = self.whatIsAtPosition(currentPos) if obj is not Entity.EMPTY: obstacle[currentPos.x, currentPos.y] = obj return obstacle
def getDirectionVector(fromPosition: Vector2, toPosition: Vector2) -> Vector2: """ Takes two vector representing positions and turns them into a direction vector :type fromPosition: Vector2 :type toPosition: Vector2 :rtype: Vector2 """ return Vector2(toPosition.x - fromPosition.x, toPosition.y - fromPosition.y)
def __init__(self, characterId, position): self._characterId = characterId self.position = Vector2(position.x, position.y) ''' The current position of the character (see :class:`.Vector2`) ''' self.mineReady = True ''' Is the mine ready to be use (see :class:`.bool`) ''' self.missile = Missile(Vector2(0, 0)) ''' The missile of the character (see :class:`.Missile`) ''' self.life = 3 '''
def __init__(self, teamId, numberOfCharacter): self._teamId = teamId self.characters = [] ''' List of all the characters (see :class:`.Character`) Note: The index of the characters start at 0 and the id of the character is the same as his index ''' for index in range(0, numberOfCharacter): self.characters.insert(index, Character(index, Vector2(0, 0)))
def __init__(self, position): self.isReady = True ''' Is the missile ready to be use (see :class:`.bool`) ''' self.position = Vector2(position.x, position.y) ''' The current position of the missile (see :class:`.Vector2`) ''' self.direction = Direction.UP '''
def test_getDirection(self): self.assertEqual(Direction.RIGHT, MathUtils.getDirection(Vector2(2, 0))) self.assertEqual(Direction.LEFT, MathUtils.getDirection(Vector2(-2, 0))) self.assertEqual(Direction.UP, MathUtils.getDirection(Vector2(0, 2))) self.assertEqual(Direction.DOWN, MathUtils.getDirection(Vector2(0, -2))) self.assertRaises(Exception, MathUtils.getDirection, (Vector2(2, 2))) self.assertRaises(Exception, MathUtils.getDirection, (Vector2(-2, -2))) self.assertRaises(Exception, MathUtils.getDirection, (Vector2(0, 0)))
class AIDefault(object): ''' Represents the AI that need to be implemented ''' world = Singleton(World) ''' The world singleton needed to get the info on the current state of the world (see :class:`.World`) ''' aiStatus = AIStatus.INIT ''' The current status of the AI example (Can be deleted) ''' position1 = Vector2(0, 0) position2 = Vector2(7, 0) position3 = Vector2(0, 7) position4 = Vector2(7, 7) def setNames(self): ''' Function call to set the name of the team and characters (Cannot be deleted) ''' teamName = "Team python" characterNames = ["Python character1", "Python character2"] self.world._setNames(teamName, characterNames) def tick(self): ''' Function call every 30 ms, this is the starting point for the AI (Cannot be deleted) ''' if self.aiStatus == AIStatus.INIT: self.initState() elif self.aiStatus == AIStatus.LOWER_RIGHT: self.lowerRight() elif self.aiStatus == AIStatus.LOWER_LEFT: self.lowerLeft() elif self.aiStatus == AIStatus.UPPER_RIGHT: self.upperRight() elif self.aiStatus == AIStatus.UPPER_LEFT: self.upperLeft() def initState(self): ''' Function call for the init state of the AI example (Can be deleted) ''' character1 = self.world.getMyTeam().getFirstCharacter() character2 = self.world.getMyTeam().getSecondCharacter() character1.goTo(self.position2) character2.goTo(self.position3) character1.shootMissile(Direction.RIGHT) character2.shootMissile(Direction.LEFT) self.aiStatus = AIStatus.LOWER_RIGHT def lowerRight(self): ''' Function call for the lowerRight state of the AI example (Can be deleted) ''' character1 = self.world.getMyTeam().getFirstCharacter() character2 = self.world.getMyTeam().getSecondCharacter() if character1.position == self.position2: character1.goTo(self.position1) character2.goTo(self.position4) character1.dropMine() character2.dropMine() character1.shootMissile(Direction.LEFT) character2.shootMissile(Direction.RIGHT) self.aiStatus = AIStatus.LOWER_LEFT def lowerLeft(self): ''' Function call for the lowerLeft state of the AI example (Can be deleted) ''' character1 = self.world.getMyTeam().getFirstCharacter() character2 = self.world.getMyTeam().getSecondCharacter() if character1.position == self.position1: character1.goTo(self.position4) character2.goTo(self.position1) character1.shootMissile(Direction.UP) character2.shootMissile(Direction.DOWN) character1.dropMine() character2.dropMine() self.aiStatus = AIStatus.UPPER_RIGHT def upperRight(self): ''' Function call for the upperRight state of the AI example (Can be deleted) ''' character1 = self.world.getMyTeam().getFirstCharacter() character2 = self.world.getMyTeam().getSecondCharacter() if character1.position == self.position4: character1.goTo(self.position3) character2.goTo(self.position2) character1.shootMissile(Direction.DOWN) character2.shootMissile(Direction.UP) character1.dropMine() character2.dropMine() self.aiStatus = AIStatus.UPPER_LEFT def upperLeft(self): ''' Function call for the upperLeft state of the AI example (Can be deleted) ''' character1 = self.world.getMyTeam().getFirstCharacter() character2 = self.world.getMyTeam().getSecondCharacter() if character1.position == self.position3: character1.goTo(self.position2) character2.goTo(self.position3) character1.shootMissile(Direction.RIGHT) character2.shootMissile(Direction.LEFT) character1.dropMine() character2.dropMine() self.aiStatus = AIStatus.LOWER_RIGHT
def __init__(self): self.teamId = 0 self.characterId = 0 self.position = Vector2(0, 0)
def test_getDirectionVector(self): a = Vector2(1, 1) b = Vector2(7, 7) self.assertEqual(Vector2(6, 6), MathUtils.getDirectionVector(a, b))
def test_getDirectionFromPositions(self): a = Vector2(0, 7) b = Vector2(7, 7) self.assertEqual(Direction.RIGHT, MathUtils.getDirectionFromPositions(a, b))