Unigine.Game Class
This class contains functions to control the game logic of the application. It provides functionality for:
- Assigning a player to the Engine Camera viewport.
- Pausing, speeding up and slowing down rendering, physics or game logic.
Usage Example#
The example below creates a PlayerSpectator and sets it as the active Engine Camera. The player is rotated around Y axis with the specified speed, which is set via setScale():
- Pressing f slows down the game logic, so player's rotation slows down too.
- Pressing g speeds up the game logic and, therefore, player's rotation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unigine;
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
// declare a PlayerSpectator
PlayerSpectator player;
public override bool Init()
{
// create a new PlayerSpectator instance
player = new PlayerSpectator();
// set necessary parameters: FOV, ZNear, ZFar, view direction vector and position.
player.Fov = 90.0f;
player.ZNear = 0.1f;
player.ZFar = 10000.0f;
player.ViewDirection = new vec3(0.0f, 1.0f, 0.0f);
player.WorldPosition = new dvec3(-1.6f, -1.7f, 1.7f);
// set the player to the Game singleton instance
Game.Player = player;
return true;
}
public override bool Update()
{
// slow down the game logic
if (App.clearKeyState('f') == 1) {
Game.Scale = 0.2f;
Log.Message("Game logic speed has been decreased. Frame duration is {0} seconds\n", Game.FTime);
}
// speed up the game logic
if (App.clearKeyState('g') == 1) {
Game.Scale = 0.2f;
Log.Message("Game logic speed has been increased. Frame duration is {0} seconds\n", Game.FTime);
}
// rotate the player 45 degrees per second around Y axis
player.WorldRotation = player.WorldRotation * new quat(0.0f, 1.0f, 0.0f, 45.0f * Game.IFps));
return true;
}
public override bool Shutdown()
{
// clear the pointer
player.clearPtr();
return true;
}
}
}
See Also#
- The article on the GameIntersection class as a usage example of game intersections
- A set of UnigineScript API samples located in the <UnigineSDK>/data/samples/systems/ folder:
- noise_00
- random_00
Game Class
Properties
Player Player#
Vec3 p0, p1;
// get the current player (engine camera)
Player player = Game.Player;
if (player.get() == null)
return 0;
// get the mouse direction from the player's position (p0) to the mouse cursor pointer (p1)
player.getDirectionFromScreen(out p0, out p1);
// create a new player
PlayerDummy player = new PlayerDummy();
// set necessary parameters
player.Fov = 60.0f;
player.WorldPosition = new dvec3(-1.0f, -1.0f, 1.0f);
// set the player to the Game singleton instance
Game.Player = player;
Player PlayerListener#
int Seed#
float Time#
float Scale#
Sets a value that is used to scale frame duration. It scales up or down the speed of rendering, physics and game logic. This function can be used to create effects of slow/accelerated motion.
For example, if the scale equals 2, the rate of simulation of all effects (such as particles) speeds up to two times faster. As for physics, in reality it will be simulated with the same fixed physics FPS, but the number of iterations will be two times higher. It is possible to scale the physics FPS separately via engine.physics.setScale() function.
This function the value set by the setIFps().
float IFps#
Node node;
// ...
// get an inverse FPS value
float ifps = Game.IFps;
// move the node up by 0.1 unit every second instead of every frame
node.worldTranslate(new Vec3(0.0f, 0.0f, 0.1f*ifps));
The function is useful when grabbing the video reel with a fixed FPS value (for example, 25 frames per second).
int Frame#
// get the current game frame
int loading_frames = engine.game.getFrame();
// perform asynchronous nodes loading
// ...
// calculate the number of game frames required for nodes loading
loading_frames = engine.game.getFrame() - loading_frames;
string Data#
<world version="1.21">
<game>
<data>User data</data>
</game>
</world>
<world version="1.21">
<game>
<data>User data</data>
</game>
</world>
bool Enabled#
vec4 RandomColor#
uint Random#
Members
Obstacle getIntersection ( vec3 p0, vec3 p1, float radius, int mask, Node[] exclude, vec3[] intersection ) #
Performs intersection to find if a pathfinding Obstacle is located within the cylinder between two points. The specified obstacles will be ignored.
Arguments
- vec3 p0 - Start point.
- vec3 p1 - End point.
- float radius - Radius of the intersection cylinder.
- int mask - Obstacle intersection mask. The obstacle is ignored if its mask does not match.
- Node[] exclude - Array with excluded obstacles. These obstacle nodes are ignored when performing intersection.
- vec3[] intersection - Intersection point.
Return value
Intersected obstacle.Obstacle getIntersection ( vec3 p0, vec3 p1, float radius, int mask, GameIntersection intersection ) #
Performs intersection to find if a pathfinding obstacle is located within the cylinder between two points.
The following example shows how you can get the intersection point (vec3) of the cylinder between two points with an obstacle. In this example we specify a cylinder from the point of the camera (vec3 p0) to the point of the mouse pointer (vec3 p1) with the specified radius. The executing sequence is the following:
- Define and initialize two points (p0 and p1) by using the Player.getDirectionFromScreen() function.
- Create an instance of the GameIntersection class to get the intersection point coordinates.
- Check, if there is an intersection with an obstacle. The Game.getIntersection() function returns an intersected obstacle when the obstacle appears in the area of the cylinder.
- After that GameIntersection instance gets the point of the nearest intersection point and you can get it by using the getPoint() function.
// AppWorldLogic.cs
/* ... */
// initialize points of the mouse direction
Vec3 p0, p1;
// get the current player (camera)
Player player = Game.Player;
if (player == null)
return 0;
// get width and height of the current application window
int width = App.getWidth();
int height = App.getHeight();
// get the current X and Y coordinates of the mouse pointer
int mouse_x = App.getMouseX();
int mouse_y = App.getMouseY();
// get the mouse direction from the player's position (p0) to the mouse cursor pointer (p1)
player.getDirectionFromScreen(out p0, out p1, mouse_x, mouse_y, width, height);
// create an instance of the GameIntersection class
GameIntersection intersection = new GameIntersection();
// try to get the intersection with an obstacle
// cylinder has radius 1.5f, intersection mask equals to 1
Obstacle obstacle = Game.GetIntersection(p0, p1, 1.5f, 1, intersection);
// check, if the intersection of mouse direction with any obstacle was occurred;
if (obstacle != null)
{
// show the coordinates of the intersection in console
Log.Message("The intersection with the obstacle was here: ({0} {1} {2})\n", intersection.getPoint().x, intersection.getPoint().y, intersection.getPoint().z);
}
/* ... */
Arguments
- vec3 p0 - Start point.
- vec3 p1 - End point.
- float radius - Radius of the intersection cylinder.
- int mask - Obstacle intersection mask. The obstacle is ignored if its mask does not match.
- GameIntersection intersection - GameIntersection class instance to put the result into.
Return value
Intersected obstacle.float GetNoise1 ( float pos, float size, int frequency ) #
Returns a noise value calculated using a Perlin noise function.Arguments
- float pos - Float position.
- float size - Size of the noise.
- int frequency - Noise frequency.
Return value
Noise value.float GetNoise2 ( vec2 pos, vec2 size, int frequency ) #
Returns a 2D noise value calculated using a Perlin noise function.Arguments
Return value
2D noise value.float GetNoise3 ( vec3 pos, vec3 size, int frequency ) #
Returns a 3D noise value calculated using a Perlin noise function.Arguments
Return value
3D noise value.double GetRandomDouble ( double from, double to ) #
Returns a pseudo-random double number within a given range (end-point not included).Arguments
- double from - The initial point of the range.
- double to - The end point of the range.
Return value
Random double integer number.float GetRandomFloat ( float from, float to ) #
Returns a pseudo-random float number within a given range (end-point not included).Arguments
- float from - The initial point of the range.
- float to - The end point of the range.
Return value
Random float number.int GetRandomInt ( int from, int to ) #
Returns a pseudo-random integer number within a given range (end-point not included).Arguments
- int from - The initial point of the range.
- int to - The end point of the range.
Return value
Random integer number.void GetMainPlayers ( Player[] players ) #
Returns the array of pointers to players that are set as main players.Arguments
- Player[] players - Array of pointers to main players.
void SetPlayerListener ( Player listener ) #
Sets the player as listener.Arguments
- Player listener - Player to be set as listener.
Player GetPlayerListener ( ) #
Returns the player which is currently the listener.void GetListeners ( Player[] players ) #
Adds all potential listeners to the specified array.Arguments
- Player[] players - List to store potential listeners.