Hello, I’m currently making a game. I’m using a tile map and am trying to get lighting and shadows working for it.
It currently looks like this -
And the bit which controls the lighting is the following class -
package kingdom.blue;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import java.awt.Color;
public final class map{
public static final int WIDTH = 100;
public static final int HEIGHT = 12;
public static final int MAPLEVEL = 2;
public static final int TILE_SIZE_X = 50;
public static final int TILE_SIZE_Y = 50;
public BufferedImage[][] data = new BufferedImage[WIDTH][HEIGHT];
public Color [] [] lightMap = new Color [WIDTH][HEIGHT];
public LoadMapImages lmi;
public int mapPosX;
public int mapPosY;
public double round = 0.5;
boolean mapLoaded;
public void renderMap(){
for(int x = 0;x < WIDTH;x++){
for(int y = 0;y < HEIGHT;y++){
data[x][y] = lmi.ground5;
data[7][HEIGHT-8] = lmi.ground3;
data[8][HEIGHT-8] = lmi.ground1;
data[9][HEIGHT-8] = lmi.ground2;
data[x][HEIGHT-1] = lmi.ground1;
data[6][HEIGHT-4] = lmi.ground2;
data[5][HEIGHT-4] = lmi.ground3;
data[12][HEIGHT-8] = lmi.ground2;
data[11][HEIGHT-8] = lmi.ground3;
//data[15][HEIGHT-4] = lmi.ground2;
//data[14][HEIGHT-4] = lmi.ground3;
data[21][HEIGHT-4] = lmi.ground2;
data[20][HEIGHT-4] = lmi.ground3;
data[24][HEIGHT-6] = lmi.ground2;
data[23][HEIGHT-6] = lmi.ground3;
data[30][HEIGHT-3] = lmi.ground2;
data[29][HEIGHT-3] = lmi.ground3;
//data[21][HEIGHT-11] = lmi.ground1;
data[21][HEIGHT-9] = lmi.ground1;
//data[22][HEIGHT-11] = lmi.ground1;
data[22][HEIGHT-9] = lmi.ground1;
//data[20][HEIGHT-11] = lmi.ground1;
data[20][HEIGHT-9] = lmi.ground1;
//data[20][HEIGHT-10] = lmi.ground1;
//data[22][HEIGHT-10] = lmi.ground1;
data[21][HEIGHT-10] = lmi.torch;
//data[24][HEIGHT-7] = lmi.torch;
lightMap [x][y] = new Color(0,0,0,200);
}
}
}
public map(Player player){
lmi = new LoadMapImages();
}
public void paint(Graphics2D g)
{
//g.setColor(Color.GRAY);
for (int x = 0; x < WIDTH; x++){
for (int y = 0; y < HEIGHT; y++)
{
g.setColor(lightMap[x][y]);
g.drawImage(data[x][y],mapPosX + x*TILE_SIZE_X, mapPosY + y*TILE_SIZE_Y, TILE_SIZE_X, TILE_SIZE_Y, null);
g.fillRect(mapPosX + x*TILE_SIZE_X,mapPosY + y*TILE_SIZE_Y, TILE_SIZE_X, TILE_SIZE_Y);
//System.out.println("map drawing" + lightMap[(int)((x+70)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y]);
}
}
}
public void scrollMapY (int scrAmount, int y, int x, int cy){
mapPosY += scrAmount;
mapPosY += scrAmount;
}
public void lightMap (int plrY, int plrX){
int baseDarkness;
//TORCH LIGHT STUFF <to be placed in own file?>
for (int x = 0; x < WIDTH; x++){
for (int y = 0; y < HEIGHT; y++){
if(data[x][y] == lmi.torch){
baseDarkness = lightMap [x+10][y].getAlpha();
int dispersalRate = baseDarkness/10;
for(int xl = 0; xl < 7; xl++){
for(int yl = 0; yl < 8; yl++){
if(y+yl < HEIGHT){
if(y+yl > -1){
lightMap [x][y] = new Color (0,0,0,10);
lightMap [x+1][y] = new Color (0,0,0,15);
lightMap [x-1][y] = new Color (0,0,0,15);
lightMap [x][y+1] = new Color (0,0,0,15);
lightMap [x][y-1] = new Color (0,0,0,15);
if(((dispersalRate*xl)+(dispersalRate*yl)) < 200){
lightMap [x+xl][y+yl] = new Color (0,0,0,((dispersalRate*xl)+(dispersalRate*yl)));
lightMap [x-xl][y+yl] = new Color (0,0,0,((dispersalRate*xl)+(dispersalRate*yl)));
}
else{
lightMap [x+xl][y+yl] = new Color (0,0,0,200);
lightMap [x-xl][y+yl] = new Color (0,0,0,200);
}
}
}
if(y-yl < HEIGHT){
if(y-yl > -1){
if(((dispersalRate*xl)+(dispersalRate*yl)) <= 200){
lightMap [x-xl][y-yl] = new Color (0,0,0,((dispersalRate*xl)+(dispersalRate*yl)));
lightMap [x+xl][y-yl] = new Color (0,0,0,((dispersalRate*xl)+(dispersalRate*yl)));
}
else{
lightMap [x-xl][y-yl] = new Color (0,0,0,200);
lightMap [x+xl][y-yl] = new Color (0,0,0,200);
}
}
}
}
}
}
}
}
}
public boolean blocked(int x,int y,int dir){
try{
//if(y <= cy){
//------------------------------------------------------------------------------------->
if(dir == 0){
if(data[(int)((x+70)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground1){
return true;
}
if(data[(int)((x+70)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground2){
return true;
}
if(data[(int)((x+70)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground3){
return true;
}
if(data[(int)((x+150)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground1){
return true;
}
if(data[(int)((x+150)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground2){
return true;
}
if(data[(int)((x+150)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground3){
return true;
}
return false;
}
else if(dir == 1){
if(data[(int)((x+20)-mapPosX)/TILE_SIZE_X][(int)((y+150) -mapPosY)/TILE_SIZE_Y] == lmi.ground1){
return true;
}
if(data[(int)((x+20)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground2){
return true;
}
if(data[(int)((x+20)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground3){
return true;
}
if(data[(int)((x+100)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground1){
return true;
}
if(data[(int)((x+100)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground2){
return true;
}
if(data[(int)((x+100)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground3){
return true;
}
return false;
}
}
catch(Exception es){
}
return false;
}
}
If anyone can help me get shadows working so that blocks can produce shadows, or help me implement a better lighting system it would be much appreciated, It’s my first real game so I’m not quite sure how to go about it…