The Entity of the CRYENGINE represents an object inside the game world with which can be interacted in various ways. The Entity can be freely moved around, rotated in any orientation and scaled to different sizes. The behavior of an Entity can also be changed by adding EntityComponents to the Entity. Entities are the basic building blocks for Sydewinder or any other CRYENGINE application.
Using an entity which is already placed in a level such as canyon.cry sample level is the easiest way to get control over an entity.
// Get Tunnel Spawn Point helper.
Entity tunnelSpawnPointHelper = Entity.Find("TunnelSpawnPoint");
Once a reference to the Entity is obtained it can be used to set for example the position of the Entity, or to change it's behavior by adding and EntityComponent
to the Entity.
Entities can only be found once the level is fully loaded. To get notified when the level is done loading an event-handler can be added to the event LevelSystem.LoadingComplete
.
Sometimes Entities need to be spawned dynamically instead of having them added to the level in the Sandbox. This can be done with the Entity.Spawn
method. To make sure that the level isn't getting filled with dynamically spawned Entities all spawned entities in Sydewinder get a predefined lifetime. This enables the objects to be created and removed if they are destroyed or leave the visible area.
// Spawn an empty Entity.
var enemy = Entity.Spawn(position, rotation, scale);
// Spawn an Entity with an Enemy component.
var enemy = Entity.SpawnWithComponent<Enemy>(position, rotation, scale);
You can set the position of an entity by assigning a Vector3 to the Position
property of the Entity.
myEntity.Position = new Vector3(x, y, z);
There are two common ways to move an Entity. Either by manually moving an object by updating the Position
property, or moving the object by setting the Physics.Velocity
property. For more information on how to use the velocity approach please refer to Physics Interaction.
An example of this can be found in the DestroyableBase
class which is the base for all Sydewinder game objects. Entities that are not physicalized or are physicalized as static (for example the walls in the background) are moved by directly setting the Positions
property. The Entities that are physicalized as rigid are moved by setting the Velocity
of the Entity. Setting the Velocity
makes sure that collisions are handled properly, where setting the Position
property forces an Entity to move to a position, which could cause Entities to not collide properly.
public virtual Vector3 Move()
{
if (!Entity.Exists)
return Vector3.Zero;
if(Entity.Physics == null ||
Entity.Physics.PhysicsType == PhysicalizationType.Static ||
Entity.Physics.PhysicsType == PhysicalizationType.None)
{
Entity.Position += Speed * FrameTime.Delta;
}
else
{
Entity.Physics.Velocity = Speed;
Entity.Physics.AngularVelocity = Vector3.Zero;
}
return Entity.Position;
}
Speed
is defined in meters per second (m/s) and is represented as a three dimensional vector. However, to ensure a constant move rate across the screen (because all PC’s have different frame rate performances) FrameTime normalizes the speed using the time that it took to render the previous frame.
Rotating an entity is similar to setting the position of an object. But rather than adding two vectors, rotating is done by multiplying the Rotation
property with another Quaternion.
The following snippet shows how to rotate the satellite rings in the background of Sydewinder around their Z-axis:
private void RotateSatelliteRings()
{
_rotatingRingOne.Rotation *= Quaternion.CreateRotationZ(MathHelpers.DegreesToRadians(FrameTime.Delta * 10f));
_rotatingRingTwo.Rotation *= Quaternion.CreateRotationZ(MathHelpers.DegreesToRadians(FrameTime.Delta * -10f));
}
Again, we are normalizing the rotation speed with FrameTime to ensure smooth movement.