forked from tokumine/JStation
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
4632a19
commit ac358b1
Showing
4 changed files
with
132 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
|
||
var Bullet = function(pos, vel) { | ||
this.pos = pos; | ||
this.vel = vel; | ||
}; | ||
|
||
Bullet.prototype.update = function(dt) { | ||
var p = this; | ||
p.time += dt; | ||
|
||
p.pos = vec2.add(p.pos, vec2.mul(dt, p.vel)); | ||
//p.pos.x = 100*Math.cos(p.time*p.vel); | ||
//p.pos.y = 100*Math.sin(p.time*p.vel); | ||
}; | ||
|
||
Bullet.prototype.render = function(ctx) { | ||
var p = this.pos; | ||
ctx.fillRect(p.x, p.y, 5, 5); | ||
}; | ||
|
||
var Bullets = function() { | ||
this.bullets = []; | ||
} | ||
|
||
Bullets.prototype.fire = function(pos, vel) { | ||
this.bullets.push(new Bullet(pos, vel)); | ||
console.log('bullets ' + this.bullets.length); | ||
} | ||
|
||
Bullets.prototype.update = function(dt) { | ||
var b = this.bullets; | ||
for(var i = 0; i < b.length; ++i) { | ||
b[i].update(dt); | ||
} | ||
}; | ||
|
||
Bullets.prototype.render = function(ctx) { | ||
var b = this.bullets; | ||
for(var i = 0; i < b.length; ++i) { | ||
b[i].render(ctx); | ||
} | ||
}; | ||
|
||
window.bullets = new Bullets(); |
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,20 @@ | ||
|
||
function vec2(x, y) { | ||
|
||
this.x = x || 0; | ||
this.y = y || 0; | ||
|
||
this.lengthSq = function() { | ||
var v = this; | ||
return v.x*v.x + v.y*v.y; | ||
} | ||
} | ||
|
||
vec2.add = function(a, b) { | ||
return new vec2(a.x + b.x, a.y + b.y); | ||
}; | ||
|
||
vec2.mul = function(scalar, b) { | ||
return new vec2(scalar*b.x, scalar*b.y); | ||
} | ||
|
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,25 @@ | ||
|
||
var Player = function(pos, controller) { | ||
this.pos = pos; | ||
this.controller = controller; | ||
}; | ||
|
||
Player.prototype.update = function(dt) { | ||
var p = this; | ||
p.time += dt; | ||
|
||
if(this.controller.controls.left) { | ||
p.pos = vec2.add(p.pos, new vec2(1, 0)); | ||
} | ||
if(this.controller.controls.fire) { | ||
window.bullets.fire(p.pos, new vec2(0, -1)); | ||
} | ||
//p.pos.x = 100*Math.cos(p.time*p.vel); | ||
//p.pos.y = 100*Math.sin(p.time*p.vel); | ||
}; | ||
|
||
Player.prototype.render = function(ctx) { | ||
var p = this.pos; | ||
ctx.fillRect(p.x, p.y, 10, 10); | ||
}; | ||
|