Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Me 213934 committed Apr 3, 2014
0 parents commit cbe2a63
Show file tree
Hide file tree
Showing 24 changed files with 1,253 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Camera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function Camera(canvas) {
// Constants
this.scale_smoothness = 0.3;
this.move_smoothness = 0.3;

// Variables
this.canvas = canvas;
this.x = 0;
this.y = 0;
this.x_target = 0;
this.y_target = 0;
this.scale = 0.5;
this.scale_target = 1;

// Methods
this.world_to_viewport = function(n, dimension) {
var canvas_side_length = (dimension == 'x') ? this.canvas.width : this.canvas.height;
var offset = (dimension == 'x') ? this.x : this.y;
return (n * this.scale) + (canvas_side_length / 2) - (offset * this.scale);
};
this.world_to_viewport_x = function(x) {
return this.world_to_viewport(x, 'x');
};
this.world_to_viewport_y = function(y) {
return this.world_to_viewport(y, 'y');
};

this.viewport_to_world = function(n, dimension) {
var canvas_side_length = (dimension == 'x') ? this.canvas.width : this.canvas.height;
var offset = (dimension == 'x') ? this.x : this.y;
return (n + (offset * this.scale) - (canvas_side_length / 2)) / this.scale;
};
this.viewport_to_world_x = function(x) {
return this.viewport_to_world(x, 'x');
};
this.viewport_to_world_y = function(y) {
return this.viewport_to_world(y, 'y');
};
this.update = function(target_x, target_y, frame_delta) {
this.x_target = target_x;
this.y_target = target_y;

// Gently move to target
if (this.scale != this.scale_target)
this.scale = Math.abs(this.scale + (frame_delta * (this.scale_target - this.scale) * this.scale_smoothness));
if (this.x != this.x_target)
this.x += frame_delta * (this.x_target - this.x) * this.move_smoothness;
if (this.y != this.y_target)
this.y += frame_delta * (this.y_target - this.y) * this.move_smoothness;
};
}
76 changes: 76 additions & 0 deletions Cell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
function Cell(xpos, ypos, radius) {
// Inherit from Mover
this.inheritFrom = Mover;
this.inheritFrom();

// Store passed
if (xpos)
this.x_pos = xpos;
if (ypos)
this.y_pos = ypos;
if (radius)
this.radius = radius;

this.default_x = this.x_pos;
this.default_y = this.y_pos;

// Properties
this.dead = false;

// Player color
this.fillStyle = "#73DBFF";

// Methods
this.reset = reset_cell;
this.update = update_cell;
this.draw = draw_cell;
this.push_up = function (mag) { if (!mag) mag=1; this.y_veloc -= mag; };
this.push_down = function (mag) { if (!mag) mag=1; this.y_veloc += mag; };
this.push_left = function (mag) { if (!mag) mag=1; this.x_veloc -= mag; };
this.push_right = function (mag) { if (!mag) mag=1; this.x_veloc += mag; };
this.area = function() {
return Math.PI * this.radius * this.radius;
}
}

function reset_cell() {
this.x_pos = this.default_x;
this.y_pos = this.default_y;
this.x_veloc = 0;
this.y_veloc = 0;
this.dead = false;
}

function update_cell(frame_delta) {
if (!this.dead)
this.update_mover(frame_delta);
}

function draw_cell(ctx, cam, shadow, player_radius) {
if (!this.dead) {
// Shadow
if (shadow) {
ctx.fillStyle = "rgba(0,0,0,0.3)"; // gray
ctx.beginPath();
ctx.arc(cam.world_to_viewport_x(this.x_pos)+1, cam.world_to_viewport_y(this.y_pos)+3, this.radius*cam.scale, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();
}

if (player_radius) {
if (this.radius > player_radius)
ctx.fillStyle = "#FF441A"; // red
else if (player_radius - this.radius < 3)
ctx.fillStyle = "#FFAF00"; // white
else
ctx.fillStyle = "#36B6FF"; // blue
}
else
ctx.fillStyle = this.fillStyle;

ctx.beginPath();
ctx.arc(cam.world_to_viewport_x(this.x_pos), cam.world_to_viewport_y(this.y_pos), this.radius*cam.scale, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();
}
}
65 changes: 65 additions & 0 deletions Mover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Super-"class" of Ball and Player
// Handles physical attributes and actions
function Mover() {
// Variables to hold size
this.radius = 20;

// Variables to hold current position
this.x_pos = 0;
this.y_pos = 0;

// Variables to hold current velocity
this.x_veloc = 0;
this.y_veloc = 0;

// Speed limits
this.x_veloc_max = 100;
this.y_veloc_max = 100;

// Variables to hold x position bounds
this.x_min = 0;
this.x_max = 640;

this.friction = 0.997;

// Methods
this.horizontalBounce = function() { this.x_veloc = -this.x_veloc; };
this.verticalBounce = function() { this.y_veloc = -this.y_veloc; };
this.distance_from = function(other) {
var dx = this.x_pos - other.x_pos;
var dy = this.y_pos - other.y_pos;
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
}
this.collides_with = function(other) {
return this.distance_from(other) < this.radius + other.radius;
};
this.set_position = function(x, y) { this.x_pos = x; this.y_pos = y; };
this.reset_mover = function() { };
this.update_mover = function(frame_delta) {
// Enforce speed limits
var xvelsign = this.x_veloc / Math.abs(this.x_veloc);
if (Math.abs(this.x_veloc) > this.x_veloc_max)
this.x_veloc = xvelsign * this.x_veloc_max;
var yvelsign = this.y_veloc / Math.abs(this.y_veloc);
if (Math.abs(this.y_veloc) > this.y_veloc_max)
this.y_veloc = yvelsign * this.y_veloc_max;

// Adjust the position, according to velocity.
this.x_pos += this.x_veloc * frame_delta;
this.y_pos += this.y_veloc * frame_delta;

// Friction
this.x_veloc *= this.friction;
this.y_veloc *= this.friction;
};
this.draw_mover = function(ctx) {
ctx.beginPath();
ctx.rect(this.x_pos, this.y_pos, this.width, this.height);
ctx.closePath();
ctx.fill();
};

this.reset = this.reset_mover; // Override me
this.update = this.update_mover; // Override me
this.draw = this.draw_mover; // Override me
}
123 changes: 123 additions & 0 deletions MusicPlayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
function MusicPlayer(songArray, sfxDict, assetDir) {
// Constants
this.default_volume = 0.6;

// Variables
this.songs = songArray;
this.sounds = sfxDict;
if (assetDir)
this.asset_dir = assetDir;
else
this.asset_dir = "assets/";
this.inited = false;
this.song_volume = this.default_volume;

// State variables
this.current_song = 0;
this.song_audio;
this.muted = false;

// Methods

this.init = function() {
this.load_song();
this.inited = true;

for (var i in this.sounds) {
var src = this.sounds[i][0];
this.sounds[i].push([new Audio(this.asset_dir + src), new Audio(this.asset_dir + src)]);
this.sounds[i].push(0);
}
};

// Load the current song into song_audio
this.load_song = function() {
if (this.songs.length > this.current_song) {
if (this.song_audio) {
this.song_audio.pause();
delete this.song_audio;
}
this.song_audio = new Audio(this.asset_dir + this.songs[this.current_song][0]);
this.song_audio.volume = this.default_volume;
this.song_audio.addEventListener('ended', function() {world.music.next_song();}, false);

// Display metadata
var infobox = document.getElementById('songinfo');
var titlebox = document.getElementById('songtitle');
var artistbox = document.getElementById('songartist');
if (infobox && titlebox && artistbox) {
titlebox.innerText = this.songs[this.current_song][1];
artistbox.innerText = this.songs[this.current_song][2];
infobox.className = "featured";
setTimeout("document.getElementById('songinfo').className='idle';", 2000);
}
}
};

// Load and play the next song in the list
this.next_song = function() {
this.current_song = (this.current_song + 1) % this.songs.length;
this.load_song();
this.play_song();
};

this.play_song = function() {
if (this.song_audio && !this.muted) {
this.song_audio.play();
}
};

this.pause_song = function() {
if (this.song_audio && !this.muted) {
this.song_audio.pause();
}
};

// Play or pause the currently selected song
this.play_pause_song = function() {
if (this.song_audio) {

}
};

this.lower_volume = function() {
this.song_volume = 0.2;
};

this.raise_volume = function() {
this.song_volume = 0.6;
};

this.mute = function() {
if (!this.muted) {
// Mute
this.song_audio.pause();
this.muted = true;
document.getElementById("mute").className = "muted";
document.getElementById("mute").children[0].innerText = "Unmute sounds [M]";
}
else {
// Unmute
this.song_audio.play();
this.muted = false;
document.getElementById("mute").className = "";
document.getElementById("mute").children[0].innerText = "Mute sounds [M]";
}
};

// Play the sound effect with the given name
this.play_sound = function(name) {
if (!this.muted) {
var sound = this.sounds[name];
if (sound) {
sound[1][sound[2]].play(); // Play current round-robin Audio object for this sound
sound[2] = (sound[2] + 1) % sound[1].length; // Increment round-robin counter
}
}
};

this.update = function() {
if (this.song_audio.volume != this.song_volume)
this.song_audio.volume += (this.song_volume - this.song_audio.volume) * 0.1;
};
}
50 changes: 50 additions & 0 deletions Player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
function Cell(xpos, ypos, radius) {
// Inherit from Mover
this.inheritFrom = Mover;
this.inheritFrom();

if (xpos)
this.x_pos = xpos;
if (ypos)
this.y_pos = ypos;
if (radius)
this.radius = radius;

// Scoring
this.score = 0;

// Store passed x pos
this.default_x = xpos - (this.width/2.0);

// Player color
this.fillStyle = "#00FF00";

// Methods
this.reset = reset_player;
this.draw = draw_player;
this.push_up = function (mag) { if (!mag) mag=1; this.y_veloc -= mag; };
this.push_down = function (mag) { if (!mag) mag=1; this.y_veloc += mag; };
this.push_left = function (mag) { if (!mag) mag=1; this.x_veloc -= mag; };
this.push_right = function (mag) { if (!mag) mag=1; this.x_veloc += mag; };
this.verticalBounce = function () { this.y_veloc = -this.y_veloc * 0.4; };
}

function reset_cell()
{
this.x_pos = this.default_x;
this.y_pos = 200;
this.last_y = 200;
this.x_veloc = 0;
this.y_veloc = 0;
}

function draw_cell(ctx)
{
ctx.fillStyle = this.fillStyle;
ctx.beginPath();
ctx.rect(this.x_pos, this.y_pos, this.width, this.height);
ctx.closePath();
ctx.fill();

this.last_y = this.y_pos;
}
Loading

0 comments on commit cbe2a63

Please sign in to comment.