This repository has been archived by the owner on Dec 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters