Lot of doc

This commit is contained in:
Thomas FORGIONE
2015-07-06 11:14:42 +02:00
parent c0eb2d26f5
commit f11b1ed7dc
15 changed files with 634 additions and 134 deletions
+14 -47
View File
@@ -116,59 +116,26 @@ L3D.CameraSelecter.prototype.update = function(event, y) {
L3D.CameraSelecter.prototype.click = function(event) {
var e;
if (this.cameras.mainCamera().pointerLocked === false) {
var newCamera = this.pointedCamera();
var newCamera = this.pointedCamera();
if (newCamera !== undefined && !(newCamera instanceof Coin)) {
if (newCamera !== undefined && !(newCamera instanceof Coin)) {
e = new L3D.BD.Event.ArrowClicked();
e.arrow_id = this.cameras.cameras.indexOf(newCamera);
e.send();
e = new L3D.BD.Event.ArrowClicked();
e.arrow_id = this.cameras.cameras.indexOf(newCamera);
e.send();
newCamera.check();
this.cameras.mainCamera().moveHermite(newCamera);
buttonManager.updateElements();
newCamera.check();
this.cameras.mainCamera().moveHermite(newCamera);
buttonManager.updateElements();
} else if (newCamera instanceof Coin) {
} else if (newCamera instanceof Coin) {
// Coin found, notify server
e = new L3D.BD.Event.CoinClicked();
e.coin_id = this.coins.indexOf(newCamera);
e.send();
newCamera.get();
}
// Coin found, notify server
e = new L3D.BD.Event.CoinClicked();
e.coin_id = this.coins.indexOf(newCamera);
e.send();
newCamera.get();
}
};
L3D.CameraSelecter.prototype.clickPointer = function(event) {
var e;
if (this.cameras.mainCamera().pointerLocked) {
var newCamera = this.pointedCamera();
if (newCamera !== undefined && !(newCamera instanceof Coin)) {
e = new L3D.BD.Event.ArrowClicked();
e.arrow_id = this.cameras.cameras.indexOf(newCamera);
e.send();
newCamera.check();
this.cameras.mainCamera().moveHermite(newCamera);
buttonManager.updateElements();
} else if (newCamera instanceof Coin) {
// Coin found, notify server
e = new L3D.BD.Event.CoinClicked();
e.coin_id = this.coins.indexOf(newCamera);
e.send();
newCamera.get();
}
}
};
+167 -55
View File
@@ -1,21 +1,55 @@
/**
* @namespace
* @memberof L3D
*/
L3D.utils = (function() {
var utils = {};
// Defines a double linked-list class
utils.List = function() {
/**
* @constructor
* @description Linked list in javascript
* @memberof L3D.utils
*/
var List = function() {
/**
* @private
* @type {Number}
* @description number of elements in the list
*/
this._size = 0;
/**
* @private
* @type {Object}
* @description first chain element of the list
*/
this._begin = null;
/**
* @private
* @type {Object}
* @description last chain element of the list
*/
this._end = null;
};
// Returns the number of element of a list
utils.List.prototype.size = function() {
/**
* Size of the list
* Complexity O(1)
* @returns {Number} the number of elements in the list
*/
List.prototype.size = function() {
return this._size;
};
// Pushes an element to the end of a class
utils.List.prototype.push = function(element) {
/**
* Push an element at the end of the list
* Complexity O(1)
* @param element {Object} object to push at the end of the list
*/
List.prototype.push = function(element) {
if (this._size === 0) {
this._begin = { data : element, next: null, prev: null };
this._end = this._begin;
@@ -28,8 +62,11 @@ utils.List.prototype.push = function(element) {
}
};
// Sort the list
utils.List.prototype.sort = function(comparator) {
/**
* Sorts the list by creating an array, sorting it, and recopying it to the list
* Complexity O(size() * log (size()))
*/
List.prototype.sort = function(comparator) {
if (comparator === undefined) {
comparator = priv.defaultComparator;
@@ -52,15 +89,23 @@ utils.List.prototype.sort = function(comparator) {
}
};
// Remove last element and returns it
utils.List.prototype.pop = function() {
/**
* Removes and returns the last element of the list
* Complexity O(1)
* @returns {Object} the last element of the list
*/
List.prototype.pop = function() {
var tmp = this._end;
this._end = null;
return tmp.data;
};
// Apply a function to each element of the list
utils.List.prototype.forEach = function(callback) {
/**
* Apply a call back to each element of the list
* Complexity O(size())
* @param callback {function} callback to call on all elements of the list
*/
List.prototype.forEach = function(callback) {
var chain = this._begin;
while (chain !== null) {
@@ -69,8 +114,12 @@ utils.List.prototype.forEach = function(callback) {
}
};
// Apply a function to each element of the list (starting from the end)
utils.List.prototype.forEachInverse = function(callback) {
/**
* Apply a call back to each element of the list in reverse order
* Complexity O(size())
* @param callback {function} callback to call on all elements of the list in reverse order
*/
List.prototype.forEachInverse = function(callback) {
var chain = this._end;
while (chain !== null) {
@@ -79,8 +128,13 @@ utils.List.prototype.forEachInverse = function(callback) {
}
};
// Get ith element of the list
utils.List.prototype.at = function(ith) {
/**
* Get the ith element of the list
* Complexity O(ith)
* @param ith {Number} index of the element to get
* @returns {Object} the ith element if it exists, null otherwise
*/
List.prototype.at = function(ith) {
if (ith < 0 || ith >= this.size()) {
return null;
}
@@ -93,16 +147,23 @@ utils.List.prototype.at = function(ith) {
return chain.data;
};
// Clear the list
utils.List.prototype.clear = function() {
/**
* Empty the list
*/
List.prototype.clear = function() {
this._begin = null;
this._end = null;
this._size = 0;
};
// Insert an element at the right place in the list
// Precondition : list must be sorted
utils.List.prototype.insertSorted = function(elt, comparator) {
/**
* Insert an element at the right place in a sorted list
* Precondition : the list must be sorted
* Complexity : O(i) where i is the number of elements lower than elt
* @param elt {Object} element to add
* @param comparator {function} classic js comparator
*/
List.prototype.insertSorted = function(elt, comparator) {
var newElement;
if (comparator === undefined) {
@@ -146,8 +207,13 @@ utils.List.prototype.insertSorted = function(elt, comparator) {
}
};
// Check if a list is sorted of not
utils.List.prototype.isSorted = function(comparator) {
/**
* Checks if a list is sorted
* Complexity : O(size()) if the list is sorted, O(i) where i is the first non-sorted element in the list
* @param comparator {function} classic js comparator
* @returns {Boolean} true if the list is sorted, false otherwise
*/
List.prototype.isSorted = function(comparator) {
var chain = this._begin;
if (comparator === undefined) {
@@ -164,61 +230,107 @@ utils.List.prototype.isSorted = function(comparator) {
return true;
};
// Gives an iterator to the begin of the list
utils.List.prototype.begin = function() {
return new utils.List.Iterator(this._begin, 0);
/**
* Returns an iterator to the begin of the list
* @returns {Iterator} an interator to the first element
*/
List.prototype.begin = function() {
return new Iterator(this._begin, 0);
};
// Gives an iterator to the end of the list
utils.List.prototype.end = function() {
return new utils.List.Iterator(this._end, this.size() - 1);
/**
* Returns an iterator to the end of the list
* @returns {Iterator} an interator to the first element
*/
List.prototype.end = function() {
return new Iterator(this._end, this.size() - 1);
};
// Class iterator
utils.List.Iterator = function(chain, counter) {
/**
* @constructor
* @description Reprensents an iterator to an element of a list
* @param chain {Object} chain element of a list
* @param counter {Number} index of the current element
* @memberof L3D.utils
*/
var Iterator = function(chain, counter) {
this._chain = chain;
this._counter = counter;
};
// Go to the next element
utils.List.Iterator.prototype.next = function() {
/**
* Go to the next element
* @method
*/
Iterator.prototype.next = function() {
this._chain = this._chain.next;
this._counter ++;
};
// Go to the previous element
utils.List.Iterator.prototype.prev = function() {
/**
* Go to the previous element
* @method
*/
Iterator.prototype.prev = function() {
this._chain = this._chain.prev;
this._counter --;
};
// Return the current element
utils.List.Iterator.prototype.get = function() {
/**
* Returns the current element
* @method
* @returns {Object} current element
*/
Iterator.prototype.get = function() {
return this._chain.data;
};
// Check if there is another element next
utils.List.Iterator.prototype.hasNext = function() {
/**
* Checks if there is a element after the current element
* @method
* @returns {Boolean} true if the element exists, false otherwise
*/
Iterator.prototype.hasNext = function() {
return this._chain.next !== null;
};
// Check if there is another element before
utils.List.Iterator.prototype.hasPrev = function() {
/**
* Checks if there is a element before the current element
* @method
* @returns {Boolean} true if the element exists, false otherwise
*/
Iterator.prototype.hasPrev = function() {
return this._chain.prev !== null;
};
// Compares to another iterator of the same list
utils.List.Iterator.prototype.lowerThan = function(it2) {
return utils.distance(this, it2) > 0;
/**
* Compares two iterators of the same list
* @param it2 {Iterator} second iterator of the comparison
* @returns {Boolean} result of this < it2
*/
Iterator.prototype.lowerThan = function(it2) {
return distance(this, it2) > 0;
};
// Compares to another iterator of the same list
utils.List.Iterator.prototype.greaterThan = function(it2) {
return utils.distance(this, it2) < 0;
/**
* Compares two iterators of the same list
* @method
* @param it2 {Iterator} second iterator of the comparison
* @returns {Boolean} result of this > it2
*/
Iterator.prototype.greaterThan = function(it2) {
return distance(this, it2) < 0;
};
// Returns the distance between two iterators of the same list
utils.distance = function(it1, it2) {
/**
* Compute the distance between two iterators
* @method
* @private
* @param it1 {Iterator} first iterator of the computation
* @param it2 {Iterator} second iterator of the computation
* @returns {Number} distance between it1 and it2
*/
var distance = function(it1, it2) {
return it2._counter - it1._counter;
};
@@ -232,11 +344,11 @@ priv.defaultComparator = function(a,b) {
return 0;
};
// Support for NodeJs
if (typeof module !== 'undefined' && module.exports) {
module.exports = utils;
} else {
return utils;
}
utils.List = List;
utils.Iterator = Iterator;
return utils;
console.log("Hello");
})();