Skip to content

Commit

Permalink
removed vars, unclear if making a difference by globalizing to preven…
Browse files Browse the repository at this point in the history
…t GC;
  • Loading branch information
Zolmeister committed Feb 16, 2014
1 parent 61095ae commit 4c4e17e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 59 deletions.
2 changes: 1 addition & 1 deletion cache.appcache
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CACHE MANIFEST
#build 1.1.2
#build 1.1.3

CACHE:
#NETWORK:
Expand Down
77 changes: 39 additions & 38 deletions fish.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,15 @@ Fish.prototype.killedBy = function(target) {
if(!this.AI || !target.AI) playPop()
this.deathParticles = this.toParticles(target)
}
var r, g, b
Fish.prototype.toParticles = function(target) {
var particles = []

var pixels = this.ctx.getImageData(0,0,this.canv.width, this.canv.height).data
for(var i = 0; i < pixels.length; i += 36 * Math.ceil(this.size/20) * (isMobile ? 6 : 1)) {
var r = pixels[i]
var g = pixels[i + 1]
var b = pixels[i + 2]
r = pixels[i]
g = pixels[i + 1]
b = pixels[i + 2]

// black pixel - no data
if(!r && !g && !b){
Expand All @@ -219,15 +220,18 @@ Fish.prototype.toParticles = function(target) {
}
return particles
}

var friction = 0.1
var t1, t2, moveDir, diff, ossilation, curv, p, i,l,pos,arcSpeed
Fish.prototype.physics = function(){

this.ossilation = Math.sin(this.frame/3)
var ossilation = this.ossilation
ossilation = this.ossilation

var t1 = this.dir
var t2 = this.targetDir
var moveDir = 1
var diff = 0
t1 = this.dir
t2 = this.targetDir
moveDir = 1
diff = 0
if(Math.abs(t1-t2)>Math.PI) {
moveDir = -1
}
Expand All @@ -236,7 +240,7 @@ Fish.prototype.physics = function(){
} else if(t1 < t2) {
diff = t2-t1*moveDir
}
var curv = this.size/15 * diff
curv = this.size/15 * diff
this.curv = curv || 0

// grow inner colors
Expand All @@ -248,10 +252,9 @@ Fish.prototype.physics = function(){

// fish is now particles
if(this.dying) {
for(var i=this.deathParticles.length-1;i>=0;i--){
var p = this.deathParticles[i]
var dist = p.physics()
if(dist < p.target.size/8+10) {
for(i=this.deathParticles.length-1;i>=0;i--){
p = this.deathParticles[i]
if(p.physics() < p.target.size/8+10) {
this.deathParticles.splice(i,1)

p.target.setSize(p.target.size+0.001 * (isMobile ? 6 : 1))
Expand All @@ -268,8 +271,8 @@ Fish.prototype.physics = function(){
}
} else {
// update collision circles
for(var i=0, l=this.circles.length;i<l;i++) {
var pos = rot(this.circleMap[i][0], this.circleMap[i][1]*ossilation, this.dir)
for(i=0, l=this.circles.length;i<l;i++) {
pos = rot(this.circleMap[i][0], this.circleMap[i][1]*ossilation, this.dir)
this.circles[i].x = pos[0] + this.x
this.circles[i].y = pos[1] + this.y
}
Expand All @@ -285,17 +288,17 @@ Fish.prototype.physics = function(){

// random walk
if(Math.random() < 0.01) this.AIDir *= -1 // 1% chance to change directions every frame
var diff = Math.random()/100 * this.AIDir
diff = Math.random()/100 * this.AIDir
this.targetDir = this.targetDir + diff
this.targetDir %= Math.PI

}

var t1 = this.dir
var t2 = typeof this.targetDir === 'undefined' ? this.dir : this.targetDir
var arcSpeed = this.arcSpeed
t1 = this.dir
t2 = typeof this.targetDir === 'undefined' ? this.dir : this.targetDir
arcSpeed = this.arcSpeed

var moveDir = 1
moveDir = 1
if(Math.abs(t1-t2)>Math.PI) {
moveDir = -1
}
Expand All @@ -315,12 +318,11 @@ Fish.prototype.physics = function(){
if(!this.isInput) {

// user is not applying input
this.accel = [0, 0]
this.accel[0] = 0
this.accel[1] = 0
} else {
this.accel = [
Math.cos(this.dir),
Math.sin(this.dir)
]
this.accel[0] = Math.cos(this.dir)
this.accel[1] = Math.sin(this.dir)
}

// update velocity vector
Expand All @@ -331,7 +333,6 @@ Fish.prototype.physics = function(){
this.velocity[1] = Math.max(-this.maxSpeed, Math.min(this.maxSpeed, this.velocity[1]))

// apply friction
var friction = 0.1
if (this.velocity[0] > 0) {
this.velocity[0] -= Math.min(friction, this.velocity[0])
}
Expand All @@ -352,21 +353,21 @@ Fish.prototype.physics = function(){

this.frame++
}
var pi = Math.PI
var dirMap = {
'up': -pi/2,
'right up': -pi/4,
'right': 0,
'down right': pi/4,
'down': pi/2,
'down left': 3*pi/4,
'left': pi,
'left up': -3*pi/4
}
Fish.prototype.updateInput = function(input, isTouch) {
// remember that up is down and down is up because of coordinate system
var pi = Math.PI
var dirMap = {
'up': -pi/2,
'right up': -pi/4,
'right': 0,
'down right': pi/4,
'down': pi/2,
'down left': 3*pi/4,
'left': pi,
'left up': -3*pi/4
}

if(isTouch) {

// touch input

// if valid
Expand Down
45 changes: 25 additions & 20 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ function draw(time) {

lag += time - previousTime
previousTime = time

if(GAME.state === 'playing'){
requestAnimFrame(draw)
requestAnimFrame(function(t){
draw(t)
})
} else {
return fadeInMenu()
}
Expand Down Expand Up @@ -260,24 +263,28 @@ function draw(time) {
}

function playerScore() {

// player score
if(player.colors.length > 4 && player.colors.every(function(col){return col.loaded >= 1})) {

// steal colors from player
player.drawColors()
var newParticles = player.toParticles(levelBar)

// staticly position
for(i=0;i<newParticles.length;i++) {
newParticles[i].x += -player.x + $canv.width/2
newParticles[i].y += -player.y + $canv.height/2
i=player.colors.length
if (i <=4 ) return
while(i-->0) {
if (player.colors[i].loaded < 1) {
return
}
}

// player score
// steal colors from player
player.drawColors()
var newParticles = player.toParticles(levelBar)

// staticly position
for(i=0;i<newParticles.length;i++) {
newParticles[i].x += -player.x + $canv.width/2
newParticles[i].y += -player.y + $canv.height/2
}

GAME.levelParticles = levelParticles.concat(newParticles)
player.colors.splice(0, 4)
GAME.levelParticles = levelParticles.concat(newParticles)
player.colors.splice(0, 4)

}
}

function paintLevelParticles() {
Expand All @@ -299,12 +306,10 @@ function draw(time) {
}

function paintFish() {
// draw fish
var w = $canv.width
var h = $canv.height
// draw fish
for(i = -1, l = fishes.length; ++i < l;) {
fish = fishes[i]
if(Math.abs(fish.x - player.x) < w/2 + 100 && Math.abs(fish.y - player.y) < h/2 + 100) {
if(Math.abs(fish.x - player.x) < $canv.width/2 + 100 && Math.abs(fish.y - player.y) < $canv.height/2 + 100) {
fish.draw(ctx)
}
}
Expand Down

0 comments on commit 4c4e17e

Please sign in to comment.