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

Commit

Permalink
[Engine]: Use Fluent Interface Design Pattern in GameObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed Feb 21, 2022
1 parent adfa3f7 commit 81578bd
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 24 deletions.
37 changes: 30 additions & 7 deletions src/GameObjects/GameObjectBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@ export default class GameObject {
this.visible = true;
}

setName(name) { this.name = name; }
setName(name) {
this.name = name;
return this;
}

// Render
// Position
setX(x) { this.setPosition(x, this.y, this.z); }
setY(y) { this.setPosition(this.x, y, this.z); }
setZ(z) { this.setPosition(this.x, this.y, z); }
setX(x) {
this.setPosition(x, this.y, this.z);
return this;
}
setY(y) {
this.setPosition(this.x, y, this.z);
return this;
}
setZ(z) {
this.setPosition(this.x, this.y, z);
return this;
}
getPosition() { return { x: this.x, y: this.y, z: this.z }; }
setPosition(x, y, z = this.z, force = false) {
if (this.bodyType === "S" && !force) return;
Expand All @@ -31,6 +43,7 @@ export default class GameObject {
this.x = x;
this.y = y;
this.z = z;
return this;
}
setRandomPosition(x = 0, y = 0, width = GlobalStateManagerInstance.viewportDimensions.width, height = GlobalStateManagerInstance.viewportDimensions.height) {
do {
Expand All @@ -39,6 +52,7 @@ export default class GameObject {
y + Math.random() * height
);
} while (!this.checkIsInsideWorldBounds());
return this;
}

getCenter() { return { x: this.getCenterX(), y: this.getCenterY() }; }
Expand All @@ -55,10 +69,19 @@ export default class GameObject {
getRightCenter() { return { x: this.getRight(), y: this.getCenterY() }; }

// Color
setFillColor(fillColor) { this.fillColor = fillColor; }
setStrokeColor(strokeColor) { this.strokeColor = strokeColor; }
setFillColor(fillColor) {
this.fillColor = fillColor;
return this;
}
setStrokeColor(strokeColor) {
this.strokeColor = strokeColor;
return this;
}

setVisible(isVisible) { this.visible = isVisible; }
setVisible(isVisible) {
this.visible = isVisible;
return this;
}

_render() {
if (!this.visible) return;
Expand Down
75 changes: 61 additions & 14 deletions src/GameObjects/PhysicsGameObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,30 @@ export default class PhysicsGameObject extends GameObject {
this._strokeDebugColor = "#016301";
}

// -- Physics
getType() { return this._type; }

setActive(isActive) { this.active = isActive; }
setActive(isActive) {
this.active = isActive;
return this;
}

// Body Type
setDynamicBody() { this.setBodyType("D"); }
setKinematicBody() { this.setBodyType("K"); }
setStaticBody() { this.setBodyType("S"); }
setTriggerBody() { this.setBodyType("T"); }
setDynamicBody() {
this.setBodyType("D");
return this;
}
setKinematicBody() {
this.setBodyType("K");
return this;
}
setStaticBody() {
this.setBodyType("S");
return this;
}
setTriggerBody() {
this.setBodyType("T");
return this;
}
setBodyType(bodyType) { // D = Dynamic, K = Kinematic, S = Static, T = Trigger
if (typeof bodyType !== "string" || this.bodyType === bodyType || bodyType.length > 1) return;
bodyType = bodyType.toUpperCase();
Expand All @@ -39,35 +53,68 @@ export default class PhysicsGameObject extends GameObject {
this.setBounce(0);
}
this.bodyType = bodyType;
return this;
}
getBodyType() { return this.bodyType; }

setVelocityX(x) { this.setVelocity(x, this.velocity.y); }
setVelocityY(y) { this.setVelocity(this.velocity.x, y); }
// Velocity
setVelocityX(x) {
this.setVelocity(x, this.velocity.y);
return this;
}
setVelocityY(y) {
this.setVelocity(this.velocity.x, y);
return this;
}
setVelocity(x, y = x) {
if (this.bodyType === "S") return;

this.velocity.x = x;
this.velocity.y = y;
return this;
}

setFrictionX(x) { this.setFriction(x, this.friction.y); }
setFrictionY(y) { this.setFriction(this.friction.x, y); }
// Friction
setFrictionX(x) {
this.setFriction(x, this.friction.y);
return this;
}
setFrictionY(y) {
this.setFriction(this.friction.x, y);
return this;
}
setFriction(x, y = x) {
this.friction.x = x;
this.friction.y = y;
return this;
}

setBounceX(x) { this.setBounce(x, this.bounce.y); }
setBounceY(y) { this.setBounce(this.bounce.x, y); }
// Bounce
setBounceX(x) {
this.setBounce(x, this.bounce.y);
return this;
}
setBounceY(y) {
this.setBounce(this.bounce.x, y);
return this;
}
setBounce(x, y = x) {
this.bounce.x = x;
this.bounce.y = y;
return this;
}

setMass(mass) { this.mass = mass; }
// Mass
setMass(mass) {
this.mass = mass;
return this;
}

setCollisionWorldBounds(collisionWorldBounds) { this.collisionWorldBounds = collisionWorldBounds; }
// Collisions
setCollisionWorldBounds(collisionWorldBounds) {
this.collisionWorldBounds = collisionWorldBounds;
return this;
}

// Check Current Collision With World Bounds
checkTopCollisionWorldBounds() { return this.getTop() <= 0; }
Expand Down
12 changes: 10 additions & 2 deletions src/GameObjects/Rectangle/CommonMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ const CommonMethods = {
getCenterY: function () { return this.y + this.height / 2; },

// Size
setWidth: function (width) { this.setSize(width, this.height); },
setHeight: function (height) { this.setSize(this.width, height); },
setWidth: function (width) {
this.setSize(width, this.height);
return this;
},
setHeight: function (height) {
this.setSize(this.width, height);
return this;
},
setSize: function (width, height = width, force = false) {
if (this.bodyType === "S" && !force) return;
this.width = width;
this.height = height;
return this;
},
getBounds: function () { return { x: this.getLeft(), y: this.getTop(), width: this.width, height: this.height }; },
getArea: function () { return this.width * this.height; },
Expand All @@ -33,6 +40,7 @@ const CommonMethods = {
refresh: function (x, y, width, height) {
this.setPosition(x, y, this.z, true);
this.setSize(width, height, true);
return this;
},

// ----- Private methods -----
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class Utils {
this.Vector2 = Vector2;
}

getVersion() { return "0.8.1"; }
getVersion() { return "Impacto: 0.8.1"; }
}

export const UtilsInstance = new Utils();

0 comments on commit 81578bd

Please sign in to comment.