gpt4 book ai didi

java - 检查图 block 是否在相机 View 中时出现性能问题 java swing

转载 作者:行者123 更新时间:2023-12-01 14:02:51 25 4
gpt4 key购买 nike

我正在制作一个基于图 block 的游戏,我使用柏林的噪音生成随机 map 。当我的 map 是 100 * 100 个图 block 时,它工作正常,但是当我的 map 是 1000 * 1000 个图 block 时,我会遇到性能问题,我通过检查我的图 block 是否在相机的 View 中而不是绘制它们来解决大部分问题,但它仍然有问题。这是我的一些代码:

加载中

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);
}

block 类

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;
}
}
}

谢谢

最佳答案

即使您选择仅渲染某些图 block ,您仍会访问 N2 个图 block 来测试可见性。相反,请使用flyweight pattern仅渲染可见图 block ,其中可见性只能由面板/图 block 几何形状确定。该方法概述here ,用于优化JTable

关于java - 检查图 block 是否在相机 View 中时出现性能问题 java swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19206992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com