Package transientlibs.objects.primitives

Source Code of transientlibs.objects.primitives.Coords

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.objects.primitives;



import transientlibs.maps.implementation.TilelessMap;
import transientlibs.slick2d.geom.Vector2f;


/**
*
* @author Elwin
*/
public class Coords {

    public float x;
    public float y;
    public float z;


    public Coords(Coords other){
        copyFrom(other);
    }
   
    public Coords () {

    x = 0;
    y = 0;
    z = 0;
    }

    public Coords (float setX, float setY) {

    x = setX;
    y = setY;
    z = 0;
    }

    public void roundValues () {
        x = Math.round(x);
        y = Math.round(y);
    }

    public Coords (Vector2f vector) {
        x = vector.x;
        y = vector.y;
        z = 0;
    }

    @Override
    public String toString (){

        return x+"/"+y;

    }

    public int getIntX (){
        return (int)(x);
    }

    public int getIntY (){
        return (int)(y);
    }


    public float returnDistance (Coords c) {
        float distance;
        float x1 = x;
        float y1 = y;
        float x2 = c.x;
        float y2 = c.y;

        distance = Math.round(Math.sqrt(Math.pow((x2 - x1), 2) +  Math.pow((y2 - y1), 2)));

        return distance;
    }


    public static void checkIfValidScrolling (TilelessMap mapLink) {
if (mapLink.eye.eyePosition.getIntX() < (mapLink.eye.eyeSizeX * -1)) {
            mapLink.eye.eyePosition.x = mapLink.eye.eyeSizeX * -1;
        }

        if (mapLink.eye.eyePosition.getIntY() < (mapLink.eye.eyeSizeY * -1)) {
            mapLink.eye.eyePosition.y = mapLink.eye.eyeSizeY * -1;
        }



        if (mapLink.eye.eyePosition.getIntX() > mapLink.sizeX) {
            mapLink.eye.eyePosition.x = mapLink.sizeX;
        }

        if (mapLink.eye.eyePosition.getIntY() > mapLink.sizeY) {
            mapLink.eye.eyePosition.y = mapLink.sizeY;
        }
    }

    public void move (float dx, float dy) {
        x += dx;
        y += dy;

    }

    public void checkIfApproximatelySame (Coords otherCoords) {
        if (Math.abs(otherCoords.x - x) < 0.1)  {x = otherCoords.x;}
        if (Math.abs(otherCoords.y - y) < 0.1)  {y = otherCoords.y;}
    }

    public boolean isIntegerSame (Coords otherCoords) {
        return ((getIntX() == otherCoords.getIntX()) && (getIntY() == otherCoords.getIntY()));
    }
   
    public boolean isSame (Coords otherCoords) {
        return ((x == otherCoords.x) && (y == otherCoords.y));
    }
   
    public boolean isSame(int onX, int onY) {
        return ((x == onX) && (y == onY));
    }
   

    public Coords returnAdjustedCoords (int direction) {
        Coords result = new Coords();

        //Log.info("Try direction "+direction);

        if (direction == 1) {
            result.x = x - 1;
            result.y = y + 1;
        }

        if (direction == 2) {
            result.x = x;
            result.y = y + 1;
        }

        if (direction == 3) {
            result.x = x + 1;
            result.y = y + 1;
        }

        if (direction == 4) {
            result.x = x - 1;
            result.y = y;
        }

        if (direction == 5) {
            result.x = x;
            result.y = y;
        }

        if (direction == 6) {
            result.x = x + 1;
            result.y = y;
        }

        if (direction == 7) {
            result.x = x - 1;
            result.y = y - 1;
        }

        if (direction == 8) {
            result.x = x;
            result.y = y - 1;
        }

        if (direction == 9) {
            result.x = x + 1;
            result.y = y - 1;
        }


        return result;
    }

    public int calculateVector (int toX, int toY) {
        return calculateVector (new Coords(toX, toY));
    }

    public int calculateVector (Coords targetCoords) {
        if ((targetCoords.x > x) && (targetCoords.y > y)) {
            return 3;
        } else

        if ((targetCoords.x > x) && (targetCoords.y == y)) {
            return 6;
        } else

        if ((targetCoords.x == x) && (targetCoords.y > y)) {
            return 2;
        } else

        if ((targetCoords.x < x) && (targetCoords.y > y)) {
            return 7;
        } else


        if ((targetCoords.x > x) && (targetCoords.y < y)) {
            return 9;
        } else


        if ((targetCoords.x == x) && (targetCoords.y < y)) {
            return 8;
        } else

        if ((targetCoords.x < x) && (targetCoords.y < y)) {
            return 1;
        } else

        if ((targetCoords.x < x) && (targetCoords.y == y)) {
            return 4;
        }


        return -1;
    }

    // TODO: maybe move Coords to Vector2f?
    public Vector2f toVector2f() {
        return new Vector2f((float) x, (float) y);
    }

    public void copyFrom(Coords screenCoords) {
        x = screenCoords.x;
        y = screenCoords.y;
        z = screenCoords.z;

        //Log.info("Set X to "+x);
    }

    public String toRoundedString() {
        return ((Math.round(x))+"/"+(Math.round(y)));
    }

    public void reset() {
        x = -1;
        y = -1;
        z = -1;
    }

    public boolean notNull() {
        return (getIntX() != -1);
    }

}
TOP

Related Classes of transientlibs.objects.primitives.Coords

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.