-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScene.cs
55 lines (47 loc) · 1.8 KB
/
Scene.cs
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
using Android.Views.Animations;
using System;
namespace Segmentus
{
enum Side {Left, Right};
abstract class Scene : DrawablePart
{
const int SwitchDuration = 750;
const float SwitchEasingFactor = 2.2f;
HandyAnimator switchAnim;
public Scene() : base(GameView.Instance.rootPivot) {}
protected virtual void BeforeShow() => GameView.Instance.DrawEvent += OnDraw;
protected virtual void AfterShow() { }
protected virtual void BeforeHide() { }
protected virtual void AfterHide() => GameView.Instance.DrawEvent -= OnDraw;
public void Show(Side fromSide)
{
pivot.X = (fromSide == Side.Left) ? -GameView.CanonWidth : GameView.CanonWidth;
pivot.X *= GameView.scaleFactor;
ReleaseAnimation();
BeforeShow();
AnimateSwitch(pivot.X, 0, AfterShow);
SoundMaster.PlaySound(SoundMaster.SceneSwitchSound);
}
protected void Hide(Side toSide)
{
float toX = (toSide == Side.Left) ? -GameView.CanonWidth : GameView.CanonWidth;
toX *= GameView.scaleFactor;
ReleaseAnimation();
BeforeHide();
AnimateSwitch(pivot.X, toX, AfterHide);
}
void ReleaseAnimation()
{
if (switchAnim != null && switchAnim.core.IsRunning)
switchAnim.core.Cancel();
}
void AnimateSwitch(float fromX, float toX, Action action)
{
switchAnim = HandyAnimator.OfFloat(fromX, toX, SwitchDuration);
switchAnim.core.SetInterpolator(new DecelerateInterpolator(SwitchEasingFactor));
switchAnim.Update += (value) => pivot.X = (float)value;
switchAnim.After += action;
switchAnim.core.Start();
}
}
}