<commit_before>defaults = require('lodash/defaults')

Crafty.c 'Enemy',
  init: ->
    @requires '2D, WebGL, Collision, Tween, Choreography, Hideable, Flipable, Scalable, SunBlock, Hostile'
    @attr
      pointsOnHit: 0
      pointsOnDestroy: 0
      damage: 2
    @invincible = no
    @bind 'HitFlash', @applyHitFlash

  onProjectileHit: (collisions) ->
    return if Game.paused
    return if @hidden

    collisions.forEach((e) =>
      bullet = e.obj
      unless @invincible
        unless @juice is no
          @trigger('HitFlash', true)
        @absorbDamage(bullet)

        @trigger('Hit', entity: this, projectile: bullet)
      bullet.trigger('BulletHit', bullet)
    )

  onProjectileHitEnd: ->
    @trigger('HitFlash', false)

  onExplosionHit: (e) ->
    return if Game.paused
    return if @hidden
    return if @invincible
    for c in e
      splosion = c.obj
      if splosion.damage > 0
        @trigger('Hit', entity: this, projectile: splosion)
        unless @juice is no
          @trigger('HitFlash', true)
        @absorbDamage(splosion)
        splosion.damage = 0

  applyHitFlash: (onOff) ->
    if onOff
      @attr hitFlash: { _red: 255, _green: 255, _blue: 255 }
    else
      @attr hitFlash: no

  enemy: (options = {}) ->
    options = defaults(options,
      projectile: 'Bullet'
      pointsLocation: {
        x: @w / 2
        y: @h / 2
      }
    )
    @reveal()
    @pointsLocation = options.pointsLocation
    Crafty.trigger('EnemySpawned', this)
    @onHit(
      options.projectile
      (e) => @onProjectileHit(e)
      => @onProjectileHitEnd()
    )
    @onHit(
      'Explosion'
      (e) => @onExplosionHit(e)
      => @onProjectileHitEnd()
    )
    @updatedHealth?()
    this

  absorbDamage: (cause) ->
    return unless cause?<commit_msg>Fix double listening to collisions<commit_after>defaults = require('lodash/defaults')

Crafty.c 'Enemy',
  required: '2D, WebGL, Collision, Tween, Choreography, Hideable, Flipable, Scalable, SunBlock, Hostile'
  events:
    HitOn: '_onCollisonHit'
    HitOff: '_onCollisonHitOff'
    HitFlash: 'applyHitFlash'

  init: ->
    @attr
      pointsOnHit: 0
      pointsOnDestroy: 0
      damage: 2
    @invincible = no

  _onCollisonHit: (collisions) ->
    return if Game.paused
    return if @hidden

    collisions.forEach((e) =>
      bulletOrExplosion = e.obj
      unless @invincible
        if bulletOrExplosion.damage > 0
          unless @juice is no
            @trigger('HitFlash', true)
          @absorbDamage(bulletOrExplosion)
          bulletOrExplosion.damage = 0

          @trigger('Hit', entity: this, projectile: bulletOrExplosion)

      if bulletOrExplosion.has('Bullet')
        bulletOrExplosion.trigger('BulletHit', bulletOrExplosion)
    )

  _onCollisonHitOff: (component) ->
    @trigger('HitFlash', false)

  onExplosionHit: (e) ->
    return if Game.paused
    return if @hidden
    return if @invincible
    for c in e
      splosion = c.obj
      if splosion.damage > 0
        @trigger('Hit', entity: this, projectile: splosion)
        unless @juice is no
          @trigger('HitFlash', true)
        @absorbDamage(splosion)
        splosion.damage = 0

  applyHitFlash: (onOff) ->
    if onOff
      @attr hitFlash: { _red: 255, _green: 255, _blue: 255 }
    else
      @attr hitFlash: no

  enemy: (options = {}) ->
    options = defaults(options,
      projectile: 'Bullet'
      pointsLocation: {
        x: @w / 2
        y: @h / 2
      }
    )
    @reveal()
    @pointsLocation = options.pointsLocation
    Crafty.trigger('EnemySpawned', this)
    @checkHits(options.projectile, 'Explosion')
    @updatedHealth?()
    this

  absorbDamage: (cause) ->
    return unless cause?
{'fraction_non_alphanumeric': 0.11497975708502024, 'fraction_numerical': 0.009716599190283401, 'mean_word_length': 2.4635514018691587, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1047906', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1215, 'n_words': 293}