This repository has been archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.ts
86 lines (77 loc) · 2.12 KB
/
Image.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import * as PIXI from 'pixi.js';
import { layerName } from '../Renderer';
import CommandBase from './base/CommandBase';
import { Command, pure, ResourceCommand } from './base/commands';
export interface ShowHideOption {
duration?: number;
}
export interface ShowOption extends ShowHideOption {
on?: layerName;
x?: number;
y?: number;
scale?: number;
}
type HideOption = ShowHideOption;
interface LayerDef {
layer: PIXI.DisplayObject;
on: layerName;
}
export default class Image extends CommandBase {
private layers: Map<string, LayerDef> = new Map();
bg(
src: string,
{
duration = 500,
x = this.r.width / 2,
y = this.r.height / 2,
scale = 1.05,
}: Omit<ShowOption, 'on'> = {}
): Command {
const filepath = `game/images/bg ${src}.png`;
return new ResourceCommand(
filepath,
async (resource: PIXI.LoaderResource) => {
const on = 'bg';
const layer = new PIXI.Sprite(resource.texture);
layer.anchor.set(0.5, 0.5);
this.setLayerProps(layer, {
x,
y,
scale,
alpha: 0,
});
await this.r.AddLayer(layer, on);
await this.fadeIn(layer, duration);
this.layers.set(name, { layer, on });
}
);
}
show(
name: string,
src: string,
{ duration = 500, on = 'fg', ...option }: ShowOption = {}
): Command {
const filepath = `game/images/${src}.png`;
return new ResourceCommand(
filepath,
async (resource: PIXI.LoaderResource) => {
const layer = new PIXI.Sprite(resource.texture);
layer.anchor.set(0.5, 0.5);
this.setLayerProps(layer, { ...option, alpha: 0 });
await this.r.AddLayer(layer, on);
await this.fadeIn(layer, duration);
this.layers.set(name, { layer, on });
}
);
}
hide(name: string, { duration = 500 }: HideOption = {}): Command {
return pure(async () => {
const layer = this.layers.get(name);
if (layer) {
await this.fadeOut(layer.layer, duration);
this.r.RemoveLayer(layer.layer, layer.on);
this.layers.delete(name);
}
});
}
}