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

Commit

Permalink
a bunch of things
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsk committed Aug 25, 2013
1 parent c4c16bc commit 4fe0bb4
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 7 deletions.
25 changes: 24 additions & 1 deletion components/enemy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var b2BodyDef = Box2D.Dynamics.b2BodyDef
var b2Vec2 = Box2D.Common.Math.b2Vec2
var b2Body = Box2D.Dynamics.b2Body

var shootme = new Image
shootme.src = 'img/shoot-me.png'

module.exports = function(
size
, speed
Expand All @@ -27,9 +30,11 @@ module.exports = function(
this.r = size * 15 * (Math.random() * 0.25 + 0.75)
this.c = '#EB3E38'
this.flinch = 0
this.st = 0
})
.on('tick', function() {
this.flinch *= 0.95
this.st += 1
if (this.flinch)
if (this.flinch < 0.0025) {
this.flinch = 0
Expand All @@ -40,20 +45,38 @@ module.exports = function(
this.r = this.base_r * (1 - this.flinch * 0.4)
}
})
.on('draw', function(ctx) {
if (this.game.labels) {
ctx.globalAlpha = this.game.labels
ctx.drawImage(shootme
, this.body.m_xf.position.x * 30 - this.game.camera.pos[0] - 50
, this.body.m_xf.position.y * 30 - this.game.camera.pos[1] - 75 - Math.sin(this.st / 6) * 10
)
ctx.globalAlpha = 1
}
})
.on('damaged', function() {
this.flinch = 1
})
.on('died', function() {
if (this.flagged) return
this.flagged = true

this.game.shot++

if (this.game.labels)
if (this.game.shot > 5) {
this.game.labels *= 0.99
}

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}

this.game.next(function() {
for (var j = 1; j <= 3; j += 1)
for (var i = 0; i < 1; i += 0.25) {
for (var i = 0; i < 1; i += 0.34) {
var bullet = new Pellet
var dx = Math.cos(i * tau)
var dy = Math.sin(i * tau)
Expand Down
4 changes: 2 additions & 2 deletions components/explosive.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ function explosive(force) {
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
tx + dx * 0.75
, ty + dy * 0.75
))
bullet.body.ApplyImpulse({
x: dx * 50
Expand Down
11 changes: 11 additions & 0 deletions entities/bomb.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var b2BodyDef = Box2D.Dynamics.b2BodyDef
var b2Vec2 = Box2D.Common.Math.b2Vec2
var b2Body = Box2D.Dynamics.b2Body

var grabme = new Image
grabme.src = 'img/grab-me.png'

module.exports = bs.define()
.use(require('../components/attached'))
.use(require('../components/physical'))
Expand All @@ -34,6 +37,7 @@ module.exports = bs.define()
var self = this
this.c = '#362F34'
this.r = 15
this.st = 0

b2e(Box2D, this.world).fixture(
this.fixture
Expand All @@ -42,6 +46,13 @@ module.exports = bs.define()
self.trigger('explode')
})
})
.on('draw', function(ctx) {
this.st += 1
ctx.drawImage(grabme
, this.body.m_xf.position.x * 30 - this.game.camera.pos[0] - 50 - Math.sin(this.st / 6) * 10
, this.body.m_xf.position.y * 30 - this.game.camera.pos[1] - 55 - Math.sin(this.st / 52) * 8
)
})
)
.use(require('../components/draw-circle')())

2 changes: 1 addition & 1 deletion entities/pellet.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = pellet

function pellet(c) {
var pelletCounter = 0
var pelletMax = 100
var pelletMax = 80

return bs.define()
.use(require('../components/attached'))
Expand Down
3 changes: 2 additions & 1 deletion entities/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var player = bs.component([
var y = this.game.height / 60

var def = new b2BodyDef
def.position = new b2Vec2(0, -5)
def.position = new b2Vec2(20, 0)
def.type = b2Body.b2_dynamicBody
def.userData = {}
def.fixedRotation = true
Expand Down Expand Up @@ -84,6 +84,7 @@ var player = bs.component([
: this.controls.right && !this.blockedRight ? +14
: 0

this.game.ready = this.game.ready || xspd
this.pop *= 0.95
if (this.rotating) {
this.rotation += xspd > 0
Expand Down
33 changes: 33 additions & 0 deletions game.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Manager = require('bindlestiff/manager')
var inherits = require('inherits')
var trap = require('pointer-trap')
var fill = require('ndarray-fill')
var fps = require('fps')

var camera = require('./lib/camera')
Expand All @@ -10,6 +11,9 @@ var Box2D = require('box2dweb-commonjs').Box2D
var b2World = Box2D.Dynamics.b2World
var b2Vec2 = Box2D.Common.Math.b2Vec2

var main = new Image
main.src = 'img/main.png'

module.exports = Game

function Game(canvas) {
Expand All @@ -21,6 +25,21 @@ function Game(canvas) {
this.ticks = []
this.flash = 0

this.shot = 0
this.title = true
this.ready = false
this.labels = 1

this.level = 1
this.levels = {
speed : function(level) { return (Math.pow(level, 0.55) + level / 100) * 0.04 }
, health: function(level) { return Math.pow(level, 0.4) + level / 100 }
}

setInterval(function() {
this.level += 1
}.bind(this), 10000)

this.canvas = canvas
this.ctx = canvas.getContext('2d')
this.width = canvas.width
Expand Down Expand Up @@ -85,6 +104,14 @@ Game.prototype.draw = function() {
var camy = this.camera.pos[1]
var floor = Math.floor

if (this.labels)
if (this.labels < 0.005) {
this.labels = 0
} else
if (this.labels !== 1) {
this.labels *= 0.99
}

this.field.gridCache.each(function(canvas) {
var x = floor((canvas.position[0]) * canvas.width - camx)
var y = floor((canvas.position[1]) * canvas.height - camy)
Expand All @@ -104,6 +131,12 @@ Game.prototype.draw = function() {

this.camera.draw()

if (this.title) {
ctx.drawImage(main, 200-camx, -145-camy)
if (this.camera.pos[0] < -this.width) this.title = false
if (this.camera.pos[1] < -this.height) this.title = false
}

if (this.flash)
if (this.flash < 0.005) {
this.flash = 0
Expand Down
Binary file added img/grab-me.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/main.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/shoot-me.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions lib/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ function Camera(game, field) {
Camera.prototype.draw = function() {
this.target[0] = this.game.player.b2Pos.x * 30 - this.game.width / 2
this.target[1] = this.game.player.b2Pos.y * 30 - this.game.height / 2
if (!this.game.ready) {
this.target[1] -= this.game.width * 0.2
}
lerp(this.pos, this.pos, this.target, 0.05)
}

Expand Down
12 changes: 11 additions & 1 deletion lib/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var caveopts = { density: caveDensity }
function Field(game) {
if (!(this instanceof Field)) return new Field(game)

var self = this
var shape = [32, 32]
var getCanvas = this.gridCache = pool({
enable: function(c) { c.active = true }
Expand All @@ -41,7 +42,16 @@ function Field(game) {
return done(null,
fill(
ndarray = cave(zero(shape), caveopts)(10)
, function(x, y) {
, !game.ready && !(position[0] || position[1]) ? function(x, y) {
var val = ndarray.get(x, y)
if (y > 8) return (val ? (
perlin(
(position[0] * shape[0] + x) * perlinScale
, (position[1] * shape[1] + y) * perlinScale
) > perlinDensity ? 1 : 0
) : val)
return (y === 8) ? 0 : 1
} : function(x, y) {
var val = ndarray.get(x, y)
return val ? (
perlin(
Expand Down
10 changes: 9 additions & 1 deletion lib/spawner.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ function Spawner(game) {
var b2Vec2 = require('box2dweb-commonjs').Box2D.Common.Math.b2Vec2

game.field.grid.on('created', function(chunk) {
var Enemy = EnemyGenerator(2.1, speed, 1, 0).tag('spawned')
if (!game.ready) return

var Enemy = EnemyGenerator(
2.1 // size
, game.levels.speed(game.level) // speed
, 1 // health
, 0
).tag('spawned')

var x = chunk.position[0] * chunk.shape[0]
var y = chunk.position[1] * chunk.shape[1]
for (var i = 0; i < frequency; i += 1) {
Expand Down

0 comments on commit 4fe0bb4

Please sign in to comment.