<commit_before>/**
 * @license
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

foam.CLASS({
  package: 'foam.demos.physics',
  name: 'Bubbles',
  extends: 'foam.graphics.Box',

  requires: [
    'foam.physics.PhysicalCircle',
    'foam.physics.PhysicsEngine',
    'foam.util.Timer'
  ],

  properties: [
    {
      name: 'timer',
      factory: function() {
        var timer = this.Timer.create();
        timer.start();
        return timer;
      }
    },
    [ 'n',          7 ],
    [ 'width',      800 ],
    [ 'height',     600 ],
    [ 'background', '#ccf' ],
    { name: 'engine',   factory: function() {
      return this.onDestroy(this.PhysicsEngine.create().start());
    }}
  ],

  methods: [
    function initCView() {
      this.SUPER();

      var N = this.n;

      for ( var x = 0 ; x < N ; x++ ) {
        for ( var y = 0 ; y < N ; y++ ) {
          var c = this.PhysicalCircle.create({
            radius: 15,
            x: 400+(x-(N-1)/2)*70,
            y: 200+(y-(N-1)/2)*70,
            borderWidth: 6,
            arcWidth: 6, // TODO
            color: 'white',
            border: 'hsl(' + x/N*100 + ',' + (70+y/N*30) + '%, 60%)'
          });
          this.addChildren(c);

          c.y$.sub(function(c) {
            if ( c.y > 1/this.scaleY*this.height+50 ) {
              c.y = -50;
            }
          }.bind(this, c));

          c.gravity = 0.03;
          c.friction = 0.96;
          this.bounceOnWalls(c, this.width, this.height);
          this.engine.add(c);
        }
      }

      var count = 0;
      this.timer.i$.sub(function(s) {
        this.invalidated.pub();
        if ( count === 100 ) return;
        count++;
//        if ( count++ == 100 ) s.destroy();

        var b = this.PhysicalCircle.create({
          radius: 3,
          x: this.width * Math.random(),
          y: this.height/this.scaleY,
          color: 'white',
          borderWidth: 1,
          arcWidth: 1, // TODO
          border: 'blue',
          mass: 0.3
        });

        b.y$.sub(function() {
          if ( b.y < 1 ) {
            b.y = this.height;
            b.x = this.width * Math.random();
          }
        }.bind(this));

        b.vy = -4;
        b.gravity = -0.2;
        b.friction = 0.9;
        this.engine.add(b);

        this.addChildren(b);
      }.bind(this));

      this.engine.start();
    },

    function bounceOnWalls(c, w, h) {
      // TODO:
      /*<commit_msg>Avoid starting Physics engine twice.<commit_after>/**
 * @license
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

foam.CLASS({
  package: 'foam.demos.physics',
  name: 'Bubbles',
  extends: 'foam.graphics.Box',

  requires: [
    'foam.physics.PhysicalCircle',
    'foam.physics.PhysicsEngine',
    'foam.util.Timer'
  ],

  properties: [
    {
      name: 'timer',
      factory: function() {
        var timer = this.Timer.create();
        timer.start();
        return timer;
      }
    },
    [ 'n',          7 ],
    [ 'width',      800 ],
    [ 'height',     600 ],
    [ 'background', '#ccf' ],
    { name: 'engine',   factory: function() {
      return this.onDestroy(this.PhysicsEngine.create().start());
    }}
  ],

  methods: [
    function initCView() {
      this.SUPER();

      var N = this.n;

      for ( var x = 0 ; x < N ; x++ ) {
        for ( var y = 0 ; y < N ; y++ ) {
          var c = this.PhysicalCircle.create({
            radius: 15,
            x: 400+(x-(N-1)/2)*70,
            y: 200+(y-(N-1)/2)*70,
            borderWidth: 6,
            arcWidth: 6, // TODO
            color: 'white',
            border: 'hsl(' + x/N*100 + ',' + (70+y/N*30) + '%, 60%)'
          });
          this.addChildren(c);

          c.y$.sub(function(c) {
            if ( c.y > 1/this.scaleY*this.height+50 ) {
              c.y = -50;
            }
          }.bind(this, c));

          c.gravity = 0.03;
          c.friction = 0.96;
          this.bounceOnWalls(c, this.width, this.height);
          this.engine.add(c);
        }
      }

      var count = 0;
      this.timer.i$.sub(function(s) {
        this.invalidated.pub();
        if ( count === 100 ) return;
        count++;
//        if ( count++ == 100 ) s.destroy();

        var b = this.PhysicalCircle.create({
          radius: 3,
          x: this.width * Math.random(),
          y: this.height/this.scaleY,
          color: 'white',
          borderWidth: 1,
          arcWidth: 1, // TODO
          border: 'blue',
          mass: 0.3
        });

        b.y$.sub(function() {
          if ( b.y < 1 ) {
            b.y = this.height;
            b.x = this.width * Math.random();
          }
        }.bind(this));

        b.vy = -4;
        b.gravity = -0.2;
        b.friction = 0.9;
        this.engine.add(b);

        this.addChildren(b);
      }.bind(this));
    },

    function bounceOnWalls(c, w, h) {
      // TODO:
      /*
{'fraction_non_alphanumeric': 0.1541798765226097, 'fraction_numerical': 0.024361755381278157, 'mean_word_length': 1.6312554872695346, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2926717', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1907, 'n_words': 487}