Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #38 from 201flaviosilva/32-engine-scale-game-object
Browse files Browse the repository at this point in the history
[Engine]: Scale Game Object #32
  • Loading branch information
201flaviosilva authored Mar 22, 2022
2 parents 6acbfa0 + 9e34483 commit a25779b
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/GameObjects/Circle/Circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export default class Circle extends GameObject {
this._type = Types.circle;
}

get x() { return this._x - this.radius * this.origin.x; } // Get the position X relative to the origin
get y() { return this._y - this.radius * this.origin.y; } // Get the position Y relative to the origin
get x() { return this._x - this.radius * this.origin.x * this.scale.x; } // Get the position X relative to the origin
get y() { return this._y - this.radius * this.origin.y * this.scale.y; } // Get the position Y relative to the origin
}

Object.assign(Circle.prototype, CommonMethods);
4 changes: 2 additions & 2 deletions src/GameObjects/Circle/PhysicsCircle.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export default class PhysicsCircle extends GameObject {
this._type = Types.circle;
}

get x() { return this._x - this.radius * this.origin.x; } // Get the position X relative to the origin
get y() { return this._y - this.radius * this.origin.y; } // Get the position Y relative to the origin
get x() { return this._x - this.radius * this.origin.x * this.scale.x; } // Get the position X relative to the origin
get y() { return this._y - this.radius * this.origin.y * this.scale.y; } // Get the position Y relative to the origin

_debugBody() {
if (!this.active) return;
Expand Down
23 changes: 23 additions & 0 deletions src/GameObjects/GameObjectBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default class GameObject {
this.z = 0;
this.rotation = 0; // Rotation in radians
this.angle = 0; // Rotation in degrees
this.scale = { x: 1, y: 1 };

this.lastPosition = { x: this._x, y: this._y, z: this.z };
this.origin = { x: 0, y: 0 };
this.fillColor = fillColor;
Expand Down Expand Up @@ -72,6 +74,21 @@ export default class GameObject {
return this;
}

// Scale
// Origin
setScaleX(x) {
this.setScale(x, this.scale.y);
return this;
}
setScaleY(y) {
this.setScale(this.scale.x, y);
return this;
}
setScale(x = 0, y = x) {
this.scale = { x, y };
return this;
}

// Origin
setOriginX(x) {
this.setOrigin(x, this.origin.y);
Expand Down Expand Up @@ -124,6 +141,12 @@ export default class GameObject {

CanvasStateInstance.context.save();

// Scale
CanvasStateInstance.context.translate(this.x, this.y);
CanvasStateInstance.context.scale(this.scale.x, this.scale.y);
CanvasStateInstance.context.translate(-this.x, -this.y);

// Rotation
CanvasStateInstance.context.translate(this._x, this._y);
CanvasStateInstance.context.rotate(this.rotation);
CanvasStateInstance.context.translate(-this._x, -this._y);
Expand Down
4 changes: 2 additions & 2 deletions src/GameObjects/Line/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export default class Line extends GameObject {
get height() { return this.endY - this._y; }

// Positions Based in the origin
get x() { return this._x - this.width * this.origin.x; }
get y() { return this._y - this.height * this.origin.y; }
get x() { return this._x - this.width * this.origin.x * this.scale.x; }
get y() { return this._y - this.height * this.origin.y * this.scale.y; }

getTop() { return this.y; }
getBottom() { return this.y + this.height; }
Expand Down
4 changes: 2 additions & 2 deletions src/GameObjects/Polygon/Polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default class Polygon extends GameObject {
this._type = Types.polygon;
}

get x() { return this._x - this.width * this.origin.x; } // Get the position X relative to the origin
get y() { return this._y - this.height * this.origin.y; } // Get the position Y relative to the origin
get x() { return this._x - this.width * this.origin.x * this.scale.x; } // Get the position X relative to the origin
get y() { return this._y - this.height * this.origin.y * this.scale.y; } // Get the position Y relative to the origin

get width() {
if (this.vertices.length === 0) return 0;
Expand Down
4 changes: 2 additions & 2 deletions src/GameObjects/Rectangle/PhysicsRectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export default class PhysicsRectangle extends PhysicsGameObject {
this._type = Types.rectangle;
}

get x() { return this._x - this.width * this.origin.x; } // Get the position X relative to the origin
get y() { return this._y - this.height * this.origin.y; } // Get the position Y relative to the origin
get x() { return this._x - this.width * this.origin.x * this.scale.x; } // Get the position X relative to the origin
get y() { return this._y - this.height * this.origin.y * this.scale.y; } // Get the position Y relative to the origin

// ----- Private methods -----
_debugBody() {
Expand Down
4 changes: 2 additions & 2 deletions src/GameObjects/Rectangle/Rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export default class Rectangle extends GameObject {
this._type = Types.rectangle;
}

get x() { return this._x - this.width * this.origin.x; } // Get the position X relative to the origin
get y() { return this._y - this.height * this.origin.y; } // Get the position Y relative to the origin
get x() { return this._x - this.width * this.origin.x * this.scale.x; } // Get the position X relative to the origin
get y() { return this._y - this.height * this.origin.y * this.scale.y; } // Get the position Y relative to the origin
}

Object.assign(Rectangle.prototype, CommonMethods);
4 changes: 2 additions & 2 deletions src/GameObjects/Text/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export default class Text extends GameObject {
return this.fontSize;
}

get x() { return this._x - this.width * this.origin.x; } // Get the position X relative to the origin
get y() { return this._y - this.height * this.origin.y; } // Get the position Y relative to the origin
get x() { return this._x - this.width * this.origin.x * this.scale.x; } // Get the position X relative to the origin
get y() { return this._y - this.height * this.origin.y * this.scale.y; } // Get the position Y relative to the origin

// Get positions based on the origin
getTop() { return this.y; }
Expand Down
4 changes: 2 additions & 2 deletions src/GameObjects/Triangle/Triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export default class Triangle extends GameObject {
}

// Positions Based in the origin
get x() { return this._x - this.width * this.origin.x; }
get y() { return this._y - this.height * this.origin.y; }
get x() { return this._x - this.width * this.origin.x * this.scale.x; }
get y() { return this._y - this.height * this.origin.y * this.scale.y; }

getTop() { return this.y; }
getBottom() { return this.y + this.height; }
Expand Down

0 comments on commit a25779b

Please sign in to comment.