I'm trying to implement a tile-based platforming system for a 2D game demo from this C++ code into C#,
This is what I have written, It doesn't compile though due to the var variable usage.
class GameLogic
{
// Level storage
String sLevel;
int nLevelWidth;
int nLevelHeight;
// Player Properties
float fPlayerPosX = 1.0f;
float fPlayerPosY = 1.0f;
float fPlayerVelX = 0.0f;
float fPlayerVelY = 0.0f;
bool bPlayerOnGround = false;
// Camera properties
float fCameraPosX = 0.0f;
float fCameraPosY = 0.0f;
public GameLogic()
{
var GetTile = (ref int x, ref int y) =>
{
if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
return sLevel[y * nLevelWidth + x];
else
return "";
};
var SetTile = (ref int x, ref int y, string c)=>
{
if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
sLevel[y * nLevelWidth + x] = c;
};
}
}
What do you suggest I should do here, to implement this functionality. Thank you for your time.
GetTile
andSetTile
local functions in new versions of C#; again, the compiler will take care of ensuring that outer locals of local functions are captured appropriately. C# does not have a facility for "capture by value" like C++ does. (There are rare cases where values are captured instead of variables but you are not likely to run into them.)