rainet.Game.java Source code

Java tutorial

Introduction

Here is the source code for rainet.Game.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rainet;

import com.ticknorn.s;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

/**
 *
 * @author Nial8r
 */
public class Game implements Runnable {

    private static Socket client;
    private static float scale;
    public static Board gBoard;
    public final static int SIDE_YELLOW = 500;
    public final static int SIDE_BLUE = 501;
    public final static int PIECE_LINK = 700;
    public final static int PIECE_VIRUS = 701;
    public static final int PIECE_LINEBOOST_LINK = 702;
    public static final int PIECE_LINEBOOST_VIRUS = 703;
    public static final int PIECE_FIREWALL = 704;
    public static final int TERMINAL_LINEBOOST = 200;
    public static final int TERMINAL_FIREWALL = 201;
    public static final int TERMINAL_VIRUSCHECK = 202;
    public static final int TERMINAL_NOTFOUND = 203;
    public int BOARD_WIDTH, BOARD_HEIGHT;
    public static int mySide, nextSide;
    //public static int[] selected;
    public static int[] links = { -1, -1, -1, -1 }, viruses = { -1, -1, -1, -1 }, olinks = { -1, -1, -1, -1 },
            oviruses = { -1, -1, -1, -1 };

    //public static Game runGame;
    public static DataInputStream sInput;
    public static DataOutputStream sOutput;
    public static ClientInput cInput;
    public static int b = (int) (Display.getDisplayMode().getHeight() * .9);
    public static boolean resp = false;
    public static boolean myTurn = false, doSelect = true;
    public static int[] selectedP = { -1, -1 };
    public static ArrayList<Button> buttons = new ArrayList<>();
    public static int pLinkN = 0, pVirusN = 0;
    public static boolean[] hasTerminal = { true, true, true, true };
    public static boolean[] ohasTerminal = { true, true, true, true };

    //public static int[] sideProgression = new int[]{SIDE_YELLOW, SIDE_BLUE};
    //public static int turn = 0;
    //public static int[] sPiece = new int[]{-1, -1};
    //public static int[][] viruses = new int[sideProgression.length][4];
    //public static int[][] links = new int[sideProgression.length][4];
    public static Texture[] textureLinks;
    public static Texture[] textureViruses;
    public static Texture selectedT;

    private int st_w, st_h, st_s;
    private String st_t;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        scale = (float) (b / 1024.);
        System.setProperty("org.lwjgl.librarypath", new File("natives").getAbsolutePath());
        //runGame = new Game();
        String serverName = "localhost";
        int port = 7772;
        if (args.length >= 2) {
            serverName = args[0];
            port = s.getInt(args[1], port);
        }

        try {
            System.out.println("Connecting to " + serverName + " on port " + port);
            client = new Socket(serverName, port);
            System.out.println("Just connected to " + client.getRemoteSocketAddress());

            sInput = new DataInputStream(client.getInputStream());
            cInput = new ClientInput(sInput);
            Thread cInputThread = new Thread(cInput);
            cInputThread.start();

            OutputStream outToServer = client.getOutputStream();
            sOutput = new DataOutputStream(outToServer);

            sOutput.writeByte(1); //initiate connection
            sOutput.writeUTF("Hello from " + client.getLocalSocketAddress());

            sOutput.writeByte(2); //get SIDE and game info

        } catch (IOException e) {
            System.out.println("No Connection Availiable");
            //e.printStackTrace();
        }

    }

    public void init(int s) throws IOException {
        this.mySide = s;
        gBoard = new Board("res/board" + s + ".png", BOARD_WIDTH, BOARD_HEIGHT, pointToScale(22), pointToScale(183),
                pointToScale(8), pointToScale(8), pointToScale(75), pointToScale(75));
        gBoard.initBoard();
        textureLinks = new Texture[] {
                TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/piece500700_front.png")),
                TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/piece501700_front.png")) };
        textureViruses = new Texture[] {
                TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/piece500701_front.png")),
                TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/piece501701_front.png")) };
        selectedT = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/select.png"));
        buttons.add(new Button("buttonexit", pointToScale(205), pointToScale(107), pointToScale(292),
                pointToScale(36), false));
        buttons.add(new Button("button" + mySide + "lb", pointToScale(769), pointToScale(565), pointToScale(82),
                pointToScale(81), true));
        buttons.add(new Button("button" + mySide + "fw", pointToScale(873), pointToScale(565), pointToScale(82),
                pointToScale(81), true));
        buttons.add(new Button("button" + mySide + "vc", pointToScale(770), pointToScale(665), pointToScale(82),
                pointToScale(81), true));
        buttons.add(new Button("button" + mySide + "nf", pointToScale(873), pointToScale(665), pointToScale(82),
                pointToScale(81), true));
        buttons.add(new Button("piece" + mySide + "700_front", pointToScale(777), pointToScale(860),
                pointToScale(75), pointToScale(82), true));
        buttons.add(new Button("piece" + mySide + "701_front", pointToScale(871), pointToScale(860),
                pointToScale(75), pointToScale(82), true));
        buttons.add(new Button("button" + nextSide + "lb", pointToScale(769), pointToScale(374), pointToScale(82),
                pointToScale(81), true, true));
        buttons.add(new Button("button" + nextSide + "fw", pointToScale(873), pointToScale(374), pointToScale(82),
                pointToScale(81), true, true));
        buttons.add(new Button("button" + nextSide + "vc", pointToScale(770), pointToScale(274), pointToScale(82),
                pointToScale(81), true, true));
        buttons.add(new Button("button" + nextSide + "nf", pointToScale(873), pointToScale(274), pointToScale(82),
                pointToScale(81), true, true));
        //        buttons.add(new Button("piece"+nextSide+"700_front",pointToScale(775),pointToScale(146),pointToScale(75),pointToScale(82),true,true));
        //        buttons.add(new Button("piece"+nextSide+"701_front",pointToScale(868),pointToScale(146),pointToScale(75),pointToScale(82),true,true));
    }

    public void start(int w, int h, String t, int s, int bw, int bh, int n) throws IOException {
        this.st_w = w;
        this.st_h = h;
        this.st_s = s;
        this.nextSide = n;
        this.st_t = t;
        this.BOARD_WIDTH = bw;
        this.BOARD_HEIGHT = bh;
    }

    public void readServerValues() {

    }

    /**
     * renders the game
     */
    public void render() throws IOException {
        Color.white.bind();
        gBoard.draw();
        float lx = pointToScale(440), vx = pointToScale(-2), py = pointToScale(934), py2 = pointToScale(4),
                pwidth = pointToScale(75), pheight = pointToScale(83), pbuff = pointToScale(-13);
        for (int i = 0; i < 4; i++) {
            if (links[i] != -1) {
                textureLinks[links[i] == 500 ? 0 : 1].bind();
                GL11.glBegin(GL11.GL_QUADS);
                GL11.glTexCoord2f(0, 0);
                GL11.glVertex2f(lx + (pwidth + pbuff) * i, py);
                GL11.glTexCoord2f(1, 0);
                GL11.glVertex2f(lx + (pwidth + pbuff) * i + pwidth, py);
                GL11.glTexCoord2f(1, 1);
                GL11.glVertex2f(lx + (pwidth + pbuff) * i + pwidth, py + pheight);
                GL11.glTexCoord2f(0, 1);
                GL11.glVertex2f(lx + (pwidth + pbuff) * i, py + pheight);
                GL11.glEnd();
            }
            if (viruses[i] != -1) {
                textureViruses[viruses[i] == 500 ? 0 : 1].bind();
                GL11.glBegin(GL11.GL_QUADS);
                GL11.glTexCoord2f(0, 0);
                GL11.glVertex2f(vx + (pwidth + pbuff) * i, py);
                GL11.glTexCoord2f(1, 0);
                GL11.glVertex2f(vx + (pwidth + pbuff) * i + pwidth, py);
                GL11.glTexCoord2f(1, 1);
                GL11.glVertex2f(vx + (pwidth + pbuff) * i + pwidth, py + pheight);
                GL11.glTexCoord2f(0, 1);
                GL11.glVertex2f(vx + (pwidth + pbuff) * i, py + pheight);
                GL11.glEnd();
            }
            if (olinks[i] != -1) {
                textureLinks[olinks[i] == 500 ? 0 : 1].bind();
                GL11.glBegin(GL11.GL_QUADS);
                GL11.glTexCoord2f(1, 1);
                GL11.glVertex2f(vx + (pwidth + pbuff) * i, py2);
                GL11.glTexCoord2f(0, 1);
                GL11.glVertex2f(vx + (pwidth + pbuff) * i + pwidth, py2);
                GL11.glTexCoord2f(0, 0);
                GL11.glVertex2f(vx + (pwidth + pbuff) * i + pwidth, py2 + pheight);
                GL11.glTexCoord2f(1, 0);
                GL11.glVertex2f(vx + (pwidth + pbuff) * i, py2 + pheight);
                GL11.glEnd();
            }
            if (oviruses[i] != -1) {
                textureViruses[oviruses[i] == 500 ? 0 : 1].bind();
                GL11.glBegin(GL11.GL_QUADS);
                GL11.glTexCoord2f(1, 1);
                GL11.glVertex2f(lx + (pwidth + pbuff) * i, py2);
                GL11.glTexCoord2f(0, 1);
                GL11.glVertex2f(lx + (pwidth + pbuff) * i + pwidth, py2);
                GL11.glTexCoord2f(0, 0);
                GL11.glVertex2f(lx + (pwidth + pbuff) * i + pwidth, py2 + pheight);
                GL11.glTexCoord2f(1, 0);
                GL11.glVertex2f(lx + (pwidth + pbuff) * i, py2 + pheight);
                GL11.glEnd();
            }

        }

        for (Button b : buttons) {
            b.draw();
        }

        if (selectedP[0] != -1 && selectedP[1] != -1)
            gBoard.drawSelect(selectedP[0], selectedP[1]);

    }

    /**
     * scales a point to the scale of the game window
     *
     * @param n input coordinate
     * @return n,scaled to the window
     */
    public static float pointToScale(float n) {
        return scale * n;
    }

    /*    * Initialise the GL display
     * 
     * @param width The width of the display
     * @param height The height of the display
     */
    private void initGL(int width, int height, String title) {
        try {
            Display.setDisplayMode(new DisplayMode(width, height));
            Display.setLocation(6, 7);
            Display.setTitle(title);
            Display.create();
            Display.setVSyncEnabled(true);
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

        GL11.glViewport(0, 0, width, height);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, width, height, 0, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }

    public static void testM() {
        System.out.println("tjos wprlded");
    }

    public Board getBoard() {
        return this.gBoard;
    }

    public void placePiece(int psd, int pid, int px, int py, boolean pfu) throws IOException {
        this.gBoard.place(psd, pid, px, py, pfu);
        //this.gBoard.toString();
    }

    private void pollInput() throws IOException {
        while (Mouse.next()) {
            float mx = Mouse.getX(), my = Mouse.getY();
            int sx = gBoard.getSpace(mx, my)[0], sy = gBoard.getSpace(mx, my)[1];
            if (Mouse.getEventButtonState()) {
                if (Mouse.getEventButton() == 0) {
                    //gBoard.click(Mouse.getX(), Mouse.getY(),true);
                }
            } else {
                if (Mouse.getEventButton() == 0 && myTurn) {
                    if (gBoard.inGameBoard(mx, my)) {
                        if (resp) {
                            //System.out.println("Sending click at: "+sx+" "+sy);
                            sOutput.writeByte(6);
                            sOutput.writeInt(sx);
                            sOutput.writeInt(sy);
                            resp = false;
                        }
                    } else {
                        for (Button b : buttons) {
                            //System.out.println("Checking: "+buttons.indexOf(b));
                            if (b.gotClicked(mx, Display.getHeight() - my)) {
                                //System.out.println("Clicked: "+buttons.indexOf(b));
                                switch (buttons.indexOf(b)) {
                                case 0:
                                    sOutput.writeByte(7);
                                    //                                        sOutput.writeInt(selectedP[0]);
                                    //                                        sOutput.writeInt(selectedP[1]);
                                    break;
                                case 5:
                                    if (pLinkN > 0) {
                                        sOutput.writeByte(8);
                                        sOutput.writeInt(PIECE_LINK);
                                    }
                                    break;
                                case 6:
                                    if (pVirusN > 0) {
                                        sOutput.writeByte(8);
                                        sOutput.writeInt(PIECE_VIRUS);
                                    }
                                    break;
                                case 1:
                                    if (hasTerminal[0]) {
                                        sOutput.writeByte(9);
                                        sOutput.writeInt(TERMINAL_LINEBOOST);
                                    }
                                    break;
                                case 2:
                                    if (hasTerminal[1]) {
                                        sOutput.writeByte(9);
                                        sOutput.writeInt(Game.TERMINAL_FIREWALL);
                                    }
                                    break;
                                case 3:
                                    if (hasTerminal[2]) {
                                        sOutput.writeByte(9);
                                        sOutput.writeInt(Game.TERMINAL_VIRUSCHECK);
                                    }
                                    break;
                                case 4:
                                    if (hasTerminal[3]) {
                                        sOutput.writeByte(9);
                                        sOutput.writeInt(Game.TERMINAL_NOTFOUND);
                                    }
                                    break;
                                default:
                                    break;
                                }
                            }
                        }
                    }
                } else if (Mouse.getEventButton() == 1) {
                    if (!hasTerminal[0]) {
                        sOutput.writeByte(10);
                        sOutput.writeInt(sx);
                        sOutput.writeInt(sy);
                    }
                }
            }
        }
        while (Keyboard.next()) {
            switch (Keyboard.getEventCharacter()) {
            case 'g':
                int[] wtfr = gBoard.getSpace(Mouse.getX(), Mouse.getY());
                System.out.println(wtfr[0] + " " + wtfr[1]);
                sOutput.writeByte(5);
                sOutput.writeInt(wtfr[0]);
                sOutput.writeInt(wtfr[1]);
                break;
            case 'f':
                String s1 = "Links: ";
                String s2 = "Viruses: ";
                String s3 = "OLinks: ";
                String s4 = "OViruses: ";
                for (int i = 0; i < 4; i++) {
                    s1 += links[i] + " ";
                    s2 += viruses[i] + " ";
                    s3 += olinks[i] + " ";
                    s4 += oviruses[i] + " ";
                }
                System.out.println(s1 + "\n" + s2 + "\n" + s3 + "\n" + s4);
                break;
            }

        }
    }

    @Override
    public void run() {
        try {
            initGL(this.st_w, this.st_h, this.st_t);
            init(this.st_s);
            System.out.println("proceed");
            System.out.println(this.gBoard == null);
            sOutput.writeByte(3);
            while (true) {
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
                pollInput();

                //readServerValues();
                render();
                Display.update();
                Display.sync(100);
                if (Display.isCloseRequested()) {
                    client.close();
                    Display.destroy();
                    System.exit(0);
                }
            }
        } catch (IOException e) {
            System.out.println("epic fail");
        }
    }

}