其他分享
首页 > 其他分享> > 坦克大战的Lua实现

坦克大战的Lua实现

作者:互联网

坦克大战lua代码


--[
author:终是蝶衣梦晓楼
language:Lua
website:https://developers.mini1.cn/wiki/luawh.html#lua%E6%95%B0%E7%BB%84
data:2021,2,19
--]

return (function()

local X, Y, Z = 0, 0, 0 --初始出生点
local Map_Wid, Map_Len = -1,-1 --地图横长/竖宽

local Players = {} --本地玩家Id
local PlayerPos = {} --复活点
local TmCityPos = {} --玩家城池
local TmCityIdx = -1 --城池方块ID

local tickCount = 0   --ticker计数
local newTicker = 20  --每10Tick刷新
local isGameEnded = false --防止多次调用
local isGameStart = false --游戏是否已开始
local isGRuleInit = false --规则是否已设置

local bulletIdx = 15003
local LoserIdx = -1 --失败队伍
local itemcTime = 0 --随机计时

--武器类 408->冲锋枪 409->重机枪 410->狙击枪 450->子弹
local wpBlocks = {[408]={idx=15000, num=1}, [409]={idx=15005, num=1},
				  [450]={idx=15003, num=64}, [410]={idx=15004, num=1}}
local bufItems = {11023, 11024, 11025, 11015} --铁铲等Item
local itemObjIds = {} --道具ID
local wpBlockPos = {} --武器位置

local Teamx = {red=1, blue=2} --游戏队伍
local Gamex = {win=1, lose=2} --游戏结果
local CellType = {def=1, river=2, wall=3, point=4, city=5}   --地图元素
local TmEffects = {[Teamx.red]={glEffectId=-1, lastTime = 0},--雾圈效果
				   [Teamx.blue]={glEffectId=-1, lastTime = 0}}

-----------------地图相关-----------------
local datax_map = [[19,23,,,,,,,,,,,,
*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
*,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,*
*,c,q,q,q,q,q,q,q,q,q,q,q,q,q,q,q,c,*
*,c,q,0,0,0,0,y,m,x,0,0,0,0,0,0,q,c,*
*,c,q,0,0,0,r,x,x,x,r,0,0,0,0,0,q,c,*
*,c,q,0,0,0,0,0,0,0,0,0,0,0,0,0,q,c,*
*,c,q,0,0,0,0,0,0,0,u,u,u,u,u,u,q,c,*
*,c,q,x,y,y,x,x,q,v,v,v,v,v,v,v,q,c,*
*,c,q,0,0,0,0,w,w,w,w,w,w,w,w,w,q,c,*
*,c,q,0,0,0,0,y,0,0,u,y,u,u,u,0,q,c,*
*,c,q,0,y,0,y,0,0,y,0,0,y,0,0,0,q,c,*
*,c,q,0,0,0,q,y,0,0,0,y,q,0,0,0,q,c,*
*,c,q,0,0,0,y,0,0,y,0,0,y,0,y,0,q,c,*
*,c,q,0,u,u,u,y,u,0,0,y,0,0,0,0,q,c,*
*,c,q,w,w,w,w,w,w,w,w,w,0,0,0,0,q,c,*
*,c,q,v,v,v,v,v,v,v,q,x,x,y,y,x,q,c,*
*,c,q,u,u,u,u,u,u,0,0,0,0,0,0,0,q,c,*
*,c,q,0,0,0,0,0,0,0,0,0,0,0,0,0,q,c,*
*,c,q,0,0,0,0,0,b,x,x,x,b,0,0,0,q,c,*
*,c,q,0,0,0,0,0,0,x,m,y,0,0,0,0,q,c,*
*,c,q,q,q,q,q,q,q,q,q,q,q,q,q,q,q,c,*
*,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,*
*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*]]

function GetCellIdx(ccType)
	local cellIdx = -1 --默认不绘制

	if ccType == "q" then cellIdx = 1   	--基石-外墙
	elseif ccType == "c" then cellIdx = 8   --地砖-外墙
	elseif ccType == "*" then cellIdx = 678 --蓝砖-外墙
	elseif ccType == "r" then cellIdx = 991 --红队重生点
	elseif ccType == "b" then cellIdx = 992 --蓝队重生点
	elseif ccType == "u" then cellIdx = 218 --果木叶
	elseif ccType == "v" then cellIdx = 221 --红杉树叶
	elseif ccType == "x" then cellIdx = 502 --石砖
	elseif ccType == "y" then cellIdx = 101 --土砖
	elseif ccType == "m" then cellIdx = 254 --桃树
	elseif ccType == "w" then cellIdx = 3 end--河流

	return cellIdx
end
TmCityIdx = GetCellIdx('m')

function InitMapData()
	local MapData = {} --地图数据

	MapData['offset_x'] = 100
	MapData['offset_z'] = 100

	local datax = {}
	local listm = string.split(datax_map, "\n")
	for idx, line in pairs(listm) do
		if idx == 1 then --第一行获取地图宽高
			local iter = string.gmatch(line, "(%d+),")
			MapData['width'] = iter()
			MapData['height'] = iter()
		else --获取地图数据
			datax[idx-1] = string.split(line, ",")
		end
	end
	-- 设置地图data数据
	MapData['data'] = datax

	return MapData
end
-----------------工具类函数-----------------
-- 分割字符串
function string.split(str, mark)
 	if string.len(str)==0 then return nil end

	local sIdx, tsub = 1, {}
	while true do --遍历截取字符串
		local pos = string.find(str, mark, sIdx, true)
	   	if not pos then break end

	    table.insert(tsub, string.sub(str, sIdx, pos-1))
	    sIdx = pos + string.len(mark)
	end
  	table.insert (tsub, string.sub(str, sIdx))

	return tsub
end
-- 获取字典的keys
function table.keys(tab)
    local keys = {}
    for key, val in pairs(tab) do
        keys[#keys+1] = key
    end
    return keys
end

-----------------自定义函数-----------------
function InitCenterPoint()
	local ret = -1
	-- 获取出生点,并以此设置地图中心
	ret, X, Y, Z = Spawnport:getSpawnPoint()

	--地平面的y轴坐标为8
	if Y==0 then Y = 6 end
end

function InitColorBlock()

	local blockIdx = 737--简易罐子/方块碰撞
	Block:setBlockAll(0, Y+1, 0, blockIdx)
	--408, 409, 450--
	local blocks = table.keys(wpBlocks)
	Block:setBlockAll(-1, Y+1, -1, blocks[1])
	Block:setBlockAll(-1, Y+1, -2, blocks[2])

	Block:setBlockAll(-3, Y+1, -3, blocks[3])

	Block:setBlockAll(-5, Y+1, -5, 1037)
	Block:setBlockAll(-5, Y+1, -6, 1038)
	if true then return end

	local xPos = 1
	for idx = 667, 682 do
		xPos = xPos + 1
		local zPos = 1
		for data = 0, 15 do
			zPos = zPos + 1
			Block:setBlockAll(xPos, Y+1, zPos, idx, data)
		end
	end
end

function InitGameMaps(...)
	local data_map = InitMapData()
	if not data_map['data'] then return end

	local step = 1
	local floorIds = {421, 425}
	local ccdata = data_map['data']
	Map_Wid = tonumber(data_map['width'])
	Map_Len = tonumber(data_map['height'])
	for idx = 1, Map_Len do
		local xPos = X+(idx-1)*2+1
		step = step + 2
		for jdx = 1, Map_Wid do
			local zPos = Z+(jdx-1)*2+1

			step = step + 1 --地板
			mod = math.mod(step, 2)
			local floorId = floorIds[mod+1]

			local ccType = ccdata[idx][jdx]
			local cellIdx = GetCellIdx(ccType)

			local cellType = CellType.def
			if ccType=='w' then cellType = CellType.river end --河流/覆盖空气墙
			if ccType=='*' then cellType = CellType.wall end --围墙/覆盖空气墙
			if ccType=='r' or ccType=='b' then cellType = CellType.point end --复活点
			if ccType=='m' then cellType = CellType.city end --城池/被摧毁后失败

			if cellType~=CellType.city then
				InitMapCell(cellIdx, xPos, zPos, floorId, cellType)
			else --需要保护的城池
				drawBigTree(cellIdx, 255, xPos, 0, zPos)
				TmCityPos[#TmCityPos+1] = {xPos=xPos, zPos=zPos}
				if not TmEffects[Teamx.red].xPos then
					TmEffects[Teamx.red].xPos = xPos
					TmEffects[Teamx.red].zPos = zPos
				else
					TmEffects[Teamx.blue].xPos = xPos
					TmEffects[Teamx.blue].zPos = zPos
				end
			end

		end
	end
end

function InitMapCell(cellId, cxPos, czPos, floorId, cellType)
	-- 绘制地板红砖
	for xPos = cxPos-1, cxPos do
		for zPos = czPos-1, czPos do
			Block:setBlockAll(xPos, Y, zPos, floorId)
		end
	end
	if cellId <= 0 then return end

	-- 绘制红蓝双方复活点
	if cellType==CellType.point then
		--设置靠原点的
		if czPos < Map_Wid then czPos = czPos+1 end
		local playPos = {x=cxPos-1, y=Y+1, z=czPos-1}
		Block:setBlockAll(playPos.x, playPos.y, playPos.z, cellId)
		PlayerPos[#PlayerPos+1] = playPos
		return
	end

	-- 绘制地面建筑/河流
	local cyPos = cellType==CellType.river and Y-1 or Y+1
	for yPos = cyPos, cyPos+1 do
		for xPos = cxPos-1, cxPos do
			for zPos = czPos-1, czPos do
				Block:setBlockAll(xPos, yPos, zPos, cellId)
			end
		end
	end

	-- 给建筑覆盖空气墙
	if cellType==CellType.river or cellType==CellType.wall then
		local cyPos = cellType==2 and Y+1 or Y+3
		local blkIdx = cellType==2 and 1018 or 1001
		for yPos = cyPos, cyPos+1 do --覆盖空气墙
			for xPos = cxPos-1, cxPos do
				for zPos = czPos-1, czPos do
					Block:setBlockAll(xPos, yPos, zPos, blkIdx)
				end
			end
		end
	end
end

function drawBigTree(trunkId, leafId, cx, cy, cz)

	local direct = 4
	local tyPos = 0 --设置树干
	local txP1, txP2 = X+cx-1, X+cx
	local tzP1, tzP2 = Z+cz-1, Z+cz

	for idx = 1, 10 do--树高
		tyPos = Y + cy + idx
		for xPos = txP1, txP2 do
			for zPos = tzP1, tzP2 do
				Block:placeBlock(trunkId, xPos, tyPos, zPos, direct)
			end
		end
	end

	--设置树叶/N层树叶
	for level = 3, -3, -1 do
		direct = 0 --第N层树叶
		local yPos = tyPos+1-level
		for idx = 1, level do --每层树叶圈
			for xPos = txP1-idx, txP2+idx do
				for zPos = tzP1-idx, tzP2+idx do
					local isXInTrunk = xPos>txP1 and xPos<txP2
					local isYInTrunk = zPos>tzP1 and zPos<tzP2
					if not (isXInTrunk and isXInTrunk) then
						Block:placeBlock(leafId, xPos, yPos, zPos, direct)
					end
				end
				direct = direct + 2
			end
		end

		if level <= 0 then --上层树叶
			for xPos = txP1-level, txP2+level do
				for zPos = tzP1-level, tzP2+level do
					Block:placeBlock(leafId, xPos, yPos, zPos, direct)
				end
				direct = direct + 2
			end
		end
	end
end

function InitPlayerData(playerId)
	if not isGameStart then return end

	--清空背包
	--Backpack:clearPack(playerId)

	--初始化武器类型
	local itemId, itemCnt = 15004, 1 --狙击枪
	Player:gainItems(playerId, itemId, itemCnt, 1)
	local itemId, itemCnt = 15002, 1 --左轮枪
	Player:gainItems(playerId, itemId, itemCnt, 1)
	local itemId, itemCnt = 15003, 64*3--64发子弹
	Player:gainItems(playerId, itemId, itemCnt, 1)

	-- local itemId, itemCnt = 15002, 1
	-- Player:gainItems(playerId, itemId, itemCnt, 1)
	-- local itemId, itemCnt = 15004, 1
	-- Player:gainItems(playerId, itemId, itemCnt, 1)
	-- local itemId, itemCnt = 15005, 1
	-- Player:gainItems(playerId, itemId, itemCnt, 1)
end

function InitGameRule()
	isGRuleInit = true
	GameRule.EndTime = 6  		 --游戏时长
	GameRule.TeamNum = 2         --队伍数量
	GameRule.TimeLocked = 1  	 --当前时间
	GameRule.MaxPlayers = 4      --最大玩家量
	GameRule.CameraDir = 1       --缺省正视角:1
	GameRule.StartMode = 0       --开启模式 0:主开
	GameRule.StartPlayers = 1    --最低玩家量 2人
	GameRule.PlayerDieDrops = 1  --死亡掉落 1:true
	GameRule.DisplayScore = 1    --显示比分 1:true
	GameRule.AllowMidwayJoin = 1 --中途加入 1:允许
	GameRule.ScoreKillPlayer = 1 --击杀玩家 得1分
	GameRule.BlockDestroy = 1    --方块可被摧毁 1:true
	GameRule.WinLoseEndTime = 1  --游戏超时结束则全胜
end

--重置tick次数
function IsCanTick()
	tickCount = tickCount+1

	if tickCount>=newTicker then
		tickCount = 0
		return true
	end

	return false
end

--获取可放置方块的位置
function GetPositionX(x1,x2,z1,z2)
	x1 = x1 or 3*2+1
	z1 = z1 or 3*2+1
	x2 = x2 or (Map_Len-3)*2
	z2 = z2 or (Map_Wid-3)*2

	local count = 1
	local ret, blkId = 0, 0
	local xPos, zPos = 0, 0
	while true do
		count = count + 1

		xPos = math.random(x1, x2)
		zPos = math.random(z1, z2)

		ret, blkId = Block:getBlockID(xPos, Y+1, zPos)
		if ret==ErrorCode.OK and blkId==0 then break end

		if count>99 then --防止死循环
			print("Get Pos Error===>>>")
			return nil
		end
	end
	return xPos, zPos
end

--设置玩家位置
function SetPositionPx(playerId)
	if not isGameStart then return end
	if not playerId or playerId<=0 then return end

	local ret, teamId = Player:getTeam(playerId)
	if ret ~= ErrorCode.OK then return end

	local index = 1
	for idx, val in pairs(Players) do
		if val==playerId then index=idx end
	end
	local teamTdx = teamId==Teamx.red and 0 or 1
	local playIdx = index + teamTdx
	playIdx = math.mod(playIdx-1, 4)+1 --四个复活点

	local playPos = PlayerPos[playIdx]
	if not playPos then playPos = PlayerPos[1] end
	Actor:setPosition(playerId, playPos.x+0.5, playPos.y, playPos.z+0.5);
end

--清理地图上的方块/道具
function clearItems()
	for idx = 1, #wpBlockPos do
		local wpPos = wpBlockPos[idx]
		local ret, blockId = Block:getBlockID(wpPos.x, wpPos.y, wpPos.z)
		local ret = Block:destroyBlock(wpPos.x, wpPos.y, wpPos.z)
	end

	for idx = 1, #itemObjIds do
		local objId = itemObjIds[idx]
		local ret = World:despawnItemByObjid(objId)
	end

	wpBlockPos = {}--重置方块表
	itemObjIds = {}--重置道具表
end

--随机生成武器方块/铁铲道具
function rgensItems(gType)
	--生成坦克大战的铁铲
	if gType == 'T' then
		local ccIdx = math.random(1, #bufItems)
		local xPos, zPos = GetPositionX()
		local ret, objIds = World:spawnItem(xPos, Y+1, zPos, bufItems[ccIdx], 1)
		if ret==ErrorCode.OK then itemObjIds[#itemObjIds+1] = objIds end
		return
	end

	local blocks = table.keys(wpBlocks)
	if gType=='S' then --生成特殊枪型:狙击枪
		local xPos, zPos = GetPositionX()
		local ret = Block:setBlockAll(xPos, Y+1, zPos, blocks[#blocks])
		if ret == ErrorCode.OK then
			wpBlockPos[#wpBlockPos+1] = {x=xPos, y=Y+1, z=zPos}
		end
		return
	end

	--生成普通枪械或子弹
	local wpsz = #blocks-1
	local idx1 = math.random(1, wpsz)
	local idx2 = math.random(1, wpsz)

	while idx1==idx2 do
		idx2 = math.random(1, wpsz)
	end

	for idx = idx1, idx2, idx2-idx1 do
		local xPos, zPos = GetPositionX()
		local ret = Block:setBlockAll(xPos, Y+1, zPos, blocks[idx])
		if ret == ErrorCode.OK then
			wpBlockPos[#wpBlockPos+1] = {x=xPos, y=Y+1, z=zPos}
		end
	end
end

-----------------开发者事件-----------------
Game_Started = function()
	tickCount = 0
	itemcTime = 0
	isGameStart = true
	if not isGRuleInit then InitGameRule() end

	InitCenterPoint()
	InitGameMaps()
	--InitColorBlock()

	--主机PlayInit设置失败
	local ret, playerId = Player:getMainPlayerUin()
	if ret~=ErrorCode.OK or playerId<=0 then return end

	Backpack:clearAllPack(playerId)
	Player:setTeam(playerId, Teamx.red)

	InitPlayerData(playerId)--初始化玩家信息
	SetPositionPx(playerId) --初始化玩家位置

	--复活保护BUFF<BuffId要除1000>/GameRule可设置
	local buffId = math.floor(999002/1000)
	Player:addBuff(playerId, buffId, 2, 10000)

	local buffId1 = math.floor(27003/1000)
	Player:addBuff(playerId, buffId1, 2, 500000)
end

Game_RunX = function()
	if isGameEnded then return end
	if not IsCanTick() then return end
	itemcTime = itemcTime + 1

	for idx = 1, #TmEffects do -- 刷新各队雾圈
		local lastTime = TmEffects[idx].lastTime-1
		local glEffectId = TmEffects[idx].glEffectId
		if lastTime<=0 and glEffectId~=-1 then
			local ret = Game:removeRenderGlobalEffect(glEffectId)
		 	if ret == ErrorCode.OK then glEffectId = -1 end

		 	TmEffects[idx].glEffectId = glEffectId
		end
		TmEffects[idx].lastTime = lastTime
	end

	-- 每30秒刷新随机道具 15秒后清理
	if math.mod(itemcTime, 50)==0 then rgensItems('S')
	elseif math.mod(itemcTime, 30)==0 then rgensItems('N')
	elseif math.mod(itemcTime, 15)==0 then clearItems() end

	local randVal = math.random(1, 100) --随机生成铁铲
	if math.mod(randVal, 16)==0 then rgensItems('T') end
end

Game_Ended = function()
	if LoserIdx > 0 then --非超时结束
		local ret, playerId = Player:getMainPlayerUin()
		if ret~=ErrorCode.OK or playerId<=0 then return end
		local ret, teamId = Player:getTeam(playerId)
		if ret~=ErrorCode.OK or teamId<=0 then return end

		if LoserIdx==Teamx.blue then
			Battle_Ended(Teamx.red, Gamex.win)

			local strIdx = teamId==Teamx.red and 270 or 271
			local ret, txtInfox = Game:getDefString(strIdx)
			Chat:sendChat(txtInfox,  chatType)
		else
			Battle_Ended(Teamx.blue, Gamex.win)
			local strIdx = teamId==Teamx.blue and 270 or 271
			local ret, txtInfox = Game:getDefString(strIdx)
			Chat:sendChat(txtInfox,  chatType)
		end
	end
end

Game_TimeOver = function()

end

Player_Inited = function(args)
	if not isGRuleInit then InitGameRule() end

	local playerId = args['eventobjid']
	Players[#Players+1] = playerId

	local playerCount = #Players
	if math.mod(playerCount, 2)==1 then--单数红队
		Player:setTeam(playerId, Teamx.red)
	else --双数蓝队
		Player:setTeam(playerId, Teamx.blue)
	end

	--[[
	--红蓝两队人数
	local ret1, rtNum = Team:getTeamPlayers(Teamx.red, -1)
	local ret2, btNum = Team:getTeamPlayers(Teamx.blue, -1)
	if ret1 ~= ErrorCode.OK or ret1 ~= ErrorCode.OK then return end

	if rtNum <= btNum then--两队人数一样/优先加入红队
		local ret = Player:setTeam(playerId, Teamx.red)
		if ret==ErrorCode.OK then PlayerTdx = rtNum+1 end
	else
		local ret = Player:setTeam(playerId, Teamx.blue)
		if ret==ErrorCode.OK then PlayerTdx = rtNum+1 end
	end
	--]]
end

Player_JoinTeam = function(trigger_obj)
	local playerId = trigger_obj['eventobjid']

	if isGameStart then--游戏已经开始
		InitPlayerData(playerId)

		local ret, teamId = Player:getTeam(playerId)
		if teamId and teamId>0 then SetPositionPx(playerId) end
	end
end

Player_Dead = function(trigger_obj)
	local killById = trigger_obj['toobjid']
	local playerId = trigger_obj['eventobjid']

	--暂时不做处理 //TODO
end

Player_Revive = function(trigger_obj)
	local playerId = trigger_obj['eventobjid']

	--添加复活保护buff
	local buffId = math.floor(999002/1000)
	Player:addBuff(playerId, buffId, 2, 20*500)--默认5T

	SetPositionPx(playerId)
	InitPlayerData(playerId)
end

BackPack_AddItem = function(trigger_obj)
	local itemId = trigger_obj['itemid']
	local itemNum = trigger_obj['itemnum']
	local playerId = trigger_obj['eventobjid']

	--local bufItems = {11023, 11024, 11025, 11015}
	local lastTime = 0
	if itemId==bufItems[1] then lastTime=5
	elseif itemId==bufItems[2] then lastTime=10
	elseif itemId==bufItems[3] then lastTime=15
	elseif itemId==bufItems[4] then end

	if lastTime ~= 0 then--绘制雾圈
		local ret, teamId = Player:getTeam(playerId)
		local effectEntity = TmEffects[teamId]
		if effectEntity ~= nil then
			local oldTime = effectEntity.lastTime
			effectEntity.lastTime = oldTime+lastTime
			if effectEntity.glEffectId == -1 then
				local ret, glEffectId = Game:addRenderGlobalEffect("particles/Fog.ent")
				if ret==ErrorCode.OK and glEffectId >= 0 then
					Game:setRenderGlobalEffectPos(glEffectId, effectEntity.xPos, Y, effectEntity.zPos)
					Game:setRenderGlobalEffectScale(glEffectId, 1.25, 0.045, 1.25)
				end
			end
		end
	end
end

Block_Destroyed = function(trigger_obj)
	local blockId = trigger_obj.blockid
	local x, y, z = trigger_obj.x, trigger_obj.y, trigger_obj.z

	local item = wpBlocks[blockId]
	if item ~= nil then --设置道具获取
		--直接创建武器道具
		local ret, objIds = World:spawnItem(x, y, z, item.idx, item.num)
		if ret==ErrorCode.OK then itemObjIds[#itemObjIds+1] = objIds end

		--Or 直接放进玩家背包
		--local ret = Backpack:enoughSpaceForItem(actorId, item.idx, item.num)
		--if ret==ErrorCode.OK then Player:gainItems(actorId, item.idx, item.num, 1) end
	end

	if blockId==TmCityIdx then --城池被破坏
		for idx = 1, #TmCityPos do
			local cityIsDestry = true
			local cxPos = TmCityPos[idx].xPos
			local czPos = TmCityPos[idx].zPos
			for yPos=Y+1, Y+2 do--树干全被摧毁
				for xPos=cxPos-1, cxPos do
					for zPos=czPos-1, czPos do
						local ret, blkId= Block:getBlockID(xPos, yPos, zPos)
						if blkId == TmCityIdx then
							cityIsDestry = false
							break
						end
					end
				end
			end

			if cityIsDestry then
				LoserIdx = idx
				Game:doGameEnd()
				isGameEnded = true
				return
			end
		end
	end
end

--计算游戏结果
function Battle_Ended(teamId, retType)
	if teamId == Teamx.red then
		-- 设置红队游戏结果
		Team:setTeamResults(Teamx.red, retType)
		Team:setTeamPlayersResults(Teamx.red, retType)
		-- 设置蓝队游戏结果
		local blueRet = (retType==Gamex.win and Gamex.lose) or Gamex.win
		Team:setTeamResults(Teamx.blue, blueRet)
		Team:setTeamPlayersResults(Teamx.blue, blueRet)
		--print("Battle1 ================>>>", teamId, retType, blueRet)
	elseif teamId == Teamx.blue then
		-- 设置蓝队游戏结果
		Team:setTeamResults(Teamx.blue, retType)
		Team:setTeamPlayersResults(Teamx.blue, retType)
		-- 设置红队游戏结果
		local redRet = (retType==Gamex.win and Gamex.lose) or Gamex.win
		Team:setTeamResults(Teamx.red, redRet)
		Team:setTeamPlayersResults(Teamx.red, redRet)
		--print("Battle2 ================>>>", teamId, retType, redRet)
	end

	ShowGBattleUI() --设置战斗总结界面
end

--游戏结算界面
function ShowGBattleUI()
	-- 设置排行/分数
	local ret, rank = Player:getGameRanking()
	if not rank or rank==0 then rank = 1 end
    local ret, score = Player:getGameScore()

    -- 队伍数量及成员量
    local ret1, teamNum = Team:getNumTeam()
    local ret2, redNum = Team:getTeamPlayers(Teamx.red)
    local ret3, blueNum = Team:getTeamPlayers(Teamx.blue)
  	local totalPlayers = 1
  	if ret2==ErrorCode.OK and ret3==ErrorCode.OK then
  		totalPlayers = redNum+blueNum
  	end

  	local ret, txtTitle = Game:getDefString(rank==1 and 8028 or 8029)
  	local ret, txtTRank = Game:getDefString(8032)
  	local ret, txtRanker = Game:getDefString(8030)
  	local ret, txtDefeat = Game:getDefString(3176)
	UI:setGBattleUI('left_title', txtTRank..tostring(rank))--第1/1
	UI:setGBattleUI('right_title', "/"..totalPlayers)
	UI:setGBattleUI('left_desc', txtTitle)--大吉大利/继续努力
	UI:setGBattleUI('left_little_desc', txtRanker..tostring(rank))--排名
	UI:setGBattleUI('right_little_desc', txtDefeat..tostring(score))--得分
	UI:setGBattleUI('battle_btn', false)
	UI:setGBattleUI('result', false)
	UI:setGBattleUI('result_bkg', false)
	UI:setGBattleUI('reopen', true)
end

--添加监听事件--
ScriptSupportEvent:registerEvent([=[Game.Start]=], Game_Started)
ScriptSupportEvent:registerEvent([=[Game.Run]=], Game_RunX)
ScriptSupportEvent:registerEvent([=[Game.End]=], Game_Ended)
ScriptSupportEvent:registerEvent([=[Game.TimeOver]=], Game_TimeOver)

ScriptSupportEvent:registerEvent([=[Player.Init]=], Player_Inited)
ScriptSupportEvent:registerEvent([=[Player.Die]=], Player_Dead)
ScriptSupportEvent:registerEvent([=[Player.Revive]=], Player_Revive)
ScriptSupportEvent:registerEvent([=[Player.JoinTeam]=], Player_JoinTeam)
ScriptSupportEvent:registerEvent([=[Player.AddItem]=], BackPack_AddItem)

ScriptSupportEvent:registerEvent([=[Actor.Die]=], Monster_Dead)
ScriptSupportEvent:registerEvent([=[Block.DestroyBy]=], Block_Destroyed)

end)()

坦克大战lua源代码分享

reproduced by 迷你世界开发者文档

转自:https://developers.mini1.cn/wiki/luawh.html#lua%E6%95%B0%E7%BB%84

不知道为什么这串代码在luaeditor中运行的时候会有报错,原因不详,我把报错情况放到下面:
f:\desktop\demo.lua:718: in function <f:\desktop\demo.lua:1>
Stack Traceback:
f:\desktop\demo.lua:718: in function <f:\desktop\demo.lua:1>
(tail call): ?
另外我的电脑上面的lua环境好像是有点问题,不知道到底是不是环境变量的原因。。。。

最后用我最熟悉的python向大家拜一个晚年吧~

for i in range (2021):
	print("祝大家新年快乐,牛年大吉!")

标签:xPos,end,zPos,idx,--,大战,Lua,坦克,local
来源: https://blog.csdn.net/SUMPLUSS/article/details/113870773