Skip to content

Commit

Permalink
Create internal copy of width and height in Sprite class
Browse files Browse the repository at this point in the history
  • Loading branch information
Caley Brock authored and Caley Brock committed Jul 30, 2016
1 parent 9666e9a commit 56afbe9
Showing 1 changed file with 65 additions and 15 deletions.
80 changes: 65 additions & 15 deletions lib/p5.play.js
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,32 @@ function Sprite(pInst, _x, _y, _w, _h) {
*/
this.mouseIsPressed = false;

/**
* Width of the sprite's current image.
* If no images or animations are set it's the width of the
* placeholder rectangle.
* Used internally to make calculations.
*
* @property internalWidth
* @type {Number}
* @default 100
*/

this.internalWidth = _w;

/**
* Height of the sprite's current image.
* If no images or animations are set it's the height of the
* placeholder rectangle.
* Used internally to make calculations.
*
* @property internalHeight
* @type {Number}
* @default 100
*/

this.internalHeight = _h;

/**
* Width of the sprite's current image.
* If no images or animations are set it's the width of the
Expand All @@ -1020,6 +1046,18 @@ function Sprite(pInst, _x, _y, _w, _h) {
* @type {Number}
* @default 100
*/

Object.defineProperty(this, 'width', {
enumerable: true,
configurable: true,
get: function() {
return this.internalWidth;
},
set: function(value) {
this.internalWidth = value;
}
});

if(_w === undefined)
this.width = 100;
else
Expand All @@ -1034,6 +1072,18 @@ function Sprite(pInst, _x, _y, _w, _h) {
* @type {Number}
* @default 100
*/

Object.defineProperty(this, 'height', {
enumerable: true,
configurable: true,
get: function() {
return this.internalHeight;
},
set: function(value) {
this.internalHeight = value;
}
});

if(_h === undefined)
this.height = 100;
else
Expand All @@ -1048,7 +1098,7 @@ function Sprite(pInst, _x, _y, _w, _h) {
* @type {Number}
* @default 100
*/
this.originalWidth = this.width;
this.originalWidth = this.internalWidth;

/**
* Unscaled height of the sprite
Expand All @@ -1059,7 +1109,7 @@ function Sprite(pInst, _x, _y, _w, _h) {
* @type {Number}
* @default 100
*/
this.originalHeight = this.height;
this.originalHeight = this.internalHeight;

/**
* True if the sprite has been removed.
Expand Down Expand Up @@ -1172,17 +1222,17 @@ function Sprite(pInst, _x, _y, _w, _h) {
{
this.collider = this.getBoundingBox();
this.colliderType = 'image';
this.width = animations[currentAnimation].getWidth()*abs(this.scale);
this.height = animations[currentAnimation].getHeight()*abs(this.scale);
this.internalWidth = animations[currentAnimation].getWidth()*abs(this.scale);
this.internalHeight = animations[currentAnimation].getHeight()*abs(this.scale);
//quadTree.insert(this);
}

//update size and collider
if(animations[currentAnimation].frameChanged || this.width === undefined || this.height === undefined)
if(animations[currentAnimation].frameChanged || this.internalWidth === undefined || this.internalHeight === undefined)
{
//this.collider = this.getBoundingBox();
this.width = animations[currentAnimation].getWidth()*abs(this.scale);
this.height = animations[currentAnimation].getHeight()*abs(this.scale);
this.internalWidth = animations[currentAnimation].getWidth()*abs(this.scale);
this.internalHeight = animations[currentAnimation].getHeight()*abs(this.scale);
}
}

Expand Down Expand Up @@ -1217,11 +1267,11 @@ function Sprite(pInst, _x, _y, _w, _h) {
}
else if(this.colliderType === 'image')
{
this.collider.extents.x = this.width * abs(cos(t)) +
this.height * abs(sin(t));
this.collider.extents.x = this.internalWidth * abs(cos(t)) +
this.internalHeight * abs(sin(t));

this.collider.extents.y = this.width * abs(sin(t)) +
this.height * abs(cos(t));
this.collider.extents.y = this.internalWidth * abs(sin(t)) +
this.internalHeight * abs(cos(t));
}
}

Expand Down Expand Up @@ -1283,8 +1333,8 @@ function Sprite(pInst, _x, _y, _w, _h) {
if(animations[currentAnimation] && (animations[currentAnimation].getWidth() !== 1 && animations[currentAnimation].getHeight() !== 1))
{
this.collider = this.getBoundingBox();
this.width = animations[currentAnimation].getWidth()*abs(this.scale);
this.height = animations[currentAnimation].getHeight()*abs(this.scale);
this.internalWidth = animations[currentAnimation].getWidth()*abs(this.scale);
this.internalHeight = animations[currentAnimation].getHeight()*abs(this.scale);
//quadTree.insert(this);
this.colliderType = 'image';
//print("IMAGE COLLIDER ADDED");
Expand All @@ -1296,7 +1346,7 @@ function Sprite(pInst, _x, _y, _w, _h) {
}
else //get the with and height defined at the creation
{
this.collider = new AABB(pInst, this.position, createVector(this.width, this.height));
this.collider = new AABB(pInst, this.position, createVector(this.internalWidth, this.internalHeight));
//quadTree.insert(this);
this.colliderType = 'default';
}
Expand Down Expand Up @@ -1559,7 +1609,7 @@ function Sprite(pInst, _x, _y, _w, _h) {
{
noStroke();
fill(this.shapeColor);
rect(0, 0, this.width, this.height);
rect(0, 0, this.internalWidth, this.internalHeight);
}
};

Expand Down

0 comments on commit 56afbe9

Please sign in to comment.