public class GameElement
- Object
- GameElement
A single placed thing in a GameLevel – pure authoring data, deliberately not
a com.codename1.gaming.Sprite.
An element only describes what (an #getAssetId() resolved through an
AssetCatalog), where (a position / rotation / scale transform), which Layer
it belongs to, and a bag of typed authoring #properties() (a coin’s value, a
patrolling enemy’s speed, a player’s lives). What it becomes at runtime
depends on the level’s GameLevel#getMode(): a LevelRealizer turns a 2D / board
element into a com.codename1.gaming.Sprite and a 3D element into a
com.codename1.gaming.Model. The realized object keeps a reference back to its
element through setUserData, so an editor can map a picked sprite/model to the
data it came from.
The property bag stores values as they survive JSON: Double for numbers, String
for text, Boolean for flags. The typed getters coerce defensively so callers do
not care whether a number arrived as a Double, a Long or a numeric String.
Constructors
public GameElement() | |
public GameElement(String id, String assetId) |
Methods
public String getId() | |
public GameElement setId(String id) | Sets this element’s stable id (unique within a level; the editor assigns one). |
public String getName() | The optional display name typed in the editor’s Inspector. |
public GameElement setName(String name) | Sets the display name (see #getName()). |
public String getAssetId() | The catalog id of the asset this element draws (e.g. "player", "coin"). |
public GameElement setAssetId(String assetId) | Points this element at a different catalog asset (and clears the cached #resolveDef). |
public AssetDef resolveDef(AssetCatalog catalog) | The AssetDef this element’s #getAssetId() resolves to in the given catalog, cached after the first lookup. |
public String getLayer() | The name of the Layer this element belongs to. |
public GameElement setLayer(String layer) | Moves this element to the named Layer. |
public double getX() | The x position – pixels in a 2D / board level, a world-space coordinate in 3D. |
public double getY() | The y position – pixels in a 2D / board level, a world-space coordinate in 3D. |
public double getZ() | The world-space depth, meaningful only in GameLevel.Mode#THREE_D. |
public GameElement setPosition(double x, double y) | Sets the x and y position (see #getX()). |
public GameElement setPosition(double x, double y, double z) | Sets the x, y and z position (z is the 3D depth). |
public GameElement setX(double x) | Sets the x position. |
public GameElement setY(double y) | Sets the y position. |
public GameElement setZ(double z) | Sets the z (3D) depth. |
public float getRotation() | The rotation in degrees (clockwise in 2D, applied about the model’s axes in 3D). |
public GameElement setRotation(float rotation) | Sets the rotation in degrees (see #getRotation()). |
public float getScaleX() | The per-instance x scale (1 = the asset’s natural size). |
public float getScaleY() | The per-instance y scale (1 = the asset’s natural size). |
public float getScaleZ() | The per-instance z scale, used in 3D (1 = the asset’s natural size). |
public GameElement setScale(float scale) | Sets a uniform scale on all three axes. |
public GameElement setScale(float scaleX, float scaleY, float scaleZ) | Sets the per-axis scale (z applies in 3D). |
public Map<String, Object> properties() | The mutable authoring property bag. |
public boolean hasProperty(String key) | Whether an authoring property with the given key is set. |
public GameElement setProperty(String key, Object value) | Sets an authoring property (a coin’s value, an enemy’s speed, …). |
public Object getProperty(String key) | The raw value of an authoring property, or null if unset. |
public int getInt(String key, int defaultValue) | Reads a property as an int, coercing Number and numeric String values and falling back to defaultValue when the key is missing or not numeric. |
public double getDouble(String key, double defaultValue) | Reads a property as a double, coercing Number and numeric String values. |
public String getString(String key, String defaultValue) | Reads a property as a String (any value’s toString()), or defaultValue if absent. |
public boolean getBoolean(String key, boolean defaultValue) | Reads a property as a boolean. |
Inherited methods
Constructor details
GameElement
public GameElement()GameElement
public GameElement(String id, String assetId)Method details
getId
public String getId()setId
public GameElement setId(String id)getName
public String getName()null.setName
public GameElement setName(String name)#getName()).getAssetId
public String getAssetId()"player", "coin").setAssetId
public GameElement setAssetId(String assetId)#resolveDef).resolveDef
public AssetDef resolveDef(AssetCatalog catalog)AssetDef this element’s #getAssetId() resolves to in the given catalog, cached
after the first lookup. The element stays pure, catalog-independent data (only the id is
stored and serialized); this just spares per-frame callers a repeated catalog-map lookup.
Returns null for a null catalog or an unknown id. The cache is cleared by #setAssetId;
if you re-register an asset under the same id, build a fresh element or set the id again.getLayer
public String getLayer()Layer this element belongs to.setLayer
public GameElement setLayer(String layer)Layer.getX
public double getX()getY
public double getY()getZ
public double getZ()GameLevel.Mode#THREE_D.setPosition
public GameElement setPosition(double x, double y)#getX()).setPosition
public GameElement setPosition(double x, double y, double z)setX
public GameElement setX(double x)setY
public GameElement setY(double y)setZ
public GameElement setZ(double z)getRotation
public float getRotation()setRotation
public GameElement setRotation(float rotation)#getRotation()).getScaleX
public float getScaleX()1 = the asset’s natural size).getScaleY
public float getScaleY()1 = the asset’s natural size).getScaleZ
public float getScaleZ()1 = the asset’s natural size).setScale
public GameElement setScale(float scale)setScale
public GameElement setScale(float scaleX, float scaleY, float scaleZ)properties
public Map<String, Object> properties()Double / String / Boolean
(or nested Map / List if you store structured data). Prefer the typed
getters for reading.hasProperty
public boolean hasProperty(String key)setProperty
public GameElement setProperty(String key, Object value)value, an enemy’s speed, …). Store
Double / String / Boolean so the value survives a JSON round-trip.getProperty
public Object getProperty(String key)null if unset. Prefer the typed getters.getInt
public int getInt(String key, int defaultValue)Number and numeric String values and
falling back to defaultValue when the key is missing or not numeric.getDouble
public double getDouble(String key, double defaultValue)Number and numeric String values.getString
public String getString(String key, String defaultValue)toString()), or defaultValue if absent.getBoolean
public boolean getBoolean(String key, boolean defaultValue)Boolean or the strings "true"/"1".