Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:facebook/jsgamebench
Browse files Browse the repository at this point in the history
  • Loading branch information
coryondrejka committed Mar 8, 2011
2 parents 0a4cc70 + fc6016e commit 796bf22
Show file tree
Hide file tree
Showing 35 changed files with 8,447 additions and 309 deletions.
24 changes: 17 additions & 7 deletions chess/chess.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Chess = (function() {
var pbframe = 0;
var menu_time,request_time;
var explo;
var replay = {};

function startPlayback() {
playback = true;
Expand Down Expand Up @@ -48,7 +49,6 @@ var Chess = (function() {
}

function makeExplosion(pos,scale) {
console.log('make explo: ' + pos + ': ' + scale);
explo = explo || Gob.add(Utils.uuidv4(), 'small_explo', 0, pos, [0,0], 10, scale / 7);
}

Expand Down Expand Up @@ -144,7 +144,7 @@ var Chess = (function() {
}
}
if (old_game_state != game_state) {
console.log('game state: ' + game_state);
var size = [300,55];
old_game_state = game_state;
switch (game_state) {
case 'login':
Expand All @@ -157,7 +157,6 @@ var Chess = (function() {
button('Send Move', [End,End], { cmd:['sendRequest'] });
case 'playing':
button('Menu',[Start,Start], { cmd:['newGameState', 'menu'] });
var size = [300,55];
var req = Publish.hasOpponent();
if (req && game_state != 'moved') {
if (req.concede) {
Expand All @@ -170,17 +169,28 @@ var Chess = (function() {
}
Publish.addMyName(uiPos([Middle,End],size),size);
break;
case 'replay':
button('Menu',[Start,Start], { cmd:['newGameState', 'menu'] });
Publish.addReplayName(replay.p1,uiPos([Middle,Start],size),size);
Publish.addReplayName(replay.p2,uiPos([Middle,End],size),size);
break;
}
}
}

function postImageLoad() {
Board.init();
Pieces.init();
}

function clickButton() {

var hash = window.location.hash;
hash = hash.length && hash.substr(1);
if (hash) {
replay = JSON.parse(decodeURIComponent(hash));
Gob.delAll();
Board.init();
Chess.newGameState('replay');
Board.loadState(replay.board);
Chess.startPlayback();
}
}

function newGame() {
Expand Down
31 changes: 23 additions & 8 deletions chess/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ var Publish = (function() {
Chess.newGameState('playing');
Board.loadState(data.board);
Chess.startPlayback();
console.log('playing against: ' + req.from.id);
}

function getInfo() {
Expand All @@ -93,7 +92,6 @@ var Publish = (function() {

function addName(name,uid,pos,size) {
markup = '<img src="http://graph.facebook.com/'+uid+'/picture" /> '+FB.String.escapeHTML(name);
// UI.addButton('buttons', 'name_'+uid, { pos: pos, width: size[0], height: size[1], fontsize: '200%',text: markup });
UI.addHTML('buttons', 'name_'+uid, { pos: pos, width: size[0], height: size[1], uiclass: 'chess',markup: markup });
}

Expand All @@ -105,6 +103,10 @@ var Publish = (function() {
addName(req.from.name,req.from.id,pos,size);
}

function addReplayName(p,pos,size) {
addName(p.name,p.id,pos,size);
}

function addRequestButton(req,x,y) {
var req_label = 'req'+req.id;
if (UI.exists(req_label)) {
Expand Down Expand Up @@ -148,7 +150,6 @@ var Publish = (function() {
}

function sendRequest(msg,payload) {
console.log('payload:\n'+JSON.stringify(payload));
var req = player.active_req;
var cmd = {
method: 'apprequests',
Expand All @@ -158,7 +159,6 @@ var Publish = (function() {

if (req) {
cmd.to = req.from.id;
console.log('send to: ['+cmd.to+']');
}
FB.ui(cmd, function(response) {
if (response && !response.error) {
Expand All @@ -170,21 +170,35 @@ var Publish = (function() {
}

function sendMove() {
Publish.sendRequest('I made my move!',{board: Board.getState()});
if (player.active_req) {
Publish.sendRequest(player.active_req.message,{board: Board.getState()});
} else {
Publish.sendRequest('I made my move! (game: '+(new Date).getTime()+')',{board: Board.getState()});
}
}

function publishStory() {
var payload = Board.getState();
var req = player.active_req;
var payload = {
board: Board.getState(),
p1: {
id: req.from.id,
name: req.from.name
},
p2: {
id: fb_logged_in.uid,
name: player.name
}
};
var loc = window.location;
var url = loc.protocol + '//' + loc.host + '/chess#' + encodeURIComponent(FB.JSON.stringify(payload));
console.log('url: ' + url);
var cmd = {
method: 'stream.publish',
attachment: {
name: 'Watch Replay',
caption: 'Checkmate!',
description: (
'Check out my awesome game of pirate chess i played with a friend'
'Check out my awesome game of pirate chess i played with '+req.from.name
),
href: url
}
Expand All @@ -211,6 +225,7 @@ var Publish = (function() {
removeRequest: removeRequest,
addReqName : addReqName,
addMyName : addMyName,
addReplayName : addReplayName,
sendMove : sendMove
};
})();
3 changes: 2 additions & 1 deletion engine/core/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ var Init = (function() {
}
Render.setupBrowserSpecific();

var meta_viewport = document.querySelector("meta[name=viewport]");
var meta_viewport = document.querySelector &&
document.querySelector("meta[name=viewport]");
if (meta_viewport && window.devicePixelRatio >= 2 ) {
JSGlobal.lowres = false;
meta_viewport.setAttribute('content', 'user-scalable=no, width=device-width, height=device-height, initial-scale=0.5, maximum-scale=0.5');
Expand Down
10 changes: 7 additions & 3 deletions engine/core/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ var Input = (function() {


function getMouseXY(event,down) {
// document.getElementById('gamebody').focus();
event.currentTarget.focus();
// document.getElementById('gamebody').focus();
var prev = JSGlobal.mouse.buttons.slice(0);
var button = 0;
if (!event)
if (!event) {
event = window.event;
}

var currentTarget = event.currentTarget ? event.currentTarget : event.srcElement;
currentTarget && currentTarget.focus();

if (event.which) {
button = event.which - 1;
} else if (event.button==2) {
Expand Down
2 changes: 1 addition & 1 deletion engine/core/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ var UI = (function() {
text = ui.textarray ? ui.textarray[ui.target[ui.tkey]] : ui.text;
size = ui.fontsize ? 'font-size:' + ui.fontsize + ';' : '';
var color = ui.color ? 'background:' + ui.color + ';' : '';
str = '<button id="' + ui.id + '" class="' + uiclass + '" style="cursor:hand;' + posstring + size + color + zindex + '" ' + unclick + '="event.cancelBubble=true;return false;" ' + click + '="return UI.call(event,\'' + ui.id + '\');"' + '><div>' + text + '</div></button>';
str = '<button id="' + ui.id + '" class="' + uiclass + '" style="' + posstring + size + color + zindex + '" ' + unclick + '="event.cancelBubble=true;return false;" ' + click + '="return UI.call(event,\'' + ui.id + '\');"' + '><div>' + text + '</div></button>';
upel.innerHTML = str;
} else if (ui.type == HTML) {
str = '<div class="' + uiclass + '" id="' + ui.id + '" style="' + posstring + '">';
Expand Down
2 changes: 1 addition & 1 deletion engine/core/webgl_debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ var WebGLDebug = (function() {
'viewport': { 0:{'type':'int'},
1:{'type':'int'},
2:{'type':'sizei'},
3:{'type':'sizei'}},
3:{'type':'sizei'}}
};

function getArgumentString(function_name, arg_index, value) {
Expand Down
11 changes: 8 additions & 3 deletions engine/core/webgl_material.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var WebGLMaterial = (function() {
var material_program = gl.loadProgram(data.vertex_shader,
data.fragment_shader);
if (!material_program) {
console.log('Failed to load program for material type: ' + data.name);
gl.log('Failed to load program for material type: ' + data.name);
return;
}

Expand Down Expand Up @@ -117,7 +117,7 @@ var WebGLMaterial = (function() {
material_table.getMaterialType = function(name) {
var material_type = material_type_dictionary[name];
if (!material_type) {
console.log('Failed to find material type: ' + name);
gl.log('Failed to find material type: ' + name);
material_type = material_type_dictionary[ERROR_MAT_NAME];
}
return material_type;
Expand Down Expand Up @@ -154,7 +154,12 @@ var WebGLMaterial = (function() {

if (show_unloaded_materials) {
var error_material = material_dictionary[ERROR_MAT_NAME];
material.bind = error_material.bind;
material.bind = function() {
// show error once, then rebind
error_material.bind.apply(error_material, arguments);
gl.log('Bound dummy material for "' + name + '"');
material.bind = error_material.bind;
};
material.bindMatrixState = error_material.bindMatrixState;
material.getMaterialType = error_material.getMaterialType;
}
Expand Down
6 changes: 6 additions & 0 deletions engine/core/webgl_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ var WebGLUtil = (function() {
}

var bind_table = {};
bind_table['float'] = function(gl, loc) {
return function(f) {
gl.uniform1f(loc, f);
}
};

bind_table['vec2'] = function(gl, loc) {
return function(v) {
gl.uniform2f(loc, v[0], v[1]);
Expand Down
47 changes: 30 additions & 17 deletions engine/core/world_3d.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,40 @@ var World3D = (function() {
var matrix_state = {
view_matrix : Math3D.mat4x4(),
projection_matrix : Math3D.mat4x4(),
viewprojection : Math3D.mat4x4()
viewprojection : Math3D.mat4x4(),
camera_pos : [0,0,0]
};

function checkCollision(old_mid, new_mid, radius, callback) {
function checkCollision(old_mid, new_mid, radius, source, callback) {

var result = null;

for (var id in static_elements) {
var element = static_elements[id];
if (Math3D.boxSphereCollision(new_mid, radius,
element.min, element.max,
element.mid, element.radius)) {
var result = Math3D.sweptSphereBoxIntersection(element.min,
element.max,
element.mid,
element.radius,
old_mid,
new_mid,
radius);
if (result.t >= 0 && callback(result)) {
return false;
var result2 = Math3D.sweptSphereBoxIntersection(element.min,
element.max,
element.mid,
element.radius,
old_mid,
new_mid,
radius);

if (result2.t >= 0 && (!result || result2.t < result.t)) {
result = result2;
}
}
}

if (result) {
result.source = source;
if (callback(result)) {
return false;
}
}

return true;
}

Expand All @@ -68,12 +79,13 @@ var World3D = (function() {
static_elements[id] = undefined;
}

function addDynamic(model, world_matrix, bounds_mid, bounds_radius) {
function addDynamic(model, world_matrix, bounds_mid, bounds_radius, owner) {
var element = {
model: model,
matrix: Math3D.dupMat4x4(world_matrix),
mid : Math3D.dupVec3(bounds_mid),
radius : bounds_radius
radius : bounds_radius,
owner : owner
};

var id = dynamic_elements.length;
Expand All @@ -91,8 +103,8 @@ var World3D = (function() {
if (element) {
var do_update = true;
if (collision_callback) {
do_update = checkCollision(element.mid, bounds_mid,
element.radius, collision_callback);
do_update = checkCollision(element.mid, bounds_mid, element.radius,
element.owner, collision_callback);
}

if (do_update) {
Expand Down Expand Up @@ -127,6 +139,9 @@ var World3D = (function() {
matrix_state.viewprojection =
Math3D.mulMat4x4(matrix_state.projection_matrix,
matrix_state.view_matrix);
matrix_state.camera_pos[0] = camera_matrix[12];
matrix_state.camera_pos[1] = camera_matrix[13];
matrix_state.camera_pos[2] = camera_matrix[14];
}

function drawElement(model_context, element) {
Expand All @@ -135,8 +150,6 @@ var World3D = (function() {
}

// inject model matrix into matrix state
matrix_state.modelviewproj =
Math3D.mulMat4x4(matrix_state.viewprojection, element.matrix);
matrix_state.model_matrix = element.matrix;

model_context.drawModel(element.model, -1, matrix_state);
Expand Down
Loading

0 comments on commit 796bf22

Please sign in to comment.