-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameScene.lua
More file actions
158 lines (116 loc) · 4.04 KB
/
gameScene.lua
File metadata and controls
158 lines (116 loc) · 4.04 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
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
gameScene = {}
gameScene.__index = gameScene
currentGame = {}
function gameScene.create()
print("new game scene")
local self = setmetatable({}, gameScene)
currentGame = game.new()
currentGame:startRound()
return self
end
function gameScene:update(dt)
world:update(dt)
local hit
if lovr.headset.wasPressed("hand/left", "trigger") then
hit = leftPointer:getHit()
else
if lovr.headset.wasPressed("hand/right", "trigger") then
hit = rightPointer:getHit()
end
end
if hit then
for i, box in ipairs(boxes) do
if box.collider == hit.collider then
currentGame:onHit(box)
end
end
end
if currentGame:isRoundFinished() then currentGame:startRound() end
return true
end
function gameScene:draw()
local leftHit = leftPointer:getHit()
local rightHit = rightPointer:getHit()
for i, hand in ipairs(lovr.headset.getHands()) do
local position = vec3(lovr.headset.getPosition(hand))
local direction = quat(lovr.headset.getOrientation(hand)):direction()
lovr.graphics.setColor(1, 1, 1)
lovr.graphics.sphere(position, .01)
lovr.graphics.print(hand, position, 0.02)
lovr.graphics.setColor(1, 0, 0)
lovr.graphics.line(position, position + direction * 50)
end
for i, box in ipairs(boxes) do game.drawBox(box, leftHit, rightHit) end
drawScore()
end
-- #endregion GameScene
-- #region Game
game = {}
game.__index = game
function game.new()
local self = setmetatable({}, game)
self.numbersVisible = true
self.score = 0
self.round = 0
tapSound = lovr.audio.newSource('short.wav')
return self
end
function game:startRound()
self.numbersVisible = true
self.round = self.round + 1
local usedCoordinates = {}
boxes = {}
local placedBlocks = 0
while placedBlocks <= self.round do
local r = math.random(0, 3)
local c = math.random(0, 4)
if usedCoordinates[r .. "," .. c] == nil then
usedCoordinates[r .. "," .. c] = true
placedBlocks = placedBlocks + 1
x = -0.25 + c * 0.25
y = .875 + r * 0.25
local box = world:newBoxCollider(x, y, -2.5, .25)
box:setKinematic(true)
local numberBlock = numberBlock.new(box, placedBlocks)
boxes[placedBlocks] = numberBlock
end
end
end
function game:isRoundFinished() return next(boxes) == nil end
function game:onHit(numberBlock)
if numberBlock.number == nextNeededNumber() then
tapSound:stop()
tapSound:play()
local removedbox = table.remove(boxes, 1)
removedbox:destroy()
self.numbersVisible = false
self.score = self.score + numberBlock.number
else
--TODO make this change state of the game to game over? Of een game over scene erboven? die highscores etc toont
--scenes[#scenes] = selectScene:create()
scenes[#scenes+1] = hiscoreScene:create(self.score, "HARDCORE ENDURANCE")
scenes[#scenes+1] = transitionScene:createFadeOut(0.5)
end
end
function game:areNumbersVisible() return self.numbersVisible end
function nextNeededNumber() return boxes[next(boxes)].number end
-- #endregion Game
function game.drawBox(numberbox, leftHit, rightHit)
if numberbox == nil then return end
local box = numberbox.collider
local x, y, z = box:getPosition()
local isHit = (leftHit and leftHit.collider == box) or
(rightHit and rightHit.collider == box)
local boxColor = isHit and {0.50, 0.100, 0.200} or {0.20, 0.70, 0.170}
lovr.graphics.setColor(boxColor)
lovr.graphics.cube('fill', x, y, z, .25, box:getOrientation())
if currentGame:areNumbersVisible() then
lovr.graphics.setColor(0.7, 0.6, 0)
lovr.graphics.print(numberbox.number, x, y, z + 0.15, 0.25)
end
end
function drawScore()
lovr.graphics.setColor(0.7, 0.6, 0)
lovr.graphics.print("Score: " .. currentGame.score, 0.75, 3.0, -3, 0.5, 0)
end
return gameScene