This repository has been archived by the owner on Nov 30, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameGrid.lua
158 lines (134 loc) · 3.19 KB
/
GameGrid.lua
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
----------------
--[ GameGrid ]--
----------------
require 'Cube'
GameGrid = {
size = { w = 400, h = 800 },
pos = { x = 10, y = 10},
grid = {},
rods = {},
cubes = {},
block = {},
numBlocksCleared = 0,
gameOver = 0,
updateRate = 0.5,
timePassed = 0.0,
}
------------------------
--[ GameGrid Methods ]--
------------------------
function GameGrid:new()
o = {}
setmetatable(o, self)
self.__index = self
return o
end
function GameGrid:loadGameGrid()
--Load with correct level... ect.--
local x, y
for x = 1, 10 do
self.grid[x] = {}
for y = 1, 20 do
self.grid[x][y] = 0
end
end
love.keyboard.setKeyRepeat(0.25,0.05)
math.randomseed( os.time() )
--Spawn New Block--
self:spawnBlock()
end
function GameGrid:update(dt)
local dir = "none"
local x = self.cubes[#self.cubes].pos.x
local y = self.cubes[#self.cubes].pos.y
self.timePassed = self.timePassed + dt
if self.timePassed >= self.updateRate then
self.timePassed = self.timePassed - self.updateRate
if y < ((self.size.h/40)-1) then
self.grid[x+1][y+1] = 0
dir = self:checkIfBlock(x, y, "down")
end
if dir == "down" then
self.cubes[#self.cubes]:move(dir)
self.grid[self.cubes[#self.cubes].pos.x+1][self.cubes[#self.cubes].pos.y+1] = 1
else
--Spawn New Block--
self.grid[self.cubes[#self.cubes].pos.x+1][self.cubes[#self.cubes].pos.y+1] = 1
self:spawnBlock()
end
end
end
function GameGrid:draw()
--Draw Game Grid (just a white rect for now)--
if self.gameOver == 1 then
love.graphics.setColor(100, 100, 100)
else
love.graphics.setColor(230, 230, 230)
end
love.graphics.rectangle("fill", self.pos.x, self.pos.y, self.size.w, self.size.h)
--Draw Cubes--
local i
for i = 1, #self.cubes do
self.cubes[i]:draw()
end
end
function GameGrid:keypressed(key, unicode)
--Move Test Cube--
local dir = "none"
local x = self.cubes[#self.cubes].pos.x
local y = self.cubes[#self.cubes].pos.y
if key == "left" or key == "a" then
if x > 0 then
self.grid[x+1][y+1] = 0
dir = self:checkIfBlock(x, y, "left")
end
elseif key == "right" or key == "d" then
if x < ((self.size.w/40)-1) then
self.grid[x+1][y+1] = 0
dir = self:checkIfBlock(x, y, "right")
end
elseif key == "down" or key == "s" then
if y < ((self.size.h/40)-1) then
self.grid[x+1][y+1] = 0
dir = self:checkIfBlock(x, y, "down")
end
end
self.cubes[#self.cubes]:move(dir)
self.grid[self.cubes[#self.cubes].pos.x+1][self.cubes[#self.cubes].pos.y+1] = 1
end
function GameGrid:spawnBlock()
--Check if GameOver--
local x
for x = 1, 10 do
if self.grid[x][1] == 1 then
--GameOver--
self.gameOver = 1
end
end
if self.gameOver == 0 then
--Pick Random Color--
local color = math.random(0, 4)
--Make Cubes for now--
local b = Cube:new(color)
table.insert(self.cubes, b)
self.grid[self.cubes[#self.cubes].pos.x+1][self.cubes[#self.cubes].pos.y+1] = 1
end
end
--Returns direction to move--
function GameGrid:checkIfBlock(x, y, dir)
local v = dir
if dir == "left" then
if self.grid[x][y+1] == 1 then
v = "none"
end
elseif dir == "right" then
if self.grid[x+2][y+1] == 1 then
v = "none"
end
elseif dir == "down" then
if self.grid[x+1][y+2] == 1 then
v = "none"
end
end
return v
end