タイル ベースのゲームを作成しています。パーリンのノイズを使用してランダム マップを生成します。マップが 100 * 100 タイルの場合は正常に動作しますが、マップが 1000 * 1000 タイルの場合はパフォーマンスの問題が発生します。これらのほとんどは、タイルを描画するのではなくカメラのビューにあるかどうかを確認することで解決しましたが、それでもバグがあります。これが私のコードの一部です:
読み込み中
void loadMap(int size)
{
blocks.clear();
float[][] seed1 = Perlin.GenerateWhiteNoise(size, size);
float[][] seed = Perlin.GenerateSmoothNoise(seed1, 3);
for (int i = 0; i < seed.length; i++)
{
for ( int j = 0; j < seed[i].length; j++)
{
if(seed[i][j] < 0.5)
{
blocks.add(new Block(i * Block.blockSize, j * Block.blockSize, eBlockType.WATER));
}
else if(seed[i][j] > 0.8)
{
blocks.add(new Block(i * Block.blockSize, j * Block.blockSize, eBlockType.GRASS));
}
else
{
blocks.add(new Block(i * Block.blockSize, j * Block.blockSize, eBlockType.SAND));
}
}
}
}
描く
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
cameraX = -player.getX() + getWidth()/2;
cameraY = -player.getY() + getHeight()/2;
g.translate(cameraX, cameraY);
for(Block block : blocks)
{
//System.out.println(block.getX() + " " + cameraX);
Rectangle cameraView = new Rectangle(-(cameraX+cameraPadding), -(cameraY+cameraPadding), this.getWidth()+cameraPadding , this.getHeight()+cameraPadding);
if(cameraView.contains(block.getX(), block.getY()))
{
block.draw(g2);
}
else if(block.getX() > cameraView.getMaxX() && block.getY() > cameraView.getMaxY())
{
break;
}
}
player.draw(g2);
}
ブロッククラス
package com.ymail.sanchixx.game;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Block
{
enum eBlockType
{
NULL(-1), STONE(0), STONEBRICK(1), // these are collision
COLLISION_BLOCKS(2), // PlaceHolder
GRASS(3), PLANK(4), WATER(5), SAND(6); // these are non-collision
private int code;
eBlockType(int c)
{
code = c;
}
public int getCode()
{
return code;
}
};
eBlockType m_type = eBlockType.NULL;
int m_x;
int m_y;
static int blockSize = 16;
Block(int x, int y, eBlockType type)
{
m_x = x;
m_y = y;
m_type = type;
}
boolean isNonCollisionBlock()
{
if(m_type.getCode() > eBlockType.COLLISION_BLOCKS.getCode())
{
return true;
}
return false;
}
static boolean collisionDown(Entity e, int v)
{
Rectangle player = new Rectangle(e.getX(), e.getY() + v, e.getWidth(), e.getHeight());
for(Block i : Game.blocks)
{
if(!i.isNonCollisionBlock())
{
int blockWidth = blockSize;
int blockHeight = blockSize;
Rectangle block = new Rectangle(i.getX(), i.getY(), blockWidth, blockHeight);
if (player.intersects(block))
{
e.setY(e.getY() - (e.getY() - (i.getY() - e.getHeight())));
return true;
}
}
}
return false;
}
static boolean collisionUp(Entity e, int v)
{
Rectangle player = new Rectangle(e.getX(), e.getY() -v, e.getWidth(), e.getHeight());
for(Block i : Game.blocks)
{
if(!i.isNonCollisionBlock())
{
int blockWidth = blockSize;
int blockHeight = blockSize;
Rectangle block = new Rectangle(i.getX(), i.getY(), blockWidth, blockHeight);
if (player.intersects(block))
{
e.setY(e.getY() - (e.getY() - (i.getY())) + blockHeight);
return true;
}
}
}
return false;
}
static boolean collisionRight(Entity e, int v)
{
Rectangle player = new Rectangle(e.getX() + v, e.getY(), e.getWidth(), e.getHeight());
for(Block i : Game.blocks)
{
if(!i.isNonCollisionBlock())
{
int blockWidth = blockSize;
int blockHeight = blockSize;
Rectangle block = new Rectangle(i.getX(), i.getY(), blockWidth, blockHeight);
if (player.intersects(block))
{
e.setX(e.getX() - (e.getX() - (i.getX() - e.getWidth())));
return true;
}
}
}
return false;
}
static boolean collisionLeft(Entity e, int v)
{
Rectangle player = new Rectangle(e.getX() - v, e.getY(), e.getWidth(), e.getHeight());
for(Block i : Game.blocks)
{
if(!i.isNonCollisionBlock())
{
int blockWidth = blockSize;
int blockHeight = blockSize;
Rectangle block = new Rectangle(i.getX(), i.getY(), blockWidth, blockHeight);
if (player.intersects(block))
{
e.setX(e.getX() - (e.getX() - (i.getX())) + e.getWidth());
return true;
}
}
}
return false;
}
static Boolean swimmingInBlock(Entity e, eBlockType type)
{
Rectangle player = new Rectangle(e.getX(), e.getY() + e.getHeight()/2, e.getWidth(), e.getHeight()/2);
for(Block i : Game.blocks)
{
if(i.isNonCollisionBlock())
{
int blockWidth = blockSize;
int blockHeight = blockSize;
Rectangle block = new Rectangle(i.getX(), i.getY(), blockWidth, blockHeight);
if (player.intersects(block))
{
if(i.getType() != type)
{
return false;
}
}
}
}
return true;
}
eBlockType getType()
{
return m_type;
}
int getX()
{
return m_x;
}
int getY()
{
return m_y;
}
void draw(Graphics g)
{
switch(m_type)
{
case GRASS:
g.drawImage(Game.blockImg[0], m_x, m_y, null);
break;
case WATER:
g.drawImage(Game.blockImg[4], m_x, m_y, null);
break;
case SAND:
g.drawImage(Game.blockImg[6], m_x, m_y, null);
break;
case STONE:
g.drawImage(Game.blockImg[1], m_x, m_y, null);
break;
default:
break;
}
}
}
ありがとうございました