<commit_before>var _ = require('lodash')
var inherits = require('inherits')
var aabb = require('aabb-2d')
var math = require('mathjs')
var transform = require('./transform.js')
var circle = require('./geo/circle.js')
var Entity = require('crtrdg-entity')

module.exports = Player;
inherits(Player, Entity);

function Player(opts){
  this.velocity = opts.velocity
  this.speed = opts.speed
  this.friction = opts.friction
  this.geometry = circle({
    fill: opts.color, 
    stroke: opts.color,
    scale: 2,
    position: opts.position,
    angle: opts.angle,
    children: [
      circle({fill: '#EB8686', stroke: '#EB8686', position: [-0.75, -1], scale: 0.5}), 
      circle({fill: '#EB8686', stroke: '#EB8686', position: [0.75, -1], scale: 0.5})
    ]
  })
}

Player.prototype.move = function(velocity, world) {
  var self = this

  var t = transform({
    position: self.geometry.transform.position(),
    scale: self.geometry.transform.scale(),
    angle: self.geometry.transform.angle()
  })
  self.geometry.stage(t, {invert: true})

  var rad = t.angle() * Math.PI / 180
  var delta = {}
  delta.position = [
    velocity.position[0] * Math.cos(rad) - velocity.position[1] * Math.sin(rad),
    velocity.position[0] * Math.sin(rad) + velocity.position[1] * Math.cos(rad)
  ]
  delta.angle = velocity.angle

  self.geometry.transform.update(delta)
  self.geometry.stage(self.geometry.transform)

  var collisions = world.intersects(self.geometry)
  if (collisions) {
    // find colliding object with largest overlap vector
    var ind = _.indexOf(collisions, _.max(collisions, function (i) {return i.response.overlap}))
    // adjust using overlap vector
    self.velocity.position[0] -= self.velocity.position[0]
    self.velocity.position[1] -= self.velocity.position[1]
  }

}

Player.prototype.keyboardInput = function(keyboard){
  if ('E' in keyboard.keysDown){
    this.velocity.position[0] = this.speed;
  }

  if ('Q' in keyboard.keysDown){
    this.velocity.position[0] = -this.speed;
  }

  if ('S' in keyboard.keysDown) {
    this.velocity.position[1] = this.speed;
  }

  if ('W' in keyboard.keysDown) {
    this.velocity.position[1] = -this.speed;
  }<commit_msg>Improved behavior<commit_after>var _ = require('lodash')
var inherits = require('inherits')
var aabb = require('aabb-2d')
var math = require('mathjs')
var transform = require('./transform.js')
var circle = require('./geo/circle.js')
var Entity = require('crtrdg-entity')

module.exports = Player;
inherits(Player, Entity);

function Player(opts){
  this.velocity = opts.velocity
  this.speed = opts.speed
  this.friction = opts.friction
  this.geometry = circle({
    fill: opts.color, 
    stroke: opts.color,
    scale: 2,
    position: opts.position,
    angle: opts.angle,
    children: [
      circle({fill: '#EB8686', stroke: '#EB8686', position: [-0.75, -1], scale: 0.5}), 
      circle({fill: '#EB8686', stroke: '#EB8686', position: [0.75, -1], scale: 0.5})
    ]
  })
}

Player.prototype.move = function(velocity, world) {
  var self = this

  var t = transform({
    position: self.geometry.transform.position(),
    scale: self.geometry.transform.scale(),
    angle: self.geometry.transform.angle()
  })
  self.geometry.stage(t, {invert: true})

  var rad = t.angle() * Math.PI / 180
  var delta = {}
  delta.position = [
    velocity.position[0] * Math.cos(rad) - velocity.position[1] * Math.sin(rad),
    velocity.position[0] * Math.sin(rad) + velocity.position[1] * Math.cos(rad)
  ]
  delta.angle = velocity.angle

  self.geometry.transform.update(delta)
  self.geometry.stage(self.geometry.transform)

  var collisions = world.intersects(self.geometry)
  if (collisions) {
    var ind = _.indexOf(collisions, _.max(collisions, function (i) {return i.response.overlap}))
    var t = transform({
      position: self.geometry.transform.position(),
      scale: self.geometry.transform.scale(),
      angle: self.geometry.transform.angle()
    })
    self.geometry.stage(t, {invert: true})
    var correction = {
      position: [
        -delta.position[0] + collisions[ind].response.overlapV.x, 
        -delta.position[1] + collisions[ind].response.overlapV.y, 
      ]
    }
    self.geometry.transform.update(correction)
    self.geometry.stage(self.geometry.transform)
  }

}

Player.prototype.keyboardInput = function(keyboard){
  if ('E' in keyboard.keysDown){
    this.velocity.position[0] = this.speed;
  }

  if ('Q' in keyboard.keysDown){
    this.velocity.position[0] = -this.speed;
  }

  if ('S' in keyboard.keysDown) {
    this.velocity.position[1] = this.speed;
  }

  if ('W' in keyboard.keysDown) {
    this.velocity.position[1] = -this.speed;
  }
{'fraction_non_alphanumeric': 0.16087516087516088, 'fraction_numerical': 0.018876018876018877, 'mean_word_length': 3.470757430488974, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '848907', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1537, 'n_words': 317}