Tuesday, June 7, 2011

Positioning scaled CCLayer to center it on player.

I am developing a game in Cocos2D. Two days ago I've been working on my game that is Cocos2D powered and spent some time working on simple task - positioning CCLayer the way my game hero will be always in the center of the screen, on any scale level. I didn't take into account world borders because in my game player shouldn't see it anyway.

Here is the code snippet for changing CCLayer position.

Somewhere in the init method we declare _origin, that points on the center of the screen.
// ask director the window size
CGSize _winSize = [[CCDirector sharedDirector] winSize];
CGPoint _origin = CGPointMake(_winSize.width/2.0, _winSize.height/2.0);


Then we use CCLayer's convertToWorldSpace: method to get "world space" coordinates of the player and center of the screen, then find the difference between these two points, and finally place gameLayer at position where the player object will be on the center of the screen.


- (void)setViewpointCenter:(CGPoint)playerPos {

/* Convert object point and origin point to WorldSpace,
then sub it. */
CGPoint wPlayer = [gameLayer convertToWorldSpace:playerPos];
CGPoint wOrigin = [gameLayer convertToWorldSpace:_origin];

CGPoint _nodePos = ccpSub(wOrigin, wPlayer);

[gameLayer setPosition:_nodePos];

}


This method should be executed each timestep to keep your layer centered.
Happy gamedeving :)

Sunday, June 5, 2011

How to get user data from active directory using javascript.

Recently I've been working on SAP Portal password reset tool, that is asking user to enter his user id, email, name, etc. I've made a script to get required information from company's active directory domain. This javascript unfortunately working only in IE.
Function getUserName() takes currently logged in user's name.
Function getAD(userName) search our company AD to get user's email and first/last name.
This script can give you an idea on how to query active directory domain.

And the source code:
function getUserName() {
var wshNetwork = new ActiveXObject("WScript.Network");
var userName = wshNetwork.UserName;
return userName;
}

function getAD(userName) {
//var name = userName.split(".");
objConnection = new ActiveXObject("ADODB.Connection");
objConnection.Provider="ADsDSOObject";
objConnection.Open("ADs Provider");
objCommand = new ActiveXObject("ADODB.Command");
objCommand.ActiveConnection = objConnection;

objCommand.CommandText = "SELECT sAMAccountName, givenName, SN, mail FROM 'LDAP://ad.mycompany.com/OU=User Accounts,OU=User Directory,DC=ad,DC=mycompany,DC=com' WHERE objectCategory='user' and sAMAccountName = '"+userName+"'";

/* Next up is the command itself.*/
objRecordSet = objCommand.Execute();

/* Then we execute the command */
/* Once executed, the command will return an enumeration of the results.*/

var userMail,lastName,firstName;
if (objRecordSet.RecordCount == 1) {
objRecordSet.Movefirst;
userMail = objRecordSet.Fields("mail").value;
firstName = objRecordSet.Fields("givenName").value;
lastName = objRecordSet.Fields("SN").value;
}
else
{
userMail = "";
firstName = "";
lastName = "";
}
objConnection.Close;

return userMail+";"+firstName+";"+lastName;
}

var AD = getAD(getUserName()).split(";");

var mail = AD[0];
var firstName = AD[1];
var lastName = AD[2];


If this post help you, please don't hesitate to share it.