Package game.terrain

Source Code of game.terrain.Block

package game.terrain;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import engine.geometry.Polygon;
import engine.geometry.Vector;
import engine.utility.Transform;

public class Block implements Serializable {
    private static final long serialVersionUID = 4094186337308262345L;

    public static final int WIDTH = 32;
    public static final int HEIGHT = 32;

    private BlockID id;
    private int row;
    private int column;
    private transient Polygon polygon;

    public Block(BlockID id, int row, int column) {
        this.id = id;
        this.row = row;
        this.column = column;
    }

    public Polygon getPolygon() {
        if (polygon == null && id != BlockID.EMPTY) {
            List<Vector> vertices = new LinkedList<Vector>();
            final double half = 0.5;
            switch (id) {
                // Empty tile
                case EMPTY:
                    break;

                // Full tile
                case SOLID:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(1, 1));
                    vertices.add(new Vector(1, 0));
                    break;

                // Half tiles
                case HALF_2:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(1, 0));
                    break;
                case HALF_6:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(1, 1));
                    vertices.add(new Vector(1, 0));
                    break;
                case HALF_A:
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(1, 1));
                    vertices.add(new Vector(1, 0));
                    break;
                case HALF_E:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(1, 1));
                    break;

                // Quarter tiles
                case QUATER1_1:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(half, 0));
                    break;
                case QUATER1_3:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(0, half));
                    vertices.add(new Vector(1, 0));
                    break;
                case QUATER1_5:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(1, half));
                    vertices.add(new Vector(1, 0));
                    break;
                case QUATER1_7:
                    vertices.add(new Vector(1, 1));
                    vertices.add(new Vector(1, 0));
                    vertices.add(new Vector(half, 0));
                    break;
                case QUATER1_9:
                    vertices.add(new Vector(half, 1));
                    vertices.add(new Vector(1, 1));
                    vertices.add(new Vector(1, 0));
                    break;
                case QUATER1_B:
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(1, 1));
                    vertices.add(new Vector(1, half));
                    break;
                case QUATER1_D:
                    vertices.add(new Vector(0, half));
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(1, 1));
                    break;
                case QUATER1_F:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(half, 1));
                    break;

                // Three quarter tiles
                case QUATER3_1:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(half, 1));
                    vertices.add(new Vector(1, 0));
                    break;
                case QUATER3_3:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(1, half));
                    vertices.add(new Vector(1, 0));
                    break;
                case QUATER3_5:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(0, half));
                    vertices.add(new Vector(1, 1));
                    vertices.add(new Vector(1, 0));
                    break;
                case QUATER3_7:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(half, 1));
                    vertices.add(new Vector(1, 1));
                    vertices.add(new Vector(1, 0));
                    break;
                case QUATER3_9:
                    vertices.add(new Vector(half, 0));
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(1, 1));
                    vertices.add(new Vector(1, 0));
                    break;
                case QUATER3_B:
                    vertices.add(new Vector(0, half));
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(1, 1));
                    vertices.add(new Vector(1, 0));
                    break;
                case QUATER3_D:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(1, 1));
                    vertices.add(new Vector(1, half));
                    break;
                case QUATER3_F:
                    vertices.add(new Vector(0, 0));
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(1, 1));
                    vertices.add(new Vector(half, 0));
                    break;
                case EIGHTH1_C:
                    vertices.add(new Vector(0, 0.875));
                    vertices.add(new Vector(0, 1));
                    vertices.add(new Vector(1, 1));
                    vertices.add(new Vector(1, 0.875));
                    break;
                default:
                    throw new UnsupportedOperationException("Unknown tile!");
            }

            if (!vertices.isEmpty()) {
                polygon = new Polygon(vertices);
                Transform.translate(polygon, column, row);
                Transform.scale(polygon, WIDTH, HEIGHT);
            }
        }
        return polygon;
    }

    private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
        ois.defaultReadObject();
        polygon = null;
    }
}
TOP

Related Classes of game.terrain.Block

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.