tiny-ecs
Version:
A mean lean Entity-Component-System library.
192 lines (144 loc) • 4.17 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: Spatial.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: Spatial.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>module.exports = Spatial;
var Vec2 = require('./Vec2.js');
/**
* Basic component giving an entity the concept of being located somewhere.
* Creates basic spatial heirachy, positions, rotation, etc.
* @constructor
*/
function Spatial()
{
// Alloc our types
this.position = new Vec2();
this.scale = new Vec2();
this.hwidth = new Vec2();
this._absScale = new Vec2();
this._absPosition = new Vec2();
this._absHwidth = new Vec2();
// Ref to quad tree node this component is in
this._node = null;
// Setup
this.__init();
}
/**
* Setup called by pool.
* @private
*/
Spatial.prototype.__init = function()
{
this.rotation = 0;
// For spatial indexing
this._quadEntry = null;
// Parent component
this.parent = null;
this.position.clear();
this.scale.set(1, 1);
this.hwidth.clear();
this._absPosition.clear();
this._absScale.set(1, 1);
this._absHwidth.clear();
};
/**
* Attach this component to another entity's Spatial.
* @param {{spatial: Spatial}} entity An entity with a Spatial component
*/
Spatial.prototype.attachTo = function(entity)
{
if (this.parent)
throw new Error ('Entity already attached');
if (!entity || !entity.spatial)
throw new Error('Invalid / missing entity');
this.parent = entity.spatial;
};
/**
* The absolute world position of this component.
* @return {Vec2}
*/
Spatial.prototype.absPosition = function()
{
var v = this._absPosition.assign(this.position);
// Trivial case, no parent
if (!this.parent) {
return v;
}
var parent = this.parent;
// Rotate our angle by parent's angle
var r = this.absRotation();
v.rotate(parent.absRotation());
// Scale by parents scale
var pScale = parent.absScale();
v.set(v.x * pScale.x, v.y * pScale.y);
// Add parent's position
return v.add(parent.absPosition());
};
/**
* The absolute world rotation of this component.
* @return {Number}
*/
Spatial.prototype.absRotation = function()
{
// Trivial case
if (!this.parent)
return this.rotation;
// This + parent
var r = this.rotation + this.parent.absRotation();
return r % (Math.PI*2);
};
/**
* The absolute world hwidth for this component.
* @return {Vec2}
*/
Spatial.prototype.absHwidth = function()
{
var h = this._absHwidth.assign(this.hwidth);
if (!this.parent)
return h;
var scale = this.absScale();
return h.set(h.x * scale.x, h.y * scale.y);
};
/**
* The absolute world scale of this component.
* @return {Vec2}
*/
Spatial.prototype.absScale = function()
{
var v = this._absScale.assign(this.scale);
// Trivial case
if (!this.parent) {
return v;
}
var pScale = this.parent.absScale();
return v.set(v.x * pScale.x, v.y * pScale.y);
};
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Index</a></h2><h3>Classes</h3><ul><li><a href="Entity.html">Entity</a></li><li><a href="EntityManager.html">EntityManager</a></li><li><a href="Group.html">Group</a></li><li><a href="ObjectPool.html">ObjectPool</a></li><li><a href="QuadTree.html">QuadTree</a></li><li><a href="Spatial.html">Spatial</a></li><li><a href="Vec2.html">Vec2</a></li></ul>
</nav>
<br clear="both">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha3</a> on Wed Jan 29 2014 01:06:20 GMT-0600 (CST)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>