C 层的作用是 写逻辑 添加点击事件 跟C#层的Util.CallMethod 进行交互
Util.CallMethod 调用Lua层的脚本
StartCtrl = {};
local this = StartCtrl;
local start;
local transform;
local gameObject;
--构建函数--
function
StartCtrl.New()
logWarn(
"StartCtrl.New--->>");
return this;
end
function
StartCtrl.Awake()
logWarn(
"StartCtrl.Awake--->>");
panelMgr:
CreatePanel(
'Start', this.
OnCreate);
end
--启动事件--
function
StartCtrl.OnCreate(
obj)
gameObject = obj;
start = gameObject:
GetComponent(
'LuaBehaviour');
start:
AddClick(StartPanel.
registerBtn, this.
OnRegisterBtnClick);
start:
AddClick(StartPanel.
loginBtn, this.
OnLoginBtnClick);
resMgr:
LoadPrefab(
"Tank",{
"Tank1"},this.
CreateTank)
resMgr:
LoadPrefab(
"Tank",{
"Tank2"},this.
CreateEnemy)
resMgr:
LoadPrefab(
"Bullet",{
"Bullet"},this.
CreateBullet)
logWarn(
"Start lua--->>"..gameObject.
name);
end
function
StartCtrl.CreateTank(
gos)
print(
"StartCtrl.CreateTank")
print(gos[
0].
name)
this.
tank = gos[
0];
end
function
StartCtrl.CreateEnemy(
gos)
print(
"StartCtrl.CreateEnemy")
this.
enemy = gos[
0];
print(this.
enemy.
name)
end
function
StartCtrl.CreateBullet(
gos)
this.
bullet = gos[
0]
print(this.
bullet.
name)
end
--注册事件--
function
StartCtrl.OnRegisterBtnClick(
go)
if TestProtoType == ProtocalType.
BINARY
then
this.
TestRegisterBinary();
end
end
function
StartCtrl.TestRegisterBinary()
local buffer = ByteBuffer.
New();
buffer:
WriteShort(Protocal.
RegisterMsg);
buffer:
WriteByte(ProtocalType.
BINARY);
buffer:
WriteString(StartPanel.
userNameInput:
GetComponent(
"Text").
text);
buffer:
WriteString(StartPanel.
userPwdInput:
GetComponent(
"Text").
text);
networkMgr:
SendMessage(buffer);
end
--登录事件--
function
StartCtrl.OnLoginBtnClick(
go)
sceneMgr.
ChangeScene(
1)
--[[
if TestProtoType == ProtocalType.BINARY then
this.TestLoginBinary();
end
]]
end
function
StartCtrl.TestLoginBinary()
local buffer = ByteBuffer.
New();
buffer:
WriteShort(Protocal.
LoginMsg);
buffer:
WriteByte(ProtocalType.
BINARY);
buffer:
WriteString(StartPanel.
userNameInput:
GetComponent(
"Text").
text);
buffer:
WriteString(StartPanel.
userPwdInput:
GetComponent(
"Text").
text);
networkMgr:
SendMessage(buffer);
end
MainCityCtrl = {}
local this = MainCityCtrl;
local transform;
local gameObject;
local tank
local enemy
--构建函数--
function
MainCityCtrl.New()
logWarn(
"MainCityCtrl.New--->>");
return this;
end
--加载面板
function
MainCityCtrl.Awake()
logWarn(
"StartCtrl.Awake--->>");
panelMgr:
CreatePanel(
'MainCity', this.
OnCreate);
end
--加载面板的回调函数
function
MainCityCtrl.OnCreate(
obj)
gameObject = obj;
start = gameObject:
GetComponent(
'LuaBehaviour');
tank =
newObject(StartCtrl.
tank);
tank.
transform:
SetParent(MainCityPanel.
panel.
transform)
tank.
transform.
localPosition = Vector3.
one *
0.2
tank.
transform.
localScale = Vector3.
one *
0.1
for i=
1,
3
do
enemy =
newObject(StartCtrl.
enemy);
enemy.
transform:
SetParent(MainCityPanel.
panel.
transform)
enemyPos = Vector3.
New();
enemyPos.
x =
math.random(-
5.0,
5.0);
enemyPos.
y =
0.2
enemyPos.
z =
math.random(-
5.0,
5.0);
enemy.
transform.
localPosition = enemyPos
enemy.
transform.
localScale = Vector3.
one *
0.1
end
end
TankCtrl = {};
local this = TankCtrl;
local transform;
local gameObject;
local horizontal;
local vertical;
local dir
this.
num =
0
--构建函数--
function
TankCtrl.New()
logWarn(
"TankCtrl.New--->>");
return this;
end
function
TankCtrl.Awake(
go)
logWarn(
"TankCtrl.Awake--->>");
gameObject = go
transform = go.
transform
this.
CC = gameObject:
GetComponent(
'CharacterController');
firePos = transform:
Find(
"FirePos").
gameObject
end
function
TankCtrl.Update(
go)
horizontal = Input.
GetAxis(
"Horizontal");
vertical = Input.
GetAxis(
"Vertical");
if horizontal~=
0
or vertical~=
0
then
print(horizontal)
print(vertical)
dir = Vector3.
New()
dir.
x = horizontal
dir.
y =
0;
dir.
z = vertical
this.
CC:
SimpleMove(dir *
10)
this.
pos = transform.
localPosition;
this.
pos.
x = Mathf.
Clamp(this.
pos.
x,-
5.0,
5.0)
this.
pos.
z = Mathf.
Clamp(this.
pos.
z,-
5.0,
5.0)
transform.
localPosition = this.
pos
end
if Input.
GetMouseButtonDown(
0)
then
this.
bullet =
newObject(StartCtrl.
bullet)
local o= BulletCtrl:
New(
nil,this.
bullet)
BulletCtrl.
bulletTable[this.
bullet] = o
for k,v
in
pairs(BulletCtrl.
bulletTable)
do
this.
num = this.
num +
1
end
print(
"----------------------------长度"..this.
num)
this.
bullet.
transform:
SetParent(firePos.
transform)
this.
bullet.
transform.
localPosition = Vector3.
zero
this.
bullet.
transform.
localScale = Vector3.
one
this.
bullet.
transform.
eulerAngles = firePos.
transform.
eulerAngles
this.
bullet.
transform.
parent =
nil
end
if this.
num >
0
then
for k,v
in
pairs(BulletCtrl.
bulletTable)
do
if(v ~=
nil)
then
v:
Update()
end
end
end
end
--------------------------------------------------------------------------------------------
BulletCtrl = {};
local this = BulletCtrl;
local transform;
local gameObject;
BulletCtrl.
bulletTable = {}
BulletCtrl.
num =
0
--构建函数--
function
BulletCtrl:New(
o,
go)
o = o
or {}
setmetatable(o,
self)
self.
__index =
self
o.
gameObject = go.
gameObject
o.
transform = o.
gameObject.
transform
return o
end
function
BulletCtrl:Update()
if
self ~=
nil
then
self.
transform:
Translate(-
self.
transform.
forward *
10 * Time.
deltaTime)
end
end
function
BulletCtrl.OnTriggerEnter(...)
local table = {
...}
if table[
2].
name ~=
"Tank1(Clone)"
then
print(
"碰撞")
BulletCtrl.
num = BulletCtrl.
num +
1
BulletCtrl.
bulletTable[table[
1]] =
nil
this.
OnDestroy(table[
1])
this.
OnDestroy(table[
2])
end
end
function
BulletCtrl.OnDestroy(
obj)
destroy(obj)
end