Created
January 28, 2025 16:15
-
-
Save easierbycode/9860ba10e5ea18a3eb20a299523fd7e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TILESET_HEIGHT = 8; | |
const TILESET_WIDTH = 8; | |
export const isObjectEmpty = (obj) => | |
obj !== null | |
&& typeof obj === 'object' | |
// https://stackoverflow.com/a/32108184/4307769 | |
&& Object.keys(obj).length === 0 | |
&& obj.constructor === Object; | |
export const isset = (...args) => { | |
// eslint-disable-next-line no-restricted-syntax | |
for (const arg of args) { | |
if ( | |
isObjectEmpty(arg) | |
|| arg === undefined | |
|| arg === null | |
|| (Array.isArray(arg) && !arg.length) | |
) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
export const getDegreeFromRadians = (radians) => (radians * (180 / Math.PI)); | |
export const getRadiansFromDegree = (degree) => (degree * (Math.PI / 180)); | |
export const isBoolean = (val) => typeof val === 'boolean'; | |
export const rotateRectangleInsideTile = (x, y, width, height, degree) => { | |
switch (degree) { | |
case 90: { | |
return [ | |
TILESET_HEIGHT - (y + height), | |
x, | |
height, | |
width, | |
]; | |
} | |
case 180: { | |
return [ | |
TILESET_WIDTH - (x + width), | |
TILESET_HEIGHT - (y + height), | |
width, | |
height, | |
]; | |
} | |
case 270: { | |
return [ | |
y, | |
TILESET_WIDTH - (x + width), | |
height, | |
width, | |
]; | |
} | |
default: { | |
return [x, y, width, height]; | |
} | |
} | |
}; | |
/** | |
* @this Phaser.GameObject.Sprite | |
*/ | |
function onDragEvent(pointer, x, y) { | |
this.setX(x); | |
this.setY(y); | |
} | |
/** | |
* @this Phaser.GameObject.Sprite | |
*/ | |
function onDragStartEvent() { | |
this.setScale(this.scale + 1); | |
} | |
/** | |
* @this Phaser.GameObject.Sprite | |
*/ | |
function onDragEndEvent() { | |
this.setScale(this.scale - 1); | |
} | |
/** | |
* @this Phaser.GameObject.Sprite | |
*/ | |
export function setSpriteDraggable() { | |
this.setInteractive(); | |
this.scene.input.dragDistanceThreshold = 5; | |
this.scene.input.setDraggable(this); | |
this.on('dragstart', this.onDragStartEvent); | |
this.on('drag', this.onDragEvent); | |
this.on('dragend', this.onDragEndEvent); | |
} | |
/** | |
* @this Phaser.GameObject.Sprite | |
*/ | |
export function handleSpriteMovement() { | |
const cursors = this.scene.input.keyboard.createCursorKeys(); | |
const velocity = 3; | |
if (cursors.left.isDown) { | |
this.setX(this.x - velocity); | |
} else if (cursors.right.isDown) { | |
this.setX(this.x + velocity); | |
} else if (cursors.up.isDown) { | |
this.setY(this.y - velocity); | |
} else if (cursors.down.isDown) { | |
this.setY(this.y + velocity); | |
} | |
} | |
export function createExplosionCircle(scene, x, y) { | |
// Create a small red circle at the given position | |
const circle = scene.add.circle(x, y, 8, 0xff0000); | |
const circle2 = scene.add.circle(x, y, 8, 0xff0000); | |
// Optionally adjust blend mode for a more ‘fiery’ look | |
// circle.setBlendMode(Phaser.BlendModes.XOR); | |
// circle2.setBlendMode(Phaser.BlendModes.ADD); | |
// circle2.setBlendMode(Phaser.BlendModes.LUMINOSITY); | |
// circle2.setBlendMode(Phaser.BlendModes.EXCLUSION); | |
let frameCounter = 0; | |
// Tween the circle to expand and fade | |
scene.tweens.add({ | |
targets: [circle, circle2], | |
scale: { from: 1, to: 3 }, // Grow 3x in size | |
// scale: { from: 1, to: 24 }, // Grow 3x in size | |
alpha: { from: 1, to: 0 }, // Fade out | |
duration: 750, // Half a second | |
ease: 'Power2', | |
onUpdate: () => { | |
frameCounter++; | |
// circle.visible = frameCounter % 2 === 0; | |
circle.visible = frameCounter % 4 === 0; | |
}, | |
onComplete: () => { | |
circle.destroy(); // Clean up the circle | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment