Skip to content

Commit

Permalink
Mejora de colision al crear jugadores y Scores
Browse files Browse the repository at this point in the history
  • Loading branch information
uberkael committed May 1, 2016
1 parent 3d86e11 commit 9e138f7
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 89 deletions.
60 changes: 40 additions & 20 deletions Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class Board extends JPanel implements java.io.Serializable {
int dying=0;
/* Score information */
int currScore;
int highScore;
/* if the high scores have been cleared, we have to update the top of the screen to reflect that */
boolean clearHighScores=false;
int numLives=2;
Expand Down Expand Up @@ -64,7 +63,6 @@ public Board(ServicioPac server, String a) {
max=400;
gridSize=20;
New=0;
//initHighScores();
sounds=new GameSounds();
}
/* For JAR File*/
Expand Down Expand Up @@ -132,7 +130,7 @@ else if (dying==1)
if (dying!=1)
temp=100;
else
temp=2000;
temp=100;;
/* If it's time to draw a new death frame... */
if (currTime-timer>=temp)
{
Expand All @@ -149,10 +147,10 @@ else if (dying==1)
else
{
/* Game over for player. If relevant, update high score. Set gameOver flag*/
if (player.currScore>highScore)
{
//TODO//updateScore(player.currScore);
}
// if (player.currScore>tB.getHighScore())
// {
// //TODO//updateScore(player.currScore);
// }
overScreen=true;
}
}
Expand Down Expand Up @@ -296,6 +294,14 @@ else if (New==4)
// oops=true;
// else if (player.y==ghost4.y&&Math.abs(player.x-ghost4.x)<10)
// oops=true;
/* Fantasma matador */
Player k=PlayerFantasma();
if (k!=null) {
if (player.getComecoco()&&player.x==k.x&&Math.abs(player.y-k.y)<10) {
oops=true;
}
k=null; // Limpia var
}
/* Kill the pacman */
// if (oops&&!stopped) // TODO
if (oops)
Expand Down Expand Up @@ -346,16 +352,16 @@ else if (New==4)
g.setColor(Color.BLACK);
g.fillRect(0, 0, 600, 20);
puntuaciones(g);
/* If this was the last pellet */
/* If this was the last pellet */ // TODO
if (player.pelletsEaten==173)
{
/*Demo mode can't get a high score */
if (!demo)
{
if (player.currScore>highScore)
{
//updateScore(player.currScore);
}
// if (player.currScore>tB.getHighScore())
// {
// //updateScore(player.currScore);
// }
winScreen=true;
}
else
Expand Down Expand Up @@ -383,8 +389,10 @@ else if ( (player.pelletX!=lastPelletEatenX||player.pelletY!=lastPelletEatenY )|
/* Remplaza los pellets por donde pasan los amigos fantasmas*/
for (int i=0; i<l.size(); i++) {
Player a=(Player)l.get(i);
if (!a.getComecoco()&&tB.pellets[a.lastPelletX][a.lastPelletY])
// if (!a.getComecoco()&&tB.pellets[a.lastPelletX][a.lastPelletY])
if (!a.getComecoco()) {
fillPellet(a.lastPelletX, a.lastPelletY, g);
}
}
/*Draw the ghosts */
if (ghost1.frameCount<5)
Expand Down Expand Up @@ -525,7 +533,7 @@ public void fillPellet(int x, int y, Graphics g)
g.fillOval(x*20+28, y*20+28, 4, 4);
g.setColor(a); // Necesito restaurar el color
}
/* Devuelve el player de la lista segun el su nombre*/
/* Devuelve el player de la lista segun el su nombre */
public Player PlayerPropio(String nombre) throws Exception {
Player a=null;
for (int i=0; i<l.size(); i++) {
Expand All @@ -542,6 +550,16 @@ public Player PlayerPropio(String nombre) throws Exception {
throw new Exception(nombre+" no encontrado.");
}
}
/* Devuelve el player Fantasma */
public Player PlayerFantasma() {
for (int i=0; i<l.size(); i++) {
Player p=(Player)l.get(i);
if (!p.getComecoco()) {
return p;
}
}
return null;
}
/* Funcion para actualizar los datos del servidor y del player */
public void rmiUpdate() {
// Actualiza los datos del player en servidor
Expand Down Expand Up @@ -593,8 +611,8 @@ public void setAmigos(List<Player> a) {
l=a;
// Si es la primera ejecucion o son distintos se imprime la lista de usuarios
if (aumento||primeraEjecucion) {
imprimeListaAmigos();
if(primeraEjecucion) {
System.out.println("= Conectados =");
// Tambien asignamos un Player desde la lista al juego
try {
player=PlayerPropio(getNombre());
Expand All @@ -606,6 +624,7 @@ public void setAmigos(List<Player> a) {

}
}
imprimeListaAmigos();
}
/* Actualiza las pellets y los estados con la nueva informacion */
public void setTablero(Tablero a) {
Expand All @@ -625,10 +644,10 @@ public void setTablero(Tablero a) {
}
/* Imprime los amigos conectados */
public void imprimeListaAmigos() {
System.out.println("= Conectados =");
System.out.print("\r");
for (int i=0; i<l.size(); i++) {
Player p=(Player)l.get(i);
System.out.println(p.getNombre());
System.out.print(p.getNombre()+" "+p.getScore()+"\t ");
}
}
/* Actualiza los datos del player con la nueva informacion*/
Expand All @@ -648,15 +667,16 @@ public void puntuaciones(Graphics g) {
g.setColor(Color.YELLOW);
g.setFont(font);
if (demo)
g.drawString("DEMO MODE PRESS ANY KEY TO START A GAME\t High Score: "+highScore, 20, 10);
g.drawString("DEMO MODE PRESS ANY KEY TO START A GAME\t High Score: "+tB.getHighScore(), 20, 10);
else {
if(player.getComecoco()) {
g.drawString("Score: "+(player.currScore)+"\t High Score: "+highScore+" Eres Pacman!", 20, 10);
g.drawString("Score: "+(player.currScore)+"\t High Score: "+tB.getHighScore()+" Eres Pacman!", 20, 10);
}
else {
g.drawString("Score: "+(player.currScore)+"\t High Score: "+highScore+" Fantasma", 20, 10);
g.drawString("Score: "+(player.currScore)+"\t High Score: "+tB.getHighScore()+" Fantasma", 20, 10);
}
}
rmiUpdate(); // Actualiza con la nueva puntuacion
}
public void pintandoAmigos(List<Player> l, Graphics g) {
// Borra los Amigos
Expand Down
22 changes: 17 additions & 5 deletions ClientePac.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import java.io.PrintWriter;
import java.rmi.*;
import static java.lang.Thread.sleep;
class ClientePac {
static public void main (String args[]) {
// Si el cliente es un jar no tendrá cliente.permisos
// Hack para crear uno si no existe
PrintWriter out;
try
{
out=new PrintWriter("cliente.permisos");
out.println("grant { permission java.security.AllPermission; };");
out.close();
}
catch(Exception e) {
System.out.println("Error escribiendo archivo");
}
// Elimina la necesidad de permisos en linea de comando
System.setProperty("java.security.policy", "cliente.permisos");
// Elimina la capacidad de elegir puerto
Expand All @@ -14,12 +26,12 @@ static public void main (String args[]) {
System.setSecurityManager(new SecurityManager());
try {
ServicioPac srv=(ServicioPac) Naming.lookup("//"+args[0]+":"+puerto+"/Pacman");
// Crea un tablero local con el nombre del lugador
Board tablero=new Board(args[1]);
// Crea un tablero local con el nombre del lugador y el servidor
Board tablero=new Board(srv, args[1]);
// Agrega un player a la lista del servidor con el nombre
srv.crearPlayer(tablero.getNombre());
// Crea un juego de pacman local con el tablero y el servidor
new Pacman(srv, tablero);
// Crea un juego de pacman local con el tablero
new Pacman(tablero);
}
catch (RemoteException e) {
System.err.println("Error de comunicacion: "+e.toString());
Expand Down
32 changes: 12 additions & 20 deletions Pacman.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,11 @@ public class Pacman extends JApplet implements MouseListener, KeyListener
Board b;
/* This timer is used to do request new frames be drawn*/
javax.swing.Timer frameTimer;
/* Contendra el servicio servidor para actualizarle la posicion */
ServicioPac srv;
/* This constructor creates the entire game essentially */
public Pacman(ServicioPac server, Board boardNoSerializable)
public Pacman(Board board)
{
/* Asigno el servidor */
srv=server;
/* Movido para usar el player de red */
b=boardNoSerializable;
/* Asigna la lista de amigos conectados desde el servidor */
try {
b.setAmigos(srv.listaAmigos());
// System.out.println(srv.posicionPlayer(b.player));
}
catch (Exception er) {
System.err.println("Excepcion en la lista de amigos conectados");
er.printStackTrace();
}
b=board;
// Parte grafica //
b.requestFocus();
/* Create and set up window frame*/
Expand Down Expand Up @@ -94,9 +81,8 @@ public void stepFrame(boolean New)
{
// Envia la posicion actual al servidor, que la imprimirá
try {
srv.updatePlayer(b.player);
b.setAmigos(srv.listaAmigos());
// System.out.println(srv.posicionPlayer(b.player));
// Actualiza los datos del cliente
b.rmiUpdate();
}
catch (Exception er) {
System.err.println("Excepcion en la posicion o player");
Expand Down Expand Up @@ -200,8 +186,14 @@ else if (b.winScreen||b.overScreen)
b.player.currDirection='L';
b.player.direction='L';
b.player.desiredDirection='L';
b.player.x=200;
b.player.y=300;
b.player.x=200; // La x no cambia
// Impide la colision al crearse con el fantasma
if(b.PlayerFantasma()!=null&&b.PlayerFantasma().y==300) {
b.player.y=60;
}
else {
b.player.y=300;
}
b.ghost1.x=180;
b.ghost1.y=180;
b.ghost2.x=200;
Expand Down
45 changes: 38 additions & 7 deletions Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Player extends Mover implements java.io.Serializable
/* Which pellet the pacman is on top of */
int pelletX;
int pelletY;
/* Para cuando el jugador esta en modo fantasma */
int lastPelletX, lastPelletY;
/* teleport is true when travelling through the teleport tunnels*/
boolean teleport;
/* Stopped is set when the pacman is not moving or has been killed */
Expand All @@ -38,6 +40,8 @@ public Player(int x, int y, String nombre)
pelletsEaten=0;
pelletX=x/gridSize-1;
pelletY=y/gridSize-1;
lastPelletX=pelletX;
lastPelletY=pelletY;
this.lastX=x;
this.lastY=y;
this.x=x;
Expand Down Expand Up @@ -176,9 +180,9 @@ public void move()
/*Can only turn if we're in center of a grid*/
if (x %20==0&&y%20==0||
/* Or if we're reversing*/
(desiredDirection=='L'&&currDirection=='R') ||
(desiredDirection=='R'&&currDirection=='L') ||
(desiredDirection=='U'&&currDirection=='D') ||
(desiredDirection=='L'&&currDirection=='R')||
(desiredDirection=='R'&&currDirection=='L')||
(desiredDirection=='U'&&currDirection=='D')||
(desiredDirection=='D'&&currDirection=='U')
)
{
Expand Down Expand Up @@ -253,10 +257,24 @@ else if (y==9*gridSize&&x>max-gridSize*2)
/* Update what pellet the pacman is on top of */
public void updatePellet()
{
if (x%gridSize==0&&y%gridSize==0)
{
pelletX=x/gridSize-1;
pelletY=y/gridSize-1;
if (comecoco) {
if (x%gridSize==0&&y%gridSize==0)
{
pelletX=x/gridSize-1;
pelletY=y/gridSize-1;
}
}
else {
int tempX, tempY;
tempX=x/gridSize-1;
tempY=y/gridSize-1;
if (tempX!=pelletX||tempY!=pelletY)
{
lastPelletX=pelletX;
lastPelletY=pelletY;
pelletX=tempX;
pelletY=tempY;
}
}
}
/* Nombre del jugador */
Expand All @@ -282,4 +300,17 @@ public boolean getComecoco() {
public void setComecoco(boolean a) {
this.comecoco=a;
}
public void printState(boolean[][] state)
{
int num;
for(int i=0; i<20; i++)
{
for(int j=0; j<20; j++)
{
num=(this.state[i][j]) ? 1 : 8;
System.out.print(num+" ");
}
System.out.print("\n");
}
}
}
4 changes: 4 additions & 0 deletions ServicioPac.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ interface ServicioPac extends Remote {
public void crearPlayer(String nombre) throws RemoteException;
public String posicionPlayer(Player jugador) throws RemoteException;
public void updatePlayer(Player jugador) throws Exception;
public void updateTablero(Tablero a) throws Exception;
public Player cualPlayer(String nombre) throws Exception;
public void promocionaComecocos() throws Exception;
public List<Player> listaAmigos() throws Exception;
public void imprimeListaAmigos() throws Exception;
public Tablero getTablero() throws Exception;
public void updateScore(Player aspirante) throws Exception;
}

Loading

0 comments on commit 9e138f7

Please sign in to comment.