Yes, when using your concept this may not be something which really disturbs the game flow, but as you will be able to see, that doesn’t really match my project…
-- Line
-- Use this function to perform your initial setup
function setup()
-- Game Settings
Players = 1
Speed = 2.5
-- Game Settings
parameter.watch("getFps()")
gameState = 0
player = {}
touches = {}
colorTable = {color(255, 0, 0, 255), color(0, 255, 0, 255), color(0, 0, 255, 255),
color(255, 255, 0, 255), color(0, 255, 255, 255), color(255, 0, 255, 255)}
p = {}
p.spawnBorderDistance = 80
p.initialThickness = 17.5
g = {}
g.countdownDuration = 4
g.controlSpeed = 3.5
g.outerScreenSpace = 100
g.mapSizePosCorrection = vec2(g.outerScreenSpace/2, g.outerScreenSpace/2)
for i = 1, Players do
local initialColor = colorTable[i]
local initialDirection = math.random(0, 360)
--local initialDirection = 0
local initialPosition = vec2(math.random(p.spawnBorderDistance, WIDTH-p.spawnBorderDistance),
math.random(p.spawnBorderDistance, HEIGHT-p.spawnBorderDistance))
--local initialPosition = vec2(WIDTH/2, HEIGHT/2)
local initialThickness = p.initialThickness
local initialInvincibility = 3
table.insert(player, {col = initialColor, dir = initialDirection, pos = initialPosition,
prevPos = initialPosition, thick = initialThickness, alive = true,
inv = initialInvincibility})
end
map = image(WIDTH+g.outerScreenSpace, HEIGHT+g.outerScreenSpace)
setContext(map)
fill(0, 0, 0, 255)
rect(-g.outerScreenSpace/2, -g.outerScreenSpace/2, WIDTH+g.outerScreenSpace, HEIGHT+g.outerScreenSpace)
setContext()
counter1 = 0 -- couldnt think of anything else
counter2 = 0
end
-- process touches
function touched(touch)
if touch.state == ENDED then
touches[touch.id] = nil
else
touches[touch.id] = touch
end
end
function draw()
background(0, 0, 0, 255)
fill(0, 0, 0, 255)
counter1 = counter1 + 1
if counter1 > 2 then counter1 = 0 end
if gameState == 0 then
-- Menu
resetStyle()
--noStroke()
fill(127, 127, 127, 255)
ellipse(WIDTH/2, HEIGHT/3, 300)
-- check if button is touched
for k,touch in pairs(touches) do
local pos = vec2(touch.x, touch.y)
if pos:dist(vec2(WIDTH/2, HEIGHT/3)) < 150 then
g.countdown = g.countdownDuration-0.01 --smooth countdown display
gameState = 1
end
end
end
if gameState == 1 then
-- draw map background
sprite(map, WIDTH/2, HEIGHT/2)
fill(0, 0, 0, 0)
strokeWidth(10)
stroke(255, 255, 255, 255)
rect(-5, -5, WIDTH+10, HEIGHT+10)
if not (g.countdown < 0) then
fill(222, 222, 222, 255)
font("HelveticaNeue-Light")
fontSize(40)
text(math.floor(g.countdown), WIDTH/2, HEIGHT-30)
g.countdown = g.countdown - DeltaTime
end
if g.countdown < 1 then
drawPlayers(false, true)
updatePlayers()
else
drawPlayers(true, false)
end
local continue = nil
for i,pl in pairs(player) do
if pl.alive then
continue = true
break
end
end
if not continue then
gameState = 3
end
end
if gameState == 2 then
-- Ended
end
end
function drawPlayers(showDirections, drawLine)
for i,pl in pairs(player) do
local pos = pl.pos
local oldPos = pl.prevPos
local d = pl.dir
local c = pl.col
local t = pl.thick
noStroke()
fill(c)
ellipse(pos.x, pos.y, t)
if showDirections then
resetMatrix()
translate(pos.x, pos.y)
rotate(-d)
stroke(c)
strokeWidth(6)
line(0, 0, 0, t*2.3)
line(0, t*2.3, -t/1.5, t*1.3)
line(0, t*2.3, t/1.5, t*1.3)
end
if drawLine then
if not (pl.inv > 0) then -- check if player doesn't have invincibility tag
-- draw line to map
resetMatrix()
setContext(map)
lineCapMode(SQUARE)
stroke(c)
strokeWidth(t-2)
local lineOldPos = oldPos + vecPos(d-180, 2) + g.mapSizePosCorrection
local lineNewPos = pos + vecPos(d, 2) + g.mapSizePosCorrection
line(lineOldPos.x, lineOldPos.y, lineNewPos.x, lineNewPos.y)
setContext()
end
end
end
end
function updatePlayers()
for i,pl in pairs(player) do
if pl.alive then
for k,touch in pairs(touches) do
if touch.x < WIDTH/2 then
player[i].dir = player[i].dir - g.controlSpeed
else
player[i].dir = player[i].dir + g.controlSpeed
end
end
local dir = pl.dir
local pos = pl.pos
if pl.inv > 0 then -- update invincibility time
player[i].inv = pl.inv - DeltaTime
end
player[i].prevPos = pos
local acc = vecPos(dir, Speed)
player[i].pos = pos + acc
end
end
checkCollisions() -- this is where it comes to the really slow part
end
function checkCollisions()
for i,pl in pairs(player) do -- perform func for all players
if pl.alive then
for angle = -70, 70, 70 do
local position = pl.pos -- position.
local direction = pl.dir -- direction, the player is looking (moving)
local thickness = pl.thick -- thickness of the player
local checkPos = position + vecPos(direction+angle, thickness/1.3) + g.mapSizePosCorrection --last is just a constant, don't mind
red, green, blue = map:get(checkPos.x, checkPos.y) -- get color of the map at the specific point
local checksum = red+green+blue -- add all colors together
if checksum > 0 then -- see if any of the colors were above 0, -> NOT black
player[i].alive = false -- kill player
end
end
end
end
end
-- helping functions that are not game specific
-- i wrote these a year ago or something...
function getFps()
if frames == nil then frames = {} end
table.insert(frames, 1/DeltaTime)
if #frames > 60 then
table.remove(frames, 1)
end
local framesSum = 0
for i = 1, #frames do
framesSum = framesSum + frames[i]
end
local fpsCalc = math.floor(framesSum/#frames)
return fpsCalc
end
function vecPos(vec, dist)
-- Calculate Vector Data
local x = dist * math.sin(math.rad(vec))
local y = dist * math.cos(math.rad(vec))
local v = vec2(x, y)
return v
end
This was indeed the easiest way, but also the worst, in my case^^