私は、構築しようとしているゲーム用の単純なタイルベースの地形生成システムに取り組んでおり、少し問題が発生しました。個々のタイルに必要なビットマップを選択して保存する新しい方法を開発しようとしています。世界の描画を実行する前にビットマップをすべてロードし、タイルのタイプに基づいてビットマップを選択するという非常に単純な方法を使用しました。
残念ながら、タイルとワールドの種類にバリエーションを追加するため、選択プロセスはおそらくかなり複雑になるでしょう. そのため、どのタイルにするかを決定するために必要なすべてのルールと同様に、そのデータをタイル オブジェクト内に格納することをお勧めします。
私が直面している問題は、このメソッドによって、ビットマップへのポインタをロードするだけでなく、タイルごとにビットマップのコピーをロードすることです。これにより、メモリが消費され、GC が少し混乱します。
私が探しているのは、使用されるさまざまなビットマップへの静的ポインターを作成し、ビットマップの新しいコピーをロードするのではなく、タイル オブジェクトでそれらのポインターを参照する簡単な方法です。私が遭遇した問題は、コンテキストへの参照なしではビットマップをロードできず、静的メソッドでコンテキストを参照する方法がわからないことです。
これを回避する方法について誰かアイデアがありますか?
これが私のタイル クラスと、タイル タイプ クラスの例です (これらはすべてほとんど同じです)。
タイル: パッケージ com.psstudio.hub.gen;
import java.io.Serializable;
import android.graphics.Bitmap;
public class Tile implements Serializable
{
/****
* Create a BitmaHolder class that uses a case for which field type it isn, and then
* loads all applicable bitmaps into objects. The individuak tile types then reference
* those objects and return bitmaps, which are ultimately stored in this super class.
*/
double elevation = 0.0; //tile's elevation
int type = 0; //tile's type
int health = 0; //tile's health - used for harvestable tiles (trees, stone, mountain, etc.)
Bitmap bitmap; //pointer to the bitmap
public void setTile(Bitmap _bitmap, double _elev, int _type){
bitmap = _bitmap;
elevation = _elev;
type = _type;
}
public void setBreakableTile(Bitmap _bitmap, double _elev, int _type, int _health){
bitmap = _bitmap;
elevation = _elev;
type = _type;
health = _health;
}
}
タイルの種類 (草):
package com.psstudio.hub.gen.tiles;
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.psstudio.hub.R;
import com.psstudio.hub.gen.Tile;
public class Grass extends Tile {
Bitmap holder;
BitmapFactory.Options bFOptions = new BitmapFactory.Options(); //used to prevent bitmap scaling
Random r = new Random();
public Grass(Context context, int mapType, Double elevation){
super.setTile(pickBitmap(context, mapType), elevation, 4);
pickBitmap(context, mapType);
}
public Bitmap pickBitmap(Context context, int mapType){
bFOptions.inScaled = false;
switch(mapType){
case 1: //Forest Type 1
holder = BitmapFactory.decodeResource(context.getResources(), R.drawable.grass, bFOptions);
break;
}
return holder;
}
}
「holder = BitmapFactory」は、事前にロードされたものを指すのではなく、すべての草のタイルに新しいビットマップをロードしているため、私の問題を引き起こしていると思います。