-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainscene.lua
126 lines (105 loc) · 2.99 KB
/
mainscene.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
require "objects.player"
require "objects.world1"
require "objects.world2"
camx, camy = 0, 0
MainScene = Scene:extend()
function MainScene:new(world)
self.level = world or 1
self.animduration = 0.15
self.deathtime = 0
self.player = Player(0, 0)
self.state = 1
self.t = 0
self.world = nil
if self.level == 1 then
self.world = World1(self.player)
elseif self.level == 2 then
self.world = World2(self.player)
end
camx = self.player.x * 16 + 8
camy = self.player.y * 16 + 4
end
function MainScene:update(dt)
-- Animation state machine
if self.state == 1 then -- State 1: Wait for player and move whites
if self.player.alive then
if self.player:step() then
for i,v in ipairs(self.world:getPieces()) do
if v ~= self.player and v.team == "white" then
v:step()
end
end
self.world:sortPieces()
self.state = 2
end
else
if love.keyboard.isDown("return") then
current_scene = MainScene()
end
end
elseif self.state == 2 then -- State 2: Animate whites
self.t = self.t + dt
if self.t > self.animduration then
self.t = 0
self.state = 3
end
elseif self.state == 3 then -- State 3: Move blacks
for i,v in ipairs(self.world:getPieces()) do
if v.team == "black" then
v:step()
end
end
self.world:sortPieces()
self.state = 4
elseif self.state == 4 then -- State 4: Animate blacks
self.t = self.t + dt
if self.t > self.animduration then
self.t = 0
self.state = 1
end
end
-- Load next world
if self.world.cleared then
current_scene = MainScene(self.level + 1)
end
-- Start death screen animation
if not self.player.alive then
if self.deathtime == 0 then
self.deathtime = love.timer.getTime()
end
end
-- Update camera
camx = camx + (self.player.x * 16 - camx + 8) * dt * 4
camy = camy + (self.player.y * 16 - camy + 4) * dt * 4
end
function MainScene:draw()
-- Calculate animation offsets for each team
local white_t = 1
local black_t = 1
if self.state == 4 then
black_t = self.t / self.animduration
elseif self.state == 2 then
white_t = self.t / self.animduration
end
-- Draw world
self.world:draw(white_t, black_t)
-- Death screen
if self.state == 1 and not self.player.alive then
local w, h = getDimensions()
local tw = textWidth("YOU DIED")
local diff = love.timer.getTime() - self.deathtime
love.graphics.setColor(0, 0, 0, math.min(diff, 0.5))
love.graphics.rectangle("fill", 0, 0, w, h)
love.graphics.resetColor()
love.graphics.setColor(1, 0, 0, math.min(diff, 1))
drawText((w - tw) / 2, h / 2 - 8, "YOU DIED")
love.graphics.resetColor()
if diff > 1.5 then
diff = diff - 1.5
tw = textWidth("Press RETURN to restart")
love.graphics.setColor(0.8, 0.8, 0.8, math.min(diff, 1))
drawText((w - tw) / 2, h / 2 - 8 + 32, "Press RETURN to restart")
love.graphics.resetColor()
end
end
end