-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilding.lua
More file actions
84 lines (65 loc) · 2.26 KB
/
building.lua
File metadata and controls
84 lines (65 loc) · 2.26 KB
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
require 'floor'
function Building(number_of_floors)
local building = {}
local floors = {}
local speed = 1.0
local current_floor_offset_y,
target_floor_offset_y,
animation_offset_per_second,
start_animation_offset_y
local next_floor_sound
function building:load(...)
for i = 1, number_of_floors do
local floor = Floor()
floor:load(...)
floors[#floors + 1] = floor
end
next_floor_sound = love.audio.newSource("sfx/next_floor.ogg", static)
building.currentFloor = 0
current_floor_offset_y = -number_of_floors * floors[1].height
target_floor_offset_y = current_floor_offset_y
animation_offset_per_second = 0
end
function building:update(dt, player, slowDownTime)
current_floor_offset_y = current_floor_offset_y + dt * animation_offset_per_second
if current_floor_offset_y > target_floor_offset_y then
current_floor_offset_y = target_floor_offset_y
animation_offset_per_second = 0
end
for i = 1, #floors do
local isCurrentFloor = (#floors - i) == building.currentFloor and not self:isMoving()
floors[i]:update(dt, player, slowDownTime, isCurrentFloor)
end
end
function building:draw(offset_x, offset_y)
for i = 1, number_of_floors do
floors[i]:draw(offset_x, offset_y + i * floors[1].height + current_floor_offset_y)
end
end
function building:moveUp()
next_floor_sound:play()
building.currentFloor = building.currentFloor + 1
target_floor_offset_y = -(number_of_floors - building.currentFloor) * floors[1].height
animation_offset_per_second = speed * (target_floor_offset_y - current_floor_offset_y)
start_animation_offset_y = current_floor_offset_y
end
function building:canMoveUpAt(x, y)
if building.currentFloor < number_of_floors and not self:isMoving() then
return floors[number_of_floors - building.currentFloor]:canMoveUpAt(x, y)
end
return false
end
function building:isMoving()
return target_floor_offset_y ~= current_floor_offset_y
end
function building:moveProgress()
return math.abs(current_floor_offset_y - start_animation_offset_y) / (target_floor_offset_y - start_animation_offset_y)
end
function building:getLimit()
return floors[1].width, floors[1].height
end
function building:getCurrentFloor()
return building.currentFloor
end
return building
end