using UnityEngine;
using System.Collections;
using Framework.Entities;
using Framework.Utility;

public class CameraShake : CoreBehaviour
{
    public class ShakeAction : CoreAction
    {
    }

    private bool shaking = false;
    private UnityTimer shakeTimer = new UnityTimer(0.2f, false);
    private Vector3 localStartPosition;

    protected override void InnerAwake()
    {
        AddGlobalActionHandler<ShakeAction>(this, Shake);
        this.localStartPosition = this.transform.localPosition;
    }

    protected override void InnerUpdate()
    {
        if (this.shaking)
        {
            this.transform.localPosition = this.localStartPosition + (this.transform.right * Random.Range(-1f, 1f));
            this.shakeTimer.Advance();
            if (this.shakeTimer.IsComplete())
            {
                this.shaking = false;
                this.shakeTimer.Reset();
            }
        }
    }

    private void Shake(ShakeAction action)
    {
        shaking = true;
    }
}
