📄 nokiaterrain.java
字号:
index = x + m_image_width * (current_ground + c_type); for (int i = current_ground + c_type; i < m_image_height; i++) { //m_pixels[x + m_image_width * i] = c_grass_table[(x * i) % c_grass_table.length]; m_pixels[index] = c_grass_table[(x * i) % c_grass_table.length]; index += m_image_width; } } DirectGraphics dg = DirectUtils.getDirectGraphics(g); dg.drawPixels(m_pixels, false, 0, m_image_width, 0, 0, m_image_width, m_image_height, 0, DirectGraphics.TYPE_USHORT_4444_ARGB); } // =========================================================================;// Name: void createImage(Graphics g, short index, // short start_index, short end_index)// Desc: Creates image that contains background (sky + terrain). This// image is then blited to main screen. This is used to greatly// speed up rendering.//// ==========================================================================; private void createImage(Graphics g, short index, short start_index, short end_index) { if (m_allocated_images < m_images_num) // no need to find unused image { //m_images[index] = Image.createImage((int)m_image_width, (int)m_image_height); m_images[index] = m_resource_manager.getTerrainImage(); m_image_index[m_allocated_images] = index; m_allocated_images++; } else // find image that is being unused { for (int i = 0; i < m_image_index.length; i++) { if (m_image_index[i] < start_index || m_image_index[i] > end_index) { m_images[index] = m_images[m_image_index[i]]; m_images[m_image_index[i]] = null; m_image_index[i] = index; break; } } } recreateImage(g, index); } public void recreateImage(Graphics g, short index) { m_dirty_image[index] = false; drawTerrainImage(m_images[index].getGraphics(), index * m_image_width); } // =========================================================================;// Name: public void crater(int x, int size, int force)// Desc: Creates dent in terrain. It's center is at x, it has// radius of 2*size and creates inverted sinus hole with// amplitude force //// ==========================================================================; public void crater(int x, int size, int force) { // WARN ME: Optimize this code int start = x - size; int end = x + size; if (start < 0) start = 0; if (end >= m_meta_data.length) end = m_meta_data.length - 1; for (int i = start; i < end; i++) { byte frc = Common.toByte(force * Common.sin((i - (x - size)) * Common.FIXED * 180 / (size * 2))); m_meta_data[i] = (byte) Math.min(m_meta_data[i] + frc/2 + 3, 20); m_data[i] = (byte) Math.max(0, m_data[i] - frc); m_dirty_image[i / m_image_width] = true; } } // =========================================================================;// Name: public void damage(int x, int size, int force)// Desc: Damages the terrain. It's center is at x, it has// radius of 2*size and creates inverted sinus hole with// amplitude force //// ==========================================================================; public void damage(int x, int size, int force) { // WARN ME: Optimize this code int start = x - size; int end = x + size; if (start < 0) start = 0; if (end >= m_meta_data.length) end = m_meta_data.length - 1; for (int i = start; i < end; i++) { byte frc = Common.toByte((force + Math.abs(GameState.random.nextInt() % 6)) * Common.sin((i - (x - size)) * Common.FIXED * 180 / (size * 2))); m_meta_data[i] = (byte) Math.min(m_meta_data[i] + frc/2 + 3, 20); m_dirty_image[i / m_image_width] = true; } } public boolean isWater(int x) { if (x < 0) { return true; } else if (x >= m_data.length) { return true; } else if (-m_data[x] > m_water_level) { return true; } else return false; } // =========================================================================;// Name: public int getMaxHeight(int x, int size)// Desc: returns max height of interval (x, x + size)// Result is negative.//// ==========================================================================; public int getMaxHeight(int x, int size) { // WARN ME: Optimize this code int end = x + size; if (x < 0) x = 0; if (end >= m_meta_data.length) end = m_meta_data.length - 1; int max = m_water_level; for (int i = x; i < end; i++) { if (-m_data[i] < max) max = -m_data[i]; } return max; }// =========================================================================;// Name: public int getMinHeight(int x, int size)// Desc: returns min height of interval (x, x + size)//// ==========================================================================; public int getMinHeight(int x, int size) { // WARN ME: Optimize this code int end = x + size; if (x < 0) x = 0; if (end >= m_meta_data.length) end = m_meta_data.length - 1; int min = -10000; for (int i = x; i < end; i++) { if (-m_data[i] > min) min = -m_data[i]; } if (min == -10000) return m_water_level; else return min; } // =========================================================================;// Name: public boolean flatten(int x, int size)// Desc: returns flattens interval to lowest height in interval// (x, x + size). Returns true, if nothing was changed and false if // ground was flattened. //// ==========================================================================; public boolean flatten(int x, int size) { boolean flat = true; int min = getMinHeight(x, size); int end = x + size; if (x < 0) x = 0; if (end >= m_meta_data.length) end = m_meta_data.length - 1; for (int i = x; i < end; i++) { if (-m_data[i] < min) { m_data[i] = (byte)(-min); m_dirty_image[i/ m_image_width] = true; flat = false; } } return flat; } // =========================================================================;// Name: void drawTerrain(Graphics g, int sx, int sy, // int width, int height)// Desc: Draws terrain and sky to main screen. Uses number of// image buffers to speed up the process. //// ==========================================================================; public void drawTerrain(Graphics g, int sx, int sy, int width, int height) { int back_width = m_resource_manager.getTerrainBackground(0).getWidth(0); int back_s_x = (-sx/3) % back_width; int back_y = - m_image_height - sy - 1 ; DirectGraphics dg = DirectUtils.getDirectGraphics(g); g.setColor(0, 176, 224); // top sky color if (m_is_background) { m_resource_manager.getTerrainBackground(0).drawImage(g, 0, back_s_x, back_y); if (back_s_x + back_width < g.getClipWidth()) m_resource_manager.getTerrainBackground(0).drawImage(g, 0, back_s_x + back_width, back_y); if (back_s_x - back_width < g.getClipWidth()) m_resource_manager.getTerrainBackground(0).drawImage(g, 0, back_s_x - back_width, back_y); g.fillRect(g.getClipX(), 0, g.getClipWidth(), back_y - m_resource_manager.getTerrainBackground(0).getHeight(0) + 1); } else g.fillRect(g.getClipX(), 0, g.getClipWidth(), back_y + 1); m_resource_manager.getSun().drawImage(g, 0, 40, back_y - m_resource_manager.getTerrainBackground(0).getHeight(0) + 1 - 30); //g.fillRect(g.getClipX(), 0, g.getClipWidth(), g.getClipHeight()); //if (true) return; //sy = 0;//- sy;// + ; sy = - m_image_height - sy;/* if (sy > 0) { g.setColor(80, 80, 160); // top sky color g.fillRect(g.getClipX(), 0, g.getClipWidth(), sy); }*/ int index_x = sx + g.getClipX(); int x = g.getClipX(); short start_index = (short)(index_x / m_image_width); // index of image to draw short end_index = (short)((index_x + g.getClipWidth()) / m_image_width + 1); if (index_x < 0 && sx % m_image_width != 0) { start_index--; } for (short index = start_index; index < end_index; index++) { if (index >= 0 && index < m_images.length) { if (m_images[index] == null) createImage(g, index, start_index, end_index); else if (m_dirty_image[index]) recreateImage(g, index); //dg.drawImage(m_images[index], x - index_x % m_image_width, sy, 0, 0); g.drawImage(m_images[index], x - index_x % m_image_width, sy, 0); } else if (index < 0) { int tx = (index_x % m_image_width); // its 2am and I have no idea why this code works. *sigh* if (index_x % m_image_width != 0) tx+= m_image_width; g.drawImage(m_sky_image, x - tx, sy, 0); g.setColor(0, 128, 255); g.fillRect(x - tx, m_image_height + m_water_level + sy, m_image_width, -m_water_level); } else { g.drawImage(m_sky_image, x - index_x % m_image_width, sy, 0); g.setColor(0, 128, 255); g.fillRect(x - index_x % m_image_width, m_image_height + m_water_level + sy, m_image_width, -m_water_level); } index_x += m_image_width; x += m_image_width; } } public int getBottomBound() { return 0; } public int getLeftBound() { return 0; } public int getRightBound() { return m_data.length; } public int getTopBound() { return -(m_image_height - 1); } public void setWaterLevel(short level) { m_water_level = level; } public void destruct() { for(int i = 0; i < m_images.length; i++) { if (m_images[i] != null) { m_resource_manager.releaseTerrainImage(m_images[i]); } m_images[i] = null; } m_images = null; m_game_state = null; m_data = null; m_meta_data = null; m_pixels = null; if (m_sky_image != null) m_resource_manager.releaseTerrainImage(m_sky_image); m_sky_image = null; m_dirty_image = null; m_image_index = null; m_temp_point = null; m_sky_color = null; m_resource_manager = null; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -