Not a big update

This commit is contained in:
Squishy (C6H12O6+NaCl+H2O)
2024-04-13 22:34:57 +07:00
parent 47a884700f
commit a3bd3b6933
6 changed files with 142 additions and 8 deletions

137
game/VirtualControl.lua Normal file
View File

@@ -0,0 +1,137 @@
local gc_newQuad=love.graphics.newQuad
---Get distance between two points
---@param x1 number
---@param y1 number
---@param x2 number
---@param y2 number
---@return number
local function math_distance(x1,y1,x2,y2)
return ((x1-x2)^2+(y1-y2)^2)^.5
end
local function mDrawQ(obj,quad,x,y,a,k)
local _,_,w,h=quad:getViewport()
love.graphics.draw(obj,quad,x,y,a,k,nil,w*.5,h*.5)
end
local empty_quad=gc_newQuad(0,0,1,1,1,1)
-- A table containing quads used to draw icons for virtual control system.
local virtual_quad=setmetatable((function()
local t={}
local w=180
for i,name in next,{
'left','right','up','down',
'rotate_left','rotate_right','rotate_left2','rotate_right2',
} do if #name>0 then t[name]=gc_newQuad((i-1)%4*w,math.floor((i-1)/4)*w,w,w,5*w,7*w) end end
return t
end)(),{
__index=function() return empty_quad end
})
local virtual_texture=love.graphics.newImage('game/VirtualControlTexture.png')
local button={}
button.__index=button
function button:new(data)
local data=data or {}
return setmetatable({
show=data.show or true,
x=data.x or 320,
y=data.y or 180,
r=data.r or 80, -- size
shape=data.shape or 'circle',
key=data.key or 'X',
iconSize=data.iconSize or 80,
alpha=data.alpha or 0.75,
quad=virtual_quad[data.key]
},self)
end
function button:press()
self.pressed=true
self.lastPressTime=love.timer.getTime()
love.keypressed(self.key)
end
function button:release()
self.pressed=false
love.keyreleased(self.key)
end
function button:drag(dx,dy)
self.x,self.y=self.x+dx,self.y+dy
end
function button:draw(forceLight)
love.graphics.setLineWidth(4)
if self.shape=='circle' then
love.graphics.setColor(1,1,1,self.pressed and .5 or .05)
love.graphics.circle('fill',self.x,self.y,self.r-4)
love.graphics.setColor(1,1,1)
love.graphics.circle('line',self.x,self.y,self.r-2)
elseif self.shape=='square' then
love.graphics.setColor(1,1,1,self.pressed and .5 or .05)
love.graphics.rectangle('fill',self.x-self.r-4,self.y-self.r-4,self.r*2+8,self.r*2+8)
love.graphics.setColor(1,1,1)
love.graphics.rectangle('line',self.x-self.r-2,self.y-self.r-2,self.r*2+4,self.r*2+4)
end
if self.iconSize>0 and self.quad then
love.graphics.setColor(1,1,1,forceLight and 1 or self.alpha)
local _,_,w,h=self.quad:getViewport()
mDrawQ(
virtual_texture,
self.quad,
self.x,self.y,0,
self.iconSize/100*math.min(self.r*2/w,self.r*2/h)
)
end
end
function button:getDistance(x,y)
if self.shape=='circle' then
return math_distance(x,y,self.x,self.y)/self.r
elseif self.shape=='square' then
return math.max(math.abs(x-self.x),math.abs(y-self.y))/self.r
end
end
local touches={}
VCTRL={
button:new{x=50,y=50,key='left'},
}
VCTRL.focus=nil -- Focusing buttons
function VCTRL.press(x,y,id)
local obj,closestDist=false,1e99
for i=1,#VCTRL do
local w=VCTRL[i]
if w.show then
local d=w:getDistance(x,y)
if d<=1 and d<closestDist then
obj,closestDist=w,d
end
end
end
if obj then
touches[id]=obj
obj:press(x,y,id)
VCTRL.focus=obj
end
end
function VCTRL.release(id)
if touches[id] then
touches[id]:release()
end
touches[id]=nil
end
function VCTRL.drag(dx,dy,id)
if touches[id] then
touches[id]:drag(dx,dy)
end
end
function VCTRL.draw(forceLight)
for i=1,#VCTRL do
if VCTRL[i].show then
VCTRL[i]:draw(forceLight)
end
end
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View File

@@ -13,7 +13,7 @@ PENTO_MODE = false
SAVE_DIR = 'saves/'
REPLAY_DIR = 'saves/replays/'
if not love.filesystem.exists(REPLAY_DIR) then
if not love.filesystem.getInfo(REPLAY_DIR) then
love.filesystem.createDirectory(REPLAY_DIR)
end
CONFIG_FILE = 'config.sav'
@@ -29,6 +29,7 @@ function love.load()
require "load.bigint"
loadSave()
require "scene"
require "game.VirtualControl" -- VCTRL
love.mouse.setVisible(false)
love.window.setMode(love.graphics.getWidth(), love.graphics.getHeight(), {resizable = true});
@@ -58,9 +59,9 @@ function love.draw()
love.graphics.clear()
love.graphics.push()
-- get offset matrix
scene:render()
VCTRL.draw()
love.graphics.pop()
if DEBUG_showKey then

View File

@@ -29,7 +29,6 @@ function GameScene:new(player_name, replay_file, replay_grade)
rotate_left2=false,
rotate_right=false,
rotate_right2=false,
rotate_180=false,
hold=false,
}
self.inputs_waiting2trigger={ -- Used for buffering the input, in case the input is too fast (1f)
@@ -41,7 +40,6 @@ function GameScene:new(player_name, replay_file, replay_grade)
rotate_left2=false,
rotate_right=false,
rotate_right2=false,
rotate_180=false,
hold=false,
}
self.paused = false

View File

@@ -22,7 +22,7 @@ function NameEntryScene:new()
self.delete_confirm = false
self.delete_input_count = 0
self.gradeNames = {
"19k", "18k", "17k", "16k", "15k", "14k", "13k", "12k", "11k",
"19k", "18k", "17k", "16k", "15k", "14k", "13k", "12k", "11k",
"10k", "9k", "8k", "7k", "6k", "5k", "4k", "3k", "2k", "1k",
"1D", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D"
}

View File

@@ -26,7 +26,6 @@ function TrainingScene:new()
rotate_left2=false,
rotate_right=false,
rotate_right2=false,
rotate_180=false,
hold=false,
}
self.inputs_waiting2trigger={ -- Used for buffering the input, in case the input is too fast (1f)
@@ -38,7 +37,6 @@ function TrainingScene:new()
rotate_left2=false,
rotate_right=false,
rotate_right2=false,
rotate_180=false,
hold=false,
}
self.paused = false