import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;
import javax.swing.*;
//-----------------------------------------------------------------
//LevelGen.java
//
// This class will extract level information from Level.java, create a two dimensional array to be
// drawn onto the screen for our level.
//-----------------------------------------------------------------
public class LevelGen extends JPanel {
final int MAP_ROWS = 5;
final int MAP_COLUMNS = 5;
final int IMG_WIDTH = 64;
final int IMG_HEIGHT = 64;
String line1 = "dbgbs";
String line2 ="bbsgg";
String line3 = "gggbg";
String line4 = "bbbss";
String line5 = "bsbgg";
String strMap = line1 + line2 + line3 + line4 + line5;
Toolkit toolkit = getToolkit();
int numStone =0;
int numBomb = 0;
int numGem = 0;
int dozerX = 64;
int dozerY = 64;
private Image imgStone = null;
private Image imgGem = null;
private Image imgBomb = null;
private Image imgDirt = null;
private ImageIcon imgDozer = new ImageIcon("dozer.png");
int x = 0;
int y = 0;
Block[][] map = new Block[MAP_ROWS][MAP_COLUMNS];
public LevelGen()
{
fillMap();
imgStone = toolkit.createImage("stone.png");
imgGem = toolkit.createImage("gem.png");
imgBomb = toolkit.createImage("bomb.png");
imgDirt = toolkit.createImage("dirt.png");
setBackground(Color.black);
setFocusable(true);
addKeyListener (new DirectionListener());
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
Color transparent = new Color(0, 0, 0, 0);
for (int i = 0; i < MAP_ROWS ; i++){
y = (i*IMG_HEIGHT);
for (int j = 0; j < MAP_ROWS ; j++)
{
// checks if block is supposed to be visible
if (map[i][j].isVisible())
{
//char type = strMap.charAt(count);
char type = map[i][j].getType();
x = (j*IMG_WIDTH);
switch ((char)type)
{
case 'S':
//draw new stone image
g.drawImage(imgStone, x, y, transparent, this);
break;
case 'B':
// draw new bomb image
g.drawImage(imgBomb, x, y, transparent, this);
break;
case 'G':
// draw new gem image
g.drawImage(imgGem, x, y, transparent, this);
break;
case 'D':
// draw dozer;
imgDozer.paintIcon(this, g, dozerX, dozerY);
default:break;
}
}
else
{
g.setColor(Color.black);
g.fillRect(x, y, IMG_HEIGHT, IMG_WIDTH);
}
printArray();
}
}
// retrieve value from strMap and convert it into line by line
// information about what tile to place in each array field
//map[i][j] = strMap.charAt(count);
}
public void fillMap ()
{
int count = 0;
for (int i = 0; i < MAP_ROWS ; i++){
y = 0;
y = y + (i*IMG_HEIGHT);
for (int j = 0; j < MAP_ROWS ; j++)
{
map[i][j] = new Block();
char a = strMap.charAt(count);
x = 0;
x = x + (j*IMG_WIDTH);
map[i][j].setVisible(true);
switch ((char)(a))
{
case 's':
//fill array at i & j with stone values
map[i][j].setPos(x, y);
map[i][j].setType("Stone");
break;
case 'b':
// draw new bomb image
map[i][j].setPos(x, y);
map[i][j].setType("Bomb");
break;
case 'g':
// draw new gem image
map[i][j].setPos(x, y);
map[i][j].setType("Gem");
break;
case 'd':
map[i][j].setPos(x, y);
map[i][j].setType("Dozer");
dozerX = x;
dozerY = y;
default:break;
}
count++;
}
}
}
/* public void calcElements ()
{
int count = 0;
char a = strMap.charAt(count);
for (int i = 0; i < strMap.length() ; i++)
{
switch ((char)(a))
{
case 's':
numStone++;
break;
case 'g':
numGem++;
break;
case 'b':
numBomb++;
//break;
default:break;
}
}
}
*/
//
private class DirectionListener implements KeyListener
{
public void keyPressed (KeyEvent e)
{
int i, j;
i =0;
j =0;
switch (e.getKeyCode())
{
case KeyEvent.VK_UP:
if(dozerY >= IMG_HEIGHT)
{
dozerY = dozerY - IMG_HEIGHT;
i = dozerY - IMG_HEIGHT;
i = i / IMG_HEIGHT;
j = dozerX / IMG_WIDTH;
map[i][j].setVisible(false);
repaint();
}
break;
case KeyEvent.VK_DOWN:
if (dozerY < (HEIGHT - IMG_HEIGHT))
{
dozerY = dozerY + IMG_HEIGHT;
i = dozerY + IMG_HEIGHT;
i = i / IMG_HEIGHT;
j = j / IMG_WIDTH;
map[i][j].setVisible(false);
repaint();
printArray();
}
break;
case KeyEvent.VK_LEFT:
if (dozerX >= IMG_WIDTH){dozerX= dozerX- IMG_WIDTH;}
break;
case KeyEvent.VK_RIGHT:
if(dozerX < (WIDTH-IMG_WIDTH)){dozerX += IMG_WIDTH;}
break;
}
}
public void keyTyped (KeyEvent e) {}
public void keyReleased (KeyEvent e) {}
}
public void printArray ()
{
for (int i = 0; i < MAP_ROWS ; i++)
{
for (int j = 0; j < MAP_COLUMNS ; j++)
{
System.out.print("" + map[i][j].getType());
System.out.println("" + map[i][j].getPos());
}
System.out.println("");
}
}
}