def create_bullet(dispatcher, entity_id, x, y, vx, vy): bullet = Entity(entity_id) bullet.add_component( WidgetComponent(dispatcher, Widget([['*']], [['red']]))) bullet.add_component(PositionComponent(dispatcher, x, y, vx, vy)) bullet.add_component(ProjectileCollisionComponent(dispatcher, damage=1)) bullet.add_component(DestructorComponent(dispatcher)) dispatcher.add_event(BearEvent('ecs_create', bullet)) dispatcher.add_event( BearEvent('ecs_add', (bullet.id, bullet.position.x, bullet.position.y))) return bullet
def create_spawner_house(dispatcher, atlas, x, y): house = Entity('house') house.add_component( WidgetComponent(dispatcher, Widget(*atlas.get_element('spawner')))) house.add_component(DestructorComponent(dispatcher)) house.add_component(PositionComponent(dispatcher, x, y)) dispatcher.add_event(BearEvent('ecs_create', house)) dispatcher.add_event( BearEvent('ecs_add', (house.id, house.position.x, house.position.y)))
def create_barrel(atlas, dispatcher, x, y): barrel_entity = Entity(id='Barrel') widget = SimpleAnimationWidget(Animation( (atlas.get_element('barrel_1'), atlas.get_element('barrel_2')), 2), emit_ecs=True) widget_component = WidgetComponent(dispatcher, widget, owner=barrel_entity) position_component = PositionComponent(dispatcher, x=x, y=y, owner=barrel_entity) collision = CollisionComponent(dispatcher, owner=barrel_entity) dispatcher.add_event( BearEvent(event_type='ecs_create', event_value=barrel_entity)) dispatcher.add_event( BearEvent(event_type='ecs_add', event_value=('Barrel', x, y)))
def create_cop(atlas, dispatcher, x, y): """ Create a cop entity :param dispatcher: :return: """ cop_entity = Entity(id='cop') t = Widget(*atlas.get_element('cop_r')) widget = deserialize_widget(repr(t)) widget_component = WidgetComponent(dispatcher, widget, owner=cop_entity) position_component = WalkerComponent(dispatcher, x=x, y=y, owner=cop_entity) collision = WalkerCollisionComponent(dispatcher, owner=cop_entity) passing = PassingComponent(dispatcher, shadow_pos=(0, 15), shadow_size=(13, 3), owner=cop_entity) dispatcher.add_event( BearEvent(event_type='ecs_create', event_value=cop_entity)) dispatcher.add_event( BearEvent(event_type='ecs_add', event_value=('cop', x, y)))
def create_wall(dispatcher, atlas, entity_id, x, y): wall = Entity(entity_id) wall.add_component(PositionComponent(dispatcher, x, y)) wall.add_component(CollisionComponent(dispatcher)) wall.add_component(PassingComponent(dispatcher)) wall.add_component(DestructorComponent(dispatcher)) images_dict = { 'wall_3': atlas.get_element('wall_3'), 'wall_2': atlas.get_element('wall_2'), 'wall_1': atlas.get_element('wall_1') } wall.add_component( SwitchWidgetComponent( dispatcher, SwitchingWidget(images_dict=images_dict, initial_image='wall_3'))) wall.add_component( VisualDamageHealthComponent(dispatcher, hitpoints=3, widgets_dict={ 3: 'wall_3', 2: 'wall_2', 1: 'wall_1' })) dispatcher.add_event(BearEvent('ecs_create', wall)) dispatcher.add_event( BearEvent('ecs_add', (wall.id, wall.position.x, wall.position.y))) pass
def create_enemy_tank(dispatcher, atlas, entity_id, x, y): # ControllerComponent # DestructorHealthComponent # WalkerCollisionComponent # SwitchWidgetComponent enemy = Entity(id=entity_id) # Adding all necessary components, in our case input (which also spawns # bullets), two collision-related ones, position, health and a destructor # for orderly entity removal. enemy.add_component(WalkerCollisionComponent(dispatcher)) enemy.add_component(PassingComponent(dispatcher)) enemy.add_component(PositionComponent(dispatcher, x, y)) enemy.add_component(DestructorHealthComponent(dispatcher, hitpoints=1)) enemy.add_component(DestructorComponent(dispatcher)) enemy.add_component(ControllerComponent(dispatcher)) # Also a WidgetComponent, which requires a Widget images_dict = { 'enemy_r': atlas.get_element('enemy_r'), 'enemy_l': atlas.get_element('enemy_l'), 'enemy_d': atlas.get_element('enemy_d'), 'enemy_u': atlas.get_element('enemy_u') } enemy.add_component( SwitchWidgetComponent( dispatcher, SwitchingWidget(images_dict=images_dict, initial_image='enemy_r'))) dispatcher.add_event(BearEvent('ecs_create', enemy)) dispatcher.add_event( BearEvent('ecs_add', (enemy.id, enemy.position.x, enemy.position.y))) return enemy
def create_player_tank(dispatcher, atlas, x, y): # Creating the actual entity, which currently has only a name player = Entity(id='player') # Adding all necessary components, in our case input (which also spawns # bullets), two collision-related ones, position, health and a destructor # for orderly entity removal. player.add_component(InputComponent(dispatcher)) player.add_component(WalkerCollisionComponent(dispatcher)) player.add_component(PassingComponent(dispatcher)) player.add_component(PositionComponent(dispatcher, x, y)) player.add_component(DestructorHealthComponent(dispatcher, hitpoints=5)) player.add_component(DestructorComponent(dispatcher)) # Also a WidgetComponent, which requires a Widget images_dict = { 'player_r': atlas.get_element('player_r'), 'player_l': atlas.get_element('player_l'), 'player_d': atlas.get_element('player_d'), 'player_u': atlas.get_element('player_u') } player.add_component( SwitchWidgetComponent( dispatcher, SwitchingWidget(images_dict=images_dict, initial_image='player_r'))) dispatcher.add_event(BearEvent('ecs_create', player)) dispatcher.add_event( BearEvent('ecs_add', (player.id, player.position.x, player.position.y))) return player