Skip to content

Commit

Permalink
Begin prep for abilities and UI, broke up code a little, ported windo…
Browse files Browse the repository at this point in the history
…w-manager

Added window-manager module that was used in Leap of Faith and Lumina
repositories. Turns out the entire module seems to only use Victor for
the x,y data storage object and not for any vector math (which makes
sense with a UI engine), so it now uses a simple Vector(x, y) from the
utilities (no longer relying on Victor).

Set up ability enum and internal scrolling between them in the client.
InputManager was useless so set it up to accept and re-emit eventual
abilityChange events so clients can listen for them to know the other
user's current ability and display it in the interface.
  • Loading branch information
TheBentoBox committed May 2, 2016
1 parent 42fac70 commit 9c374d1
Show file tree
Hide file tree
Showing 9 changed files with 1,357 additions and 52 deletions.
5 changes: 5 additions & 0 deletions client/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ canvas {
background-size: cover;
}

#UI {
z-index: 1;
position: absolute;
}

#canvasContainer {
width: 100%;
text-align: center;
Expand Down
55 changes: 45 additions & 10 deletions client/kinesthesia.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"use strict";
// if game exists use the existing copy
// else create a new object literal
var game = game || {};

// IIFE for entire game
(function() {
game.main = (function() {

// web socket to use
var socket;
Expand All @@ -10,11 +13,25 @@ var socket;
var canvas;
var canvasPos;
var ctx;
var windowManager = game.windowManager; // reference to the engine's window manager

// game/server feedback box
var msgBox;

//{ GAME VARS
// abilities the player can switch between
var ABILITIES = {
CANNONBALL: {
name: "Cannonball"
},
GRENADE: {
name: "Grenade"
},
GRAVITY_WELL: {
name: "Gravity Well"
}
};

// size constants
var playerRad = 4;
var gemRad = 15;
Expand All @@ -39,7 +56,7 @@ var player = {
x: -100,
y: -100
},
grabbed: 0,
currentAbility: ABILITIES['CANNONBALL'],
score: 0
};

Expand All @@ -51,17 +68,12 @@ var opponent = {
x: -100,
y: -100
},
grabbed: 0,
currentAbility: ABILITIES['CANNONBALL'],
score: 0
};

//} GAME VARS

// FUNCTION: clamp value between min and max
function clamp(value, min, max) {
return Math.max(min, Math.min(value, max));
}

// FUNCTION: connect socket and setup callbacks
function setupSocket() {

Expand All @@ -78,8 +90,6 @@ function setupSocket() {
socket.emit("sendId", { id: userdata._id });
});



// Listens for notifaction from the server that we're the host of the game
socket.on("notifyHost", function() {
IS_HOST = true;
Expand Down Expand Up @@ -217,6 +227,7 @@ function initializeGame() {
// Sets up matter world and input callbacks for using abilities
initializeMatter();
initializeInput();
setupUI();

// The host starts up the world and begins an update loop
if (IS_HOST) {
Expand Down Expand Up @@ -270,6 +281,9 @@ function initializeMatter() {
function initializeInput() {
// setup canvas mouse down behavior
canvas.addEventListener('mousedown', function(e) {
e.stopPropagation();
e.preventDefault();

player.pos.x = clamp(e.x - canvasPos.left, 0, canvas.width);
player.pos.y = clamp(e.y - canvasPos.top, 0, canvas.height);

Expand All @@ -287,6 +301,7 @@ function initializeInput() {
return;
}

// prep the new body
var newBody = Bodies.circle(lastClick.x, lastClick.y, gemRad);
var vel = {
x: Math.min(25, Math.abs(lastClick.x - player.pos.x)) * Math.sign(lastClick.x - player.pos.x),
Expand All @@ -296,6 +311,25 @@ function initializeInput() {
Body.setVelocity(newBody, vel);
add(newBody);
});

// scrolling to change between abilities
document.addEventListener('wheel', function(e) {
// scroll down - next ability
if (e.deltaY > 0) {
player.currentAbility = next(ABILITIES, player.currentAbility);
}
// scroll up - previous
else {
player.currentAbility = previous(ABILITIES, player.currentAbility);
}
});
}

// INITIAL UI SETUP
function setupUI() {
windowManager.makeUI("abilityHUD", 0, 0, 100, 50);
windowManager.makeText("abilityHUD", "username", 5, 5, canvas.width/10, canvas.height/5, userdata.username, "12pt 'Roboto'", "white");
windowManager.toggleUI("abilityHUD");
}

// INITAL GAME SETUP: sets up starting world objects
Expand Down Expand Up @@ -385,6 +419,7 @@ function emitBodies() {
function update() {
// emit player position
socket.emit("update", {pos: player.pos});
game.windowManager.updateAndDraw([]);

// request next frame
requestAnimationFrame(update);
Expand Down
125 changes: 125 additions & 0 deletions client/utilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
"use strict";

var KEY = { // "enum" equating keycodes to names (e.g. keycode 32 = spacebar)
TAB: 9,
SPACE: 32,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
ONE: 49,
TWO: 50,
THREE: 51,
FOUR: 52,
FIVE: 53,
SIX: 54,
A: 65,
C: 67,
D: 68,
E: 69,
H: 72,
P: 80,
Q: 81,
R: 82,
S: 83,
W: 87,
X: 88,
Z: 90
};

// get mouse pos on canvas
function getMouse(e){
var mouse = {position: {}}
mouse.position = Vector(e.pageX - e.target.offsetLeft, e.pageY - e.target.offsetTop);
return mouse;
};

// returns random within a range
function rand(min, max) {
return Math.random() * (max - min) + min;
};

// returns a value that is constrained between min and max (inclusive)
function clamp(val, min, max){
return Math.max(min, Math.min(max, val));
};

// fills a text with correct CSS and cleans up after itself
function fillText(ctx, string, x, y, css, color) {
ctx.save();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = css;
ctx.fillStyle = color;
ctx.fillText(string, x, y);
ctx.restore();
};

// fills a text with correct CSS and cleans up after itself
function fillTextAligned(ctx, string, x, y, css, color) {
ctx.save();
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.font = css;
ctx.fillStyle = color;
ctx.fillText(string, x, y);
ctx.restore();
};

// activate fullscreen
function requestFullscreen(element) {
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.mozRequestFullscreen) {
element.mozRequestFullscreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
}
// no response if unsupported
};

// This gives Array a randomElement() method
Array.prototype.randomElement = function(){
return this[Math.floor(Math.random() * this.length)];
}

// HELPER: gets next key in a JSON object
// from StackOverflow: http://stackoverflow.com/questions/12505598/get-next-key-value-pair-in-an-object
function next(db, key) {
// sort through and find the key after
var found = false;
//console.log(key);
for(var k in db){
//console.log(k);
if (found) { return db[k]; }
if (db[k] == key) { found = true; }
}

// if we reach here and found is true, then we found our key but it was the last one
// so we'll grab the first one
if (found) for (var k in db) return db[k];
}

// HELPER: gets the previous key in a JSON object
function previous(db, key) {
// sort through and find the key after
var last = undefined;
for(var k in db){
if (db[k] == key) { if (last) return db[last]; }
last = k;
}

// if we reach here, then we found our key but it was when last wasn't set
// this means our key was first, so return the last one
return db[last];
};

// Helper: a basic x/y vector constructor for use in utilities/window manager
function Vector(x, y) {
return {
x: x,
y: y
};
}
Loading

0 comments on commit 9c374d1

Please sign in to comment.