Package game.core

Source Code of game.core.Collision

package game.core;

import beans.serializable.EventBloc;
import beans.enumeration.EventBlocType;
import game.entity.Player;
import java.awt.Color;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.Map;
import java.util.Map.Entry;

/**
* Cette classe gére les differents systemes de collision
* @author mastersnes
*/
public class Collision {

    /**
     * Verifie si le player est en collision avec un des blocs de la liste solidBlockList<br>
     * apres lui avoir rajouté les mouvements x et y
     *
     * @param colision
     * @param player
     * @param mouvementX
     * @param mouvementY
     * @return
     * @see Player
     */
    public static boolean isInCollision(final BufferedImage colision, final Player player,
            final int mouvementX, final int mouvementY) {
        final int decalage = 10;
        final int x1 = player.getX() + 5 + mouvementX;
        final int w1 = player.WIDTH - 10;
        final int y1 = player.getY() + 10 + mouvementY;
        final int h1 = player.HEIGHT - 20;

        if (x1 < 0 || y1 < 0) {
            return true;
        }

        final Color pixelHautGauche = new Color(colision.getRGB(x1, y1));
        final Color pixelHautDroit = new Color(colision.getRGB(x1 + w1, y1));
        final Color pixelHautMilieu = new Color(colision.getRGB(x1 + (w1 / 2), y1));

        final Color pixelBasGauche = new Color(colision.getRGB(x1, y1 + h1));
        final Color pixelBasDroit = new Color(colision.getRGB(x1 + w1, y1 + h1));
        final Color pixelBasMilieu = new Color(colision.getRGB(x1 + (w1 / 2), y1 + h1));

        final Color pixelGaucheMilieu = new Color(colision.getRGB(x1, y1 + (h1 / 2)));
        final Color pixelDroiteMilieu = new Color(colision.getRGB(x1 + w1, y1 + (h1 / 2)));

        if (pixelHautGauche.equals(Color.BLACK) || pixelHautDroit.equals(Color.BLACK) || pixelHautMilieu.equals(Color.BLACK)) {
            return true;
        }
        if (pixelBasGauche.equals(Color.BLACK) || pixelBasDroit.equals(Color.BLACK) || pixelBasMilieu.equals(Color.BLACK)) {
            return true;
        }
        if (pixelGaucheMilieu.equals(Color.BLACK) || pixelDroiteMilieu.equals(Color.BLACK)) {
            return true;
        }

        return false;

    }

    /**
     * Verifie si le joueur est en colision avec un evenement de la liste
     * @param colisionEvent
     * @param player
     * @param mouvementX
     * @param mouvementY
     * @return
     * @see EventBloc
     * @see Player
     */
    public static boolean isInEventCollision(final Map<Point, EventBloc> colisionEvent, final Player player, final int mouvementX, final int mouvementY) {
        if (player.getAncientBloc() == null) {
            return false;
        }
        final int x = (mouvementX == 0) ? 0 : ((mouvementX < 0) ? -1 : 1);
        final int y = (mouvementY == 0) ? 0 : ((mouvementY < 0) ? -1 : 1);
        final Point playerPosition = new Point(player.getAncientBloc().x + x, player.getAncientBloc().y + y);
        for (final Entry<Point, EventBloc> entry : colisionEvent.entrySet()) {
            final EventBloc eventBloc = entry.getValue();
            final boolean colision = (eventBloc.isColisionOff() && eventBloc.getCurrentMode() == EventBlocType.OFF) || (eventBloc.isColisionOn() && eventBloc.getCurrentMode() == EventBlocType.ON);
            final Point eventPosition = entry.getKey();
            if (colision && eventPosition.equals(playerPosition)) {
                return true;
            }
        }
        return false;
    }
}
TOP

Related Classes of game.core.Collision

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.