Untitled
2 years ago in Plain Text
-- TODO: phase out global variables
-- p.s. yeah right. this makes my functions look cleaner
size = 50
shipAngle = 0
arenaW = 1024
arenaH = 768
function locXY(locx, locy, globx, globy, angle)
tempx = locx * size
tempy = locy * size
newx = (tempx * math.cos(angle) - tempy * math.sin(angle)) + shipV.OX
newy = (tempx * math.sin(angle) + tempy * math.cos(angle)) + shipV.OY
return newx, newy
end
function scaleshape(locx, locy, globx, globy, sca)
tx = locx * size * sca + globx % arenaW
ty = locy * size * sca + globy % arenaH
return tx, ty
end
function updateshape(x, y, angle, speed, dt)
x = (x + math.cos(angle) * speed * dt)
y = (y + math.sin(angle) * speed * dt)
return x, y
end
-- loads function variables
function love.load()
background = love.graphics.newImage("grid.png")
success = love.window.setMode(arenaW, arenaH, {resizable=true, highdpi = true})
-- moving velocities
rotateSpeed = 10
accelSpeed = 100
rockSpeed = 30
-- cursor vertex dimensions
shipV = {
Xspeed = 0,
Yspeed = 0,
theta = 0,
OX = arenaW / 2,
OY = arenaH / 2,
C = {
AX = 1,
AY = 0, -- front right
BX = -1,
BY = 1, -- top left
CX = 0,
CY = 0, -- center
DX = -1,
DY = -1 -- bottom left
},
erts = {}
}
-- shapes
rockSize = 60
rockSpeed = 80
shapes = { hex = {
v = {
0,
1,
-1,
0.5,
-1,
-0.5,
0,
-1,
1,
-0.5,
1,
0.5
},
color = {0, 0, 0}
}, square = {
v = {
1,
1,
-1,
1,
-1,
-1,
1,
-1
},
color = {0, 0, 0}
}, tri = {
v = {
0,
1,
-1,
-1,
1,
-1
},
color = {0, 0, 0}
},
}
for model, shape in pairs(shapes) do
scale = math.random(0,2)
shape.angle = love.math.random() * (2 * math.pi)
shape.size = rockSize
shape.speed = rockSpeed
shape.OX = arenaW + math.random(-arenaW, arenaW) % arenaW
shape.OY = arenaH + math.random(-arenaH, arenaH) % arenaH
for i = 1, #shape.color do
shape.color[i] = math.random()
end
for i = 1, #shape.v, 2 do
shape.v[i], shape.v[i + 1] = scaleshape(shape.v[i], shape.v[i + 1], shape.OX, shape.OY, scale)
end
end
end
function shipfn(dt)
-- Basic Ship Controls
-- rotation controls
if love.keyboard.isDown('right') then
shipV.theta = shipV.theta + rotateSpeed * dt
end
if love.keyboard.isDown('left') then
shipV.theta = shipV.theta - rotateSpeed * dt
end
-- movement controls
if love.keyboard.isDown('up') then
shipV.Xspeed = shipV.Xspeed + math.cos(shipV.theta) * accelSpeed * dt
shipV.Yspeed = shipV.Yspeed + math.sin(shipV.theta) * accelSpeed * dt
end
if love.keyboard.isDown('down') then
shipV.Xspeed = shipV.Xspeed - math.cos(shipV.theta) * accelSpeed * dt
shipV.Yspeed = shipV.Yspeed - math.sin(shipV.theta) * accelSpeed * dt
end
end
-- asteroid data
function asteroidfn(dt)
-- ASTEROIDS --
-- TODO: make polygons and add bounce
for model, shape in pairs(shapes) do
shape.OX = (shape.OX + math.cos(shape.angle) * shape.speed * dt) % arenaW
shape.OY = (shape.OY + math.sin(shape.angle) * shape.speed * dt) % arenaH
for i = 1, #shape.v, 2 do
shape.v[i], shape.v[i + 1] = updateshape(shape.v[i], shape.v[i + 1], shape.angle, shape.speed, dt)
end
end
end
-- update loop
function love.update(dt)
-- update ship data
shipfn(dt)
-- update shapes
asteroidfn(dt)
-- takes in default vectors and outputs updated values
tax, tay = locXY(shipV.C.AX, shipV.C.AY, shipV.OX, shipV.OY, shipV.theta)
tbx, tby = locXY(shipV.C.BX, shipV.C.BY, shipV.OX, shipV.OY, shipV.theta)
tcx, tcy = locXY(shipV.C.CX, shipV.C.CY, shipV.OX, shipV.OY, shipV.theta)
tdx, tdy = locXY(shipV.C.DX, shipV.C.DY, shipV.OX, shipV.OY, shipV.theta)
shipV.erts = {tax, tay, tbx, tby, tcx, tcy, tdx, tdy}
-- wrappers / update function
shipV.theta = shipV.theta % (2 * math.pi)
shipV.OX = (shipV.OX + shipV.Xspeed * dt) % arenaW
shipV.OY = (shipV.OY + shipV.Yspeed * dt) % arenaH
end
-- what do you think draw does?!
function love.draw()
for y = -1, 1 do
for x = -1, 1 do
love.graphics.setColor(1, 1, 1)
love.graphics.draw(background, arenaW, arenaH)
love.graphics.origin()
love.graphics.translate(x * arenaW, y * arenaH)
love.graphics.setColor(0.2, 0, 1)
love.graphics.polygon('fill', shipV.erts)
for shapesIndex, shape in pairs(shapes) do
love.graphics.setColor(shape.color[1], shape.color[2], shape.color[3])
love.graphics.polygon('fill', shape.v)
end
end
end
love.graphics.origin()
love.graphics.setColor(0,0,0)
love.graphics.print(table.concat({
-- 'shipX: '..shipV.OX,
-- 'shipY: '..shipV.OY,,
'shipAX: '..shipV.erts[1] - arenaW / 2,
'shipAY: '..-1 * (shipV.erts[2] - arenaH / 2),
'shipBX: '..shipV.erts[3] - arenaW / 2,
'shipBY: '..-1 * (shipV.erts[4] - arenaH / 2),
'shipCX: '..shipV.erts[5] - arenaW / 2,
'shipCY: '..-1 * (shipV.erts[6] - arenaH / 2),
'shipDX: '..shipV.erts[7] - arenaW / 2,
'shipDY: '..-1 * (shipV.erts[8] - arenaH / 2),
'triAX: '..shapes.tri.v[1],
'triAY: '..shapes.tri.v[2],
'triBX: '..shapes.tri.v[3],
'triBY: '..shapes.tri.v[4],
'triCX: '..shapes.tri.v[5],
'triCY: '..shapes.tri.v[6],
-- 'shipSpdX '..shipSpeedX,
-- 'shipSpdY '..shipSpeedY
},'\n')
)
end