<commit_before>IndieSprite = require 'lib/surface/IndieSprite'
{me} = require 'lib/auth'

module.exports = class WizardSprite extends IndieSprite
  # Wizard targets are constantly changing, so a simple tween doesn't work.
  # Instead, the wizard stores its origin point and the (possibly) moving target.
  # Then it figures out its current position based on tween percentage and
  # those two points.
  tweenPercentage: 1.0
  originPos: null
  targetPos: null
  targetSprite: null
  reachedTarget: true
  spriteXOffset: 4  # meters from target sprite
  spriteYOffset: 0  # meters from target sprite

  subscriptions:
    'bus:player-states-changed': 'onPlayerStatesChanged'
    'me:synced': 'onMeSynced'
    'surface:sprite-selected': 'onSpriteSelected'
    'echo-self-wizard-sprite': 'onEchoSelfWizardSprite'
    'echo-all-wizard-sprites': 'onEchoAllWizardSprites'

  shortcuts:
    'up': 'onMoveKey'
    'down': 'onMoveKey'
    'left': 'onMoveKey'
    'right': 'onMoveKey'

  constructor: (thangType, options) ->
    if @isSelf = options.isSelf
      options.colorConfig = $.extend(true, {}, me.get('wizard')?.colorConfig) or {}
    super thangType, options
    @targetPos = @thang.pos
    if @isSelf
      @setNameLabel me.displayName()
    else if options.name
      @setNameLabel options.name

  makeIndieThang: (thangType, options) ->
    thang = super thangType, options
    thang.isSelectable = false
    thang.bobHeight = 0.75
    thang.bobTime = 2
    thang.pos.z += thang.bobHeight
    thang

  finishSetup: ->
    @updateBaseScale()
    @scaleFactor = @thang.scaleFactor if @thang?.scaleFactor
    @updateScale()
    @updateRotation()
    # Don't call general update() because Thang isn't built yet

  setNameLabel: (name) ->
    if @options.codeLanguage and @options.codeLanguage isnt 'javascript' and not @isSelf
      name += " (#{@options.codeLanguage})"  # TODO: move on second line, capitalize properly
    super name

  toggle: (to) ->
    @imageObject?.visible = to
    label[if to then 'show' else 'hide']() for name, label of @labels
    mark.mark?.visible = to for name, mark of @marks

  onPlayerStatesChanged: (e) ->
    for playerID, state of e.states
      continue unless playerID is @thang.id
      @setEditing state.wizard?.editing
      continue if playerID is me.id  # ignore changes for self wizard sprite
      @setNameLabel state.name
      continue unless state.wizard?
      if targetID = state.wizard.targetSprite
        return console.warn 'Wizard Sprite couldn\'t find target sprite', targetID unless targetID of @options.sprites
        @setTarget @options.sprites[targetID]
      else
        @setTarget state.wizard.targetPos

  onMeSynced: (e) ->
    return unless @isSelf
    @setNameLabel me.displayName() if @imageObject.visible  # not if we hid the wiz
    newColorConfig = me.get('wizard')?.colorConfig or {}
    shouldUpdate = not _.isEqual(newColorConfig, @options.colorConfig)
    @options.colorConfig = $.extend(true, {}, newColorConfig)
    if shouldUpdate
      @setupSprite()
      @playAction(@currentAction)

  onSpriteSelected: (e) ->
    return unless @isSelf
    @setTarget e.sprite or e.worldPos

  animateIn: ->
    @imageObject.scaleX = @imageObject.scaleY = @imageObject.alpha = 0
    createjs.Tween.get(@imageObject)
      .to({scaleX: 1, scaleY: 1, alpha: 1}, 1000, createjs.Ease.getPowInOut(2.2))
    @labels.name?.show()

  animateOut: (callback) ->
    tween = createjs.Tween.get(@imageObject)
      .to({scaleX: 0, scaleY: 0, alpha: 0}, 1000, createjs.Ease.getPowInOut(2.2))
    tween.call(callback) if callback
    @labels.name?.hide()

  setEditing: (@editing) ->
    if @editing
      @thang.actionActivated = @thang.action isnt 'cast'
      @thang.action = 'cast'
    else
      @thang.action = 'idle' if @thang.action is 'cast'

  setInitialState: (targetPos, @targetSprite) ->
    @targetPos = @getPosFromTarget(@targetSprite or targetPos)
    @endMoveTween()
<commit_msg>Fixed harmless Wizard startup animation error.<commit_after>IndieSprite = require 'lib/surface/IndieSprite'
{me} = require 'lib/auth'

module.exports = class WizardSprite extends IndieSprite
  # Wizard targets are constantly changing, so a simple tween doesn't work.
  # Instead, the wizard stores its origin point and the (possibly) moving target.
  # Then it figures out its current position based on tween percentage and
  # those two points.
  tweenPercentage: 1.0
  originPos: null
  targetPos: null
  targetSprite: null
  reachedTarget: true
  spriteXOffset: 4  # meters from target sprite
  spriteYOffset: 0  # meters from target sprite

  subscriptions:
    'bus:player-states-changed': 'onPlayerStatesChanged'
    'me:synced': 'onMeSynced'
    'surface:sprite-selected': 'onSpriteSelected'
    'echo-self-wizard-sprite': 'onEchoSelfWizardSprite'
    'echo-all-wizard-sprites': 'onEchoAllWizardSprites'

  shortcuts:
    'up': 'onMoveKey'
    'down': 'onMoveKey'
    'left': 'onMoveKey'
    'right': 'onMoveKey'

  constructor: (thangType, options) ->
    if @isSelf = options.isSelf
      options.colorConfig = $.extend(true, {}, me.get('wizard')?.colorConfig) or {}
    super thangType, options
    @targetPos = @thang.pos
    if @isSelf
      @setNameLabel me.displayName()
    else if options.name
      @setNameLabel options.name

  makeIndieThang: (thangType, options) ->
    thang = super thangType, options
    thang.isSelectable = false
    thang.bobHeight = 0.75
    thang.bobTime = 2
    thang.pos.z += thang.bobHeight
    thang

  finishSetup: ->
    @updateBaseScale()
    @scaleFactor = @thang.scaleFactor if @thang?.scaleFactor
    @updateScale()
    @updateRotation()
    # Don't call general update() because Thang isn't built yet

  setNameLabel: (name) ->
    if @options.codeLanguage and @options.codeLanguage isnt 'javascript' and not @isSelf
      name += " (#{@options.codeLanguage})"  # TODO: move on second line, capitalize properly
    super name

  toggle: (to) ->
    @imageObject?.visible = to
    label[if to then 'show' else 'hide']() for name, label of @labels
    mark.mark?.visible = to for name, mark of @marks

  onPlayerStatesChanged: (e) ->
    for playerID, state of e.states
      continue unless playerID is @thang.id
      @setEditing state.wizard?.editing
      continue if playerID is me.id  # ignore changes for self wizard sprite
      @setNameLabel state.name
      continue unless state.wizard?
      if targetID = state.wizard.targetSprite
        return console.warn 'Wizard Sprite couldn\'t find target sprite', targetID unless targetID of @options.sprites
        @setTarget @options.sprites[targetID]
      else
        @setTarget state.wizard.targetPos

  onMeSynced: (e) ->
    return unless @isSelf
    @setNameLabel me.displayName() if @imageObject.visible  # not if we hid the wiz
    newColorConfig = me.get('wizard')?.colorConfig or {}
    shouldUpdate = not _.isEqual(newColorConfig, @options.colorConfig)
    @options.colorConfig = $.extend(true, {}, newColorConfig)
    if shouldUpdate
      @setupSprite()
      @playAction(@currentAction) if @currentAction

  onSpriteSelected: (e) ->
    return unless @isSelf
    @setTarget e.sprite or e.worldPos

  animateIn: ->
    @imageObject.scaleX = @imageObject.scaleY = @imageObject.alpha = 0
    createjs.Tween.get(@imageObject)
      .to({scaleX: 1, scaleY: 1, alpha: 1}, 1000, createjs.Ease.getPowInOut(2.2))
    @labels.name?.show()

  animateOut: (callback) ->
    tween = createjs.Tween.get(@imageObject)
      .to({scaleX: 0, scaleY: 0, alpha: 0}, 1000, createjs.Ease.getPowInOut(2.2))
    tween.call(callback) if callback
    @labels.name?.hide()

  setEditing: (@editing) ->
    if @editing
      @thang.actionActivated = @thang.action isnt 'cast'
      @thang.action = 'cast'
    else
      @thang.action = 'idle' if @thang.action is 'cast'

  setInitialState: (targetPos, @targetSprite) ->
    @targetPos = @getPosFromTarget(@targetSprite or targetPos)
    @endMoveTween()

{'fraction_non_alphanumeric': 0.11319330901773361, 'fraction_numerical': 0.006791598541064017, 'mean_word_length': 3.435025097601785, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '229455', 'n_tokens_mistral': 2630, 'n_tokens_neox': 2541, 'n_words': 681}