/
index.html
193 lines (157 loc) · 10.1 KB
/
index.html
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fatina Sample</title>
<script src="../../fatina-plugin-animator.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.7.3/pixi.min.js"></script>
</head>
<body>
<center>
<div id="canvas1"></div>
<div>
<button class="btn btn-default" onclick="animUnity.play('run_left')"><<</button>
<button class="btn btn-default" onclick="animUnity.play('walk_left')"><</button>
<button class="btn btn-info" onclick="animUnity.play('idle')">=</button>
<button class="btn btn-default" onclick="animUnity.play('walk_right')">></button>
<button class="btn btn-default" onclick="animUnity.play('run_right')">>></button>
<span> </span>
<span> </span>
<span> </span>
<span> </span>
<button class="btn btn-default" onclick="animYuko.play('run_left')"><<</button>
<button class="btn btn-default" onclick="animYuko.play('walk_left')"><</button>
<button class="btn btn-info" onclick="animYuko.play('idle')">=</button>
<button class="btn btn-default" onclick="animYuko.play('walk_right')">></button>
<button class="btn btn-default" onclick="animYuko.play('run_right')">>></button>
<br><br>
<button class="btn btn-primary" onclick="animYuko.play('fade_in')">FadeIn</button>
<button class="btn btn-primary" onclick="animYuko.play('fade_out')">FadeOut</button>
<button class="btn btn-primary" onclick="animYuko.play('jump')">Jump</button>
<button class="btn btn-success" onclick="Fatina.plugin.tickerManager.pauseAll('character')">Pause</button>
<button class="btn btn-success" onclick="Fatina.plugin.tickerManager.resumeAll('character')">Resume</button>
<button class="btn btn-success" onclick="Fatina.plugin.tickerManager.get('character').setTimescale(4)">x4</button>
<button class="btn btn-success" onclick="Fatina.plugin.tickerManager.get('character').setTimescale(1)">x1</button>
<button class="btn btn-success" onclick="Fatina.plugin.tickerManager.get('character').setTimescale(0.25)">x0.25</button>
</div>
<br><br>
<a class="btn btn-primary" href="https://github.com/kefniark/Fatina/blob/gh-pages/src/static/animator/index.html" target="_blank">Read the code</a>
</center>
<script>
// Pixi app Initialization
var app = new PIXI.Application(1024 / 2, 768 / 2, { backgroundColor: 0x0c243c });
document.getElementById('canvas1').appendChild(app.view);
PIXI.loader.add("/Fatina/animator/unity.json").add("/Fatina/animator/yuko.json")
.load(function () {
CreateUnityCharacter();
CreateYukoCharacter();
});
var animUnity, animYuko;
document.addEventListener('DOMContentLoaded', () => {
// Fatina Initialization and load Animation Plugin
Fatina.init();
Fatina.loadPlugin(FatinaPluginAnimator.get());
// Register a first animation type: Animation Spritesheet
Fatina.plugin.animatorManager.register('spritesheet', (object, params) => {
console.log('init Spritesheet Anim', params);
// Create a tween on a proxy object and use the value to change the sprite
let proxy = { index: 0 };
const tween = Fatina.tween(proxy, ['index'])
.from({ index: 0 })
.to({ index: params.number - 1 }, params.duration)
.onStart(() => {
if (!params.direction) {
return;
}
// Change the orientation based on the direction param
object.scale.x = Math.abs(object.scale.x) * params.direction;
})
.onUpdate(() => object.setTexture(params.textures[proxy.index]))
.setSteps(params.number - 1);
// Some animation don't need loop
if (!params.disableLoop) {
tween.setLoop(-1);
}
return tween;
}, 'character');
// Register a second animation type: Jump (a simple relative move with yoyo=1)
Fatina.plugin.animatorManager.register('jump', (object, params) => {
console.log('Instantiate Jump Anim', object.name, object.position, params);
return Fatina.tween(object.position, ['y'])
.setRelative(true)
.to({ y: - 70 }, 400)
.setEasing('outSine')
.yoyo(1)
.onStart(() => object.animator.play('jump_up'))
}, 'character');
// Register a third animation type: Fade
Fatina.plugin.animatorManager.register('fade', (object, params) => {
console.log('Instantiate Fade Anim', params);
return Fatina.tween(object, ['alpha']).to({ alpha: params.value }, params.duration)
}, 'character');
})
// Game code, let's create a characters with animation
function CreateYukoCharacter() {
// Create a normal Pixi Sprite
let yukochan = new PIXI.Sprite(PIXI.TextureCache['Yuko_Idle_1']);
yukochan.name = "Yuko";
yukochan.anchor.set(0.5);
yukochan.scale.x = 1.4;
yukochan.scale.y = 1.4;
yukochan.x = app.renderer.width / 2 + 50;
yukochan.y = app.renderer.height / 2;
app.stage.addChild(yukochan);
// Let's add a animator component to that sprite and add (instantiate) reusable animations (idle, walk, jump, ...)
let anim = Fatina.plugin.animatorManager.addAnimatorTo(yukochan)
// First Group of Animation: Sprite
.addAnimation('idle', 'spritesheet', { group: 'sprite' }, { number: 4, duration: 1000, textures: GetTextures('Yuko_Idle_') })
.addAnimation('walk_right', 'spritesheet', { group: 'sprite' }, { direction: 1, number: 6, duration: 1000, textures: GetTextures('Yuko_Walk_') })
.addAnimation('run_right', 'spritesheet', { group: 'sprite' }, { direction: 1, number: 8, duration: 1000, textures: GetTextures('Yuko_Run_') })
.addAnimation('walk_left', 'spritesheet', { group: 'sprite' }, { direction: -1, number: 6, duration: 1000, textures: GetTextures('Yuko_Walk_') })
.addAnimation('run_left', 'spritesheet', { group: 'sprite' }, { direction: -1, number: 8, duration: 1000, textures: GetTextures('Yuko_Run_') })
.addAnimation('jump_fall', 'spritesheet', { group: 'sprite', unstoppable: true, next: 'jump_landing' }, { disableLoop: true, number: 3, duration: 240, textures: GetTextures('Yuko_Jump_Fall_') })
.addAnimation('jump_landing', 'spritesheet', { group: 'sprite', unstoppable: true, next: 'idle' }, { disableLoop: true, number: 1, duration: 80, textures: GetTextures('Yuko_Jump_Landing') })
.addAnimation('jump_midair', 'spritesheet', { group: 'sprite', unstoppable: true, next: 'jump_fall' }, { disableLoop: true, number: 4, duration: 320, textures: GetTextures('Yuko_Jump_MidAir_') })
.addAnimation('jump_up', 'spritesheet', { group: 'sprite', unstoppable: true, next: 'jump_midair' }, { disableLoop: true, number: 2, duration: 160, textures: GetTextures('Yuko_Jump_Up_') })
// Second Group of Animation: Alpha
.addAnimation('fade_in', 'fade', { group: 'alpha', finalValue: true }, { value: 1, duration: 800 })
.addAnimation('fade_out', 'fade', { group: 'alpha', finalValue: true }, { value: 0, duration: 800 })
// Third Group of Animation: Jump (defined as unstoppable)
.addAnimation('jump', 'jump', { group: 'move', unstoppable: true });
// Play the default state: idle
anim.play('idle');
animYuko = anim;
}
// Game code, let's create a second character with almost the same animation, just different sprites
function CreateUnityCharacter() {
// Create a normal Pixi Sprite
let tokochan = new PIXI.Sprite(PIXI.TextureCache['Unitychan_Idle_1']);
tokochan.name = "Toko";
tokochan.anchor.set(0.5);
tokochan.scale.x = 1.4;
tokochan.scale.y = 1.4;
tokochan.x = app.renderer.width / 2 - 50;
tokochan.y = app.renderer.height / 2;
app.stage.addChild(tokochan);
// As you can see bellow, it's exactly the same code, just the array of texture is different
let anim = Fatina.plugin.animatorManager.addAnimatorTo(tokochan)
.addAnimation('idle', 'spritesheet', { group: 'sprite' }, { number: 4, duration: 1000, textures: GetTextures('Unitychan_Idle_') })
.addAnimation('walk_right', 'spritesheet', { group: 'sprite' }, { direction: 1, number: 14, duration: 2000, textures: GetTextures('Unitychan_Walk_') })
.addAnimation('run_right', 'spritesheet', { group: 'sprite' }, { direction: 1, number: 18, duration: 2000, textures: GetTextures('Unitychan_Run_') })
.addAnimation('walk_left', 'spritesheet', { group: 'sprite' }, { direction: -1, number: 14, duration: 2000, textures: GetTextures('Unitychan_Walk_') })
.addAnimation('run_left', 'spritesheet', { group: 'sprite' }, { direction: -1, number: 18, duration: 2000, textures: GetTextures('Unitychan_Run_') });
// Play the default state: idle
anim.play('idle');
animUnity = anim;
}
function GetTextureId(a, name) {
return parseInt(a.replace(name, ""), 10);
}
function GetTextures(name) {
return Object.keys(PIXI.TextureCache).filter(x => x.indexOf(name) !== -1).sort((a, b) => GetTextureId(a, name) - GetTextureId(b, name)).map(x => PIXI.TextureCache[x])
}
</script>
</body>
</html>