Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Depreciating legacy entities #74

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added objects/arrow/arrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
122 changes: 122 additions & 0 deletions objects/arrow/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
module.exports = {
animations: {},
properties: {
sprite: {
spriteSheet: "arrow",
layers: [],
},
},
spriteSheets: {
arrow: {
fileName: "./arrow.png",
frameDimensions: {
width: 32,
height: 18,
},
},
press_spacebar: {
fileName: "./press_spacebar.png",
type: "image",
},
},
events: {
onMapDidLoad: handleMapDidLoad,
onPlayerDidInteract: handlePlayerInteraction,
},
};

//#region Event Handlers
function handleMapDidLoad(self, event, world) {
setupArrow(self);
setupTextImage(self);
// needed to also show/hide the text image attached to the arrow
// whenever either method is called on the arrow object
setupFunctionExtensions(self);
}

function handlePlayerInteraction(self, event, world) {
// checking for whether the target of the player's interaction event is this object, to avoid
// having the arrow hidden during any arbitrary player interaction
if (event.target === self) {
hide(self);
}
}
//#endregion

//#region Functions
function setupArrow(self) {
orientVertically(self, self.angle);
applyHoverTween(self.game, self.sprite);
}

function orientVertically(self, angle = 270) {
self.sprite.angle = angle;
self.sprite.anchor.setTo(0, 0.5);
}

function setupTextImage(self) {
self.textImage = self.game.add.sprite(
self.sprite.x + 3,
self.sprite.y - self.sprite.width,
"press_spacebar"
);
self.textImage.anchor.setTo(0.5, 1);

applyHoverTween(self.game, self.textImage);
}

function applyHoverTween(game, sprite) {
const hoverAdjustment = 2;

const tween = game.add.tween(sprite).to(
{
y: sprite.y + hoverAdjustment,
},
400, // time
Phaser.Easing.Exponential.In,
undefined,
undefined,
-1, // repeat infinitely
true // yoyo)
);

tween.start();
}

// Extends both the "show" and "hide" methods on the SpriteObject class to also show/hide the text image
function setupFunctionExtensions(self) {
extendMethod(self, "show", () => setTextImageVisibility(self, true));
extendMethod(self, "hide", () => setTextImageVisibility(self, false));
}

/**
* Extends the functionality of an object's method
* @param {object} obj - A reference to the object that has the target method
* @param {string} targetMethod - A string used to access the method belonging to the obj
* @param {function} callback - The function which extends the functionality of the target method
*/
function extendMethod(obj, targetMethod, callback) {
if (!obj) {
console.warn(
`Obj is missing, null, or undefined! Valid obj must be provided to extend a method belonging to it.`
);
}

const selfFunc = obj[targetMethod];

if (selfFunc) {
obj[targetMethod] = () => {
selfFunc.call(obj);
callback(obj);
};
} else {
console.warn(`No method [${targetMethod}] could be found on [${obj}]!`);
}
}

function setTextImageVisibility(self, visibility) {
if (self.textImage) {
self.textImage.visible = visibility;
}
}
//#endregion
Binary file added objects/arrow/press_spacebar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions objects/collider/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ module.exports = {
layers: [],
},
},
events: {
onMapDidLoad: handleMapDidLoad,
},
};

function handleMapDidLoad(self, event, world) {
// legacy entity would make the collider invisible during construction,
// so we're mirroring that behavior here to avoid having pink collider
// objects from showing up in the game world
self.sprite.alpha = 0;
}