License text

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER 
 * Copyright  2008, 2010 Oracle and/or its affiliates.  All rights reserved. 
 * Use is subject to license terms.
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met: 
 * 
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer. 
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Oracle Corporation nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

    

package dukeman;

import javafx.scene.*;
import javafx.scene.image.*;

// @author Pavel Porvatov

public class Duke extends CustomNode {
    def image = ImageView { };

    def direction = Direction { };

    var lastRightDirection = true;
    
    var animationIndex: Integer;

    override public function create(): Node {
        image
    }

    public function initialize(level: LevelData) {
        translateX = level.dukeInitCoord.x;
        translateY = level.dukeInitCoord.y;

        direction.offsetX = 0;
        direction.offsetY = 0;

        prepareImage(true);
    }

    function prepareImage(standing: Boolean) {
        animationIndex++;

        if (standing) {
            // Prepare transforms if needed
            if (sizeof image.transforms != 1) {
                transforms = [
                    Utils.OFFSET_TRANSFORM
                ];
            }

            if (animationIndex > 11) {
                animationIndex = 0;
            }

            image.image = Config.images[
                if (animationIndex >= 9) Config.IMAGE_FRONTDUKE1 else
                    Config.IMAGE_FRONTDUKE0 + animationIndex / 3]
        } else {
            // Prepare transforms if needed
            if (lastRightDirection) {
                if (sizeof transforms != 1) {
                    transforms = [
                        Utils.OFFSET_TRANSFORM
                    ];
                }
            } else {
                if (sizeof transforms != 2) {
                    transforms = [
                        Utils.FLIP_TRANSFORM,
                        Utils.BIG_OFFSET_TRANSFORM
                    ];
                }
            }

            if (animationIndex > 6 * 3 - 1) {
                animationIndex = 0;
            }

            var index = animationIndex / 3;

            image.image = Config.images[
                if (index <= 3) Config.IMAGE_DUKE0 + index else
                    Config.IMAGE_DUKE0 + 6 - index]
        }
    }

    public function move(level: LevelData, keyboardDirection: Direction): Void {
        var standing = true;

        for (i in [0..<Config.DUKE_SPEED]) {
            // Try keyboard direction
            if (not keyboardDirection.isEmpty() and
                    level.canMove(this, keyboardDirection)) {
                direction.offsetX = keyboardDirection.offsetX;
                direction.offsetY = keyboardDirection.offsetY;
            } else if (direction.isEmpty() or
                    not level.canMove(this, direction)) {
                direction.offsetX = 0;
                direction.offsetY = 0;

                break;
            }

            level.move(this, direction);

            if (direction.offsetX != 0) {
                lastRightDirection = direction.offsetX > 0;
            }

            standing = false;
        }

        prepareImage(standing);
    }

    // Deterrmines intersection with other node
    public function isIntersected(node: Node): Boolean {
        return translateX + Config.CELL_SIZE > node.translateX and
            translateX < node.translateX + Config.CELL_SIZE and
            translateY + Config.CELL_SIZE > node.translateY and
            translateY < node.translateY + Config.CELL_SIZE
    }
}