rendered paste bodyimport java.awt.Cursor;
import java.text.DecimalFormat;
import java.net.Socket;
import java.awt.Graphics;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
import java.io.IOException;
import java.net.SocketException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseAdapter;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.awt.Robot;
import java.awt.AWTException;
import java.awt.image.BufferedImage;
import java.awt.Toolkit;
import java.awt.Point;
public class PlayPanel extends GamePanel {
public static boolean STOPTHREAD_AFTER_START = false;
private boolean requestedFocus = false;
public static int objectsDrawn = 0;
// Used to assign a specific id to any object
// Server only
public static long instanceID = 0;
private boolean pressU, pressD, pressL, pressR;
public static String name;
public static boolean host; // are you hosting?
public static String gameType = null;
// data lists
public static BulletList bList; // bullets
public static GrenadeList nList; // nades
// mouse controller
private Robot mouseForcer;
// map details
public static String mapFile;
public static int mapWidth;
public static int mapHeight;
public static int redSpawns = 0;
public static int blueSpawns = 0;
public static int[][] map;
public static final int OBJ_FLOOR = 0;
public static final int OBJ_WALL = 1;
public static final int OBJ_CRATE = 2;
public static final int OBJ_RED_SPAWN = 3;
public static final int OBJ_BLUE_SPAWN = 4;
// images
private BufferedImage floorImg = Sprite.loadImage("Images/floor.gif",false);
private BufferedImage wallImg = Sprite.loadImage("Images/wall.gif",false);
private BufferedImage crateImg = Sprite.loadImage("Images/crate.gif",false);
// view vars
public static int viewX = 0;
public static int viewY = 0;
// minimap
public static int miniMapX = 20;
public static int miniMapY = 20;
public static boolean showMiniMap = true;
// mouse vars
private Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(Sprite.loadImage("Images/cursor.gif",true),new Point(16,16),"Blah");
private int mouseX = 0;
private int mouseY = 0;
private int mousePrevX = 0;
private int mousePrevY = 0;
private int mouseDir = 0;
private int mousePrevDir = 0;
private boolean mouseMoving = false;
private boolean sentDir = false;
private int sentMouseDir = 0;
// fps
private DecimalFormat df = new DecimalFormat("0");
// ping board
private boolean showPingBoard = false;
private static final int PING_BOARD_X = 150;
private static final int PING_BOARD_Y = 75;
// time vars/flags for each game round
public double timeLimit = Time.minutesToNano(2);
public double timeLimitHalf = timeLimit / 2.0;
public double timeLimitQuarter = timeLimit / 4.0;
public double timeLimitQuarterHalf = timeLimit / 8.0;
public long timeStart;
public boolean showQuarterHalfMessage = false;
public boolean showQuarterMessage = false;
public boolean showHalfMessage = false;
public long doneTimeStart;
// respawn time
private double respawnTime = Time.secondsToNano(5);
// For hosting only
public static GameServer gameServer;
public static int maxPlayers;
// For hosting and joining
Socket sock;
GameReceiver receiver;
GameSender sender;
// chat vars
private final int maxChatLines = 5;
public Chat gameChat = new Chat(maxChatLines);
private String inputLine = "";
private boolean chatting = false; // whether we are typing
// database of all the players
public static PlayerList pList;
public static String rawScoreData = null;
private boolean showMyPlayerDetails = false;
// Yourself
public static Player you = null;
private GameFrame gameFrame;
// Sound
private SoundStore soundManager = SoundStore.get();;
public static Sound NADE_EXPLODE_SND;
public static Sound GUN_PLASMA_SND;
// Debug mode constructor
public PlayPanel(GameFrame frame, String yourName, String gameType, String mapFile) {
//GameVars.bgMusic.playAsMusic(1f,1f);
you = null;
// set up your own equipment
for (int i = 0; i < GameVars.equipment.length; i++) {
GameVars.equipment[i] = "n/a";
}
GameVars.equipment[3] = "upgrade_optics";
// load map
this.mapFile = mapFile;
if (!loadMap(mapFile)) {
JOptionPane.showMessageDialog(this,"Could not find: "+mapFile);
System.exit(0);
}
// init lists
pList = new PlayerList();
nList = new GrenadeList();
bList = new BulletList();
// you are host
host = true;
this.maxPlayers = 1;
try {
mouseForcer = new Robot();
}
catch (AWTException e) {
System.out.println("Could not set up mouseForcer");
}
GameState.current = GameState.IN_GAME;
this.name = yourName;
gameFrame = frame;
this.gameType = gameType;
// set up server
gameServer = new GameServer(this,3000);
// start timer if you are playing tag/deathmatch/teammatch
if (gameType.equals(GameVars.TAG) || gameType.equals(GameVars.DEATH_MATCH) || gameType.equals(GameVars.TEAM_MATCH)) {
timeStart = System.nanoTime();
}
// add listeners
this.addKeyListener(keyL);
this.addMouseListener(mouseL);
this.addMouseMotionListener(mouseMotionL);
// change the cursor
this.setCursor(cursor);
// connect to your own server
joinGame("127.0.0.1",3000);
}
// Hosting constructor
public PlayPanel(GameFrame frame, String yourName, String gameName, String gameType, int maxPlayers, String mapFile) {
soundManager.init();
try {
NADE_EXPLODE_SND = soundManager.getOgg("Sound/nade_explode.ogg");
GUN_PLASMA_SND = soundManager.getOgg("Sound/gun_plasma.ogg");
}
catch (IOException ioe) {
System.out.println(ioe);
}
you = null;
// load map
this.mapFile = mapFile;
if (!loadMap(mapFile)) {
JOptionPane.showMessageDialog(this,"Could not find: "+mapFile);
System.exit(0);
}
// init lists
pList = new PlayerList();
nList = new GrenadeList();
bList = new BulletList();
// you are host
host = true;
this.maxPlayers = maxPlayers;
try {
mouseForcer = new Robot();
}
catch (AWTException e) {
System.out.println("Could not set up mouseForcer");
}
GameState.current = GameState.IN_GAME;
this.name = yourName;
gameFrame = frame;
this.gameType = gameType;
// set up server
gameServer = new GameServer(this,3000);
// now that server is set up, attempt to submit to lobby game list
frame.lobby.sender.send("*HOSTING*"+GameVars.myIP+"*3000*"+gameName+"*"+gameType);
// start timer if you are playing tag/deathmatch/teammatch
if (gameType.equals(GameVars.TAG) || gameType.equals(GameVars.DEATH_MATCH) || gameType.equals(GameVars.TEAM_MATCH)) {
timeStart = System.nanoTime();
}
// add listeners
this.addKeyListener(keyL);
this.addMouseListener(mouseL);
this.addMouseMotionListener(mouseMotionL);
// change the cursor
this.setCursor(cursor);
// connect to your own server
joinGame("127.0.0.1",3000);
}
// Join Constructor
public PlayPanel(GameFrame frame, String yourName, Socket sock, String gameType) {
you = null;
// init lists
pList = new PlayerList();
nList = new GrenadeList();
bList = new BulletList();
GameState.current = GameState.IN_GAME;
this.name = yourName;
gameFrame = frame;
host = false;
this.gameType = gameType;
// tell lobby that you are in a game
frame.lobby.sender.send("*PLAYING*");
this.addKeyListener(keyL);
this.addMouseListener(mouseL);
this.addMouseMotionListener(mouseMotionL);
this.setCursor(cursor);
this.sock = sock;
try {
sock.setTcpNoDelay(true);
}
catch (SocketException se) {
System.out.println("Could not disable Nagle's Algorithm");
}
sender = new GameSender(sock);
receiver = new GameReceiver(this, sock);
}
// loads a map
public boolean loadMap(String mapFile) {
try {
redSpawns = 0;
blueSpawns = 0;
BufferedReader in = new BufferedReader(new FileReader("./Maps/"+mapFile));
String[] data = GameVars.getItems(in.readLine());
int newWidth = Integer.parseInt(data[1]);
int newHeight = Integer.parseInt(data[2]);
map = new int[newWidth][newHeight];
for (int j = 0; j < newHeight; j++) {
data = GameVars.getItems(in.readLine());
for (int i = 0; i < newWidth; i++) {
map[i][j] = Integer.parseInt(data[i]);
if (map[i][j] == OBJ_RED_SPAWN) {
redSpawns++;
}
else if (map[i][j] == OBJ_BLUE_SPAWN) {
blueSpawns++;
}
}
}
mapWidth = newWidth*50;
mapHeight = newHeight*50;
return true;
}
catch (IOException e) {
return false;
}
}
// for hosting only
public void joinGame(String ip,int port) {
try {
sock = new Socket(ip, port);
}
catch (UnknownHostException uhe) {
System.err.println("Unable to connect: "+uhe);
JOptionPane.showMessageDialog(null,"Could not connect to game.","Message",JOptionPane.OK_OPTION);
System.exit(0);
}
catch (IOException ioe) {
System.err.println("Unable to connect: "+ioe);
JOptionPane.showMessageDialog(null,"Could not connect to game.","Message",JOptionPane.OK_OPTION);
System.exit(0);
}
try {
sock.setTcpNoDelay(true);
}
catch (SocketException se) {
System.out.println("Could not disable Nagle's Algorithm");
}
sender = new GameSender(sock);
receiver = new GameReceiver(this, sock);
}
// Closes the socket connection to the game server
// Must be called before closing in and out readers
// Closing the sock terminates data = in.readLine()
public void closeSock() {
try {
sock.close();
} catch (IOException ioe) {
System.out.println("IOException: " + ioe);
}
}
KeyAdapter keyL = new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
// player movement keys
if (!chatting) {
if (keyCode == KeyEvent.VK_W) {
pressU = true;
}
if (keyCode == KeyEvent.VK_S) {
pressD = true;
}
if (keyCode == KeyEvent.VK_D) {
pressR = true;
}
if (keyCode == KeyEvent.VK_A) {
pressL = true;
}
}
if (chatting) {
if (keyCode == e.VK_BACK_SPACE) {
if (inputLine.length() > 0) {
inputLine = inputLine.substring(0, inputLine.length() - 1);
}
}
}
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
// Enter key
if (keyCode == e.VK_ENTER) {
if (chatting) {
boolean sendMessage = true;
String message = inputLine.trim();
if (message.length() == 0) {
sendMessage = false;
}
if (sendMessage) {
inputLine = "";
sender.send("*MESSAGE*" + gameFrame.lobby.name + ": " + message);
// return the user to the game
chatting = false;
}
else {
// the message was empty so user must want to exit chat
chatting = false;
}
}
else {
// user must want to chat
chatting = true;
}
}
// Escape key
if (keyCode == KeyEvent.VK_ESCAPE) {
stopThread();
}
// F1 key for debugging
if (keyCode == KeyEvent.VK_F1) {
if (showMyPlayerDetails) {
showMyPlayerDetails = false;
}
else {
showMyPlayerDetails = true;
}
}
// F2 key for minimap toggle
if (keyCode == KeyEvent.VK_F2) {
if (showMiniMap) {
showMiniMap = false;
}
else {
showMiniMap = true;
}
}
// player movement keys, reload key
if (!chatting) {
if (keyCode == KeyEvent.VK_W) {
pressU = false;
}
if (keyCode == KeyEvent.VK_S) {
pressD = false;
}
if (keyCode == KeyEvent.VK_D) {
pressR = false;
}
if (keyCode == KeyEvent.VK_A) {
pressL = false;
}
if (keyCode == KeyEvent.VK_R) {
if (you != null) {
if (!you.respawning) {
you.getPrimaryGun().reload();
}
}
}
}
// chatting keys
if (chatting) {
char keyChar = e.getKeyChar();
String validChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`1234567890-= ~!@#$%^&*()_+{}|:<>?[];',./";
if (validChar.indexOf(String.valueOf(keyChar)) != -1) {
inputLine = inputLine + keyChar;
}
}
}
};
MouseMotionAdapter mouseMotionL = new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
//if (getSource() == e.MOUSE_MOVED) {
// mouseMoving = true;
//}
//else {
// mouseMoving = false;
//}
mouseX = e.getX();
mouseY = e.getY();
}
public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
};
MouseAdapter mouseL = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int button = e.getButton();
if (button == e.BUTTON1) {
if (GameState.current == GameState.IN_GAME) {
if (gameType.equals(GameVars.DEATH_MATCH) ||
gameType.equals(GameVars.TEAM_MATCH)) {
if (you != null) {
if (!you.respawning) {
you.getPrimaryGun().fire(mouseDir, sender);
}
}
}
}
}
if (button == e.BUTTON3) {
if (GameState.current == GameState.IN_GAME) {
if (gameType.equals(GameVars.DEATH_MATCH) ||
gameType.equals(GameVars.TEAM_MATCH)) {
if (you != null) {
if (!you.respawning && you.getNades() > 0) {
you.setNades(you.getNades() - 1);
sender.send("*NEW_NADE*");
}
}
}
}
}
}
// if the mouse gets out of the game, move it back in
public void mouseExited(MouseEvent e) {
if (GameVars.autoKeepCursor) {
mouseForcer.mouseMove(gameFrame.getX() + mouseX,
gameFrame.getY() + mouseY);
}
}
};
public void gameUpdate() {
if (!this.hasFocus() && !requestedFocus) {
this.setFocusable(true);
this.requestFocus();
requestedFocus = true;
}
if (STOPTHREAD_AFTER_START) {
stopThread();
}
if (you != null) {
// perform primary gun update
you.getPrimaryGun().gunUpdate();
// calculate fire direction
double yourX = you.getX();
double yourY = you.getY();
double yourXScreen = yourX - viewX;
double yourYScreen = yourY - viewY;
double angle = Math.atan2( -(mouseY - yourYScreen), (mouseX - yourXScreen));
angle = (angle + Math.PI * 2) % (Math.PI * 2);
mouseDir = (int) Math.toDegrees(angle);
you.setFireDirection(mouseDir);
// check if your direction has changed
if (mousePrevDir != mouseDir) {
}
}
if (mousePrevX != mouseX || mousePrevY != mouseY) {
mouseMoving = true;
sentDir = false;
mousePrevX = mouseX;
mousePrevY = mouseY;
}
else {
mouseMoving = false;
if (!sentDir) {
if (Math.abs(mouseDir-sentMouseDir) > 30) {
if (host) {
ListNode current = gameServer.clientList.first;
while (current != null) {
Client client = (Client) current.getValue();
if (client.inGame) {
if (client.getClientName() != name) {
client.sendMessage("*DIR*" + name + "*" + mouseDir);
}
}
current = current.next();
}
}
else {
this.sender.send("*DIR*" + mouseDir);
}
sentDir = true;
sentMouseDir = mouseDir;
}
}
}
// perform time check if we are playing tag/deathmatch/teammatch and we are host
if (host) {
if (gameType.equals(GameVars.TAG) || gameType.equals(GameVars.DEATH_MATCH) || gameType.equals(GameVars.TEAM_MATCH)){
if (GameState.current == GameState.IN_GAME) {
long timeDiff = System.nanoTime() - timeStart;
if (timeDiff > (long) timeLimit) {
// stop the game
GameState.current = GameState.ROUND_OVER;
// tell everyone the round is over
gameServer.broadcast("*MESSAGE3*Round finished");
ListNode current = pList.first;
while (current != null) {
Player temp = (Player)current.getValue();
// if anyone is still respawing, respawn them
if (temp.respawning) {
// respawn the dead guy
pList.respawn(temp.getName());
// tell everyone that this player has respawned
gameServer.broadcast("*RESPAWN*" + temp.getName());
}
ListNode current2 = gameServer.clientList.first;
while (current2 != null) {
Client client = (Client) current2.getValue();
if (client.inGame) {
client.sendMessage("*MOVE*" + name + "*-1*" +
(int) temp.getX() + "*" +
(int) temp.getY());
if (temp.getName().equals(client.getClientName())) {
client.sendMessage("*POINTS*"+temp.getScore());
}
}
current2 = current2.next();
}
// send player info to master server for point additions
if (!gameType.equals(GameVars.TAG)) {
if (!GameVars.DEBUG_MODE) {
this.gameFrame.lobby.sender.send("*RESULT*" + temp.getName() +
"*" + temp.getScore());
}
}
current = current.next();
}
// send server generated score board
String[] data = pList.sortScoreDescending();
String rawData = "*SCORE*";
for (int i = 0; i < data.length; i++) {
rawData = rawData + data[i] + "|"+pList.getPlayer(data[i]).getScore()+"*";
}
gameServer.broadcast(rawData);
// start the done timer
doneTimeStart = System.nanoTime();
}
if (timeDiff > (long) (timeLimit - timeLimitQuarterHalf) &&
!showQuarterHalfMessage) {
gameServer.broadcast("*MESSAGE3*"+Time.nanoToSecondsString(timeLimitQuarterHalf) +" left");
showQuarterHalfMessage = true;
}
if (timeDiff > (long) (timeLimit - timeLimitQuarter) &&
!showQuarterMessage) {
gameServer.broadcast("*MESSAGE3*"+Time.nanoToSecondsString(timeLimitQuarter)+" left");
showQuarterMessage = true;
}
if (timeDiff > (long) (timeLimit - timeLimitHalf) && !showHalfMessage) {
gameServer.broadcast("*MESSAGE3*"+Time.nanoToSecondsString(timeLimitHalf)+" left");
showHalfMessage = true;
}
}
else if (GameState.current == GameState.ROUND_OVER) {
long timeDiff = System.nanoTime() - doneTimeStart;
if ((double)timeDiff > 12000000000.0) { // past 12 seconds
// reset everyone's scores on server
ListNode current = pList.first;
while (current != null) {
Player temp = (Player)current.getValue();
temp.resetScore();
current = current.next();
}
// tell everyone that the game has started
gameServer.broadcast("*START*");
// start the game
GameState.current = GameState.IN_GAME;
// randomize the next it if we are playing tag
if (gameType.equals(GameVars.TAG)) {
int random = (int) (Math.random() * pList.getTotal());
gameServer.broadcast("*NEWIT*" + pList.getPlayer(random).getName());
}
// reset time and time flags
timeStart = System.nanoTime();
showQuarterHalfMessage = false;
showQuarterMessage = false;
showHalfMessage = false;
}
}
}
}
if (GameState.current == GameState.IN_GAME) {
if (you != null) {
// update your view
updateView();
if (!you.respawning) {
int dir = getKeyBoardDir();
if (you.getDirection() != dir) {
if (host) {
you.setDirection(dir);
if (GameVars.serverType == GameVars.SERVTYPE_DEADRECK) {
ListNode current = gameServer.clientList.first;
while (current != null) {
Client client = (Client) current.getValue();
if (client.inGame) {
if (!client.getClientName().equals(name)) {
client.sendMessage("*MOVE*" + name + "*" + dir +
"*" + (int) you.getX() + "*" +
(int) you.getY());
}
}
current = current.next();
}
}
// dont need to set speed to 0 because the update velocity doesnt
// update if the dir is not in 0-360
}
else {
you.setDirection(dir);
sender.send("*MOVE*" + dir);
}
}
}
}
}
// update bullets if we are playing deathmatch
if (gameType.equals(GameVars.DEATH_MATCH) || gameType.equals(GameVars.TEAM_MATCH)) {
bList.updateAll();
}
// update grenades if we are playing deathmatch
if (gameType.equals(GameVars.DEATH_MATCH) || gameType.equals(GameVars.TEAM_MATCH)) {
nList.updateAll();
}
// update all player's movements
ListNode current = pList.first;
while (current != null) {
Player player = (Player) current.getValue();
player.update();
if (host) {
// if this player is respawning, check timer to respawn him
if (player.respawning) {
if (System.nanoTime() - player.lastDeathTime > respawnTime) {
// respawn the dead guy
pList.respawn(player.getName());
// tell everyone that this player has respawned
gameServer.broadcast("*RESPAWN*" + player.getName());
player.setDirection(-1);
ListNode current2 = gameServer.clientList.first;
while (current2 != null) {
Client client = (Client) current2.getValue();
if (client.inGame) {
if (!client.getClientName().equals(name)) {
client.sendMessage("*MOVE*" + name + "*-1*" +
(int) player.getX() + "*" +
(int) player.getY());
}
}
current2 = current2.next();
}
}
}
else {
if (GameVars.serverType == GameVars.SERVTYPE_ABSOLUTE) {
// if the player is still moving, then broadcast, except yourself
if (player.getDirection() != -1) {
ListNode current2 = gameServer.clientList.first;
while (current2 != null) {
Client client = (Client) current2.getValue();
if (client.inGame) {
if (!client.getClientName().equals(PlayPanel.name)) {
client.sendMessage("*MOVE_POS*" + player.getName() + "*" +
(int) player.getX() + "*" +
(int) player.getY());
}
}
current2 = current2.next();
}
}
}
}
// check player collision if we are playing tag
if (gameType.equals(GameVars.TAG)) {
if (Player.whoIsIt.equals(player.getName())) {
ListNode current2 = pList.first;
while (current2 != null) {
Player temp = (Player) current2.getValue();
if (!temp.getName().equals(player.getName())) {
if (player.intersects(temp)) {
temp.incScore( -1);
double randomX = Math.random() * (GameFrame.frameWidth - 15);
double randomY = Math.random() * (GameFrame.frameHeight - 15);
temp.setLocation(randomX, randomY);
gameServer.broadcast("*NEWIT*" + temp.getName());
gameServer.broadcast("*MESSAGE3*" + temp.getName() +
" is now it.");
}
}
current2 = current2.next();
}
}
}
// check bullet collision if we are playing team match
if (gameType.equals(GameVars.TEAM_MATCH)) {
if (!player.respawning) {
ListNode current2 = bList.first;
while (current2 != null) {
Bullet temp = (Bullet) current2.getValue();
// make sure this isnt this guy's bullet
if (!temp.getOwner().equals(player.getName())) {
if (temp.getBounds().intersects(player.getBounds())) {
// give the bullet's owner a point if he didn't kill someone on his team
Player bulletOwner = pList.getPlayer(temp.getOwner());
int bulletOwnerTeam = bulletOwner.getTeam();
int playerTeam = player.getTeam();
if (bulletOwnerTeam != playerTeam) {
bulletOwner.incScore(1);
// tell everyone who died
gameServer.broadcast("*DEAD_PLAYER*" + player.getName());
// start timer on the dead guy's respawn
player.lastDeathTime = System.nanoTime();
// tell everyone to get rid of that bullet
gameServer.broadcast("*DEAD_BULLET*" + temp.getID());
// tell everyone who killed who
gameServer.broadcast("*MESSAGE2*" + bulletOwner.getName() +
" killed " + player.getName());
}
}
}
current2 = current2.next();
}
}
}
// check bullet collision if we are playing deathmatch
if (gameType.equals(GameVars.DEATH_MATCH)) {
if (!player.respawning) {
ListNode current2 = bList.first;
while (current2 != null) {
Bullet temp = (Bullet) current2.getValue();
// make sure this isnt this guy's bullet
if (!temp.getOwner().equals(player.getName())) {
if (temp.getBounds().intersects(player.getBounds())) {
// give the bullet's owner a point
Player bulletOwner = pList.getPlayer(temp.getOwner());
bulletOwner.incScore(1);
// tell everyone who died
gameServer.broadcast("*DEAD_PLAYER*" + player.getName());
// start timer on the dead guy's respawn
player.lastDeathTime = System.nanoTime();
// tell everyone to get rid of that bullet
gameServer.broadcast("*DEAD_BULLET*" + temp.getID());
// tell everyone who killed who
gameServer.broadcast("*MESSAGE2*" + bulletOwner.getName() +
" killed " + player.getName());
}
}
current2 = current2.next();
}
}
}
}
current = current.next();
}
}
// Updates the coordinates of the view
private void updateView() {
// if you are in the game
if (you != null) {
viewX = (int)you.getX() - GameFrame.frameWidth/2;
viewY = (int)you.getY() - GameFrame.frameHeight/2;
}
}
public static boolean isSolid(int tile) {
switch (tile) {
case OBJ_CRATE: return true;
case OBJ_WALL: return true;
}
return false;
}
public void gameRender() {
objectsDrawn = 0;
// clear background
dbg.setColor(Color.black);
dbg.fillRect(0, 0, this.getWidth(), this.getHeight());
dbg.setFont(GameVars.normalFont);
int[][] tempMap = map;
for (int i = 0; i < tempMap.length; i++) {
for (int j = 0; j < tempMap[0].length; j++) {
if (viewX < i*50+50 && viewX+GameFrame.frameWidth > i*50 && viewY < j*50+50 && viewY+GameFrame.frameHeight > j*50) {
if (tempMap[i][j] == OBJ_FLOOR) {
dbg.drawImage(floorImg, i * 50 - viewX, j * 50 - viewY, null);
objectsDrawn++;
}
else if (tempMap[i][j] == OBJ_WALL) {
dbg.drawImage(wallImg, i * 50 - viewX, j * 50 - viewY, null);
objectsDrawn++;
}
else if (tempMap[i][j] == OBJ_CRATE) {
dbg.drawImage(crateImg, i * 50 - viewX, j * 50 - viewY, null);
objectsDrawn++;
}
else if (tempMap[i][j] == OBJ_RED_SPAWN) {
dbg.drawImage(floorImg, i * 50 - viewX, j * 50 - viewY, null);
objectsDrawn++;
}
else if (tempMap[i][j] == OBJ_BLUE_SPAWN) {
dbg.drawImage(floorImg, i * 50 - viewX, j * 50 - viewY, null);
objectsDrawn++;
}
}
}
}
// draw minimap
if (showMiniMap) {
dbg.setColor(Color.white);
dbg.drawRect(miniMapX, miniMapY, (tempMap.length * 50) / 10,
(tempMap[0].length * 50) / 10);
dbg.drawRect(miniMapX + viewX / 10, miniMapY + viewY / 10,
GameFrame.frameWidth / 10, GameFrame.frameHeight / 10);
for (int i = 0; i < tempMap.length; i++) {
for (int j = 0; j < tempMap[0].length; j++) {
if (tempMap[i][j] == OBJ_WALL) {
dbg.setColor(Color.white);
dbg.fillRect(miniMapX + (i * 50) / 10, miniMapY + (j * 50) / 10, 5,
5);
}
else if (tempMap[i][j] == OBJ_CRATE) {
dbg.setColor(Color.yellow);
dbg.fillRect(miniMapX + (i * 50) / 10, miniMapY + (j * 50) / 10, 5,
5);
}
}
}
ListNode current = pList.first;
while (current != null) {
Player obj = (Player) current.getValue();
if (!obj.respawning) {
if (gameType.equals(GameVars.TEAM_MATCH)) {
if (obj.getTeam() == GameVars.TEAM_RED) {
dbg.setColor(Color.red);
}
else if (obj.getTeam() == GameVars.TEAM_BLUE) {
dbg.setColor(Color.blue);
}
}
else {
if (obj.getName().equals(name)) {
dbg.setColor(Color.red);
}
else {
dbg.setColor(Color.yellow);
}
}
dbg.drawRect(PlayPanel.miniMapX + (int) ((obj.getX()) / 10),
PlayPanel.miniMapY + (int) ((obj.getY()) / 10), 1, 1);
}
current = current.next();
}
}
// draw game objects
nList.drawAll(dbg);
pList.drawAll(dbg);
bList.drawAll(dbg);
if (GameVars.equipment[3].equals("upgrade_optics")) {
if (you != null) {
double yourX = you.getX();
double yourY = you.getY();
if (!you.respawning) {
// draw sniper line
dbg.setColor(Color.green);
dbg.drawLine((int) (yourX - viewX), (int) (yourY - viewY),
(int) (mouseX), (int) (mouseY));
// draw detect radius
// dbg.setColor(Color.green);
// dbg.drawOval((int) (yourX - viewX) - 300, (int) (yourY - viewY) - 300, 600, 600);
// draw enemy indicators
ListNode current = pList.first;
while (current != null) {
Player temp = (Player) current.getValue();
if (!temp.getName().equals(name)) {
if (GameVars.distance(yourX, yourY, temp.getX(), temp.getY()) < 400) {
if (gameType.equals(GameVars.DEATH_MATCH) ||
gameType.equals(GameVars.TAG)) {
if (!temp.respawning) {
dbg.setColor(Color.red);
dbg.drawLine((int) (yourX - viewX), (int) (yourY - viewY),
(int) (temp.getX() - viewX),
(int) (temp.getY() - viewY));
}
}
if (gameType.equals(GameVars.TEAM_MATCH)) {
if (!temp.respawning) {
if (temp.getTeam() != you.getTeam()) {
dbg.setColor(Color.red);
dbg.drawLine((int) (yourX - viewX), (int) (yourY - viewY),
(int) (temp.getX() - viewX),
(int) (temp.getY() - viewY));
}
}
}
}
}
current = current.next();
}
}
}
}
// draw the chat
dbg.setFont(GameVars.normalFont);
gameChat.drawChat(dbg,10,380);
// draw score board if the round is over
if (GameState.current == GameState.ROUND_OVER) {
drawScoreBoard(dbg);
}
dbg.setColor(Color.white);
dbg.setFont(GameVars.normalFont);
dbg.drawString("FPS: "+df.format(fpsCounter.getAverageFPS()),5,15);
if (showMyPlayerDetails) {
if (you != null) {
dbg.drawString("Player Pos: " + (int)you.getX() + "," + (int)you.getY(), 400, 15);
}
dbg.drawString("View Pos: " + viewX + "," + viewY,400,30);
dbg.drawString("Fire Pos: " + mouseX + "," + mouseY, 400, 45);
dbg.drawString("Fire Dir: " + mouseDir, 400, 60);
dbg.drawString("Objects Drawn: " + objectsDrawn, 400, 75);
}
//dbg.drawString("Time: "+this.nanosecToString(System.nanoTime()-timeStart),150,15);
if (chatting) {
dbg.drawString("Say: " + inputLine + "|", 10, 480);
}
if (you != null) {
if (you.respawning) {
dbg.setColor(Color.GREEN);
dbg.setFont(GameVars.largeFont);
dbg.drawString("Respawning...", 120, 150);
}
dbg.setFont(GameVars.headingFont);
if (you.getPrimaryGun().isReloading()) {
dbg.setColor(Color.red);
dbg.drawString("RELOADING",490,330);
}
dbg.setColor(Color.blue);
dbg.drawString(you.getPrimaryGun().getName(),490,355);
dbg.setColor(Color.orange);
dbg.drawString("Ammo: "+you.getPrimaryGun().getAmmo(),490,370);
dbg.drawString("Nades: "+you.getNades(),490,385);
}
}
public void drawScoreBoard(Graphics g) {
int x = 150;
int y = 75;
Color color = new Color(50,50,50,175);
g.setColor(color);
g.fillRect(x,y,300,350);
g.setColor(Color.GREEN);
g.setFont(GameVars.headingFont);
g.drawString("Round Results",x+20,y+25);
g.setColor(Color.YELLOW);
g.setFont(GameVars.normalFont);
g.drawString("Player Name",x+20,y+45);
g.drawString("Score",x+130,y+45);
g.setColor(Color.WHITE);
boolean done = false;
int count = 0;
String temp = rawScoreData;
int redPoints = 0;
int bluePoints = 0;
while (!done) {
if (rawScoreData == null) {
done = true;
}
else {
int i = temp.indexOf("|");
if (i == -1) {
done = true;
}
else {
String player = temp.substring(0,i);
temp = temp.substring(i + 1);
i = temp.indexOf("*");
String score = temp.substring(0,i);
temp = temp.substring(i + 1);
Player obj = pList.getPlayer(player);
if (obj != null) {
if (obj.getTeam() == GameVars.TEAM_RED) {
g.setColor(Color.red);
redPoints += Integer.parseInt(score);
}
else if (obj.getTeam() == GameVars.TEAM_BLUE) {
g.setColor(Color.blue);
bluePoints += Integer.parseInt(score);
}
}
g.drawString(player,x+20,y+65+count*20);
g.drawString(score,x+130,y+65+count*20);
count++;
}
}
}
g.setFont(GameVars.normalFont);
g.setColor(Color.YELLOW);
g.drawString("Next round will start in a moment",x+20,y+325);
if (gameType.equals(GameVars.TEAM_MATCH)) {
g.setFont(GameVars.headingFont);
g.setColor(Color.red);
g.drawString("Red Score: "+redPoints,x+20,y+300);
g.setColor(Color.blue);
g.drawString("Blue Score: "+bluePoints,x+165,y+300);
g.setFont(GameVars.largeFont);
if (redPoints > bluePoints) {
g.setColor(Color.red);
g.drawString("Red Team Wins",x+35,y+380);
}
if (bluePoints > redPoints) {
g.setColor(Color.blue);
g.drawString("Blue Team Wins",x+35,y+380);
}
if (redPoints == bluePoints) {
g.setColor(Color.white);
g.drawString("It is a Tie",x+35,y+380);
}
g.setFont(GameVars.normalFont);
}
}
private void drawPingBoard(Graphics g) {
Color color = new Color(50,50,50,175);
g.setColor(color);
g.fillRect(PING_BOARD_X,PING_BOARD_Y,180,20*pList.getTotal());
g.setFont(GameVars.normalFont);
int i = 0;
ListNode current = pList.first;
while (current != null) {
Player temp = (Player)current.getValue();
g.setColor(Color.white);
g.drawString(GameVars.getValidText(g, temp.getName(), 100), PING_BOARD_X + 5, PING_BOARD_Y + 15+20*i);
g.setColor(Color.green);
//g.drawString(""+temp.getPing()+" ms",PING_BOARD_X+115,PING_BOARD_Y+15);
i++;
current = current.next();
}
}
public void onExit() {
// To close the connection
try {
sender.closeOutStream();
receiver.closeInStream();
if (sock != null) {
sock.close();
}
}
catch (IOException ioe) {
System.err.println(ioe);
}
catch (NullPointerException npe) {
System.err.println(npe);
}
if (host) {
if (you != null) {
if (you.getTeam() == GameVars.TEAM_RED) {
GameVars.TOTAL_RED--;
}
if (you.getTeam() == GameVars.TEAM_BLUE) {
GameVars.TOTAL_BLUE--;
}
}
if (!GameVars.DEBUG_MODE) {
// tell lobby to take off your game listing
gameFrame.lobby.sender.send("*HOSTINGOFF*");
}
// shutdown server
gameServer.stopServer();
}
else {
// tell lobby that you are back from a game
gameFrame.lobby.sender.send("*BACK*");
}
if (GameVars.DEBUG_MODE) {
System.exit(0);
}
else {
//GUN_PLASMA_SND.restartMusic(1f,1f);
//GUN_PLASMA_SND.restartMusic();
//GameVars.NADE_EXPLODE_SND.restartMusic();
gameFrame.lobby.lf.setVisible(true);
gameFrame.lobby.startThread(20, 50);
gameFrame.dispose();
}
}
private int getKeyBoardDir() {
if (pressU && !pressD && !pressR && !pressL) { // up
return 90;
}
else if (!pressU && pressD && !pressR && !pressL) { // down
return 270;
}
else if (!pressU && !pressD && pressR && !pressL) { // right
return 0;
}
else if (!pressU && !pressD && !pressR && pressL) { // left
return 180;
}
else if (pressU && !pressD && pressR && !pressL) { // up-right
return 45;
}
else if (pressU && !pressD && !pressR && pressL) { // up-left
return 135;
}
else if (!pressU && pressD && pressR && !pressL) { // down-right
return 315;
}
else if (!pressU && pressD && !pressR && pressL) { // down-left
return 225;
}
else { // stop
return -1;
}
}
}