Пример #1
0
    def checkDamageCollisions(self):
        for player in Global.getGameTanks():
            player.cshape = cm.AARectShape(player.position, player.width // 2,
                                           player.height // 2)

        # for enemy in Global.objects['enemies']:
        #     enemy.cshape = cm.AARectShape(
        #         enemy.position,
        #         enemy.width // 2,
        #         enemy.height // 2
        #     )

        damage_collisions = Global.CollisionManager.objs_colliding(self)

        if damage_collisions:
            for damage_wall in Global.getGameWalls():
                if damage_wall.type == 'destroyable':
                    if damage_wall in damage_collisions:
                        damage_wall.damage(self.bullet)

            for player in Global.getGameTanks():
                if player in damage_collisions:
                    player.damage(self.bullet)

            for obj in Global.getGameObjects():
                if obj in damage_collisions:
                    obj.damage(self.bullet)
Пример #2
0
    def fire(self,
             id=None,
             position=None,
             animation_position=None,
             rotation=None,
             animation_rotation=None,
             last_update_time=None,
             parent_id=None):
        bullet = LightBullet()

        # if not position: position = self.firePosition()
        # if not rotation: rotation = self.fireRotation()
        if not last_update_time: last_update_time = time()

        bullet.id = id
        bullet.parent_id = parent_id
        bullet.position = position
        bullet.start_position = position
        bullet.rotation = rotation + self.getAngleDeflection()
        bullet.last_update_time = last_update_time

        Global.addBulletToGame(bullet)
        bullet.do(BulletMovingHandlers())

        animation = StandartBulletFireAnimation()
        animation.appendAnimationToLayer(animation_position,
                                         animation_rotation)
Пример #3
0
def set_walls():
    add_background()

    for wall in Global.all_walls:
        #src = wall.get(NetworkDataCodes.SRC).replace('assets/', 'assets/map/')
        src = wall.src.replace('assets/', 'assets/map/')
        type = wall.type

        try:
            brick_wall = Wall(src, type)
        except ResourceNotFoundException:
            continue

        if type != 0 and type != 1:
            Global.addWallToGame(brick_wall)

        #brick_wall.id = wall.get(NetworkDataCodes.ID)
        brick_wall.id = wall.id
        #x, y = wall.get(NetworkDataCodes.POSITION)
        x, y = wall.position

        brick_wall.update_position((x, y))
        brick_wall.type = type
        brick_wall.src = src

        #scale = wall.get(NetworkDataCodes.SCALE, None)
        scale = wall.scale
        if scale: brick_wall.scale = scale

        Global.Layers.addWall(brick_wall, brick_wall.type)

    add_clans_objects()
Пример #4
0
def addGamePlayer(type, clan, position=(100, 100), rotation=0, add_moving_handler=False, id=None, bot=False):
    tank = TankHelper.getTankByType(int(type))

    if id:
        tank.id = id
    else:
        tank.id = Global.getNextId()

    tank.clan = clan
    tank.bot = bot
    tank.position = position
    # tank.gun_rotation = rotation
    tank.rotation = rotation
    #self.sendAllTanksToClients()

    if add_moving_handler:
        tank.do(UserTankMovingHandlers())

    if bot:
        tank.do(BotTankMovingHandlers())
        # tank.moving_handler = BotTankMovingHandlers(tank)
        # tank.moving_handler.start()

    Global.addTankToObjectsAndSprites(tank)

    return tank.id
Пример #5
0
def sendBulletToOtherPlayers(bullet):
    bullet_data = bullet.getObjectFromSelf()
    bullet_data['action'] = NetworkActions.TANK_FIRE

    if Global.IsGeneralServer:
        Global.addToQueue(bullet_data)
    else:
        Global.TankNetworkListenerConnection.Send(bullet_data)
Пример #6
0
def load_map():
    for wall in Global.get_map():
        wall = LandingObject(wall)
        wall.id = Global.getNextId()
        Global.all_walls.append(wall)

        # if wall.type != 0 and wall.type != 1:
        #     Global.walls.append(wall)

    set_walls()
Пример #7
0
def SendDestroyBulletEvent(bullet):
    bullet_data = bullet.getObjectFromSelf()
    bullet_data['action'] = NetworkActions.DESTROY
    Global.addToQueue(bullet_data)
    # removeBullet({
    #     "action": Global.NetworkActions.DESTROY,
    #     NetworkDataCodes.TYPE: self.type,
    #     NetworkDataCodes.POSITION: self.position,
    #     NetworkDataCodes.ID: self.id
    # })
    def printDebugInfo(self):
        bullets = Global.getGameBullets()
        tanks = Global.getGameTanks()
        walls = Global.getGameWalls()
        CM = Global.CollisionManager
        LayersTanks = Global.Layers.tanks
        LayersWalls = Global.Layers.walls
        LayersBullets = Global.Layers.bullets
        LayersAnimations = Global.Layers.globalPanel

        print('bullets', len(bullets), 'LayersBullets',
              len(LayersBullets.children), 'LayersAnimations',
              len(LayersAnimations.children), 'CollisionManager', len(CM.objs),
              'tanks', len(tanks))
Пример #9
0
    def checkDamageCollisionsOLD(self):
        damage_collisions = Global.CollisionManager.objs_colliding(self)

        if damage_collisions:
            for damage_wall in Global.getGameWalls():
                if damage_wall in damage_collisions:
                    damage_wall.damage(self.bullet)

        for player in Global.getGameTanks():

            #if parent_id == player.id: continue

            player_points = player.getPoints()
            if Collisions.check(player_points, self.bullet.position):
                player.damage(self.bullet)
Пример #10
0
    def damage(self, bullet):
        dx = (self.width + self.height) * self.scale / 2
        dmg = DamageHelper.get_damage(self.position, bullet, dx)

        self.health -= dmg

        Global.damageSomeObject(id=self.id, dmg=dmg, health=self.health)

        Global.Queue.append({
            "action": NetworkActions.DAMAGE,
            NetworkDataCodes.TYPE: NetworkDataCodes.TANK,
            NetworkDataCodes.TANK_ID: self.id,
            NetworkDataCodes.HEALTH: self.health,
            NetworkDataCodes.DAMAGE: dmg
        })
Пример #11
0
    def checkWithObjects(object, parent_id=None):
        try:
            collisions = Global.CollisionManager.objs_colliding(object)
        except AttributeError:
            return False

        if collisions:
            for tank in Global.getGameTanks():
                if tank in collisions:
                    if parent_id and parent_id == tank.id: continue

                    return True

            for obj in Global.getGameObjects():
                if obj in collisions:
                    return True
Пример #12
0
    def checkManualCollisionsWidthWalls(object):
        object_points = object.getPoints()

        for wall in Global.getGameWalls():
            wall_points = wall.getPoints()

            if Collisions.wallNearObject(object_points, wall_points):
                if Collisions.intersection(object_points, wall_points):
                    return True
Пример #13
0
    def getOrCreate(id, position, rotation, clan):

        obj = Global.getGameObject(id)

        if obj: return obj

        return ObjectFactory.create(id=id,
                                    position=position,
                                    rotation=rotation,
                                    clan=clan)
Пример #14
0
    def destroy(self, object):
        if object.get(NetworkDataCodes.TYPE) == NetworkDataCodes.WALL:
            id = object.get(NetworkDataCodes.ID)
            wall = Global.getGameWall(id)
            wall.destroy()

        if object.get(NetworkDataCodes.TYPE) == NetworkDataCodes.TANK:
            id = object.get(NetworkDataCodes.TANK_ID)
            tank = Global.getGameTank(id)
            tank.destroy()

        if object.get(NetworkDataCodes.TYPE) == NetworkDataCodes.STANDART_BULLET or \
                        object.get(NetworkDataCodes.TYPE) == NetworkDataCodes.HEAVY_BULLET:

            id = object.get(NetworkDataCodes.BULLET_ID)
            position = object.get(NetworkDataCodes.POSITION)
            bullet = Global.getGameBullet(id)
            if not bullet: return

            bullet.destroy(position)
Пример #15
0
    def checkWithWalls(object):
        try:
            collisions = Global.CollisionManager.objs_colliding(object)
        except AttributeError:
            return False

        if collisions:
            for wall in Global.getGameWalls():
                if wall in collisions:
                    return True

        return False
Пример #16
0
    def __init__(self, id=0, position=(0, 0), rotation=0, clan=1):
        super(Building, self).__init__(self.src)
        if not id: id = Global.getNextId()

        self.id = id
        self.position = position
        self.rotation = rotation
        self.clan = clan

        self.healthHelper = HealthSprite()
        self.updateHealthPosition()

        self.cshape = cm.AARectShape(self.position, self.width // 2,
                                     self.height // 2)
Пример #17
0
    def getPlayerByShortestDistanse(self):
        shortest_distanse = 0
        shortest_player = None

        for player in Global.getGameTanks():
            if player.clan == self.target.clan: continue

            distanse = self.getDistanceByPlayer(player)

            if not shortest_distanse or distanse < shortest_distanse:
                shortest_distanse = distanse
                shortest_player = player

        return shortest_player, shortest_distanse
Пример #18
0
    def fire(self):
        if not self.canFire: return

        id = Global.getNextId()
        rotation = self.rotation - 90
        animation_rotation = self.rotation
        parent_id = self.id

        animation_position = self.getFirePosition(-30, -10)
        position = self.getFirePosition(-30, -10)

        self.weapon.fire(id=id, position=position, animation_position=animation_position,
                         animation_rotation=animation_rotation, rotation=rotation, parent_id=parent_id)
        self.canFire = False
        Timer(self.bulletFreezTime, self.acceptFire).start()
Пример #19
0
    def getBuildingByShortestDistanse(self):
        shortest_distanse = 0
        shortest_building = None

        for building in Global.getGameObjects():
            # if building.type != 5: continue
            if building.clan == self.target.clan: continue

            x1, y1 = self.target.position
            x2, y2 = building.position
            distanse = getLength(x1, y1, x2, y2)

            if not shortest_distanse or distanse < shortest_distanse:
                shortest_distanse = distanse
                shortest_building = building

        return shortest_building, shortest_distanse
Пример #20
0
    def Network(self, data):
        #print('Network Receive', data)

        if data.get('action') == NetworkActions.INIT:
            index = int(data.get('connection_index'))
            type = data.get(NetworkDataCodes.TYPE)
            clan = data.get(NetworkDataCodes.CLAN)

            channel = Global.PullConnsctions[index]

            id = addGamePlayer(type=type, clan=clan, position=(200, 100))

            channel.Send({'action': NetworkActions.INIT, 'id': id})

        if data.get('action') == NetworkActions.TANK_MOVE:
            tank = Global.getGameTank(data.get(NetworkDataCodes.TANK_ID))
            tank.update(data)

        if data.get('action') == NetworkActions.TANK_FIRE:
            self.events.fire(data)

        if data.get('action') == NetworkActions.DAMAGE:
            self.events.damage(data)
Пример #21
0
    def create(instance,
               parent_id,
               position,
               rotation=0,
               last_update_time=time(),
               id=None,
               animation_instance=None,
               animation_position=None,
               animation_rotation=None,
               add_moving_handler=None):

        if not id: id = Global.getNextId()
        if not animation_position: animation_position = position
        if not animation_rotation: animation_rotation = rotation

        bullet = instance()
        bullet.id = id
        bullet.parent_id = parent_id
        bullet.position = position
        bullet.start_position = position
        bullet.rotation = rotation
        bullet.last_update_time = last_update_time

        bullet.animation_position = animation_position
        bullet.animation_rotation = animation_rotation

        addBulletToGame(bullet)

        if add_moving_handler:
            bullet.do(BulletMovingHandlers())

        if animation_instance:
            animation = animation_instance()
            animation.appendAnimationToLayer(animation_position,
                                             animation_rotation)

        return bullet
Пример #22
0
 def getEnemyCenter(self, clan):
     for obj in Global.getGameObjects():
         if isinstance(obj, Center) and obj.clan != clan:
             return obj
Пример #23
0
    def damage(self, object):
        id = object.get(NetworkDataCodes.TANK_ID)
        dmg = object.get(NetworkDataCodes.DAMAGE)
        health = object.get(NetworkDataCodes.HEALTH)

        Global.damageSomeTank(id=id, dmg=dmg, health=health)
Пример #24
0
    def destroy(self):
        animation = ExplosionTankAnimation()
        animation.appendAnimationToLayer(self.position)

        Global.EventDispatcher.dispatch_event('tank_destroy', self)
        Global.removeTankFromGame(self)