-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameScreen.lua
201 lines (166 loc) · 5.08 KB
/
GameScreen.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
-- GameScreen
StaticObject = require "StaticObject"
local GameScreen = {}
GameScreen.__index = GameScreen
GameScreen.font = love.graphics.newImageFont('Resources/Fonts/numberFont.png', " 1234567890", 1)
GameScreen.magicSymbol = love.graphics.newImage('Resources/Images/magicSymbol.png')
GameScreen.lowMagicSymbol = love.graphics.newImage('Resources/Images/lowMagicSymbol.png')
GameScreen.veryLowMagicSymbol = love.graphics.newImage('Resources/Images/veryLowMagicSymbol.png')
function GameScreen:init(folder, player)
self.portals ={}
self.background = love.graphics.newImage(folder .. '/background.png')
self.objectMap = love.image.newImageData(folder .. '/object_map.png')
self.objects = {}
self.interactables = {}
self:addPlayer(player)
self:initObjects()
love.graphics.setFont(GameScreen.font)
self.timer = 0
self.timeBetweenFrames = 0
self.lastTimeBetweenFrames = 0
end
function GameScreen:reset()
love.graphics.setFont(GameScreen.font)
end
function GameScreen:draw()
if self.dirty then
self.sort(self.objects, function (a, b)
return a:getBaseCentreY() > b:getBaseCentreY()
end
)
love.graphics.scale( 2, 2 )
love.graphics.draw(self.background, 0, 0)
self.dirty = false
for i = 1, table.getn(self.objects) do
self.objects[i]:draw()
end
if player.magic < 10 then
love.graphics.draw(GameScreen.veryLowMagicSymbol, 4, 4)
love.graphics.setColor(1, 116/255, 116/255, 1)
love.graphics.printf("" .. self.player.magic, 14, 6, 400, 'left')
love.graphics.setColor(1, 1, 1, 1)
elseif player.magic < 25 then
love.graphics.draw(GameScreen.lowMagicSymbol, 4, 4)
love.graphics.setColor(1, 243/255, 137/255, 1)
love.graphics.printf("" .. self.player.magic, 14, 6, 400, 'left')
love.graphics.setColor(1, 1, 1, 1)
else
love.graphics.draw(GameScreen.magicSymbol, 4, 4)
love.graphics.printf("" .. self.player.magic, 14, 6, 400, 'left')
end
self.lastTimeBetweenFrames = self.timeBetweenFrames;
self.timeBetweenFrames = 0
love.graphics.printf("" .. self.lastTimeBetweenFrames, 300, 4, 400, 'left')
love.graphics.scale( 0.5, 0.5 )
self.dirty = false
else
self.timeBetweenFrames = self.timeBetweenFrames + 1
end
end
function GameScreen:update(dt)
self.timer = self.timer + dt
local disheveledCount = 0
for i = 1, table.getn(self.objects) do
if self.objects[i].dirty == true then
self.dirty = true
elseif self.objects[i].disheveled ~= nil and self.objects[i].disheveled then
disheveledCount = disheveledCount + 1
end
end
if disheveledCount > self.maxDisheveled then
self.dirty = true
end
for i = 1, table.getn(self.interactables) do
local a = self.interactables[i]
if a == nil or (a.dying ~= nil and a.dying == true) then
table.remove(self.interactables, i)
i = i - 1
end
end
for i = 1, table.getn(self.objects) do
local a = self.objects[i]
if a == nil or ( a.dying ~= nil and a.dying == true ) then
table.remove(self.objects, i)
i = i - 1
elseif self.objects[i].update ~= nil then
local tmp = self.objects[i]:update()
if tmp ~= nil then return tmp end
end
end
end
function GameScreen:addStaticObject(path, x, y)
local tmpObj = StaticObject.new(path)
tmpObj:setPos(x, y)
table.insert(self.objects, tmpObj)
end
function GameScreen:placeRandomly(object)
local canPlaceObject = false
local x = 0
local y = 0
while (canPlaceObject == false) do
x = math.modf(math.random(0, 399 - tmpObj:getWidth()))
y = math.modf((math.random(0 + tmpObj:getBaseOffset(), 301 - tmpObj:getHeight())))
canPlaceObject = self:canPlace(tmpObj, x, y)
end
tmpObj:setX(x)
tmpObj:setY(y)
end
function GameScreen:canPlace(object, x, y)
for i=0, object:getWidth() do
local r1, g1, b1, a1 = self.objectMap:getPixel(x + i, y)
local r2, g2, b2, a2 = self.objectMap:getPixel(x + i, y + object:getHeight())
if (r1 == 0 and g1 == 0 and b1 == 0) or (r2 == 0 and g2 == 0 and b2 == 0) then
return false
end
end
for j=0, object:getHeight() do
local r1, g1, b1, a1 = self.objectMap:getPixel(x, y + j)
local r2, g2, b2, a2 = self.objectMap:getPixel(x + object:getWidth(), y + j)
if (r1 == 0 and g1 == 0 and b1 == 0) or (r2 == 0 and g2 == 0 and b2 == 0) then
return false
end
end
return true
end
function GameScreen:addPlayer(player)
self.player = player
self.player:setObjectMap(self.objectMap)
-- print("player")
self:addObject(self.player)
end
function GameScreen:addObject(object)
table.insert(self.objects, object)
end
function GameScreen:keyreleased( key )
return self.player:keyreleased(key, self.interactables)
end
function GameScreen.isBefore(a, b)
return a.getY() < b.getY()
end
function GameScreen.sort( arr, compare )
length = table.getn(arr)
if length <= 1 then
return
end
for i = 1, length - 1 do
for j=i + 1, 2, -1 do
while (compare(arr[j - 1], arr[j])) do
tmp = arr[j - 1]
arr[j - 1] = arr[j]
arr[j] = tmp
end
end
end
end
function GameScreen:keypressed( key )
if (self.player ~= nil) then
self.player:keypressed( key )
end
end
--Constructor
-- function GameScreen.new(folder)
-- local self = setmetatable({}, GameScreen)
-- self:init(folder)
-- return self
-- end
return GameScreen