Commit bd13ba6c authored by Nicola Spanti's avatar Nicola Spanti

Adding comments for documenting and checking in src/js/specific/

parent 7ed837aa
/*
* Copyright (C) 2015, Nicola Spanti (also known as RyDroid) <dev@nicola-spanti.info>
* Copyright (C) 2015, Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
......@@ -25,6 +25,9 @@ function GameSimpleMainMenu(namesOfElements)
this.namesOfElements = new Set(namesOfElements);
}
/**
* @param {number|Number=} value
*/
GameSimpleMainMenu.scrollTop = function(value)
{
'use strict';
......@@ -57,7 +60,7 @@ GameSimpleMainMenu.prototype.getButton = function(name)
'use strict';
if(!this.namesOfElements.has(name))
return false;
return null;
return document.getElementById('button_'+ name);
};
......@@ -86,7 +89,7 @@ GameSimpleMainMenu.prototype.getActivity = function(name)
'use strict';
if(!this.namesOfElements.has(name))
return false;
return null;
return document.getElementById(name);
};
......@@ -177,8 +180,8 @@ GameSimpleMainMenu.prototype.addDefaultEventListenersToButton = function(name)
throw new Error(name +' is not an element of the menu');
var types = GameSimpleMainMenu.DEFAULT_BUTTON_EVENT_LISTENER_TYPES.toArray();
for(var i=0; i < types.length; ++i)
for(var index = 0; index < types.length; ++index)
{
this.addDefaultEventListenerToButtonUnsafe(name, types[i]);
this.addDefaultEventListenerToButtonUnsafe(name, types[index]);
}
};
/*
* Copyright (C) 2015, Nicola Spanti (also known as RyDroid) <dev@nicola-spanti.info>
* Copyright (C) 2015, 2017 Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
......@@ -16,8 +16,16 @@
*/
/** const */
var GNU_RUNNER_ALIVE_DEFAULT_IMAGE_PATH = 'images/runners/gnu-head_30-years-anniversary.svg';
/** const */
var GNU_RUNNER_DEAD_DEFAULT_IMAGE_PATH = 'images/runners/gnu-head_ghost.svg';
/**
* @constructor
* @param {string|String=} img_path
*/
function GnuRunner(img_path)
{
......@@ -26,32 +34,56 @@ function GnuRunner(img_path)
if(isString(img_path))
this.imageAlive = ImagesStatusCache.getImageStatus(img_path);
else
this.imageAlive = ImagesStatusCache.getImageStatus('images/runners/gnu-head_30-years-anniversary.svg');
this.imageDead = ImagesStatusCache.getImageStatus('images/runners/gnu-head_ghost.svg');
this.setDefaultImageAlive();
this.setDefaultImageDead();
this.isAlive = true;
this.nbGoodItemsCollected = 0;
}
/**
* @return {ImageStatus}
*/
GnuRunner.prototype.getImageStatus = function()
{
"use strict";
return this.isAlive ? this.imageAlive : this.imageDead;
};
/**
* @return {Image}
*/
GnuRunner.prototype.getImage = function()
{
"use strict";
return this.getImageStatus().image;
return this.getImageStatus().getImage();
};
GnuRunner.prototype.setDefaultImageAlive = function()
{
"use strict";
this.imageAlive = ImagesStatusCache.getImageStatus(GNU_RUNNER_ALIVE_DEFAULT_IMAGE_PATH);
};
GnuRunner.prototype.setDefaultImageDead = function()
{
"use strict";
this.imageDead = ImagesStatusCache.getImageStatus(GNU_RUNNER_DEAD_DEFAULT_IMAGE_PATH);
};
/**
* @return {boolean}
*/
GnuRunner.prototype.isReady = function()
{
"use strict";
return this.imageAlive.isReady && this.imageDead.isReady;
};
/**
* @param {ObjectDrawing2DInformation} drawingInformation
*/
GnuRunner.prototype.setDrawingInformation = function(drawingInformation)
{
if(!(drawingInformation instanceof ObjectDrawing2DInformation))
......@@ -65,13 +97,16 @@ GnuRunner.prototype.die = function()
this.isAlive = false;
};
GnuRunner.prototype.hitBy = function(object)
/**
* @param {RunnerPathItem} anObject
*/
GnuRunner.prototype.hitBy = function(anObject)
{
"use strict";
if(object instanceof RunnerPathItem)
if(anObject instanceof RunnerPathItem)
{
if(object.isGood)
if(anObject.isGood)
{
++this.nbGoodItemsCollected;
/* TODO */
......@@ -81,6 +116,9 @@ GnuRunner.prototype.hitBy = function(object)
}
};
/**
* @return {number|Number}
*/
GnuRunner.prototype.getScore = function()
{
"use strict";
......
......@@ -39,13 +39,15 @@ function GnuRunnersGame(afterLosingFunction)
throw new Error('There is no context of the canvas');
}
this.audio = new AudioManager(document.querySelector('audio'), false, false);
this.audio = new AudioManager(
document.querySelector('audio'), false, false
);
this.paths = new Array(2);
for(var i=0; i < this.paths.length; ++i)
for(var index=0; index < this.paths.length; ++index)
{
this.paths[i] = new NearRunnerPathsGroup(2);
this.paths[i].paths[0].runner = new GnuRunner();
this.paths[index] = new NearRunnerPathsGroup(2);
this.paths[index].paths[0].runner = new GnuRunner();
}
this.clock = new Clock();
......@@ -73,14 +75,14 @@ GnuRunnersGame.prototype.setElementsDrawingInformation = function()
{
"use strict";
for(var i=0; i < this.paths.length; ++i)
var width = this.canvas.width / this.paths.length;
var height = this.canvas.height;
for(var index = 0; index < this.paths.length; ++index)
{
this.paths[i].setDrawingInformation(
new ObjectDrawing2DInformation(
new Position2D(i / this.paths.length * this.canvas.width, 0),
this.canvas.width / this.paths.length,
this.canvas.height
)
var position = new Position2D(index / this.paths.length * this.canvas.width, 0);
var path = this.paths[index];
path.setDrawingInformation(
new ObjectDrawing2DInformation(position, width, height)
);
}
};
......@@ -92,6 +94,9 @@ GnuRunnersGame.prototype.setWindowSize = function()
this.setElementsDrawingInformation();
};
/**
* @return {number|Number}
*/
GnuRunnersGame.prototype.getPercentageReady = function()
{
"use strict";
......@@ -102,13 +107,17 @@ GnuRunnersGame.prototype.getPercentageReady = function()
}
var percentage = 0;
for(var i=0; i < this.paths.length; ++i)
for(var index = 0; index < this.paths.length; ++index)
{
percentage += this.paths[i].getPercentageReady();
var path = this.paths[index];
percentage += path.getPercentageReady();
}
return percentage / this.paths.length;
};
/**
* @return {boolean}
*/
GnuRunnersGame.prototype.isReady = function()
{
"use strict";
......@@ -118,9 +127,10 @@ GnuRunnersGame.prototype.isReady = function()
return false;
}
for(var i=0; i < this.paths.length; ++i)
for(var index=0; index < this.paths.length; ++index)
{
if(!this.paths[i].isReady())
var path = this.paths[index];
if(!path.isReady())
{
return false;
}
......@@ -128,6 +138,9 @@ GnuRunnersGame.prototype.isReady = function()
return true;
};
/**
* @return {boolean}
*/
GnuRunnersGame.prototype.isOver = function()
{
"use strict";
......@@ -136,7 +149,10 @@ GnuRunnersGame.prototype.isOver = function()
{
for(j=0; j < this.paths[i].paths.length; ++j)
{
if(this.paths[i].paths[j].runner instanceof GnuRunner && !this.paths[i].paths[j].runner.isAlive)
if(
this.paths[i].paths[j].runner instanceof GnuRunner &&
!this.paths[i].paths[j].runner.isAlive
)
{
return true;
}
......@@ -221,17 +237,23 @@ GnuRunnersGame.prototype.eventKeyUpListener = function(event)
}
};
GnuRunnersGame.prototype.getPathFromEvent = function(event)
{
"use strict";
var bodyWidth = document.querySelector('body').clientWidth;
var index = parseInt(event.clientX / bodyWidth * this.paths.length, 10);
var path = this.paths[index];
return path;
};
GnuRunnersGame.prototype.eventClickListener = function(event)
{
"use strict";
if(this.paths.length > 0 && !this.isOver())
{
this.paths[
parseInt(
event.clientX / document.querySelector('body').clientWidth * this.paths.length,
10)
].changeRunnerToAnOtherPath();
var path = this.getPathFromEvent(event);
path.changeRunnerToAnOtherPath();
}
};
......@@ -245,8 +267,12 @@ GnuRunnersGame.prototype.addEventListeners = function()
GnuRunnersGame.prototype.removeEventListeners = function()
{
"use strict";
window.removeEventListener('keyup', this.eventKeyUpListener.bind(this), false);
window.removeEventListener('click', this.eventClickListener.bind(this), false);
window.removeEventListener(
'keyup', this.eventKeyUpListener.bind(this), false
);
window.removeEventListener(
'click', this.eventClickListener.bind(this), false
);
};
GnuRunnersGame.prototype.manageGamepadEvent = function(gamepad)
......@@ -279,9 +305,10 @@ GnuRunnersGame.prototype.manageGamepadsEvents = function()
var gamepads = getGamepads();
if(!isGamepadArrayEmpty(gamepads))
{
for(var i=0; i < gamepads.length; ++i)
for(var index = 0; index < gamepads.length; ++index)
{
this.manageGamepadEvent(gamepads[i]);
var gamepad = gamepads[index];
this.manageGamepadEvent(gamepad);
}
}
};
......@@ -293,7 +320,9 @@ GnuRunnersGame.prototype.requestAnimationFrameCallback = function()
// Without this condition, even after cancelAnimationFrame, this function is called!
if(this.idDrawingForAFrame != null)
{
this.idDrawingForAFrame = window.requestAnimationFrame(this.requestAnimationFrameCallback.bind(this));
this.idDrawingForAFrame = window.requestAnimationFrame(
this.requestAnimationFrameCallback.bind(this)
);
}
};
......@@ -309,14 +338,22 @@ GnuRunnersGame.prototype.start = function()
{
element.addEventListeners();
element.clock.start();
if(typeof(window.requestAnimationFrame) == 'function' && typeof(window.cancelAnimationFrame) == 'function')
if(
typeof(window.requestAnimationFrame) == 'function' &&
typeof(window.cancelAnimationFrame) == 'function'
)
{
element.idDrawingForAFrame = 0;
element.idDrawingForAFrame = window.requestAnimationFrame(element.requestAnimationFrameCallback.bind(element));
element.idDrawingForAFrame = window.requestAnimationFrame(
element.requestAnimationFrameCallback.bind(element)
);
}
else
{
element.idDrawingForAFrame = window.setInterval(element.updateAndDraw.bind(element), element.timeInMillisecondsBetweenEachFrame);
element.idDrawingForAFrame = window.setInterval(
element.updateAndDraw.bind(element),
element.timeInMillisecondsBetweenEachFrame
);
}
}
else
......@@ -332,7 +369,10 @@ GnuRunnersGame.prototype.stop = function()
{
"use strict";
if(typeof(window.requestAnimationFrame) == 'function' && typeof(window.cancelAnimationFrame) == 'function')
if(
typeof(window.requestAnimationFrame) == 'function' &&
typeof(window.cancelAnimationFrame) == 'function'
)
{
window.cancelAnimationFrame(this.idDrawingForAFrame);
}
......@@ -357,16 +397,17 @@ GnuRunnersGame.prototype.lose = function()
};
/**
* @return {Number} score value
* @return {number|Number} score value
*/
GnuRunnersGame.prototype.getScoreValue = function()
{
"use strict";
var score = 0;
for(var i=0; i < this.paths.length; ++i)
for(var index = 0; index < this.paths.length; ++index)
{
score += this.paths[i].getScore();
var path = this.paths[index];
score += path.getScore();
}
return score;
};
......@@ -384,12 +425,14 @@ GnuRunnersGame.prototype.update = function()
this.clock.update();
this.frameDeltaTimeFactor = this.clock.getDeltaTime() / this.timeInMillisecondsBetweenEachFrame;
// If the computer has enough power to make a frame each this.timeInMillisecondsBetweenEachFrame, factor=1
// But, if for example the computer needs more time to make a frame, this factor will permit to run the game as the normal speed even if there is less FPS
// But, if for example the computer needs more time to make a frame,
// this factor will permit to run the game as the normal speed even if there is less FPS
this.manageGamepadsEvents();
for(var i=0; i < this.paths.length; ++i)
for(var index = 0; index < this.paths.length; ++index)
{
this.paths[i].update(this.clock, this.distancePerMicroseconds);
var path = this.paths[index];
path.update(this.clock, this.distancePerMicroseconds);
}
}
};
......
......@@ -21,17 +21,28 @@ var GnuRunnersGameDrawer2D = {};
GnuRunnersGameDrawer2D.RUNNER_PATH_RUNNER_WIDTH_FACTOR = 0.8;
GnuRunnersGameDrawer2D.RUNNER_PATH_ITEM_WIDTH_FACTOR = GnuRunnersGameDrawer2D.RUNNER_PATH_RUNNER_WIDTH_FACTOR * GnuRunnersGameDrawer2D.RUNNER_PATH_RUNNER_WIDTH_FACTOR;
/**
* @param {HTMLCanvasElement} canvas
* @param {CanvasRenderingContext2D} context
*/
GnuRunnersGameDrawer2D.drawBackground = function(canvas, context)
{
"use strict";
ObjectDrawer2D.drawBackgroundWithStyle(canvas, context, "rgb(240, 240, 240)");
ObjectDrawer2D.drawBackgroundWithStyle(
canvas, context, "rgb(240, 240, 240)"
);
};
/**
* @param {CanvasRenderingContext2D} context
* @param {RunnerPathItem} item
*/
GnuRunnersGameDrawer2D.drawRunnerPathItem = function(context, item)
{
"use strict";
/* This verification "fix" problem with undefined/null and not fully loaded images (NS_ERROR_NOT_AVAILABLE) */
// This verification "fix" problem with undefined/null
// and not fully loaded images (NS_ERROR_NOT_AVAILABLE)
if(item instanceof RunnerPathItem && item.isReady())
{
ObjectDrawer2D.drawImage(
......@@ -42,15 +53,24 @@ GnuRunnersGameDrawer2D.drawRunnerPathItem = function(context, item)
}
};
/**
* @param {CanvasRenderingContext2D} context
* @param {Array.<RunnerPathItem>} items
*/
GnuRunnersGameDrawer2D.drawRunnerPathItems = function(context, items)
{
"use strict";
for(var i=0; i < items.length; ++i)
for(var index = 0; index < items.length; ++index)
{
GnuRunnersGameDrawer2D.drawRunnerPathItem(context, items[i]);
var item = items[index];
GnuRunnersGameDrawer2D.drawRunnerPathItem(context, item);
}
};
/**
* @param {CanvasRenderingContext2D} context
* @param {GnuRunner} runner
*/
GnuRunnersGameDrawer2D.drawGnuRunner = function(context, runner)
{
"use strict";
......@@ -65,6 +85,10 @@ GnuRunnersGameDrawer2D.drawGnuRunner = function(context, runner)
}
};
/**
* @param {CanvasRenderingContext2D} context
* @param {RunnerPath} path
*/
GnuRunnersGameDrawer2D.drawRunnerPath = function(context, path)
{
"use strict";
......@@ -76,19 +100,25 @@ GnuRunnersGameDrawer2D.drawRunnerPath = function(context, path)
}
};
/**
* @param {CanvasRenderingContext2D} context
* @param {Array.<RunnerPath>} paths
*/
GnuRunnersGameDrawer2D.drawRunnerPaths = function(context, paths)
{
"use strict";
for(var i=0; i < paths.length; ++i)
for(var index = 0; index < paths.length; ++index)
{
GnuRunnersGameDrawer2D.drawRunnerPath(
context,
paths[i]
);
var path = paths[index];
GnuRunnersGameDrawer2D.drawRunnerPath(context, path);
}
};
/**
* @param {CanvasRenderingContext2D} context
* @param {NearRunnerPathsGroup} pathsGroup
*/
GnuRunnersGameDrawer2D.drawNearRunnerPathsGroup = function(context, pathsGroup)
{
"use strict";
......@@ -107,47 +137,116 @@ GnuRunnersGameDrawer2D.drawNearRunnerPathsGroup = function(context, pathsGroup)
}
};
/**
* @param {CanvasRenderingContext2D} context
* @param {Array.<NearRunnerPathsGroup>} paths
*/
GnuRunnersGameDrawer2D.drawNearRunnerPathsGroups = function(context, paths)
{
"use strict";
for(var i=0; i < paths.length; ++i)
for(var index = 0; index < paths.length; ++index)
{
var path = paths[index];
GnuRunnersGameDrawer2D.drawNearRunnerPathsGroup(
context,
paths[i]
context, path
);
}
};
/**
* @return {number|Number}
*/
GnuRunnersGameDrawer2D.getTopPanelHeightFromCanvas = function(canvas)
{
"use strict";
return parseInt(canvas.height / 10, 10);
};
/**
* @return {number|Number}
*/
GnuRunnersGameDrawer2D.getTopPanelHeightFromGame = function(game)
{
"use strict";
return GnuRunnersGameDrawer2D.getTopPanelHeightFromCanvas(game.canvas);
};
/**
* @param {number|Number} topPanelHeight
* @return {number|Number}
*/
GnuRunnersGameDrawer2D.getTopPanelFontSizeFromHeight = function(topPanelHeight)
{
"use strict";
return parseInt(topPanelHeight * 0.8, 10);
};
/**
* @return {number|Number}
*/
GnuRunnersGameDrawer2D.getTopPanelFontSizeFromGame = function(game)
{
"use strict";
var topPanelHeight = GnuRunnersGameDrawer2D.getTopPanelHeightFromGame(game);
var fontSize = GnuRunnersGameDrawer2D.getTopPanelFontSizeFromHeight(topPanelHeight);
return fontSize;
};
/**
* @param {GnuRunnersGame} game
*/
GnuRunnersGameDrawer2D.drawTopPanelBackground = function(game)
{
"use strict";
if(ObjectDrawer2D.current_quality_mode >= ObjectDrawer2D.HIGH_QUALITY_MODE)
{
game.context.fillStyle = "rgba(0, 0, 0, 0.5)";
}
else
{
// A transparent panel is better, but not needed
game.context.fillStyle = "rgb(150, 150, 150)";
}
var topPanelHeight = GnuRunnersGameDrawer2D.getTopPanelHeightFromGame(game);
game.context.fillRect(0, 0, game.canvas.width, topPanelHeight);
};
/**
* @param {GnuRunnersGame} game
*/
GnuRunnersGameDrawer2D.drawTopPanelScore = function(game)
{
"use strict";
var fontSize = GnuRunnersGameDrawer2D.getTopPanelFontSizeFromGame(game);
game.context.fillStyle = "rgb(240, 240, 240)";
game.context.font = fontSize +'px "Droid Sans"';
game.context.textAlign = 'right';
game.context.fillText(
new String(game.getScoreValue()),
game.canvas.width * 0.96, fontSize
);
};
/**
* @param {GnuRunnersGame} game
*/
GnuRunnersGameDrawer2D.drawTopPanel = function(game)
{
"use strict";
if(!game.isOver())
{
// Draw panel
var topPanelHeight = parseInt(game.canvas.height / 10, 10);
if(ObjectDrawer2D.current_quality_mode >= ObjectDrawer2D.HIGH_QUALITY_MODE)
{
game.context.fillStyle = "rgba(0, 0, 0, 0.5)";
}
else
{
// A transparent panel is better, but not needed
game.context.fillStyle = "rgb(150, 150, 150)";
}
game.context.fillRect(0, 0, game.canvas.width, topPanelHeight);
// Print score
var fontSize = parseInt(topPanelHeight * 0.8, 10);
game.context.fillStyle = "rgb(240, 240, 240)";
game.context.font = fontSize +'px "Droid Sans"';
game.context.textAlign = 'right';
game.context.fillText(new String(game.getScoreValue()), game.canvas.width * 0.96, fontSize);
GnuRunnersGameDrawer2D.drawTopPanelBackground(game);
GnuRunnersGameDrawer2D.drawTopPanelScore (game);
}
};
/**
* @param {GnuRunnersGame} game
*/
GnuRunnersGameDrawer2D.drawGame = function(game)
{
"use strict";
......
/*
* Copyright (C) 2015, Nicola Spanti (also known as RyDroid) <dev@nicola-spanti.info>
* Copyright (C) 2015, 2017 Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
......@@ -33,9 +33,9 @@ function NearRunnerPathsGroup(nb)
else
{
this.paths = new Array(nb);
for(var i=0; i < nb; ++i)
for(var index=0; index < nb; ++index)
{
this.paths[i] = new RunnerPath();
this.paths[index] = new RunnerPath();
}
}
}
......@@ -43,23 +43,33 @@ function NearRunnerPathsGroup(nb)
this.distance = 0;
}
/**
* @return {number|Number}
*/
NearRunnerPathsGroup.prototype.getPercentageReady = function()
{
"use strict";
var percentage = 0;
for(var i=0; i < this.paths.length; ++i)
percentage += this.paths[i].getPercentageReady();
for(var index=0; index < this.paths.length; ++index)
{
var path = this.paths[index];
percentage += path.getPercentageReady();
}
return percentage / this.paths.length;
};
/**
* @return {boolean}
*/
NearRunnerPathsGroup.prototype.isReady = function()
{
"use strict";
for(var i=0; i < this.paths.length; ++i)
for(var index = 0; index < this.paths.length; ++index)
{
if(!this.paths[i].isReady())
var path = this.paths[index];
if(!path.isReady())
return false;
}
return true;
......@@ -71,27 +81,33 @@ NearRunnerPathsGroup.prototype.setDrawingInformation = function(drawingInformati
throw new TypeError('drawingInformation must be an instance of ObjectDrawing2DInformation');
this.drawingInformation = drawingInformation;
var pathDrawingInformation = this.drawingInformation.getCopy();
pathDrawingInformation.setWidth(this.drawingInformation.width / this.paths.length);
for(var i=0; i < this.paths.length; ++i)
var pathDrawingInformationCache = this.drawingInformation.getCopy();
pathDrawingInformationCache.setWidth(this.drawingInformation.width / this.paths.length);
for(var index=0; index < this.paths.length; ++index)
{
var pathDrawingInformation = this.drawingInformation.getCopy();
pathDrawingInformation.setWidth(this.drawingInformation.width / this.paths.length);