programmazione

C++ – Strings

C++ – Strings

Inizialization


...
string mystring = "This is a string";
string mystring ("This is a string");
string mystring {"This is a string"};
...

Basic Statement


// include standard input and output operations
#include <iostream>
// include strings
#include <string>
using namespace std;

int main ()
{
  string mystring;
  mystring = "This is the initial string content";
  // character output - variable - end line
  cout << mystring << endl;
  mystring = "This is a different string content";
  cout << mystring << endl;
  return 0;
}

Escape Codes

\n newline
\r carriage return
\t tab
\v vertical tab
\b backspace
\f form feed (page feed)
\a alert (beep)
\’ single quote (‘)
\” double quote (“)
\? question mark (?)
\\ backslash (\)

By |C++, Video Games Development|Commenti disabilitati su C++ – Strings

Unity3D – Scrolling Typewriter effect – JavaScript

Unity3D – Scrolling Typewriter effect – JavaScript

Create a scene with:

– TextMesh
– Empty Object, assign TypeWriter.js

#pragma strict

var displayText : TextMesh;

var textShownOnScreen: String ;
var fullText : String = "The text you want shown on screen with typewriter effect.";
var wordsPerSecond : float = 2; // speed of typewriter, words per second
var timeElapsed : float = 0;   
 
function Update()
{
    timeElapsed += Time.deltaTime;
    textShownOnScreen = GetWords(fullText, timeElapsed * wordsPerSecond);
    displayText.text = textShownOnScreen;
}
 
function GetWords(text : String, wordCount : int): String
{
    var words : int = wordCount;
 
    // loop through each character in text
    for (var i : int = 0; i < text.Length; i++)
    { 
        if (text[i] == ' ')
        {
            words--;
        }
 
        if (words <= 0)
        {
            return text.Substring(0, i);
        }
    }
 
    return text;
}

Inspector, assign a 3DText to var displayText.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Scrolling Typewriter effect – JavaScript

Unity 3D Game Engine – Load Next Level

Unity 3D Game Engine – Load Next Level

Syntax:


// Load the level named "level2".
Application.LoadLevel ("level2");

// Load the level index 1, 
// you find this value inside Buil Settings> 'Scenes in Build'
Application.LoadLevel (1);    

To improve performance a big game is broken over many levels.
Inside Unity3D you can use Scenes to create levels.

1. MAIN TOP MENU> File> New Scene, create 2 scenes: level1 and level2
2. Project window> DOUBLE CLICK over level1 to select the scene
3. MAIN TOP MENU> GameObject> Create Empty ‘GameController’> Inspector> ‘Add Component’> GameController.JS


#pragma strict

var scores : int;

function Start () {
scores = 10;

}

// On Click Counter START #########################
//This is the variable we are using to store the number of clicks
var clickCounter: int;
//This creates a button and adds +1 to clickCounter variable every 1 click
function OnGUI () {
    if (GUI.Button (Rect (10,10,150,100), "You clicked:" + clickCounter)) {
        clickCounter ++;        
    }
}
// On Click Counter END ###########################

function Update () {

   if (clickCounter > 10) {
    scores = 20;
    // Load the level named "level2".
	Application.LoadLevel ("level2");       
    }

} // END Update

4. MAIN TOP MENU> File> Buil Settings> DRAG AND DROP level1 and level2 scenes over ‘Scenes in Build’ window
Add the scenes into Build or Application.LoadLevel (“level2”); will not work!

When you click 11 times over the GUI.Button, level2 will be loaded.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Load Next Level

Unity 3D – Hide Mouse Cursor

Unity 3D – Hide Mouse Cursor

1. Load a Scene

2. Select MainCamera, assign HideCursor.JS

#pragma strict
  
function Start () {  
	// Hide the cursor
	Screen.showCursor = false;	
}
   

function Update () {  
}


3. Build it and run

NOTICE: If you try the game during production you will see the mouse cursor! You have to build to see the final result!

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – Hide Mouse Cursor

Unity 3D Game Engine – Quaternions

Unity 3D Game Engine – Quaternions

In mathematics, the quaternions are a number system that extends the complex numbers. They were first described by Irish mathematician William Rowan Hamilton in 1843 and applied to mechanics in three-dimensional space.

According to Euler’s rotation theorem, any rotation or sequence of rotations of a rigid body or coordinate system about a fixed point is equivalent to a single rotation by a given angle θ about a fixed axis (called Euler axis) that runs through the fixed point. The Euler axis is typically represented by a unit vector u→. Therefore, any rotation in three dimensions can be represented as a combination of a vector u→ and a scalar θ. Quaternions give a simple way to encode this axis–angle representation in four numbers, and to apply the corresponding rotation to a position vector representing a point relative to the origin in R3.

Unity utilizes the quaternion system to manage the rotation of game objects.

In Unity Quaternions have 4 componments: X Y Z W and they work together to define any rotations.

Look At

Inside Hierarchy create:

1. Sphere and attach MotionScript.js


#pragma strict

public var speed : float = 3f;


function Update () 
{
    transform.Translate(-Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0);
}

2. Cube and attach LookAtScript.js


#pragma strict

public var target : Transform;

function Update () 
{
    var relativePos : Vector3 = target.position - transform.position;
    transform.rotation = Quaternion.LookRotation(relativePos);
}

Inspector> LookAtScript.js> DRAG AND DROP Sphere over var target

3. Play> Move the Sphere using keyboard arrow, the Cube will look at the Sphere

Gravity – Orbit

Inside Hierarchy create:

1. Sphere

2. Cube and attach GravityScript.js


#pragma strict

public var target : Transform;
    
    
function Update () 
{
    var relativePos : Vector3 = (target.position + new Vector3(0, 1.5f, 0)) - transform.position;
    var rotation : Quaternion = Quaternion.LookRotation(relativePos);
    
    var current : Quaternion = transform.localRotation;
    
    // Spherical Linear Interpolation
    transform.localRotation = Quaternion.Slerp(current, rotation, Time.deltaTime);
    transform.Translate(0, 0, 3 * Time.deltaTime);
}
 

Inspector> GravityScript.js> DRAG AND DROP Sphere over var target

3. Play> the Cube will rotate around the Sphere as a planet around the sun.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Quaternions