Class 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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionThe catalog id of the asset this element draws (e.g."player","coin").booleangetBoolean(String key, boolean defaultValue) Reads a property as a boolean.doubleReads a property as a double, coercingNumberand numericStringvalues.getId()intReads a property as an int, coercingNumberand numericStringvalues and falling back todefaultValuewhen the key is missing or not numeric.getLayer()The name of theLayerthis element belongs to.getName()The optional display name typed in the editor's Inspector.getProperty(String key) The raw value of an authoring property, ornullif unset.floatThe rotation in degrees (clockwise in 2D, applied about the model's axes in 3D).floatThe per-instance x scale (1= the asset's natural size).floatThe per-instance y scale (1= the asset's natural size).floatThe per-instance z scale, used in 3D (1= the asset's natural size).Reads a property as a String (any value'stoString()), ordefaultValueif absent.doublegetX()The x position -- pixels in a 2D / board level, a world-space coordinate in 3D.doublegetY()The y position -- pixels in a 2D / board level, a world-space coordinate in 3D.doublegetZ()The world-space depth, meaningful only inGameLevel.Mode#THREE_D.booleanhasProperty(String key) Whether an authoring property with the given key is set.The mutable authoring property bag.resolveDef(AssetCatalog catalog) TheAssetDefthis element's#getAssetId()resolves to in the given catalog, cached after the first lookup.setAssetId(String assetId) Points this element at a different catalog asset (and clears the cached#resolveDef).Sets this element's stable id (unique within a level; the editor assigns one).Moves this element to the namedLayer.Sets the display name (see#getName()).setPosition(double x, double y) Sets the x and y position (see#getX()).setPosition(double x, double y, double z) Sets the x, y and z position (z is the 3D depth).setProperty(String key, Object value) Sets an authoring property (a coin'svalue, an enemy'sspeed, ...).setRotation(float rotation) Sets the rotation in degrees (see#getRotation()).setScale(float scale) Sets a uniform scale on all three axes.setScale(float scaleX, float scaleY, float scaleZ) Sets the per-axis scale (z applies in 3D).setX(double x) Sets the x position.setY(double y) Sets the y position.setZ(double z) Sets the z (3D) depth.
-
Constructor Details
-
GameElement
public GameElement() -
GameElement
-
-
Method Details
-
getId
-
setId
Sets this element's stable id (unique within a level; the editor assigns one). -
getName
The optional display name typed in the editor's Inspector. A named element becomes a generated field in the companion class; may benull. -
setName
Sets the display name (see#getName()). -
getAssetId
The catalog id of the asset this element draws (e.g."player","coin"). -
setAssetId
Points this element at a different catalog asset (and clears the cached#resolveDef). -
resolveDef
TheAssetDefthis 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. Returnsnullfor 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
The name of theLayerthis element belongs to. -
setLayer
Moves this element to the namedLayer. -
getX
public double getX()The x position -- pixels in a 2D / board level, a world-space coordinate in 3D. -
getY
public double getY()The y position -- pixels in a 2D / board level, a world-space coordinate in 3D. -
getZ
public double getZ()The world-space depth, meaningful only inGameLevel.Mode#THREE_D. -
setPosition
Sets the x and y position (see#getX()). -
setPosition
Sets the x, y and z position (z is the 3D depth). -
setX
Sets the x position. -
setY
Sets the y position. -
setZ
Sets the z (3D) depth. -
getRotation
public float getRotation()The rotation in degrees (clockwise in 2D, applied about the model's axes in 3D). -
setRotation
Sets the rotation in degrees (see#getRotation()). -
getScaleX
public float getScaleX()The per-instance x scale (1= the asset's natural size). -
getScaleY
public float getScaleY()The per-instance y scale (1= the asset's natural size). -
getScaleZ
public float getScaleZ()The per-instance z scale, used in 3D (1= the asset's natural size). -
setScale
Sets a uniform scale on all three axes. -
setScale
Sets the per-axis scale (z applies in 3D). -
properties
-
hasProperty
Whether an authoring property with the given key is set. -
setProperty
Sets an authoring property (a coin'svalue, an enemy'sspeed, ...). StoreDouble/String/Booleanso the value survives a JSON round-trip. -
getProperty
-
getInt
Reads a property as an int, coercingNumberand numericStringvalues and falling back todefaultValuewhen the key is missing or not numeric. -
getDouble
Reads a property as a double, coercingNumberand numericStringvalues. -
getString
-
getBoolean
Reads a property as a boolean. Accepts aBooleanor the strings"true"/"1".
-