Skip to content

Commit

Permalink
Add some effect properties methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rexrainbow committed Jul 15, 2023
1 parent 29bf176 commit 127faaf
Show file tree
Hide file tree
Showing 18 changed files with 445 additions and 0 deletions.
Binary file added assets/images/distortion/distortion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/distortion/distortion2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/distortion/distortion3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/distortion/distortion4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/distortion/distortion6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/distortion/distortion7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/distortion/noise.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/distortion/noisesmall.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions examples/effect-properties/displacement.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set main=./examples/effect-properties/displacement.js
cd ..
cd ..
npm run watch
50 changes: 50 additions & 0 deletions examples/effect-properties/displacement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import phaser from 'phaser/src/phaser.js';
import EffectPropertiesPlugin from '../../plugins/effectproperties-plugin.js';

class Demo extends Phaser.Scene {
constructor() {
super({
key: 'examples'
})
}

preload() {
this.load.image('logo', 'assets/images/logo.png');

//this.load.image('distort', 'assets/images/distortion/noisesmall.png');
this.load.image('distort', 'assets/images/distortion/distortion.png');
}

create() {
var image = this.add.image(400, 300, 'logo');
this.plugins.get('rexEffectProperties').add(image);

image.displacementKey = 'distort';
image.displacementX = -0.03;
image.displacementY = -0.03;

}

update() { }
}

var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
},
scene: Demo,
plugins: {
global: [{
key: 'rexEffectProperties',
plugin: EffectPropertiesPlugin,
start: true
}]
}
};

var game = new Phaser.Game(config);
4 changes: 4 additions & 0 deletions examples/effect-properties/gradient.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set main=./examples/effect-properties/gradient.js
cd ..
cd ..
npm run watch
45 changes: 45 additions & 0 deletions examples/effect-properties/gradient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import phaser from 'phaser/src/phaser.js';
import EffectPropertiesPlugin from '../../plugins/effectproperties-plugin.js';

class Demo extends Phaser.Scene {
constructor() {
super({
key: 'examples'
})
}

preload() {
this.load.image('logo', 'assets/images/logo.png');
}

create() {
var image = this.add.image(400, 300, 'logo');
this.plugins.get('rexEffectProperties').add(image);

image.gradientColor = [0x0000ff, 0x00ff00];

}

update() { }
}

var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
},
scene: Demo,
plugins: {
global: [{
key: 'rexEffectProperties',
plugin: EffectPropertiesPlugin,
start: true
}]
}
};

var game = new Phaser.Game(config);
14 changes: 14 additions & 0 deletions plugins/behaviors/effectproperties/AddDisplacementProperties.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default AddDisplacementProperties;

declare namespace AddDisplacementProperties {
interface DisplacementGameObject extends Phaser.GameObjects.GameObject {
displacementKey: string | null | false;
displacementX: number;
displacementY: number;

}
}

declare function AddDisplacementProperties(
gameObject: Phaser.GameObjects.GameObject
): AddDisplacementProperties.DisplacementGameObject;
84 changes: 84 additions & 0 deletions plugins/behaviors/effectproperties/AddDisplacementProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import HasProperty from '../../utils/object/HasProperty.js';
import GetFXFactory from './GetFXFactory.js';

var AddDisplacementProperties = function (gameObject) {
// Don't attach properties again
if (HasProperty(gameObject, 'displacementKey')) {
return gameObject;
}

var fxFactory = GetFXFactory(gameObject);
if (!fxFactory) {
return gameObject;
}

var displacementKey,
displacementX = 0.005,
displacementY = 0.005;
Object.defineProperty(gameObject, 'displacementKey', {
get: function () {
return displacementKey;
},
set: function (value) {
if (displacementKey === value) {
return;
}

displacementKey = value;

if ((displacementKey === null) || (displacementKey === false)) {
if (gameObject._displacement) {
fxFactory.remove(gameObject._displacement);
gameObject._displacement = undefined;
}
} else {
if (!gameObject._displacement) {
gameObject._displacement = fxFactory.addDisplacement(displacementKey, displacementX, displacementY);
}

gameObject._displacement.setTexture(displacementKey);
}

},
})

Object.defineProperty(gameObject, 'displacementX', {
get: function () {
return displacementX;
},
set: function (value) {
if (displacementX === value) {
return;
}

displacementX = value;

if (gameObject._displacement) {
gameObject._displacement.x = displacementX;
}
},
})

Object.defineProperty(gameObject, 'displacementY', {
get: function () {
return displacementY;
},
set: function (value) {
if (displacementY === value) {
return;
}

displacementY = value;

if (gameObject._displacement) {
gameObject._displacement.y = displacementY;
}
},
})

gameObject.displacementKey = null;

return gameObject;
}

export default AddDisplacementProperties;
6 changes: 6 additions & 0 deletions plugins/behaviors/effectproperties/AddEffectProperties.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import AddCircleProperties from './AddCircleProperties';
import AddContrastProperties from './colormatrix/AddContrastProperties';
import AddDesaturateProperties from './colormatrix/AddDesaturateProperties';
import AddDesaturateLuminanceProperties from './colormatrix/AddDesaturateLuminanceProperties';
import AddDisplacementProperties from './AddDisplacementProperties';
import AddGlowProperties from './AddGlowProperties';
import AddGradientProperties from './AddGradientProperties';
import AddGrayscaleProperties from './colormatrix/AddGrayscaleProperties';
import AddHueProperties from './colormatrix/AddHueProperties';
import AddKodachromeProperties from './colormatrix/AddKodachromeProperties';
Expand Down Expand Up @@ -44,7 +46,9 @@ declare namespace AddEffectProperties {
contrast?: boolean,
desaturate?: boolean,
desaturateLuminance?: boolean,
displacement?: boolean,
glow?: boolean,
gradient?: boolean,
grayscale?: boolean,
hue?: boolean,
kodachrome?: boolean,
Expand Down Expand Up @@ -76,7 +80,9 @@ declare namespace AddEffectProperties {
AddContrastProperties.ContrastGameObject,
AddDesaturateProperties.DesaturateGameObject,
AddDesaturateLuminanceProperties.DesaturateLuminanceGameObject,
AddDisplacementProperties.DisplacementGameObject,
AddGlowProperties.GlowGameObject,
AddGradientProperties.GradientGameObject,
AddGrayscaleProperties.GrayscaleGameObject,
AddHueProperties.HueGameObject,
AddKodachromeProperties.KodachromeGameObject,
Expand Down
20 changes: 20 additions & 0 deletions plugins/behaviors/effectproperties/AddGradientProperties.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default AddGradientProperties;

declare namespace AddGradientProperties {
interface GradientGameObject extends Phaser.GameObjects.GameObject {
gradientColor: [number, number] | null | false;
gradientColor1: number | null | false;
gradientColor2: number | null | false;
gradientAlpha: number;
gradientFromX: number;
gradientFromY: number;
gradientToX: number;
gradientToY: number;
gradientSize: number;

}
}

declare function AddGradientProperties(
gameObject: Phaser.GameObjects.GameObject
): AddGradientProperties.GradientGameObject;
Loading

0 comments on commit 127faaf

Please sign in to comment.