Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package mpogassignment01;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.net.ServerSocket;
- import java.net.InetAddress;
- import java.io.InterruptedIOException;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.ArrayList;
- import static java.awt.event.KeyEvent.*;
- import javax.swing.JOptionPane;
- /**
- *
- * @author jiahui
- */
- public class Assignment01 extends GameEngine {
- final static int PORT = 3000;
- enum State {
- MENU, CONNECTING, WAITING, CHATTING
- };
- State state;
- Socket sock;
- ServerSocket servSock;
- //String text;
- BufferedReader socketIn;
- PrintWriter socketOut;
- Graphics2D g2d;
- ArrayList<String> messages = new ArrayList<String>();
- private Image mainMenuImage;
- boolean checkChoice = false;
- String userName = " ";
- int checkNo = 0;
- int Health = 100;
- int Exp = 50;
- // try {
- // mainMenuImage = Image.createImage("/Main.png");
- // } catch (IOException ioe) {
- // System.err.println("Failed to load Main Menu Image!");
- // } // Load Main Menu Image
- void displayMessage(String s) {
- if (messages.size() > 10) // if array is too full:
- {
- messages.remove(0);
- }
- messages.add(s);
- }
- void reset()
- {
- checkNo = 0;
- }
- String userInput = " ";
- boolean processUserInput() {
- char c =getLastKey();
- if (c=='\0')
- return false;
- if ((int)c==VK_ENTER)
- return true; // end of entry
- if((int)c==VK_BACK_SPACE || (int)c==VK_DELETE)
- {
- if(!userInput.isEmpty())
- {
- userInput=userInput.substring(0, userInput.length()-1);
- }
- return false; // not yet
- }
- userInput+=c;
- return false;
- }
- public static void main(String[] args) {
- new Assignment01().go(1000, 700, false);
- }
- public void startup() {
- state = State.MENU;
- String input = JOptionPane.showInputDialog("Please Enter a Nickname!", "New Player");
- userName = input;
- }
- public void update() {
- if (keys[VK_ESCAPE]) {
- close(); // close me
- }
- if (state == State.MENU) {
- updateMenu();
- } else if (state == State.WAITING) {
- updateWaiting();
- } else if (state == State.CONNECTING) {
- updateConnecting();
- } else {
- updateChatting();
- }
- }
- void updateMenu() {
- if (keys[VK_C]) {
- state = State.CONNECTING;
- getLastKey();
- userInput="localhost";
- //userInput="172.17.66.58"; // jiahui
- //userInput="172.17.73.253"; //brenda
- }
- if (keys[VK_S]) {
- state = State.WAITING;
- setupServer();
- }
- }
- void updateWaiting() {
- try {
- sock = servSock.accept();
- sock.setSoTimeout(1);
- displayMessage("We have a connection from " + sock.getLocalAddress().getHostAddress() + ":" + sock.getPort());
- socketIn = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- socketOut = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
- // we have a connection, to change state to Chatting
- state = State.CHATTING;
- getLastKey(); // will clear the last keypress
- } catch (InterruptedIOException e) {
- } catch (IOException e) {
- displayMessage("Error:" + e);
- }
- }
- void updateConnecting() {
- if (processUserInput()) { //if user pressed return
- //connect to this FP:
- try {
- sock = new Socket(userInput, PORT);
- sock.setSoTimeout(1);
- //TODO: set up socketIn,socketOut
- socketIn = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- socketOut = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
- displayMessage("connected");
- // we have a connection, to change state to Chatting
- state = State.CHATTING;
- userInput = "";
- } catch (Exception e) {
- displayMessage("Error:" + e);
- }
- }
- }
- String recvSocket() {
- //receive the message
- try {
- String s = socketIn.readLine();
- return s;
- } catch (InterruptedIOException e) {
- } catch (IOException e) {
- displayMessage("Error: " + e);
- e.printStackTrace();
- }
- return null;
- }
- void sendSocket(String s) {
- socketOut.println(s);
- socketOut.flush();
- }
- void updateChatting() {
- String s = recvSocket();
- if (s!=null){
- System.out.println(s);
- if(s.startsWith("CHAT ")){
- s=s.substring(5); // chop off "CHAT"
- displayMessage("I got a message:" + s);
- }
- if (s.startsWith("GAME ")){
- s=s.substring(5); // chop off "CHAT"
- //TODO: do something useful with the game
- displayMessage("PLAYER " + s);
- }
- }
- if (keys[VK_LEFT] && checkNo==0){
- sendSocket("GAME ATTACK");
- //checkChoice = true;
- Exp = Exp - 10;
- checkNo++;
- }
- //attack
- if (keys[VK_RIGHT] && checkNo==0){
- sendSocket("GAME DEFEND");
- //checkChoice = true;
- Exp = Exp - 5;
- checkNo++;
- }
- //idle
- if(keys[VK_UP] && checkNo==0){
- sendSocket("GAME IDLE");
- //checkChoice = true;
- checkNo++;
- if (Exp == 50)
- Exp = 50;
- else
- Exp = Exp + 10;
- }
- if (processUserInput()) { // if user pressed return
- if (userInput.isEmpty() == false) { // if data was entered
- displayMessage("Sending:" + userInput);
- socketOut.println("CHAT " + userInput);
- socketOut.flush();
- userInput = ""; // clear user input
- }
- }
- }
- public void setupServer() {
- try {
- servSock = new ServerSocket(3000);
- servSock.setSoTimeout(1);
- InetAddress local = InetAddress.getLocalHost();
- displayMessage("starting....");
- } catch (InterruptedIOException e) {
- } catch (IOException er) {
- displayMessage("Error Found: " + er);
- }
- }
- public void draw(Graphics2D g2d) {
- if (keys[VK_ESCAPE]) {
- close(); // close me
- }
- if (state == State.MENU) {
- drawMenu(g2d);
- } else if (state == State.WAITING) {
- drawWaiting(g2d);
- } else if (state == State.CONNECTING) {
- drawConnecting(g2d);
- } else {
- drawChatting(g2d);
- }
- }
- void drawMenu(Graphics2D g2d) {
- clear(g2d, Color.WHITE);
- g2d.setColor(Color.BLACK);
- g2d.drawString("Press 'C' for Client, 'S' for Server", 10, 50);
- }
- void drawWaiting(Graphics2D g2d) {
- clear(g2d, Color.YELLOW);
- g2d.setColor(Color.RED);
- g2d.drawString("Waiting for connection", 10, 50);
- for (int i = 0; i < messages.size(); i++) {
- g2d.drawString(messages.get(i), 10, 60 + 10 * i);
- }
- }
- void drawConnecting(Graphics2D g2d) {
- clear(g2d, Color.LIGHT_GRAY);
- g2d.setColor(Color.BLACK);
- g2d.drawString("Please enter server name", 10, 50);
- g2d.drawString(">" + userInput + ",", 10, 70);
- }
- void drawChatting(Graphics2D g2d) {
- clear(g2d, Color.CYAN);
- g2d.setColor(Color.BLACK);
- g2d.drawString(userName + " are connected", 40,530);
- g2d.drawString("my health: " + Health, 40,50);
- g2d.drawString("my exp: " + Exp, 240,50);
- for (int i = 0; i < messages.size(); i++) {
- g2d.drawString(messages.get(i), 40, 550 + 10 * i);
- g2d.drawString(userName+" > " + userInput + "|", 40, 670);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement