/*
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 */
package pacman;


import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.util.Duration;
import static pacman.Pacman.*;


/**
 *
 * @author akouznet
 */
public class Mikhalych2 extends Arc {
    
    private Timeline timeline;

    public Mikhalych2() {
        setCenterX(MIKHALYCH_WIDTH);
        setCenterY(MIKHALYCH_WIDTH);
        setRadiusX(MIKHALYCH_WIDTH);
        setRadiusY(MIKHALYCH_WIDTH);
        setFill(Color.ORANGE);
        setType(ArcType.ROUND);
        setStartAngle(5);
        setLength(350);
        
        timeline = new Timeline();
        timeline.getKeyFrames().setAll(
                new KeyFrame(Duration.ZERO, 
                    new KeyValue(startAngleProperty(), 0),
                    new KeyValue(lengthProperty(), 360)),
                new KeyFrame(Duration.seconds(1 / SPEED / 2), 
                    new KeyValue(startAngleProperty(), 45),
                    new KeyValue(lengthProperty(), 360 - 45 * 2)));
        timeline.setAutoReverse(true);
        timeline.setCycleCount(Timeline.INDEFINITE);
    }

    void openMouth() {
        timeline.play();
    }
    
    void closeMouth() {
        timeline.stop();
    }

    public void die() {
        timeline.stop();
        
        Timeline dying = new Timeline();
        dying.setDelay(Duration.seconds(0.7));
        dying.getKeyFrames().setAll(
                new KeyFrame(Duration.millis((180 - getStartAngle()) * 1000. / 180.), 
                    new KeyValue(startAngleProperty(), 180, Interpolator.EASE_OUT),
                    new KeyValue(lengthProperty(), 0, Interpolator.EASE_OUT))); 
        dying.setOnFinished(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                getGame().getMikhalych().dead();
            }
        });
        dying.play();
    }
}
