This is my first Roguelike. It's my first serious program, really. Up until now, I've only made "Hello World!" type stuff. I know that this is probably not the best thing to tackle first off, but I couldn't think of anything else. It's programmed in java. It's also not done yet. All I have implemented so far its some basic tracking of the world, a very simple start on having creatures, and the renderer and display. The reason I'm posting on the board is because my code is getting a little sloppy and I'd like some advice on how to clean it up before I implement anything more. The big thing thats bothering me is the Renderer's render function. It does what it's supposed to, but its become a giant block of code. Is there a way to slim it down, or at least make it more readable?
You may notice that the world tracking system is a little weird. The way it works is that the WorldManager tracks the information that is relevant across the setting, while the Areas represent buildings or compounds. The LocalAreas represent individual rooms, and are where the characters physical presence is tracked. Instead of having a giant array or something, the LocalAreas each remember what other LocalAreas border them. This allows me to do interesting things, like have a gentle slope that causes you to walk through a tunnel and come out at a higher point than you went in. Or have a terrible desert where you always seem to walk in circles, no matter how certain you are that you took a straight path. anyway, here are the files. I'm also open to any other criticism or advice you may have.
package roguelike;
import java.awt.Point;
import java.util.*;
/**
*
* @author Angle
*/
public class Area {
private HashSet<LocalArea> subs;
private HashSet<Entity> occupants;
public Area (int size, int dangerLevel) {
subs = new HashSet<LocalArea> ();
occupants = new HashSet<Entity> ();
}
public void addLocalArea(LocalArea l){
subs.add(l);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package roguelike;
import java.awt.Point;
/**
*
* @author Angle
*/
public class Character extends Entity{
public Character(LocalArea l){
coords = new Point(6,6);
location = l;
}
public Character(Area a){
}
}
package roguelike;
import java.awt.Canvas;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
*
* @author Angle
*/
public class Display extends Canvas{
private Font font = new Font("monospaced", Font.PLAIN, 16);
public Display(){
}
public void paint(Graphics g) {
g = (Graphics2D)g;
g.setFont(font);
String string = new String();
int i, j;
for(i=0; i<Renderer.getRenderer().getDimension().height; i++){
for (j=0; j<Renderer.getRenderer().getDimension().width; j++){
string += Renderer.getRenderer().getDisplay(j,i);
}
g.drawString(string, (1), (i*20)+20);
string = "";
}
return;
}
}
package roguelike;
import java.awt.Point;
/**
*
* @author Angle
*/
public abstract class Entity {
LocalArea location;
Point coords;
public void move(int x, int y){
coords.x += x;
coords.y += y;
}
public LocalArea getLocation(){
return location;
}
public Point getCoords(){
return coords;
}
public void setLocation(LocalArea l){
location = l;
}
public void setCoords(Point p){
coords = p;
}
}
package roguelike;
import java.awt.Dimension;
import java.awt.Point;
import java.util.HashSet;
/**
*
* @author Angle
*/
public class LocalArea {
//This is the map of the basic terrain in the LocalArea, e.g. walls, floors, doors, ramps, stairs, open space, etc.
private char[][] terrain;
private HashSet<BorderArea> borders;
private Dimension dimension;
public class BorderArea {
LocalArea borderArea;
Point relCoord;
int rotation;
int elevation;
boolean mirrored;
public BorderArea(LocalArea l, Point p) {
borderArea = l;
relCoord = p;
rotation = 0;
elevation = 0;
mirrored = false;
borders.add(this);
return;
}
public LocalArea getLocalArea(){
return borderArea;
}
public Point getRelCoord(){
return relCoord;
}
public int getRelX(){
return relCoord.x;
}
public int getRelY(){
return relCoord.y;
}
}
public LocalArea (int width, int height) {
terrain = new char[width][height];
borders = new HashSet<BorderArea>();
dimension = new Dimension(width, height);
for (int i=0; i<width; i++){
for (int j=0; j<height; j++) {
terrain[i][j] = '.';
}
}
}
public void addBorder(LocalArea l, Point p){
new BorderArea(l, p);
}
public HashSet<BorderArea> getBorders(){
return borders;
}
public char getCoord(Point p){
return terrain[p.x][p.y];
}
public char[][] getTerrain(){
return terrain;
}
public int getWidth(){
return dimension.width;
}
public int getHeight(){
return dimension.height;
}
}
package roguelike;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Angle
*/
public class Main extends Panel {
public Main () {
setLayout (new BorderLayout ());
add(new Display());
if ( WorldManager.getWorldManager() != null) {
Renderer.getRenderer().render(Player.getPlayer().getCharacter());
}
}
/**
* @param args the command line arguments
*/
public static void main (String[] args) {
Frame frame = new Frame ("Roguelike");
frame.add (new Main ());
frame.addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent e) {
System.exit (0);
}
});
frame.pack ();
frame.setVisible (true);
}
}
package roguelike;
/**
*
* @author Angle
*/
public class Player {
private Character character;
private static Player player;
public void awaitInput(){
}
public static Player getPlayer(){
if (player == null){
player = new Player();
}
return player;
}
Player(){
}
public void setCharacter(LocalArea l){
character = new Character(l);
}
public Character getCharacter(){
return character;
}
}
package roguelike;
import java.awt.*;
/**
*
* @author Angle
*/
public class Renderer
{
//display is the variable that holds the area seen by the Player.
private char[][] display;
private Dimension dimension;
private static Renderer renderer;
private Renderer(){
setDimension(50, 50);
}
public static Renderer getRenderer(){
if (renderer == null){
renderer = new Renderer();
}
return renderer;
}
private void setDimension(int width, int height) {
display= new char[width][height];
for (int i=0; i<height; i++){
for (int j=0; j<width; j++){
display[j][i] = ' ';
}
}
dimension = new Dimension(width, height);
}
public Dimension getDimension() {
return dimension;
}
public char getDisplay(int a, int b){
return display[a][b];
}
public void render(Character c){
LocalArea l = c.getLocation();
Point o = new Point(0,0);
int i, j, n, m;
for (i=((dimension.height/2)-(l.getHeight()/2)), n = 0; i<((dimension.height/2)+(l.getHeight()/2)); i++,n++)
{
try {
for (j=((dimension.width/2)-(l.getWidth()/2)), m = 0; j<((dimension.width/2)+(l.getWidth()/2)); j++, m++)
{
o.setLocation(n, m);
display[j][i] = l.getCoord(o);
}
} catch (Exception e){}
}
o = c.getCoords();
display[((dimension.width/2)-(l.getHeight()/2))+o.x][((dimension.width/2)-(l.getWidth()/2))+o.y]= '@' ;
for (LocalArea.BorderArea a: l.getBorders())
{
LocalArea la=a.getLocalArea();
for (i=((dimension.height/2)-((la.getHeight()/2)+a.getRelY())), n = 0; i<((dimension.height/2)+(la.getHeight()/2)-a.getRelY()); i++,n++)
{
try {
for (j=((dimension.width/2)-((la.getWidth()/2)+a.getRelX())), m = 0; j<((dimension.width/2)+(la.getWidth()/2)-a.getRelX()); j++, m++)
{
o.setLocation(n, m);
display[j][i] = la.getCoord(o);
}
} catch (Exception e){}
}
}
}
}
package roguelike;
import java.awt.Point;
import java.util.*;
/**
*
* @author Angle
*/
public class WorldManager {
private HashSet<Area> areas;
private static WorldManager worldmanager;
private WorldManager () {
areas = new HashSet<Area> ();
Area startArea = new Area(1, 1);
areas.add(startArea);
LocalArea localArea = new LocalArea(12,12);
/*this next block goes through clockwise and sets the localArea to have
*itself as a border 6 times in a honeycomb pattern.
*/
localArea.addBorder(localArea, new Point(-6, 12));
localArea.addBorder(localArea, new Point(-12, 0));
localArea.addBorder(localArea, new Point(-6, -12));
localArea.addBorder(localArea, new Point(6, -12));
localArea.addBorder(localArea, new Point(12, 0));
localArea.addBorder(localArea, new Point(6, 12));
startArea.addLocalArea(localArea);
Player.getPlayer().setCharacter(localArea);
}
public static WorldManager getWorldManager(){
if (worldmanager == null){
worldmanager = new WorldManager();
}
return worldmanager;
}
}
Edit - Whoah didn't mean to post yet. hold please.
Edit2 - I really need to be more careful about pressing the right ctl-key. I just reset the thing and lost my whole huge rant. aargh!!
Edit3 - Finally Wote everything. I think.