package game; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import myutils.MyColor; import myutils.MyPosition; import myutils.MyRandom; /** * Map Generator */ public class MapGenerator { private BufferedImage mMapImage; private MyRandom mMyRandom; /** * Gets */ public BufferedImage getMapImage() { return mMapImage; } /** * Initialization */ public MapGenerator (MyRandom pMyRandom) { mMyRandom = pMyRandom; } /** * Generate map */ public void generateMap (int pWidth, int pHeight, BufferedImage pBaseImg, BufferedImage pUpImg, BufferedImage pDownImg) { // ----- Map generation ----- mMapImage = new BufferedImage(pWidth, pHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D mG = mMapImage.createGraphics(); // Hill terrain generation TerrainGenerator mTerrain = new TerrainGenerator (mMyRandom); mTerrain.generate(); // Put the generated hills into the the buffered image (in black) int mHeightTerrain = (int) mMyRandom.getRandInt (400, 600); for( int x = 0; x < mTerrain.getSize(); ++x ) { float z = mTerrain.getCell (x, 128); column (mMapImage, x, mHeightTerrain - (int) (z * (pHeight / 2)), pHeight); } // Put some random staff into the buffered image (in black) mG.setColor(Color.black); for (int i = 0; i < mMyRandom.getRandInt(3, 15); i ++) { int mX = (int) mMyRandom.getRandInt(0, pWidth); int mY = (int) mMyRandom.getRandInt(40, pHeight / 2); int mSizeX = (int) mMyRandom.getRandInt(40, 120); int mSizeY = (int) mMyRandom.getRandInt(40, 120); mG.fillOval (mX, mY, mSizeX, mSizeY); for (int j = 0; j < mMyRandom.getRandInt(1, 4); j ++) { mG.fillOval((int) mMyRandom.getRandInt(mX - 20, mX + 20), (int) mMyRandom.getRandInt(mY + 20, mY + 60), (int) mMyRandom.getRandInt(mSizeX - 10, mSizeX + 20), (int) mMyRandom.getRandInt(mSizeY - 10, mSizeY + 20)); } } // Texturize the map texturize (mMapImage, pBaseImg, pUpImg, pDownImg); } /** * Texturize the map */ private void texturize (BufferedImage pSource, BufferedImage pBaseImg, BufferedImage pUpImg, BufferedImage pDownImg) { int mX, mY; // Pixel position int mX2, mY2; // Pixel position for source base texture int mH; // Pixel position in y (for drawing "up" and "down" images) int mAlt; // Height of a section of terrain (for avoiding texturizing out of range) int mUntil; // For adjusting the texturize column in sections of height int mR, mG, mB, mA; // Color int mC; // Color int mWidthTexWay = 0; // X position in a "up" or "down" image // ----- Texturize with the "base" texture ----- mX2 = 0; mY2 = 0; for (mX = 0; mX < pSource.getWidth(); mX ++) { mY2 = 0; for (mY = 0; mY < pSource.getHeight(); mY ++) { // Get the alpha value of the pixel mC = pSource.getRGB (mX, mY); mA = MyColor.getA(mC); // It it is part of the terrain texturize with "base" texture if (mA != 0) { // Get the pixel color of the base texture mC = pBaseImg.getRGB (mX2, mY2); mR = MyColor.getR(mC); mG = MyColor.getG(mC); mB = MyColor.getB(mC); // Put the pixel into the source pSource.setRGB (mX, mY, mC); } mY2++; if (mY2 > pBaseImg.getHeight() - 1) mY2 = 0; } mX2++; if (mX2 > pBaseImg.getWidth() - 1) mX2 = 0; } // ----- Texturize with the "down" texture ----- for (mY = 0;mY < pSource.getHeight() - 1; mY ++) { for (mX = 0; mX < pSource.getWidth(); mX ++) { // Get the alpha value of the pixel mC = pSource.getRGB (mX, mY); mA = MyColor.getA(mC); // If it is part of the terrain... if (mA != 0) { // Get the DOWN pixel alpha value mC = pSource.getRGB (mX, mY + 1); mA = MyColor.getA(mC); // If this pixel is not terrain then texturize with "up" texture if (mA == 0) { // We have to take care of the heigh and texturize as needed mAlt = 0; mA = 1; while (mA != 0 && (mY - mAlt) > 0) { // Get the alpha value of the pixel mC = pSource.getRGB (mX, mY - mAlt); mA = MyColor.getA(mC); // Increment the height mAlt++; } // Adjusting mUntil if (mAlt < pDownImg.getHeight()) mUntil = mAlt / 2; else mUntil = pDownImg.getHeight(); // We draw a column of "down" texture in up direction for (mH = 0; mH < mUntil; mH++) { if (mWidthTexWay > pDownImg.getWidth () - 1) mWidthTexWay = 0; // Get the pixel of the "down" texture mC = pDownImg.getRGB (mWidthTexWay, mH); mR = MyColor.getR(mC); mG = MyColor.getG(mC); mB = MyColor.getB(mC); // Draw "down" texture avoiding color key if ((mY - mH) < pSource.getHeight()) { if (mR == 255 && mG == 0 && mB == 255) ; else // Put the pixel into the source pSource.setRGB (mX, mY - mH, mC); } } // Increment the width way mWidthTexWay++; } } } } // ----- Texturize with the "up" texture ----- for (mY = 1; mY < pSource.getHeight(); mY ++) { for (mX = 0; mX < pSource.getWidth(); mX ++) { // Get the alpha value of the pixel mC = pSource.getRGB (mX, mY); mA = MyColor.getA(mC); // If it is part of the terrain... if (mA != 0) { // Get the UP pixel alpha value mC = pSource.getRGB (mX, mY - 1); mA = MyColor.getA(mC); // If this pixel is not terrain then texturize with "up" texture if (mA == 0) { // We have to take care of the heigh and texturize as needed mAlt = 0; mA = 1; while (mA != 0 && (mAlt + mY) < pSource.getHeight()) { // Get the alpha value of the pixel mC = pSource.getRGB (mX, mY + mAlt); mA = MyColor.getA(mC); // Increment the height mAlt++; } // Adjusting mUntil if (mAlt / 2 < pUpImg.getHeight()) mUntil = (int) (mAlt / 1.4); else mUntil = pUpImg.getHeight(); if (mUntil > pUpImg.getHeight()) mUntil = pUpImg.getHeight(); // We draw a column of "down" texture in up direction for (mH = 0; mH < mUntil; mH++) { if (mWidthTexWay > pUpImg.getWidth () - 1) mWidthTexWay = 0; // Get the pixel of the "up" texture mC = pUpImg.getRGB (mWidthTexWay, mH); mR = MyColor.getR(mC); mG = MyColor.getG(mC); mB = MyColor.getB(mC); // Draw "up" texture avoiding color key if (mR != 255 && mG != 0 && mB != 255 && (mY + mH - 2) > 0) { // Put the pixel into the source pSource.setRGB (mX, mY + mH - 2, mC); } } // Increment the width way mWidthTexWay++; } } } } } /** * Put the player in an area (trying to accomodate it between mX1 and mX2) * * Precondition 1: There is at less one pixel in the map where accomodate the player * Precondition 2: pX1 > 0 && pX2 < Screen.width() */ public MyPosition putPlayer (int pX1, int pX2) { int mC, mC1, mC2, mC3, mC4; // Colors int mA, mA1, mA2, mA3, mA4; // Alpha components int mHeight = mMapImage.getHeight(); // Screen height int mV; // Vertical position int mH; // Horizontal position final int MAX_POSITIONS = 60; // Maximum number of possible positions MyPosition mPositions[] = new MyPosition [MAX_POSITIONS]; // Array of possible positions int mNumPositions = 0; // Number of possible positions int mXs [] = new int [6]; mXs [2] = 0; // Search in the area for two possible positions where put the player for (mH = pX1; mH < pX2; mH += 5) { for (mV = 80; mV < mHeight - 20; mV++) { mC = mMapImage.getRGB (mH, mV); mA = MyColor.getA(mC); // If it is earth if (mA != 0) { // Rectangle for storing the player mC1 = mMapImage.getRGB (mH - 20, mV - 1); mC2 = mMapImage.getRGB (mH + 20, mV - 1); mC3 = mMapImage.getRGB (mH - 20, mV - 40); mC4 = mMapImage.getRGB (mH + 20, mV - 40); mA1 = MyColor.getA(mC1); mA2 = MyColor.getA(mC2); mA3 = MyColor.getA(mC3); mA4 = MyColor.getA(mC4); // If there is upper space if (mA1 == 0 && mA2 == 0 && mA3 == 0 && mA4 == 0) { mPositions[mNumPositions] = new MyPosition(); mPositions[mNumPositions].mX = mH; mPositions[mNumPositions].mY = mV; mNumPositions++; // If we have already found all the possible positions break if (mNumPositions > MAX_POSITIONS - 1) break; } } } // If we have already found all the possible positions break if (mNumPositions > MAX_POSITIONS - 1) break; } if (mNumPositions == 0) { mPositions [0] = new MyPosition(); return mPositions [0]; } // Choose one of the possible positions (aleatory) MyPosition mPos = mPositions [(int) mMyRandom.getRandInt(0, mNumPositions - 1)]; return mPos; } /** * Draw a black vertical line */ private void column (BufferedImage pImage, int pX, int pY, int pYmax) { for (int i = pY; i < pYmax; i++) pImage.setRGB (pX, i, MyColor.rgb (0, 0, 0)); } }