-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
146 lines (124 loc) · 3.58 KB
/
main.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
----------------------------
-- Author: M. Utku Altinkaya
----------------------------
require "support"
require "scene"
require "game"
require "menu"
--The Game Singleton, manages scenes and transitions
game = {}
function game:init()
self.state = nil
self.transition = nil
end
function game:draw()
if self.transition ~= 0 then --if transition taking place
if self.oldState then
effect:send("opacity", self.transition)
self.oldState:draw()
end
effect:send("opacity", 1-self.transition)
end
self.state:draw()
end
function game:update(dt)
self.state:update(dt)
if self.transition ~= 0 then -- update transition state
self.transition = math.max(self.transition-dt/1, 0)
if self.transition == 0 then
self.oldState = nil
effect:send("opacity", 1)
end
end
end
-- change current scene by initiating fade transition
function game:setState(newState)
self.transition = 1
self.oldState = self.state
self.state = newState
end
function game:keypressed( key, unicode )
self.state:keypressed( key, unicode )
end
function game:keyreleased( key, unicode )
self.state:keyreleased( key, unicode )
end
function game:mousepressed( x, y, button )
self.state:mousepressed( x, y, button )
end
------------------------------------------------------------------------
function love.load()
-- initialize game singleton
game:init()
-- define few globals and load resources
-- a resource loader and configuration can be useful but this was enough
-- for my purposes
font = { default = love.graphics.newFont(24),
large = love.graphics.newFont(28)}
color = {
main = {63,193,255},
text = {91,102,63},
gameOver = {0,0,0,255},
overlay = {255,255,255,235} }
sound = {
click = love.audio.newSource("assets/click.ogg", "static"),
fail = love.audio.newSource("assets/fail.ogg", "static")}
music = {
default = love.audio.newSource("assets/music.mp3")}
settings = {
difficulty = 3}
scoreboard = table.load("scores.dat") or
{ {name="utku", score=100},
{name="utku", score=200},
{name="utku", score=300},
{name="utku", score=400},
{name="utku", score=500}}
offX = 64
offY = 32
scoreForLines = {40, 100, 300, 1200}
windowWidth = 1024
windowHeight = 768
-- load block images for falling particles
blocks = {}
for i, name in ipairs({"blue", "lime", "lightBlue", "orange", "green", "purple", "red"}) do
table.insert(blocks, love.graphics.newImage("assets/"..name.."_blok.png"))
end
--game:setState(GameScene:create())
game:setState(MenuScene:create())
--game:setState(GameOverScene:create(190))
love.keyboard.setKeyRepeat(true)
love.graphics.setBackgroundColor(30, 30, 30)
-- I created this shader to have opacity control over entire scene, fade in transitions are
-- handled this way
effect = love.graphics.newPixelEffect [[
extern number opacity;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
{
vec4 pcolor = color * Texel(texture, texture_coords);
pcolor.a *= opacity;
return pcolor;
}
]]
love.graphics.setPixelEffect(effect)
end
function love.draw()
game:draw()
-- love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 10, 10)
end
function love.update(dt)
game:update(dt)
end
function love.keypressed( key, unicode )
game:keypressed( key, unicode )
end
function love.keyreleased( key, unicode )
game:keyreleased( key, unicode )
end
function love.mousepressed( x, y, button )
game:mousepressed( x, y, button )
end
function love.quit()
-- store highscores
table.save(scoreboard, "scores.dat")
end