aboutsummaryrefslogtreecommitdiffstats
path: root/Game.lua
blob: c4cc8f55d0a82575f4beeeb76a1ce76390e53145 (plain)
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
local Game = {}

local buttonRadius = 5

function Game:init()
	self.button = love.graphics.newCanvas( 100, 100, "normal", 16 )
	love.graphics.setCanvas(self.button)
		self.button:clear()
		love.graphics.setBlendMode('alpha')
		love.graphics.setColor(0, 0, 0, 255)
		love.graphics.circle( "fill", 50, 50, (buttonRadius/100)*Config.height, 100 )
		love.graphics.setColor(255, 0, 0, 255)
		love.graphics.circle( "fill", 50, 50, ((buttonRadius-1)/100)*Config.height, 100 )
	love.graphics.setCanvas()

	self.circle = love.graphics.newCanvas( 100, 100, "normal", 16 )
	love.graphics.setCanvas(self.circle)
		self.circle:clear()
		love.graphics.setBlendMode('alpha')
		love.graphics.setColor(0, 0, 0, 128)
		love.graphics.circle( "fill", 50, 50, (buttonRadius/100)*Config.height, 100 )
		love.graphics.setColor(255, 0, 0, 128)
		love.graphics.circle( "fill", 50, 50, ((buttonRadius-1)/100)*Config.height, 100 )
	love.graphics.setCanvas()
end

function Game:enter(previous, song, id)
	self.song = song
	self.level = id
	for k,v in pairs(self.song.bpms) do self.beatDuration = 60000*4/v break end
	--self.beatDuration = 60000*4/self.song.bpms["0.000"]
	self.music = love.audio.newSource(song.file:match("(.*/)") .. song.music)
	love.graphics.setBackgroundColor(104, 136, 248)
end

local tx=0
local ty=0
local tp=0

function love.touchpressed(x, y, p)
	tp = p
	tx = x
	ty = y
end	

local speed = 50
local offset = -1000*45/speed
local timer = offset
local musicplaying = false
local lastnote = -1
local beat = 0
local noteTime = 0
local beatTime = 0

local displayedNotes = {}

function Game:update(dt)
	if timer < 0 then
		timer = timer + dt*1000
	else
		if not musicplaying then
			musicplaying = true
			self.music:play()
		end
		timer = self.music:tell("seconds")*1000
	end

	for k, v in pairs(displayedNotes) do
		if v.x > 95-buttonRadius or v.y > 95-buttonRadius or v.x < 5-buttonRadius or v.y < 5-buttonRadius then
			table.remove(displayedNotes, k)
		elseif v.orientation == "left" then
			displayedNotes[k].y = v.y + dt*speed
		elseif v.orientation == "down" then
			displayedNotes[k].x = v.x + dt*speed
		elseif v.orientation == "up" then
			displayedNotes[k].y = v.y - dt*speed
		elseif v.orientation == "right" then
			displayedNotes[k].x = v.x - dt*speed
		end
	end

	beat = math.ceil((timer-offset)/self.beatDuration)
	if beat < 1 then return end

	noteTime = self.beatDuration/#self.song.notes[self.level][SMFileSchema.NotesInfos.noteData][2][beat]
	beatTime = (timer-offset)-(beat-1)*self.beatDuration
	notes = math.ceil(beatTime/noteTime)

	if lastnote ~= notes then
		lastnote = notes
		notes = self.song.notes[self.level][SMFileSchema.NotesInfos.noteData][2][beat][notes]
		if notes[1] == "1" then table.insert(displayedNotes, { x = 50-buttonRadius, y = 50-buttonRadius, orientation = "left" }) end
		if notes[2] == "1" then table.insert(displayedNotes, { x = 50-buttonRadius, y = 50-buttonRadius, orientation = "down" }) end
		if notes[3] == "1" then table.insert(displayedNotes, { x = 50-buttonRadius, y = 50-buttonRadius, orientation = "up" }) end
		if notes[4] == "1" then table.insert(displayedNotes, { x = 50-buttonRadius, y = 50-buttonRadius, orientation = "right" }) end
	end

end

function Game:draw()
	love.graphics.clear()
	love.graphics.setColor(255, 0, 0)
	love.graphics.setBlendMode('alpha')
	love.graphics.print(timer, 320, 300)
	for k,v in pairs(self.song.bpms) do love.graphics.print(v, 200, 300) end
	love.graphics.print(beat, 200, 200)
	love.graphics.print(lastnote, 200, 100)
	love.graphics.print(noteTime, 200, 50)
	love.graphics.print(beatTime, 200, 150)

		love.graphics.print(tp, 350, 150)
		love.graphics.print(tx, 370, 150)
		love.graphics.print(ty, 390, 150)

	love.graphics.setBlendMode('premultiplied')
	love.graphics.draw(self.circle, Utils.percentCoordinates(5-buttonRadius, 50-buttonRadius))
	love.graphics.draw(self.circle, Utils.percentCoordinates(50-buttonRadius, 95-buttonRadius))
	love.graphics.draw(self.circle, Utils.percentCoordinates(50-buttonRadius, 5-buttonRadius))
	love.graphics.draw(self.circle, Utils.percentCoordinates(95-buttonRadius, 50-buttonRadius))
	--love.graphics.draw(self.button, Utils.percentCoordinates(x, 50))

	for k, v in pairs(displayedNotes) do
		love.graphics.draw(self.button, Utils.percentCoordinates(v.x, v.y))
	end
end

return Game