-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlevelSelectionState.lua
147 lines (127 loc) · 4.46 KB
/
levelSelectionState.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
local State = require("state")
local Label = require("label")
local Map = require("map")
local Menu = require("menu")
local SelectionList = require("selectionList")
local CountdownState = require("countdownState")
local AdvancedSettingsState = require("advancedSettingsState")
local LevelSelectionState = {}
LevelSelectionState.__index = LevelSelectionState
setmetatable(LevelSelectionState, State)
function LevelSelectionState.create(parent)
local self = setmetatable(State.create(), LevelSelectionState)
self.inputs = parent.inputs
self.cursors = parent.cursors
self.bg = ResMgr.getImage("bg_stars.png")
self.imgBlueprint = ResMgr.getImage("blueprint.png")
self.imgDogear = ResMgr.getImage("blueprint_dogear.png")
self.imgPreview = ResMgr.getImage("preview_assets.png")
self.imgTexture = ResMgr.getImage("blueprint_texture.png")
self.batch = love.graphics.newSpriteBatch(self.imgPreview, 128)
self:addComponent(Label.create("SELECT A LEVEL", 0, 25, WIDTH, "center"))
self.list = self:addComponent(SelectionList.create(WIDTH/2-295, 62, 260, 15, 21, self))
self:updateMapList()
self.list:setSelection(config.level)
self.menu = self:addComponent(Menu.create(WIDTH/2, 300, 298, 32, 10, self))
self.menu:addButton("START GAME", "start")
self.menu:addButton("ADVANCED SETTINGS", "advanced")
self.menu:addButton("BACK", "back")
return self
end
function LevelSelectionState:enter()
MusicMgr.playMenu()
end
function LevelSelectionState:draw()
love.graphics.draw(self.bg, 0, 0)
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("fill", WIDTH/2, 62, 297, 228)
love.graphics.setColor(255, 194, 49)
love.graphics.rectangle("line", WIDTH/2+0.5, 62.5, 297, 228)
love.graphics.setColor(255,255,255)
love.graphics.draw(self.imgBlueprint, WIDTH/2+8, 70)
love.graphics.draw(self.batch, WIDTH/2+11, 73)
love.graphics.draw(self.imgDogear, WIDTH/2+265, 70)
love.graphics.setBlendMode("multiply")
love.graphics.draw(self.imgTexture, WIDTH/2+11, 73)
love.graphics.setBlendMode("alpha")
end
function LevelSelectionState:buttonPressed(id, source)
if id == "advanced" then
playSound("quack")
pushState(AdvancedSettingsState.create(self))
elseif id == "start" then
playSound("quack")
popState()
pushState(IngameState.create(self, self:getFilename()))
pushState(CountdownState.create())
elseif id == "back" then
playSound("quack")
popState()
end
end
function LevelSelectionState:getFilename()
return self.list:getSelection()
end
function LevelSelectionState:updateMapList()
local items = {}
local labels = {}
local files = love.filesystem.getDirectoryItems("res/maps")
for i,v in ipairs(files) do
table.insert(items, "res/maps/" .. v)
table.insert(labels, v:upper())
end
files = love.filesystem.getDirectoryItems("usermaps")
for i,v in ipairs(files) do
table.insert(items, "usermaps/" .. v)
table.insert(labels, "CUSTOM: " .. v:upper())
end
self.list:setItems(items, labels)
end
function LevelSelectionState:selectionChanged(source)
playSound("click")
self.batch:clear()
local quadSub = love.graphics.newQuad(0, 23, 23, 22, 78, 63)
local quadPit = love.graphics.newQuad(0, 0, 23, 23, 78, 63)
local quadFenceHor = love.graphics.newQuad(23, 0, 28, 5, 78, 63)
local quadFenceVer = love.graphics.newQuad(35, 7, 5, 28, 78, 63)
local quadSpawnUp = love.graphics.newQuad(44, 23, 25, 18, 78, 63)
local quadSpawnRight = love.graphics.newQuad(32, 48, 32, 11, 78, 63)
local quadSpawnDown = love.graphics.newQuad(1, 45, 25, 18, 78, 63)
local quadSpawnLeft = love.graphics.newQuad(46, 7, 32, 11, 78, 63)
local map = Map.create(self:getFilename())
-- Add tiles
for iy=0,8 do
for ix=0,11 do
local tile = map:getTile(ix, iy)
if tile >= 10 and tile <= 14 then
self.batch:add(quadSub, ix*23, iy*23+1)
elseif tile == 2 then
self.batch:add(quadPit, ix*23, iy*23)
elseif tile == 4 then
self.batch:add(quadSpawnUp, ix*23-1, iy*23+2)
elseif tile == 5 then
self.batch:add(quadSpawnRight, ix*23-1, iy*23+9)
elseif tile == 6 then
self.batch:add(quadSpawnDown, ix*23-1, iy*23+9)
elseif tile == 7 then
self.batch:add(quadSpawnLeft, ix*23-8, iy*23+9)
end
end
end
-- Add fences
for iy=0,8 do
for ix=0,11 do
local wall = map:getWall(ix, iy)
if iy > 0 and wall % 2 == 1 then
self.batch:add(quadFenceHor, ix*23-3, iy*23-3)
end
if ix > 0 and wall > 1 then
self.batch:add(quadFenceVer, ix*23-3, iy*23-3)
end
end
end
end
function LevelSelectionState:leave()
config:update("level", self.list.selection)
end
return LevelSelectionState