Пример #1
0
function health(amount) {
  return bs.component('health')
    .on('init', function() {
      this.health = amount
      this.dead = false
    })
    .on('damaged', function(dmg) {
      this.health -= dmg
      if (!this.dead && this.health <= 0) {
        this.dead = true
        this.trigger('died')
      }
    })
}
Пример #2
0
function createBody(body, fixture) {
  return bs.component('body')
    .needs('attached')
    .needs('physical')
    .on('init', function() {
      this.body = this.world.CreateBody(body.call(this, this.world))
      this.fixture = this.body.CreateFixture(fixture.call(this, this.body, this.world))
    })
    .on('destroy', function() {
      this.body.DestroyFixture(this.fixture)
      this.world.DestroyBody(this.body)
      this.game.remove(this)
      delete this.body
      delete this.world
      delete this.game
    })
}
Пример #3
0
function pellet(c) {
  var pelletMax = 40

  return bs.define()
    .use(require('../components/attached'))
    .use(require('../components/physical'))
    .use(require('../components/body')(
      function createBody() {
        var bd = new b2BodyDef
        bd.position = new b2Vec2(0, -5)
        bd.type = b2Body.b2_dynamicBody
        bd.userData = {}
        bd.fixedRotation = true
        return bd
      },
      function createFixture() {
        var fd = new b2FixtureDef
        fd.restitution = 0.5
        fd.shape = new b2CircleShape(0.5/3)
        this.r = 5
        return fd
      }
    ))
    .use(require('../components/bounce-burst'))
    .use(bs.component()
      .on('init', function() {
        pelletCounter += 1
        if (pelletCounter > pelletMax) this.flagged = true
        this.c = c
        this.t = 140 - ((Math.random() * 40)|0)
      })
      .on('tick', function() {
        this.t -= 1
        if (!this.t) this.flagged = true
      })
      .on('destroy', function() {
        pelletCounter -= 1
      })
    )
    .use(require('../components/draw-circle')(5))
    .use(require('../components/gravity'))
}
Пример #4
0
module.exports = function drawCircle(r, c) {
  var tau = Math.PI * 2

  this.c = this.c || c
  this.r = this.r || (+r|0) || 15

  return bs.component()
    .needs('attached')
    .needs('body')
    .on('draw', function(ctx) {
      ctx.fillStyle = this.c
      ctx.beginPath()
      ctx.arc(
          this.body.m_xf.position.x * 30 - this.game.camera.pos[0]
        , this.body.m_xf.position.y * 30 - this.game.camera.pos[1]
        , this.r
        , 0
        , tau
        , true
      )
      ctx.closePath()
      ctx.fill()
    })
}
Пример #5
0
var bs = require('bindlestiff')

var Bullet = require('./player-bullet')

var tau = Math.PI * 2
var round = Math.round
var abs = Math.abs
var b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
var b2FixtureDef = Box2D.Dynamics.b2FixtureDef
var b2BodyDef = Box2D.Dynamics.b2BodyDef
var b2Vec2 = Box2D.Common.Math.b2Vec2
var b2Body = Box2D.Dynamics.b2Body
var tempPosition = [0,0]

var player = bs.component([
    'player'
  , 'body'
])
  .needs('physical')
  .needs('attached')
  .needs('controllable')
  .on('init', function() {
    var x = this.game.width / 60
    var y = this.game.height / 60

    var def = new b2BodyDef
    def.position = new b2Vec2(20, 0)
    def.type = b2Body.b2_dynamicBody
    def.userData = {}
    def.fixedRotation = true

    this.flinch = 0
Пример #6
0
var bs = require('bindlestiff')

module.exports = PhysicsSystem

function PhysicsSystem(game) {
  if (!(this instanceof PhysicsSystem)) return new PhysicsSystem(game)
  this.game = game
}

PhysicsSystem.prototype.tick = function() {
  var physical = this.game.manager.find('physical')

  for (var i = 0, l = physical.length; i < l; i += 1) {
    var p = physical[i]

    p.position[0] += p.speed[0]
    p.position[1] += p.speed[1]
  }
}

PhysicsSystem.component = bs.component('physical')
  .on('init', function() {
    this.position = new Float64Array(2)
    this.speed = new Float64Array(2)
  })