PageRenderTime 39ms CodeModel.GetById 28ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/circlecollision/Circle.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 155 lines | 108 code | 22 blank | 25 comment | 3 complexity | d24783faa00235479bef36e03bf72f7b MD5 | raw file
  1/**
  2	  ####  #####  ##### ####    ###  #   # ###### ###### ##     ##  #####  #     #      ########    ##    #  #  #####
  3	 #   # #   #  ###   #   #  #####  ###    ##     ##   ##  #  ##    #    #     #     #   ##   #  #####  ###   ###
  4	 ###  #   #  ##### ####   #   #   #   ######   ##   #########  #####  ##### ##### #   ##   #  #   #  #   # #####
  5 -
  6 File:
  7 	PackedCircle.js
  8 Created By:
  9 	Mario Gonzalez
 10 Project	:
 11 	None
 12 Abstract:
 13 	 A single packed circle.
 14	 Contains a reference to it's div, and information pertaining to it state.
 15 Basic Usage:
 16	http://onedayitwillmake.com/CirclePackJS/
 17*/
 18
 19(function() {
 20
 21	// Retrieve the namespace
 22	RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.modules.circlecollision");
 23
 24    /**
 25     * @constructor
 26     */
 27	RealtimeMultiplayerGame.modules.circlecollision.PackedCircle = function()
 28	{
 29		this.boundsRule = RealtimeMultiplayerGame.modules.circlecollision.PackedCircle.BOUNDS_RULE_IGNORE;
 30		this.position = new RealtimeMultiplayerGame.model.Point(0,0);
 31		this.offset = new RealtimeMultiplayerGame.model.Point(0,0);
 32		this.targetPosition = new RealtimeMultiplayerGame.model.Point(0,0);
 33		return this;
 34	};
 35
 36	RealtimeMultiplayerGame.modules.circlecollision.PackedCircle.prototype = {
 37		id: 			0,
 38		delegate:		null,
 39		position:		new RealtimeMultiplayerGame.model.Point(0,0),
 40		offset:			new RealtimeMultiplayerGame.model.Point(0,0),	// Offset from delegates position by this much
 41	   	radius:			0,
 42		radiusSquared:	0,
 43
 44		targetPosition:	null,	// Where it wants to go
 45		targetChaseSpeed: 0.02,
 46
 47		isFixed:		false,
 48		boundsRule:		0,
 49		collisionMask:	0,
 50		collisionGroup:	0,
 51
 52		BOUNDS_RULE_WRAP:		1,      // Wrap to otherside
 53		BOUNDS_RULE_CONSTRAINT:	2,      // Constrain within bounds
 54		BOUNDS_RULE_DESTROY:	4,      // Destroy when it reaches the edge
 55		BOUNDS_RULE_IGNORE:		8,		// Ignore when reaching bounds
 56
 57		containsPoint: function(aPoint)
 58		{
 59			var distanceSquared = this.position.getDistanceSquared(aPoint);
 60			return distanceSquared < this.radiusSquared;
 61		},
 62
 63		getDistanceSquaredFromPosition: function(aPosition)
 64		{
 65			var distanceSquared = this.position.getDistanceSquared(aPosition);
 66			// if it's shorter than either radius, we intersect
 67			return distanceSquared < this.radiusSquared;
 68		},
 69
 70		intersects: function(aCircle)
 71		{
 72			var distanceSquared = this.position.getDistanceSquared(aCircle.position);
 73			return (distanceSquared < this.radiusSquared || distanceSquared < aCircle.radiusSquared);
 74		},
 75
 76/**
 77 * ACCESSORS
 78 */
 79		setPosition: function(aPosition)
 80		{
 81			this.position = aPosition;
 82			return this;
 83		},
 84
 85		setDelegate: function(aDelegate)
 86		{
 87			this.delegate = aDelegate;
 88			return this;
 89		},
 90
 91		setOffset: function(aPosition)
 92		{
 93			this.offset = aPosition;
 94			return this;
 95		},
 96
 97		setTargetPosition: function(aTargetPosition)
 98		{
 99			this.targetPosition = aTargetPosition;
100			return this;
101		},
102
103		setTargetChaseSpeed: function(aTargetChaseSpeed)
104		{
105			this.targetChaseSpeed = aTargetChaseSpeed;
106			return this;
107		},
108
109		setIsFixed: function(value)
110		{
111			this.isFixed = value;
112			return this;
113		},
114
115		setCollisionMask: function(aCollisionMask)
116		{
117			this.collisionMask = aCollisionMask;
118			return this;
119		},
120
121		setCollisionGroup: function(aCollisionGroup)
122		{
123			this.collisionGroup = aCollisionGroup;
124			return this;
125		},
126
127		setRadius: function(aRadius)
128		{
129			this.radius = aRadius;
130			this.radiusSquared = this.radius*this.radius;
131			return this;
132		},
133
134		initialize : function(overrides)
135		{
136			if (overrides)
137			{
138				for (var i in overrides)
139				{
140					this[i] = overrides[i];
141				}
142			}
143
144			return this;
145		},
146
147		dealloc: function()
148		{
149			this.position = null;
150			this.offset = null;
151			this.delegate = null;
152			this.targetPosition = null;
153		}
154	};
155})();