Skip to content
This repository has been archived by the owner on Dec 27, 2018. It is now read-only.

Commit

Permalink
bombs!
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsk committed Aug 25, 2013
1 parent 4208abb commit 0f15378
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 2 deletions.
1 change: 1 addition & 0 deletions components/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = createBody

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))
Expand Down
2 changes: 1 addition & 1 deletion components/enemy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function(
return bs.define()
.use(require('../components/attached'))
.use(require('../components/physical'))
.use(require('../components/health')(10))
.use(require('../components/health')(5))
.use(bs.component()
.on('init', function() {
this.base_r =
Expand Down
58 changes: 58 additions & 0 deletions components/explosive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var bs = require('bindlestiff')

module.exports = explosive

function explosive(force) {
var Bullet = require('../entities/player-bullet')
var Box2D = require('box2dweb-commonjs').Box2D
var b2Vec2 = Box2D.Common.Math.b2Vec2
var tau = Math.PI * 2

return bs.component('explosive')
.needs('attached')
.needs('physical')
.needs('body')
.on('explode', function() {
if (this.flagged) return
this.flagged = true

var bodies = this.game.find('body')
var tx = this.body.m_xf.position.x
var ty = this.body.m_xf.position.y
var center = this.body.m_sweep.c
var tempVec = {x:0,y:0}

for (var i = 0; i < bodies.length; i += 1) {
var b = bodies[i]
if (b !== this.game.player.body) {
var p = b.body.m_xf.position
var dy = p.y - ty
var dx = p.x - tx
if (Math.abs(dy) + Math.abs(dx) < 30) {
var a = Math.atan2(dy, dx)
tempVec.x = Math.cos(a) * 40
tempVec.y = Math.sin(a) * 40
b.body.ApplyImpulse(tempVec, center)
b.trigger('damaged', 3)
}
}
}

this.game.next(function() {
for (var i = 0; i < 1; i += 0.05) {
var bullet = new Bullet
var dx = Math.cos(i * tau)
var dy = Math.sin(i * tau)
bullet.body.SetPosition(new b2Vec2(
tx + dx * 0.5
, ty + dy * 0.5
))
bullet.body.ApplyImpulse({
x: dx * 50
, y: dy * 50
}, center)
this.add(bullet)
}
})
})
}
47 changes: 47 additions & 0 deletions entities/bomb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var Box2D = require('box2dweb-commonjs').Box2D
var b2e = require('box2d-events')
var bs = require('bindlestiff')

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

module.exports = bs.define()
.use(require('../components/attached'))
.use(require('../components/physical'))
.use(require('../components/body')(
function createBody() {
var bd = new b2BodyDef
bd.position = new b2Vec2(Math.random()*5, Math.random()*5-5)
bd.type = b2Body.b2_dynamicBody
bd.userData = {}
bd.fixedRotation = false
bd.m_linearDamping = 1
return bd
},
function createFixture() {
var fd = new b2FixtureDef
fd.restitution = 0.5
fd.shape = new b2CircleShape(0.5)
return fd
}
))
.use(require('../components/explosive')(100))
.use(bs.component()
.on('init', function() {
var self = this
this.c = '#362F34'
this.r = 15

b2e(Box2D, this.world).fixture(
this.fixture
).on('begin', function(a, b) {
if (a.m_body === self.game.player.body)
self.trigger('explode')
})
})
)
.use(require('../components/draw-circle')())

8 changes: 7 additions & 1 deletion entities/player-bullet.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ module.exports = bs.define()
.use(bs.component()
.on('init', function() {
this.c = '#362F34'
}))
this.t = 60 * 2
})
.on('tick', function() {
this.t -= 1
if (!this.t) this.flagged = true
})
)
.use(require('../components/draw-circle')(5))
.use(require('../components/harmful')(1, 1))
.use(require('../components/gravity'))
Expand Down
1 change: 1 addition & 0 deletions entities/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,6 @@ module.exports.prototype.fireBullet = function() {
x: rx * 35 + this.body.m_linearVelocity.x
, y: ry * 35 + this.body.m_linearVelocity.y
}, bullet.body.GetWorldCenter())

this.game.enqueue(bullet)
}
9 changes: 9 additions & 0 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function Game(canvas) {
Manager.call(this)

this.queue = []
this.ticks = []

this.canvas = canvas
this.ctx = canvas.getContext('2d')
Expand Down Expand Up @@ -51,6 +52,10 @@ Game.prototype.start = function() {

var framecounter = fps({ every: 1, decay: 0.5 })
Game.prototype.tick = function() {
for (var i = 0; i < this.ticks.length; i += 1)
this.ticks[i].call(this)
this.ticks.length = 0

framecounter.tick()
this.world.Step(this.tickrate, 8, 3)
this.camera.tick()
Expand Down Expand Up @@ -103,3 +108,7 @@ Game.prototype.draw = function() {
Game.prototype.enqueue = function(entity) {
this.queue.push(entity)
}

Game.prototype.next = function(tick) {
this.ticks.push(tick)
}
12 changes: 12 additions & 0 deletions lib/spawner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module.exports = Spawner
function Spawner(game) {
if (!(this instanceof Spawner)) return new Spawner(game)

var bombChance = 1
var frequency = 5
var speed = 0.25

var Bomb = require('../entities/bomb').tag('spawned')
var EnemyGenerator = require('../components/enemy')
var b2Vec2 = require('box2dweb-commonjs').Box2D.Common.Math.b2Vec2

Expand All @@ -22,6 +24,16 @@ function Spawner(game) {
enemy.body.SetPosition(new b2Vec2(x + ex, y + ey))
}
}

if (Math.random() < bombChance) {
var ex = Math.random() * 30
var ey = Math.random() * 30
if (chunk.get(ex|0, ey|0)) {
var b = new Bomb
game.add(b)
b.body.SetPosition(new b2Vec2(x + ex, y + ey))
}
}
})

game.field.grid.on('removed', function(chunk) {
Expand Down

0 comments on commit 0f15378

Please sign in to comment.