Lua实现Unity堆叠消除小游戏,游戏功能每次随机生成四个不同形状的方块,将方块拖入10*10的格子棋盘,如果放入的棋盘格都有空格则可以放入方块,当放满一行或者一列时,消除满行满列的放入格。
- 创建拼图地图格数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
---拼图地图格数据
---@class ROPuzzleMapTileData
local m = {
---@type number 位置标记
index = 0,
---@type number 所在行
x = 0,
---@type number 所在列
y = 0,
---@type number 地图格上放置的方块ID 为0 表示空格
blockId = 0,
}
function m.New(index,x,y,id)
local o = Clone(m)
o:Init(index,x,y,id)
return o
end
function m:Init(index,x,y,id)
self.index = index
self.x = x
self.y = y
self.blockId = id
end
return m
- 创建拼图游戏数据结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
---@class ROPuzzleGameData
local m = {
---@type CfgRestaurantOperationPuzzleControlData
cfgPuzzleGameControlData = nil,
---@type number 当前积分
score = 0,
---@type ROPuzzleMapTileData[]
tileMaps = {},
---@type number 最大行数
maxLine = 10,
---@type number 最大列数
maxColumn = 10,
---@type number 单次生成的方块数量(上限为4)
randomBlock = {},
---@type boolean 游戏是否已经开始(点了开始按钮视为游戏开始)
isGameStart = false,
}
local ROPuzzleMapTileData = require("IQIGame.Module.CommonActivity.RestaurantOperation.ROPuzzleGame.ROPuzzleMapTileData")
function m.New(cfgData)
local o = Clone(m)
o:Init(cfgData)
return o
end
function m:Init(cfgData)
self.score = 0
self.cfgPuzzleGameControlData = cfgData
self.tileMaps = self:CreateTiles()
self.isGameStart = true
end
---开始新游戏,创建拼图地图
function m:CreateTiles()
local tab = {}
local index = 0
for line = 1, self.maxLine do
for column = 1, self.maxColumn do
index = index + 1
local tileData = ROPuzzleMapTileData.New(index,line,column,0)
tab[index] = tileData
end
end
return tab
end
---随机生成四个方块
function m:RefreshRandomBlock()
self.randomBlock = {}
local blockGroup = table.clone(self.cfgPuzzleGameControlData.BlockGroup)
---洗牌操作
for i = #blockGroup, 2, -1 do
local random_index = math.random(i)
blockGroup[i], blockGroup[random_index] = blockGroup[random_index],blockGroup[i]
end
---根据配置数据取方块
for i = 1, self.cfgPuzzleGameControlData.BlockNum do
local id = blockGroup[i]
table.insert(self.randomBlock,id)
end
end
---消除满行满列,并返回消除的行列
---@return number, number[],number[] 消除的总行数,同时消除的行和列
function m:Eliminate()
local lines = {}
local columns = {}
---行检测
for i = 1, self.maxLine do
local isFull = true
for j = 1, self.maxColumn do
local pos = (i-1) * self.maxLine + j
if self.tileMaps[pos].blockId == 0 then
isFull = false
break
end
end
if isFull then
table.insert(lines,i)
end
end
---列检测
for j = 1, self.maxColumn do
local isFull = true
for i = 1, self.maxLine do
local pos = (i-1) * self.maxLine + j
if self.tileMaps[pos].blockId == 0 then
isFull = false
break
end
end
if isFull then
table.insert(columns,j)
end
end
local totalNum = #lines + #columns
return totalNum,lines,columns
end
---增加积分
---@param score number
function m:AddScore(score)
self.score = self.score + score
end
---检测方块还有没有空地能放下
---@param blockCell PuzzleGameBlockCell
---@return boolean
function m:CheckBlockCellCanPutDown(blockCell)
local posTab = blockCell.blockData
for i, tileData in pairs(self.tileMaps) do
---找到了一个空格子
local canPutDown = false
if tileData.blockId == 0 then
---所有检测点都满足返回true
for j, pos in pairs(posTab) do
end
end
end
return false
end
return m
通过调用RefreshRandomBlock()方法,每次随机生成不重复的四个方块