예제 #1
0
 def __limit_length(self):
     length = glm.distance(self.start, self.end)
     if length > ARROW_MAX_LENGTH:
         self.end = vec3((self.end.x - self.start.x) / length * ARROW_MAX_LENGTH + self.start.x, 
                     (self.end.y - self.start.y) / length * ARROW_MAX_LENGTH + self.start.y,
                     0)
     self.length = glm.distance(self.start, self.end)
예제 #2
0
 def distance(self, other:"Transformation") -> float:
     td = glm.distance(self.translation, other.translation)
     sd = glm.distance(self.scale, other.scale)
     qn : glm.Quat = glm.inverse(self.quaternion)  # type: ignore
     qn = qn * other.quaternion
     if qn.w<0: qn = -qn
     qd = glm.length(qn - glm.quat())
     return td+sd+qd
예제 #3
0
    def find_nearest_ball(self, nodes):
        closest_node = ball.ball(50, glm.vec3(5000,5000,5000))

        nearest = glm.distance(self.pos.center_pos , closest_node.pos.center_pos)
        for n in nodes:
            distance = glm.distance(self.pos.center_pos, n.pos.center_pos)
            if (distance < nearest):
                nearest = distance
                closest_node = n
        return closest_node 
예제 #4
0
    def drive(self):
        if self.state is not character.DIE:
            if self.state is character.DRIVING:
                self.collider.pos = self.vehicle.get_seat_pos(
                    self.seat_index, 2)
                # self.collider.pos = self.graphics.pos
                self.engine.physics.dynamic.append(self.collider)
                self.collider.vel = glm.vec3(*self.vehicle.collider.vel)

                if self.seat_index is 0:
                    self.vehicle.accel = 0
                    self.vehicle.turn = 0
                    self.vehicle.turn_off()

                self.vehicle.occupied_seats.remove(self.seat_index)

                self.vehicle = None
                self.set_state(character.JUMPING)
            else:
                for vehicle in self.engine.game_manager.vehicles:
                    if glm.distance(self.graphics.pos,
                                    vehicle.collider.pos) < 7:
                        self.seat_index = vehicle.get_open_seat()
                        if self.seat_index >= 0:
                            self.vehicle = vehicle
                            self.set_state(character.DRIVING)
                            break
예제 #5
0
 def __get_force(self, obj) -> vec3:
     '''
     Return the force between self and obj.
     '''
     vect = vec3(obj.pos.x - self.pos.x, obj.pos.y - self.pos.y, 0)
     dist = glm.distance(self.pos, obj.pos)
     factor = self.mass * obj.mass / dist**3  #Power of 3 because the directional vector is not normalized
     return vec3(vect.x * factor, vect.y * factor, 0)
예제 #6
0
 def __new_object_stage1_cont(self):
     """
     Private function to update celestial body radius
     """        
     if self.curr_celestial:
         center = self.curr_celestial.rect.center
         center_v = vec3(center[0], center[1], 0)
         curpos = pygame.mouse.get_pos()
         curpos_v = vec3(curpos[0], curpos[1], 0)
         self.curr_celestial.radius = math.floor(glm.distance(center_v, curpos_v))   
예제 #7
0
    def getFarthestTileOfType(self, type, worldPosition):
        tiles = self.graph.getTilesOfType(type)
        farthestTile = None
        for tile in tiles:
            if farthestTile == None:
                farthestTile = tile
                continue

            farthestTileWorldPosition = self.map.getWorldPosition(farthestTile)
            farthestTileDistance = glm.distance(
                glm.vec2(farthestTileWorldPosition[0],
                         farthestTileWorldPosition[1]),
                glm.vec2(worldPosition[0], worldPosition[1]))
            tileWorldPosition = self.map.getWorldPosition(tile)
            tileDistance = glm.distance(
                glm.vec2(tileWorldPosition[0], tileWorldPosition[1]),
                glm.vec2(worldPosition[0], worldPosition[1]))
            if tileDistance > farthestTileDistance:
                farthestTile = tile
        return farthestTile
예제 #8
0
    def calcNeighborAcc(self, neighbor):
        #Distancia entre G e g
        distance = glm.distance(self.posicao, neighbor.posicao)

        #calculo da forca escalar Fg = G (M1 * M2 / d **2)
        escalarForce = 6.67e-11 * ((self.massa * neighbor.massa) /
                                   (distance**2))

        #proporcao (Regra de 3), da forca em cada eixo
        self.accForce += (
            (neighbor.posicao - self.posicao) * escalarForce) / distance
예제 #9
0
    def getClosestTileOfType(self, type, worldPosition):
        tiles = self.graph.getTilesOfType(type)
        closestTile = None
        for tile in tiles:
            if closestTile == None:
                closestTile = tile
                continue

            closestTileWorldPosition = self.map.getWorldPosition(closestTile)
            closestTileDistance = glm.distance(
                glm.vec2(closestTileWorldPosition[0],
                         closestTileWorldPosition[1]),
                glm.vec2(worldPosition[0], worldPosition[1]))
            tileWorldPosition = self.map.getWorldPosition(tile)
            tileDistance = glm.distance(
                glm.vec2(tileWorldPosition[0], tileWorldPosition[1]),
                glm.vec2(worldPosition[0], worldPosition[1]))
            if tileDistance < closestTileDistance:
                closestTile = tile
        return closestTile
예제 #10
0
    def getClosestWalkable(self, mapPosition):
        if self.graph.isWalkable(mapPosition) == True:
            return mapPosition

        tiles = self.graph.getWalkableTiles()
        closestTile = None
        for tile in tiles:
            if closestTile == None:
                closestTile = tile
                continue

            closestTileDistance = glm.distance(
                glm.vec2(closestTile[0], closestTile[1]),
                glm.vec2(mapPosition[0], mapPosition[1]))
            tileDistance = glm.distance(
                glm.vec2(tile[0], tile[1]),
                glm.vec2(mapPosition[0], mapPosition[1]))
            if tileDistance < closestTileDistance:
                closestTile = tile
        return closestTile
예제 #11
0
파일: object.py 프로젝트: defgsus/thegame
    def update(self, time: float, dt: float):
        location = self.location

        if self._last_location is None:
            speed = 0.
        else:
            speed = glm.distance(location.xy, self._last_location.xy) / dt
            if speed:
                self.direction_of_movement = glm.normalize(location.xy - self._last_location.xy)

        self._last_location = self._location.__copy__()

        self.average_speed += min(1., 8. * dt) * (speed - self.average_speed)
예제 #12
0
    def getFarthestCover(self, coverWorldPosition, farWorldPosition):
        tiles = self.graph.getWalkableTiles()
        closestTile = None
        for tile in tiles:
            tileWorldPosition = self.map.getWorldPosition(tile)
            if self.map.isVisible(coverWorldPosition,
                                  tileWorldPosition) == True:
                continue

            if closestTile == None:
                closestTile = tile
                continue

            closestTileWorldPosition = self.map.getWorldPosition(closestTile)
            closestTileDistance = glm.distance(
                glm.vec2(closestTileWorldPosition[0],
                         closestTileWorldPosition[1]),
                glm.vec2(farWorldPosition[0], farWorldPosition[1]))
            tileDistance = glm.distance(
                glm.vec2(tileWorldPosition[0], tileWorldPosition[1]),
                glm.vec2(farWorldPosition[0], farWorldPosition[1]))
            if tileDistance > closestTileDistance:
                closestTile = tile
        return closestTile
예제 #13
0
    def update(self, dt):
        if self.state is IDLE:
            self.angle += 30 * dt
            p_col = self.engine.game_manager.player.collider
            if glm.distance(self.pos.xz, p_col.pos.xz) < 2:
                if p_col.pos.y <= self.pos.y < p_col.pos.y + p_col.height:
                    self.set_state(DIE)

        elif self.state is DIE:
            self.timer -= dt
            if self.timer > 0:
                self.scale = self.timer
            else:
                self.despawn()
                self.engine.game_manager.powerups.remove(self)
                self.on_collect()
예제 #14
0
 def __init__(self, name, position=None, up=None, center=None):
     self._name = name
     self._position = position if position else glm.vec3()
     self._up = up if up else glm.vec3(0.0, 1.0, 0.0)
     self._center = center if center else glm.vec3()
     self._input = {"move": False, "x": 0.0, "y": 0.0}
     self.state = {
         "moving": False,
         "looking": False,
         "target_position": None,
         "target_center": None,
         "move_speed": 0,
         "look_speed": 0
     }
     self.distance = glm.distance(self._position, self._center)
     self.view_matrix = glm.mat4()
예제 #15
0
    def __new_object_stage2(self):
        """
        Private function to complete creating a new celestial object
        """
        if self.curr_celestial:
            # Replace functions for next stage
            self.__dynamic_input_funcs["temp"] = self.app.inputs.inputs["update"].always(self.__new_object_stage2_cont)
            self.__dynamic_input_funcs["temp2"] = self.app.inputs.inputs["new_object"].on_press(self.__new_object_stage3)
            
            center = self.curr_celestial.rect.center
            center_v = vec3(center[0], center[1], 0)
            curpos = pygame.mouse.get_pos()
            curpos_v = vec3(curpos[0], curpos[1], 0)
            self.curr_celestial.radius = math.floor(glm.distance(center_v, curpos_v))
            self.setting_velocity = True

            center = self.curr_celestial.rect.center
            self.curr_velo_arrow = VelocityArrow(center)
예제 #16
0
    def __init__(self, start: vec3, end: vec3, radius: float):
        super().__init__()
        self.start: vec3 = vec3(start)
        self.end: vec3 = vec3(end)
        self.radius: float = radius

        self.height: float = glm.distance(self.start, self.end)
        self.normal: vec3 = glm.normalize(self.end - self.start)

        self.b1: vec3
        self.b2: vec3
        self.b1, self.b2 = orthonormal_basis_of(self.normal)

        # calculate rotation matrix for cylinder
        self.trans_matrix: mat4 = glm.orientation(vec3(0.0, 0.0, 1.0), self.normal)

        # add translation to matrix
        self.trans_matrix[0][3] = start.x
        self.trans_matrix[1][3] = start.y
        self.trans_matrix[2][3] = start.z

        # get corners of OBB
        points = glm.array(
            vec3(self.radius, self.radius, 0.0),
            vec3(-self.radius, self.radius, 0.0),
            vec3(self.radius, -self.radius, 0.0),
            vec3(-self.radius, -self.radius, 0.0),
            vec3(self.radius, self.radius, self.height),
            vec3(-self.radius, self.radius, self.height),
            vec3(self.radius, -self.radius, self.height),
            vec3(-self.radius, -self.radius, self.height))

        # inflate OBB into AABB
        # TODO:
        #     BoundingBox _should_ have default values - we shouldn't have to
        #     initialize with inf and -inf manually. but if we do that,
        #     it seems to initialize with other BoundingBox's values.
        #     Something weird is going on with (I presume) glm pointers?
        self._bbox = BoundingBox(vec3(inf), vec3(-inf))
        for v in points:
            self._bbox.vec3_extend(vec3(vec4(v, 1.0) * self.trans_matrix))
예제 #17
0
파일: Agents.py 프로젝트: defgsus/thegame
    def update(self, dt):
        if self.cur_index + 1 >= len(self.path):
            return False

        this_pos = self.waypoints.id_to_pos[self.path[
            self.cur_index]] + glm.vec3(.5, .5, 0)
        next_node = self.path[self.cur_index + 1]
        next_pos = self.waypoints.id_to_pos[next_node] + glm.vec3(.5, .5, 0)
        #print(self.path, self.cur_index, next_node, self.agent.sposition, next_pos)

        dist = glm.distance(self.agent.sposition, next_pos)
        dir = (next_pos - self.agent.sposition) / dist

        self.agent.move(dir)
        #if self.agent.name == "player":
        #    print(this_pos.y, next_pos.y, self.agent.sposition.y)
        if next_pos.z > this_pos.z + .3 and next_pos.z > self.agent.sposition.z:
            self.agent.jump()

        if dist <= self.min_dist:
            self.cur_index += 1
예제 #18
0
    def process(self):
        if self.world.state == res.STATE_PAUSED:
            return

        for hero_id, (hero_transformation,
                      hero_velocity,
                      hero_bounding_box,
                      hero_collision) in \
                self.world.get_components(com.Transformation,
                                          com.Velocity,
                                          com.BoundingBox,
                                          com.CollisionComponent):

            hero_rotation = hero_transformation.rotation.x
            target_velocity = hero_velocity.value * self.world.delta
            hero_comfort_zone = hero_bounding_box.radius

            hero_rectangle = hero_bounding_box.shape
            hero_target_pos = hero_transformation.position + target_velocity
            hero_rectangle_width = (
                hero_rectangle.width * abs(math.cos(hero_rotation)) +
                hero_rectangle.depth * abs(math.sin(hero_rotation)))
            hero_rectangle_depth = (
                hero_rectangle.width * abs(math.sin(hero_rotation)) +
                hero_rectangle.depth * abs(math.cos(hero_rotation)))
            hero_rectangle_height = hero_rectangle.height

            hero_collision.is_colliding_x = False
            hero_collision.is_colliding_y = False
            hero_collision.is_colliding_z = False

            for villain_id, (
                    villain_transformation,
                    villain_bounding_box) in self.world.get_components(
                        com.Transformation, com.BoundingBox):

                villain_rectangle = villain_bounding_box.shape

                # Don't hit your self
                if villain_id == hero_id: continue

                # Are they in each others comfort zones?
                if (glm.distance(villain_transformation.position,
                                 villain_transformation.position) >
                    (hero_comfort_zone + villain_bounding_box.radius)):
                    continue

                diff = glm.vec3(
                    villain_transformation.position.x - hero_target_pos.x,
                    villain_transformation.position.y - hero_target_pos.y,
                    villain_transformation.position.z - hero_target_pos.z)
                gap = glm.vec3(
                    (hero_rectangle_width + villain_rectangle.width) -
                    abs(diff.x),
                    (hero_rectangle_depth + villain_rectangle.depth) -
                    abs(diff.y),
                    (hero_rectangle_height + villain_rectangle.height) -
                    abs(diff.z))

                old_gap_x = ((hero_rectangle_width + villain_rectangle.width) -
                             abs(villain_transformation.position.x -
                                 hero_transformation.position.x))

                # One side is outside
                if gap.x < 0.0 or gap.y < 0.0 or gap.z < 0.0: continue
                # Has collided and append
                if self.world.has_components(villain_id, com.CollisionReport):
                    self.world.component_for_entity(
                        villain_id, com.CollisionReport).failed.append(hero_id)

                old_diff = hero_transformation.position - villain_transformation.position

                if gap.x <= gap.y and gap.x <= gap.z:
                    offset = max(
                        hero_rectangle_width + villain_rectangle.width, 0)
                    hero_target_pos.x = villain_transformation.position.x + math.copysign(
                        offset, old_diff.x)
                    hero_collision.is_colliding_x = True
                elif gap.y <= gap.z:
                    if old_gap_x <= 0:
                        continue
                    offset = max(
                        hero_rectangle_depth + villain_rectangle.depth, 0)
                    hero_target_pos.y = villain_transformation.position.y + math.copysign(
                        offset, old_diff.y)
                    hero_collision.is_colliding_y = True
                else:
                    offset = hero_rectangle_height + villain_rectangle.height
                    hero_target_pos.z = villain_transformation.position.z + math.copysign(
                        offset, old_diff.z)
                    hero_collision.is_colliding_z = True

            hero_velocity.value = (
                hero_target_pos -
                hero_transformation.position) / self.world.delta
예제 #19
0
파일: Agents.py 프로젝트: defgsus/thegame
 def finished(self):
     if self.cur_index >= len(self.path):
         return True
     return glm.distance(self.agent.sposition,
                         glm.vec3(*self.goal_pos)) <= self.min_dist
예제 #20
0
    def near(self, neighbor, minDistance):
        distance = glm.distance(self.posicao, neighbor.posicao)
        if distance < minDistance:
            return True

        return False
예제 #21
0
def circle_collision(center, point, radius):
    if glm.distance(point, center) < radius:
        return center, glm.normalize(point - center), radius
예제 #22
0
    def think(self):
        if self.lastSeenBotEntity != None and self.timer.read(
        ) >= self.seeDelay:
            seenBotInfo = self.sight.getSeenBotInfo(self.lastSeenBotEntity)
            distance = glm.distance(
                glm.vec2(seenBotInfo.transform.position[0],
                         seenBotInfo.transform.position[1]),
                glm.vec2(self.transform.position[0],
                         self.transform.position[1]))
            colliderRange = self.collider.size[
                0] / 2.0 + seenBotInfo.collider.size[0] / 2.0

            # Take cover
            if self.fsm.isCurrentState("TakeCover") == True:
                if self.agent.finishedMove == True:
                    if self.fsm.isCurrentState("Idle") == False:
                        self.fsm.changeCurrentState(decisionMaking.Idle())
            elif self.fsm.isCurrentState("TakeHealthCover") == True:
                if self.agent.finishedMove == True:
                    if self.fsm.isCurrentState("Heal") == False:
                        self.fsm.changeCurrentState(decisionMaking.Heal())
            elif self.fsm.isCurrentState("TakeWeaponCover") == True:
                if self.agent.finishedMove == True:
                    if self.fsm.isCurrentState("Reload") == False:
                        self.fsm.changeCurrentState(decisionMaking.Reload())
            # Heal or retreat
            elif self.health.currentHP < seenBotInfo.health.currentHP * 0.9:
                if self.health.firstAidBoxHP > 0:
                    if self.canTakeCover == True:
                        if self.fsm.isCurrentState("TakeHealthCover") == False:
                            closestCover = self.getClosestCover(
                                seenBotInfo.transform.position,
                                self.transform.position)
                            worldDestinationPosition = self.map.getWorldPosition(
                                closestCover)
                            self.fsm.changeCurrentState(
                                decisionMaking.TakeHealthCover(
                                    self.transform.position,
                                    worldDestinationPosition))
                    else:
                        if self.fsm.isCurrentState("Heal") == False:
                            self.fsm.changeCurrentState(decisionMaking.Heal())
                else:
                    if self.fsm.isCurrentState("TakeCover") == False:
                        farthestCover = self.getFarthestCover(
                            seenBotInfo.transform.position,
                            self.transform.position)
                        worldDestinationPosition = self.map.getWorldPosition(
                            farthestCover)
                        self.fsm.changeCurrentState(
                            decisionMaking.TakeCover(self.transform.position,
                                                     worldDestinationPosition))
            # Shoot
            elif self.weapon.currentAmmo > 0:
                if distance <= self.weapon.primaryWeaponRange:
                    if distance >= colliderRange:
                        if self.fsm.isCurrentState(
                                "ShootPrimaryWeapon") == False:
                            self.fsm.changeCurrentState(
                                decisionMaking.ShootPrimaryWeapon(
                                    self.lastSeenBotEntity))
                    else:
                        if self.fsm.isCurrentState("MoveAwayFromBot") == False:
                            self.fsm.changeCurrentState(
                                decisionMaking.MoveAwayFromBot(
                                    self.lastSeenBotEntity))
                else:
                    if self.fsm.isCurrentState("MoveTowardsBot") == False:
                        self.fsm.changeCurrentState(
                            decisionMaking.MoveTowardsBot(
                                self.lastSeenBotEntity))
            # Shoot
            elif seenBotInfo.health.currentHP - self.weapon.secondaryWeaponDamage * 3 <= 0:
                if distance <= self.weapon.secondaryWeaponRange:
                    if distance >= colliderRange:
                        if self.fsm.isCurrentState(
                                "ShootSecondaryWeapon") == False:
                            self.fsm.changeCurrentState(
                                decisionMaking.ShootSecondaryWeapon(
                                    self.lastSeenBotEntity))
                    else:
                        if self.fsm.isCurrentState("MoveAwayFromBot") == False:
                            self.fsm.changeCurrentState(
                                decisionMaking.MoveAwayFromBot(
                                    self.lastSeenBotEntity))
                else:
                    if self.fsm.isCurrentState("MoveTowardsBot") == False:
                        self.fsm.changeCurrentState(
                            decisionMaking.MoveTowardsBot(
                                self.lastSeenBotEntity))
            # Reload
            elif self.weapon.ammoBoxAmmo > 0:
                if self.canTakeCover == True:
                    if self.fsm.isCurrentState("TakeWeaponCover") == False:
                        closestCover = self.getClosestCover(
                            seenBotInfo.transform.position,
                            self.transform.position)
                        worldDestinationPosition = self.map.getWorldPosition(
                            closestCover)
                        self.fsm.changeCurrentState(
                            decisionMaking.TakeWeaponCover(
                                self.transform.position,
                                worldDestinationPosition))
                else:
                    if self.fsm.isCurrentState("Reload") == False:
                        self.fsm.changeCurrentState(decisionMaking.Reload())
            # Shoot
            else:
                if distance <= self.weapon.secondaryWeaponRange:
                    if distance >= colliderRange:
                        if self.fsm.isCurrentState(
                                "ShootSecondaryWeapon") == False:
                            self.fsm.changeCurrentState(
                                decisionMaking.ShootSecondaryWeapon(
                                    self.lastSeenBotEntity))
                    else:
                        if self.fsm.isCurrentState("MoveAwayFromBot") == False:
                            self.fsm.changeCurrentState(
                                decisionMaking.MoveAwayFromBot(
                                    self.lastSeenBotEntity))
                else:
                    if self.fsm.isCurrentState("MoveTowardsBot") == False:
                        self.fsm.changeCurrentState(
                            decisionMaking.MoveTowardsBot(
                                self.lastSeenBotEntity))
        else:
            # Take cover
            if self.fsm.isCurrentState("TakeCover") == True:
                if self.agent.finishedMove == True:
                    if self.fsm.isCurrentState("Idle") == False:
                        self.fsm.changeCurrentState(decisionMaking.Idle())
            elif self.fsm.isCurrentState("TakeHealthCover") == True:
                if self.agent.finishedMove == True:
                    if self.fsm.isCurrentState("Heal") == False:
                        self.fsm.changeCurrentState(decisionMaking.Heal())
            elif self.fsm.isCurrentState("TakeWeaponCover") == True:
                if self.agent.finishedMove == True:
                    if self.fsm.isCurrentState("Reload") == False:
                        self.fsm.changeCurrentState(decisionMaking.Reload())
            # Heal
            elif self.health.currentHP < self.health.maxHP * 0.5 and self.health.firstAidBoxHP > 0:
                if self.fsm.isCurrentState("Heal") == False:
                    self.fsm.changeCurrentState(decisionMaking.Heal())
            elif self.health.currentHP < self.health.maxHP * 0.5 and self.canPickUpObjects:
                if self.fsm.isCurrentState("GoToHealthSpawner") == False:
                    closestHealthSpawner = self.getClosestHealthSpawner(
                        self.transform.position)
                    worldDestinationPosition = self.map.getWorldPosition(
                        closestHealthSpawner)
                    self.fsm.changeCurrentState(
                        decisionMaking.GoToHealthSpawner(
                            self.transform.position, worldDestinationPosition))
            # Reload
            elif self.weapon.currentAmmo < self.weapon.maxAmmo * 0.5 and self.weapon.ammoBoxAmmo > 0:
                if self.fsm.isCurrentState("Reload") == False:
                    self.fsm.changeCurrentState(decisionMaking.Reload())
            elif self.weapon.currentAmmo < self.weapon.maxAmmo * 0.5 and self.canPickUpObjects:
                if self.fsm.isCurrentState("GoToWeaponSpawner") == False:
                    closestWeaponSpawner = self.getClosestWeaponSpawner(
                        self.transform.position)
                    worldDestinationPosition = self.map.getWorldPosition(
                        closestWeaponSpawner)
                    self.fsm.changeCurrentState(
                        decisionMaking.GoToWeaponSpawner(
                            self.transform.position, worldDestinationPosition))
            # Search
            elif self.lastKnownDirection != None:
                if self.fsm.isCurrentState("LookAtBullet") == True:
                    if self.agent.finishedRotate == True:
                        self.lastKnownDirection = None
                else:
                    self.fsm.changeCurrentState(
                        decisionMaking.LookAtBullet(self.lastKnownDirection))
            elif self.lastKnownPosition != None:
                if self.fsm.isCurrentState("GoToLastKnownPosition") == True:
                    if self.agent.finishedMove == True:
                        self.lastKnownPosition = None
                else:
                    self.fsm.changeCurrentState(
                        decisionMaking.GoToLastKnownPosition(
                            self.transform.position, self.lastKnownPosition))
            # Reload
            elif self.weapon.ammoBoxAmmo == 0 and self.canPickUpObjects:
                if self.fsm.isCurrentState("GoToWeaponSpawner") == False:
                    closestWeaponSpawner = self.getClosestWeaponSpawner(
                        self.transform.position)
                    worldDestinationPosition = self.map.getWorldPosition(
                        closestWeaponSpawner)
                    self.fsm.changeCurrentState(
                        decisionMaking.GoToWeaponSpawner(
                            self.transform.position, worldDestinationPosition))
            # Heal
            elif self.health.firstAidBoxHP == 0 and self.canPickUpObjects:
                if self.fsm.isCurrentState("GoToHealthSpawner") == False:
                    closestHealthSpawner = self.getClosestHealthSpawner(
                        self.transform.position)
                    worldDestinationPosition = self.map.getWorldPosition(
                        closestHealthSpawner)
                    self.fsm.changeCurrentState(
                        decisionMaking.GoToHealthSpawner(
                            self.transform.position, worldDestinationPosition))
            # Search
            else:
                if self.agent.finishedMove == True:
                    randomTile = self.getRandomTile()
                    worldDestinationPosition = self.map.getWorldPosition(
                        randomTile)
                    self.fsm.changeCurrentState(
                        decisionMaking.GoToRandomPosition(
                            self.transform.position, worldDestinationPosition))
예제 #23
0
    def plan_move(self, field):

        if (self.state == state.IDLE):
            if (len(field.power_cells) > 0):
                self.state = state.COLLECT
        elif (self.state == state.COLLECT):
            # pick the target
            if ((self.balls_held < self.ball_capacity) and (len(field.power_cells) > 0)):
                nearest= self.find_nearest_ball(field.power_cells)

                # get the heading (destination - current)
                direction = glm.vec3(nearest.pos.center_pos - self.pos.center_pos); 
                self.pos.target = direction

                #see if we're going to collide with another robot
                draw_ray=[]
                draw_ray.append(glm.vec3(0,-20,0)*self.pos.velocity)

                angle_heading = draw.angleBetween(glm.vec3(0,-1,0), self.pos.heading)
                ray_collisions = draw.collide_rays(field, draw_ray, self.pos.center_pos, angle_heading)     
                # if ray_collisions is not empty, we had a hit
                # depending on where the hit was, we should turn left, right or decrease speed              
                # take a hard right, JIC
                if (len(ray_collisions) > 0):
                    self.pos.heading = draw.rotate_vector(self.pos.heading, 1)
                    print ("Collision")

                col_count = len(ray_collisions)
                col=0
                while (col < col_count):
                    print(ray_collisions[col].x, ray_collisions[col].y, ray_collisions[col].z)
                    col += 1

                # get the distance (TODO: scale accel by distance)
                distance = glm.distance(nearest.pos.center_pos, self.pos.center_pos);  
                if (distance > self.width/2):

                    print ("nearest %d %d %d %d %d %d %d %d" %(nearest.id, nearest.pos.center_pos.x, nearest.pos.center_pos.y, distance, self.pos.target.x, self.pos.target.y, direction.x, direction.y))

                    # TODO: set accel rate based on distance to target
                    self.pos.accelleration += self.max_accel/20
                    if (self.pos.accelleration > self.max_accel):
                        self.pos.accelleration = self.max_accel

                    # adjust the velocity to  velocity += (accelrate/30)
                    self.pos.velocity += self.pos.accelleration 

                    # limit velocity to top speed 
                    if (self.pos.velocity > self.top_speed):
                        self.pos.velocity = self.top_speed
                    if (self.pos.velocity < 0):
                        self.pos.velocity = 0

                    print ("velocity now %d %d"%(self.pos.velocity, self.top_speed))
                else:
                    print ("Caught %d"%(nearest.id))
                    self.pos.velocity = 0
                    self.pos.accelleration = 0;
                    self.balls_held += 1
                    field.remove_ball(nearest.id)
            else:
                self.state = state.FIRE
        elif (self.state == state.FIRE):
            # get the heading (destination - current)
            direction = glm.vec3(self.firing_target - self.pos.center_pos); 

            # convert it to a scale of 1
            #direction = glm.normalize(direction);  
            self.pos.target = direction

                # get the distance (TODO: scale accel by distance)
            distance = glm.distance(self.firing_target, self.pos.center_pos);  

            if (distance > self.firing_range):

                # TODO: set accel rate based on distance to target
                self.pos.accelleration = self.max_accel

                # adjust the velocity to  velocity += (accelrate/30)
                self.pos.velocity += (self.pos.accelleration*(1/20)) 

                # limit velocity to top speed 
                if (self.pos.velocity > self.top_speed):
                    self.pos.velocity = self.top_speed
                if (self.pos.velocity < 0):
                    self.pos.velocity = 0
            else:
                # in firing range
                if (self.firing_timer == 0):
                    self.firing_timer = (time.time()*1000)+self.firing_rate

                if (self.firing_timer <= (time.time()*1000)):
                    self.pos.velocity = 0
                    self.pos.accelleration = 0
                    self.balls_held -= 1
                    self.firing_timer = 0

                    shot_val = random.random();
                    if (shot_val < self.firing_accuracy):
                        # handle scoring
                        # ball goes to queue for re-introduction
                        print("Hit")
                    else:
                        # ball goes back to field
                        # angle of deflection and 1/2 launching speed 
                        print("Miss")

                if (self.balls_held == 0):
                    self.state = state.IDLE