If you think the Android project Tanks listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.ThirtyNineEighty.Game.Gameplay.Characteristics;
/*www.java2s.com*/import android.util.Log;
import com.ThirtyNineEighty.System.GameContext;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.util.HashMap;
// Characteristic file format:
// 20 chars - VisualModelName (in folder Models with extension raw)
// 20 chars - PhysicalModelName (in folder Models with extension ph)
// 20 chars - TextureName (in folder Textures with extension png)
// float - Health
// float - Speed
publicclass CharacteristicFactory
{
privatestaticfinal String Tag = "CharacteristicFactory";
publicstaticfinal String TANK = "tank";
publicstaticfinal String BULLET = "bullet";
privatestaticfinal HashMap<String, Characteristic> cache = new HashMap<String, Characteristic>();
publicstatic Characteristic get(String type)
{
String fileName = String.format("Characteristics/%s.ch", type);
Characteristic c = cache.get(type);
if (c != null)
return c;
try
{
InputStream stream = GameContext.getAppContext().getAssets().open(fileName);
int size = stream.available();
byte[] data = newbyte[size];
stream.read(data);
stream.close();
ByteBuffer buffer = ByteBuffer.wrap(data);
String visualModelName = readString(buffer, 20);
String phModelName = readString(buffer, 20);
String textureName = readString(buffer, 20);
c = new Characteristic(visualModelName, phModelName, textureName);
c.setHealth(buffer.getFloat());
c.setSpeed(buffer.getFloat());
cache.put(type, c);
}
catch (IOException e)
{
Log.e(Tag, "get method error", e);
}
return c;
}
privatestatic String readString(ByteBuffer buffer, int length)
{
StringBuilder builder = new StringBuilder(length);
for(int i = 0; i < length; i++)
builder.append(buffer.getChar());
return builder.toString().trim();
}
}