<commit_before>###

# Level

The level is used to orchestrate the placement of
LevelBlock's to form a level.showDialog: ->
new @Game.Dialog @settings.dialog
A level can be created through the LevelGenerator

Optionally, data can be passed in. All the blocks in
a level can access and use and modify this data.

###

Game = @Game

class Game.Level

  constructor: (@generator, @data = {}) ->
    @blocks = []
    @bufferLength = Crafty.viewport.width * 3
    @generationPosition = x: 0, y: 40
    @sceneryEvents = []
    @visibleHeight = Crafty.viewport.height - @generationPosition.y
    @shipType = 'PlayerSpaceship'

    { @namespace } = @data
    @currentScenery = @data.startScenery

  setScenery: (scenery) ->
    @_loadAssetsForScenery(scenery).then =>
      @currentScenery = scenery
      @_setupLevelScenery()

  _loadAssetsForScenery: (scenery) ->
    blockType = "#{@namespace}.#{scenery}"
    blockKlass = @generator.buildingBlocks[blockType]
    blockKlass::loadAssets().then =>
      nextLoading = []
      if prev = blockKlass::autoPrevious
        nextLoading.push => @_loadAssetsForScenery(prev)
      if next = blockKlass::autoNext
        nextLoading.push => @_loadAssetsForScenery(next)
      if nextLoading.length > 0
        WhenJS.sequence(nextLoading)

  start: ->
    Crafty.viewport.x = 0
    Crafty.viewport.y = 0
    @_controlsEnabled = yes
    @setForcedSpeed 1
    @setWeaponsEnabled yes

    @_scrollWall = Crafty.e('ScrollWall').attr
      x: 0
      y: 0

    @_playersActive = no

    Crafty.bind 'EnemySpawned', =>
      @data.enemiesSpawned ?= 0
      @data.enemiesSpawned += 1

    Crafty.e('2D, DOM, Text, HUD, LevelTitle')
      .attr(w: 200, h: 20)
      .positionHud(x: (Crafty.viewport.width - 200), y: 10, z: 2)
      .textFont(
        size: '10px'
        family: 'Press Start 2P'
      )
      .textColor '#A0A0A0'
      .text ''

    @_setupLevelScenery()

    Crafty.bind 'PlayerDied', =>
      playersActive = no
      Crafty('Player ControlScheme').each ->
        playersActive = yes if @lives > 0

      unless playersActive
        @stop()
        Crafty.trigger('GameOver')

    Crafty.bind 'GameLoop', @_waveTicks

  _waveTicks: (fd) =><commit_msg>Add space for titles<commit_after>###

# Level

The level is used to orchestrate the placement of
LevelBlock's to form a level.showDialog: ->
new @Game.Dialog @settings.dialog
A level can be created through the LevelGenerator

Optionally, data can be passed in. All the blocks in
a level can access and use and modify this data.

###

Game = @Game

class Game.Level

  constructor: (@generator, @data = {}) ->
    @blocks = []
    @bufferLength = Crafty.viewport.width * 3
    @generationPosition = x: 0, y: 40
    @sceneryEvents = []
    @visibleHeight = Crafty.viewport.height - @generationPosition.y
    @shipType = 'PlayerSpaceship'

    { @namespace } = @data
    @currentScenery = @data.startScenery

  setScenery: (scenery) ->
    @_loadAssetsForScenery(scenery).then =>
      @currentScenery = scenery
      @_setupLevelScenery()

  _loadAssetsForScenery: (scenery) ->
    blockType = "#{@namespace}.#{scenery}"
    blockKlass = @generator.buildingBlocks[blockType]
    blockKlass::loadAssets().then =>
      nextLoading = []
      if prev = blockKlass::autoPrevious
        nextLoading.push => @_loadAssetsForScenery(prev)
      if next = blockKlass::autoNext
        nextLoading.push => @_loadAssetsForScenery(next)
      if nextLoading.length > 0
        WhenJS.sequence(nextLoading)

  start: ->
    Crafty.viewport.x = 0
    Crafty.viewport.y = 0
    @_controlsEnabled = yes
    @setForcedSpeed 1
    @setWeaponsEnabled yes

    @_scrollWall = Crafty.e('ScrollWall').attr
      x: 0
      y: 0

    @_playersActive = no

    Crafty.bind 'EnemySpawned', =>
      @data.enemiesSpawned ?= 0
      @data.enemiesSpawned += 1

    Crafty.e('2D, DOM, Text, HUD, LevelTitle')
      .attr(w: 250, h: 20)
      .positionHud(x: (Crafty.viewport.width - 250), y: 10, z: 2)
      .textFont(
        size: '10px'
        family: 'Press Start 2P'
      )
      .textColor '#A0A0A0'
      .text ''

    @_setupLevelScenery()

    Crafty.bind 'PlayerDied', =>
      playersActive = no
      Crafty('Player ControlScheme').each ->
        playersActive = yes if @lives > 0

      unless playersActive
        @stop()
        Crafty.trigger('GameOver')

    Crafty.bind 'GameLoop', @_waveTicks

  _waveTicks: (fd) =>
{'fraction_non_alphanumeric': 0.12009970541581691, 'fraction_numerical': 0.014049399501472921, 'mean_word_length': 2.8051724137931036, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '747281', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1481, 'n_words': 349}