/js/lib/circlecollision/Circle.js
JavaScript | 155 lines | 108 code | 22 blank | 25 comment | 3 complexity | d24783faa00235479bef36e03bf72f7b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
- /**
- #### ##### ##### #### ### # # ###### ###### ## ## ##### # # ######## ## # # #####
- # # # # ### # # ##### ### ## ## ## # ## # # # # ## # ##### ### ###
- ### # # ##### #### # # # ###### ## ######### ##### ##### ##### # ## # # # # # #####
- -
- File:
- PackedCircle.js
- Created By:
- Mario Gonzalez
- Project :
- None
- Abstract:
- A single packed circle.
- Contains a reference to it's div, and information pertaining to it state.
- Basic Usage:
- http://onedayitwillmake.com/CirclePackJS/
- */
- (function() {
- // Retrieve the namespace
- RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.modules.circlecollision");
- /**
- * @constructor
- */
- RealtimeMultiplayerGame.modules.circlecollision.PackedCircle = function()
- {
- this.boundsRule = RealtimeMultiplayerGame.modules.circlecollision.PackedCircle.BOUNDS_RULE_IGNORE;
- this.position = new RealtimeMultiplayerGame.model.Point(0,0);
- this.offset = new RealtimeMultiplayerGame.model.Point(0,0);
- this.targetPosition = new RealtimeMultiplayerGame.model.Point(0,0);
- return this;
- };
- RealtimeMultiplayerGame.modules.circlecollision.PackedCircle.prototype = {
- id: 0,
- delegate: null,
- position: new RealtimeMultiplayerGame.model.Point(0,0),
- offset: new RealtimeMultiplayerGame.model.Point(0,0), // Offset from delegates position by this much
- radius: 0,
- radiusSquared: 0,
- targetPosition: null, // Where it wants to go
- targetChaseSpeed: 0.02,
- isFixed: false,
- boundsRule: 0,
- collisionMask: 0,
- collisionGroup: 0,
- BOUNDS_RULE_WRAP: 1, // Wrap to otherside
- BOUNDS_RULE_CONSTRAINT: 2, // Constrain within bounds
- BOUNDS_RULE_DESTROY: 4, // Destroy when it reaches the edge
- BOUNDS_RULE_IGNORE: 8, // Ignore when reaching bounds
- containsPoint: function(aPoint)
- {
- var distanceSquared = this.position.getDistanceSquared(aPoint);
- return distanceSquared < this.radiusSquared;
- },
- getDistanceSquaredFromPosition: function(aPosition)
- {
- var distanceSquared = this.position.getDistanceSquared(aPosition);
- // if it's shorter than either radius, we intersect
- return distanceSquared < this.radiusSquared;
- },
- intersects: function(aCircle)
- {
- var distanceSquared = this.position.getDistanceSquared(aCircle.position);
- return (distanceSquared < this.radiusSquared || distanceSquared < aCircle.radiusSquared);
- },
- /**
- * ACCESSORS
- */
- setPosition: function(aPosition)
- {
- this.position = aPosition;
- return this;
- },
- setDelegate: function(aDelegate)
- {
- this.delegate = aDelegate;
- return this;
- },
- setOffset: function(aPosition)
- {
- this.offset = aPosition;
- return this;
- },
- setTargetPosition: function(aTargetPosition)
- {
- this.targetPosition = aTargetPosition;
- return this;
- },
- setTargetChaseSpeed: function(aTargetChaseSpeed)
- {
- this.targetChaseSpeed = aTargetChaseSpeed;
- return this;
- },
- setIsFixed: function(value)
- {
- this.isFixed = value;
- return this;
- },
- setCollisionMask: function(aCollisionMask)
- {
- this.collisionMask = aCollisionMask;
- return this;
- },
- setCollisionGroup: function(aCollisionGroup)
- {
- this.collisionGroup = aCollisionGroup;
- return this;
- },
- setRadius: function(aRadius)
- {
- this.radius = aRadius;
- this.radiusSquared = this.radius*this.radius;
- return this;
- },
- initialize : function(overrides)
- {
- if (overrides)
- {
- for (var i in overrides)
- {
- this[i] = overrides[i];
- }
- }
- return this;
- },
- dealloc: function()
- {
- this.position = null;
- this.offset = null;
- this.delegate = null;
- this.targetPosition = null;
- }
- };
- })();