Archive Forums (lite version).  Gaming, Game Development, and Anime community!


PDA

View Full Version : Yet another RPG Engine.


neozf
03-19-2008, 06:37 PM
Hmm, this is the third one so far isn't it.
So far I've got it so you can move around, you can change maps by walking off the side of one (Which map leads to which is defined in a file). I'm going to add a piece of code soon so that you can't walk on things like tiles etc.
The code is currently quite messy and needs to be cleaned up I know.
And I might start working on the Map Maker soon.

import java.applet.*;
import java.awt.*;
import java.io.*;
public class Test extends Applet implements Runnable
{
//Map array and tiles
private int[][] map = new int[5][5];
private int[]exit = new int[4];
private Image tileGrass;
private Image tileWall;
private Image player;
private int locX=2;
private int locY=2;
private int oldLocX;
private int oldLocY;
private int gridX=100;
private int gridY=100;
private int mapName=1;
private int mapDat =1;

public void init()
{
setBackground(Color.black);
}
public void start()
{
//map=new int[5][5];
mapLoad();
tileGrass=getImage(getCodeBase(),"grass.GIF");
tileWall=getImage(getCodeBase(),"wall.GIF");
player=getImage(getCodeBase(),"player.PNG");
Thread thread = new Thread(this);
thread.start();
}
public void stop(){}
public void destory(){}
public void mapLoad()
{
try{
FileReader map = new FileReader("Maps\\"+mapName+".map");
FileReader dat = new FileReader("Maps\\"+mapDat+".dat");
BufferedReader buffer = new BufferedReader(map);
BufferedReader buffer2 = new BufferedReader(dat);
String mapTile=null;
String mapData=null;
int x=0; int y=0;
while ((mapTile=buffer.readLine())!=null)
{
mapBuild(x,y,Integer.parseInt(mapTile));
x++;
if(x==5){y++;x=0;}
}
x=0;
while ((mapData=buffer2.readLine())!=null)
{
exitBuild(x,Integer.parseInt(mapData));
x++;
}
}
catch (Exception e){}
}
public void mapBuild(int x,int y,int tile){ map[x][y]=tile; }
public void exitBuild(int x,int exitLoc){ exit[x]=exitLoc; }
public void mapChange(char direction)
{
if(direction=='N') {mapName=exit[0]; mapDat=exit[0];}
if(direction=='E') {mapName=exit[1]; mapDat=exit[1];}
if(direction=='S') {mapName=exit[2]; mapDat=exit[2];}
if(direction=='W') {mapName=exit[3]; mapDat=exit[3];}
mapLoad();
}
public void run()
{
Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
while(true)
{
repaint();

try
{
Thread.sleep(20);
}
catch(InterruptedException ex){}
Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
}

}

public void update (Graphics g)
{
paint(g);
}

public boolean keyDown(Event e, int key)
{
oldLocX=locX;
oldLocY=locY;
if(key==Event.LEFT){locX-=1;}
if(key==Event.RIGHT){locX+=1;}
if(key==Event.UP){locY-=1;}
if(key==Event.DOWN){locY+=1;}



if(locX<0&&exit[3]!=0){mapChange('W');locX=4;}else if(locX<0&&exit[3]==0) locX=oldLocX;
if(locX>4&&exit[1]!=0){mapChange('E');locX=0;}else if(locX>4&&exit[1]==0) locX=oldLocX;
if(locY<0&&exit[0]!=0){mapChange('N');locY=4;}else if(locY<0&&exit[0]==0) locY=oldLocY;
if(locY>4&&exit[2]!=0){mapChange('S');locY=0;}else if(locY>4&&exit[2]==0) locY=oldLocY;
if(map[locX][locY]==2) {locX=oldLocX;locY=oldLocY;}
return true;
}

public void paint(Graphics g)
{
Image tile=tileWall;
for (int x=0;x!=5;x++)
{
for (int y=0;y!=5;y++)
{
if(map[x][y]==1) tile = tileGrass;
if(map[x][y]==2) tile = tileWall;
g.drawImage (tile,x*50+gridX,y*50+gridY,this);
}
}
g.drawImage(player,locX*50+gridX,locY*50+gridY,thi s);
}


}Edit: Changed the way it loads the maps and changed it so you can't walk on walls anymore.

Google
--

Walrii
03-19-2008, 11:44 PM
Good job :)

This reminds me of a sort of program I fiddled with back in the day (in QBasic no less!). You had to reach the goal, but there were hazards in the way.

For example, to swim across the river you had to find the goggles / snorkel first and so on.

I wish I had that code...

Travis
03-20-2008, 12:30 AM
Chip's Challenge much, Walrii?

Snif
03-20-2008, 11:10 AM
Now for some BATTLES! Good job Neo.

Insane2757
03-20-2008, 12:20 PM
Chip's Challenge much, Walrii?

YES. Very yes.

neozf
03-21-2008, 08:04 AM
Lol, Snif: For how Imma just finish getting the basics done, maybe add a few more tiles, then try working on the battle part of it :p

neozf
03-23-2008, 07:38 PM
For some reason my Map Maker won't save files, yet the test I made which uses the exact same code works.
The problem is in the saveMap function.
import java.applet.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class mapMaker extends Applet implements Runnable
{
//CODE
private int[][] map = new int[5][5];
private int[]exit = new int[4];
private Image tileGrass;
private Image tileWall;
private Image tileBlank;
private Image arrowLeft;
private Image arrowRight;
private Image iconSave;
private Image iconLoad;
private int currentTile=1;
private int maxTile=2;

public void init()
{
setBackground(Color.black);
}

public void start()
{
tileGrass=getImage(getCodeBase(),"grass.GIF");
tileWall=getImage(getCodeBase(),"wall.GIF");
tileBlank=getImage(getCodeBase(),"blank.GIF");
arrowLeft=getImage(getCodeBase(),"arrowL.GIF");
arrowRight=getImage(getCodeBase(),"arrowR.GIF");
iconSave=getImage(getCodeBase(),"save.GIF");
iconLoad=getImage(getCodeBase(),"load.GIF");
Thread thread = new Thread(this);
thread.start();
}
public void stop(){}
public void destory(){}

public void run()
{
Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
while(true)
{
repaint();

try
{
Thread.sleep(20);
}
catch(InterruptedException ex){}
Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
}
}

public boolean mouseDown(Event e, int x, int y)
{
if(x>=100&&y>=100&&x<=350&&y<=350)
{
map[(x-100)/50][(y-100)/50]=currentTile;
}
if(x>=150&&y>=0&&x<=200&&y<=50)
{
currentTile--;
if(currentTile<1) currentTile=maxTile;
}
if(x>=250&&y>=0&&x<=300&&y<=50)
{
currentTile++;
if(currentTile>maxTile) currentTile=1;
}
if(x>=350&&y>=0&&x<=400&&y<=50)
{
String mapNum = JOptionPane.showInputDialog("Input the Map number");
int mapNumber=Integer.parseInt(mapNum);
//saveMap(mapNumber);
for(int a=0;a!=5;a++)
{
for(int b=0;b!=5;b++)
{
map[a][b]=1;
}
}
saveMap(6);
}
if(x>=450&&y>=0&&x<=500&&y<=50)
{
String mapNum = JOptionPane.showInputDialog("Input the Map number");
int mapNumber=Integer.parseInt(mapNum);
loadMap(mapNumber);
}
return true;
}

public void saveMap(int mapNumber)
{

try
{
FileOutputStream mapOut;
File map= new File("Maps\\"+mapNumber+".map");
mapOut=new FileOutputStream(map);
PrintStream p;
p=new PrintStream(mapOut);
int x=0; int y=0;
while(y!=5)
{
//System.out.println(x+ ","+y+":"+mapPrint(x,y));
p.println(mapPrint(x,y));
x++;
if(x==5) {y++;x=0;}
}
p.close();
}
catch(Exception e){}
}
public int mapPrint(int x, int y)
{
return map[x][y];
}
public void loadMap(int mapNumber)
{
try{
FileReader map = new FileReader("Maps\\"+mapNumber+".map");
FileReader dat = new FileReader("Maps\\"+mapNumber+".dat");
BufferedReader buffer = new BufferedReader(map);
BufferedReader buffer2 = new BufferedReader(dat);
String mapTile=null;
String mapData=null;
int x=0; int y=0;
while ((mapTile=buffer.readLine())!=null)
{
mapBuild(x,y,Integer.parseInt(mapTile));
x++;
if(x==5){y++;x=0;}
}
x=0;
while ((mapData=buffer2.readLine())!=null)
{
exitBuild(x,Integer.parseInt(mapData));
x++;
}
}
catch (Exception e){}
}

public void mapBuild(int x,int y,int tile)
{
map[x][y]=tile;
}
public void exitBuild(int x, int exitDir)
{
exit[x]=exitDir;
}

public void update(Graphics g)
{
paint(g);
}

public void paint(Graphics g)
{
Image tile=tileBlank;
g.setColor (Color.white);
g.drawRect (100,100,250,250);
for (int x=0;x!=5;x++)
{
for (int y=0;y!=5;y++)
{
if(map[x][y]==0) tile = tileBlank;
if(map[x][y]==1) tile = tileGrass;
if(map[x][y]==2) tile = tileWall;
g.drawImage (tile,x*50+100,y*50+100,this);
}
}
g.drawImage(arrowLeft,150,0,this);
if(currentTile==1)g.drawImage(tileGrass,200,0,this );
if(currentTile==2)g.drawImage(tileWall,200,0,this) ;
g.drawImage(arrowRight,250,0,this);

g.drawImage(iconSave,350,0,this);
g.drawImage(iconLoad,450,0,this);
}



}Test class codeimport java.io.*;
public class test3
{
int map[][]=new int[5][5];
public void mapMake()
{
for(int x=0;x!=5;x++)
{
for(int y=0;y!=5;y++)
{
map[x][y]=1;
}
}
saveMap(5);
}
public void saveMap(int mapNumber)
{
try
{
FileOutputStream mapOut;
File map= new File("Maps\\"+mapNumber+".map");
mapOut=new FileOutputStream(map);
PrintStream p;
p=new PrintStream(mapOut);
int x=0; int y=0;
while(y!=5)
{
System.out.println(x+ ","+y+":"+mapPrint(x,y));
p.println(mapPrint(x,y));
x++;
if(x==5) {y++;x=0;}
}
p.close();
}
catch(Exception e){}
}
public int mapPrint(int x, int y)
{
return map[x][y];
}
}Can anyone work out what might be wrong with it?

Dazed_n_Confused
03-24-2008, 02:57 AM
Did you forget to put saveMap(mapNumber); back in?
Also, you realise that isn't saving whatever map you build right? It saves an all grass map.
I'm assuming this was just leftovers from debugging and you know about that, but I don't see anything else that would make it not work. How did you run your test class?

At any rate, you should probably use 'else if' for that block of 'if' statements in the mouseDown event.

neozf
03-24-2008, 05:59 AM
Yea I realise I haven't put saveMap(mapNumber); back in and that it saves an all grass map, that was to create the exact same thing it did in the test since it worked there...
For the test class I just created an object of it, then ran mapMake() and it saves completely fine.

Walrii
03-24-2008, 10:11 PM
I looked at it... and it looks like it should work :(

Maybe though, if you're running the applet within a web browser (or maybe an applet launcher) Java won't give it read/write permissions to files. Though, if you run it from the command line, then it probably will be given read/write permissions.

That's my only thought.

neozf
03-25-2008, 04:05 AM
Maybe... I even tried having it use another class to save the file :/

Sim9
03-25-2008, 10:32 PM
An exception may be generated in the catch(Exception e){} code which could give more insight into the problem :)