[英]Translating JavaScript oop code to Lua
I'm trying to translate a bunch of code from JavaScript to Lua. 我正在尝试将一堆代码从JavaScript转换为Lua。 I'm stuck on the next bit though.
不过,我被困在下一点。 I don't understand the Lua object oriented syntax.
我不了解Lua面向对象的语法。 Can someone show me what the correct syntax is?
有人可以告诉我正确的语法是什么吗? Thanks!
谢谢!
//=======================================================
//=======================================================
//=======================================================
// The TMaze object
//------------------------------------------------------------------------------ Ctor
function TMaze(nHigh, nWide) {
this.high = nHigh;
this.wide = nWide;
this.ary = TMazeArray(nHigh, nWide);
this.totRooms = gnHigh * gnWide; // less (short) bottom line
this.curRoomCnt = 0; // rooms that have been visited/door opened
this.GetCell = function(x,y) {return this.ary[y][x];}
this.SetCell = function(x,y,value) {this.ary[y][x] = value;}
this.HasCellBit = function(x,y,value) {return ((this.ary[y][x] & value) == value);}
this.SetCellBit = function(x,y,value) {this.ary[y][x] |= value;}
this.ClearCellBit = function(x,y,value) {this.ary[y][x] &= ~value;}
this.ToggleCellBit = function(x,y,value) {this.ary[y][x] ^= value;}
this.IsEmptyCellAt = IsEmptyCellAt; // some member fns, defined below
this.IsValidXY = IsValidXY;
this.GetNewXY = GetNewXY;
}
//----------- TRUE if cell in that direction is empty and valid
//
function IsEmptyCellAt(x, y, dir) {
var o = this.GetNewXY(x, y, dir);
if (!this.IsValidXY(o.newX, o.newY)) return (false);
if (this.GetCell(o.newX, o.newY) != 0) return (false);
return true; // yes, it's possible to move into that cell
}
//------------------------------------------------------------------------------
// return -1 if that would be an invalid move (off the board)
// true if X,Y is on the board
//
function IsValidXY(x, y) {
if (y < 0) return (false);
if (y > this.high-1) return (false);
if (x < 0) return (false);
if (x > this.wide-1) return (false);
// if (y & 1) { // on off Y, max X an maxY are 1 less
// if (x > this.wide-2) return (false);
// if (y > this.high-2) return (false);
// }
return true; // possible to move into that direction
}
//------------------------------------------
// If I move in a direction, what are the new X,Y values?
// Return an object with newX and newY properties
//
function GetNewXY(x, y, dir) {
var oRet = {"newX":-1, "newY":-1, "isValid":false};
var newX = x;
var newY = y;
newX += ganDeltaX[dir]; // add the deltas
newY += ganDeltaY[dir];
if (this.IsValidXY(newX, newY)) {
oRet.newX = newX;
oRet.newY = newY;
oRet.isValid = true;
}
return oRet;
}
You can ignore the bitwise operations since I already figured out how to translate that. 您可以忽略按位运算,因为我已经弄清楚了如何翻译。 I'm mostly concerned with the oop stuff.
我最关心的是哎呀东西。
[edit] [编辑]
Here's what I have so far, but it crashes. 这是我到目前为止的内容,但是崩溃了。
--=======================================================
--=======================================================
-- The TMaze object
-------------------------------------------------------------------------------- Ctor
TMaze =
{
high = gnHigh,
wide = gnWide,
ary = TMazeArray(gnHigh, gnWide),
totRooms = gnHigh * gnWide, -- less (short) bottom line
curRoomCnt = 0, -- rooms that have been visited/door opened
}
TMaze.GetCell = function(self,x,y) return self.ary[y][x] end
TMaze.SetCell = function(self,x,y,value) self.ary[y][x] = value end
TMaze.HasCellBit = function(self,x,y,value) return bit.band(self.ary[y][x], value) == value end
TMaze.SetCellBit = function(self,x,y,value) self.ary[y][x] = bit.bor(self.ary[y][x], value) end
TMaze.ClearCellBit = function(self,x,y,value) self.ary[y][x] = bit.band(self.ary[y][x], bit.bnot(value)) end
TMaze.ToggleCellBit = function(self,x,y,value) self.ary[y][x] = bit.bxor(self.ary[y][x], value) end
--=======================================================
------------- TRUE if cell in that direction is empty and valid
--
TMaze.IsEmptyCellAt = function(self, x, y, dir)
local o = self.GetNewXY(self, x, y, dir)
if (not self.IsValidXY(self, o.newX, o.newY)) then return 0 end
if (self.GetCell(self, o.newX, o.newY) ~= 0) then return 0 end
return 1 -- yes, it's possible to move into that cell
end
--------------------------------------------------------------------------------
-- return -1 if that would be an invalid move (off the board)
-- true if X,Y is on the board
--
TMaze.IsValidXY = function(self, x, y)
if (y < 1) then return 0 end
if (y > self.high) then return 0 end
if (x < 1) then return 0 end
if (x > self.wide) then return 0 end
-- if (y & 1) then -- on off Y, max X an maxY are 1 less
-- if (x > self.wide-2) then return 0 end
-- if (y > self.high-2) then return 0 end
-- end
return 1 -- possible to move into that direction
end
--------------------------------------------
-- If I move in a direction, what are the new X,Y values?
-- Return an object with newX and newY properties
--
TMaze.GetNewXY = function(self, x, y, dir)
local oRet = {newX=-1, newY=-1, isValid=0}
local newX = x
local newY = y
newX = newX + ganDeltaX[dir] -- add the deltas
newY = newY + ganDeltaY[dir]
if (self.IsValidXY(self, newX, newY)) then
oRet.newX = newX
oRet.newY = newY
oRet.isValid = 1
end
return oRet
end
Changing 改变中
self.IsValidXY(self, newX, newY)
to 至
self:IsValidXY(newX, newY)
made it work for some reason. 由于某种原因使其工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.