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, scale)
tempx = locx * scale
tempy = locy * scale
newx = (tempx * math.cos(angle) - tempy * math.sin(angle)) + globx
newy = (tempx * math.sin(angle) + tempy * math.cos(angle)) + globy
return newx, newy
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
},
erts = {},
color = {0, 0, 0}
}, square = {
v = {
1,
1,
-1,
1,
-1,
-1,
1,
-1
},
erts = {},
color = {0, 0, 0}
}, tri = {
v = {
0,
1,
-1,
-1,
1,
-1
},
erts = {},
color = {0, 0, 0}
},
}
for model, shape in pairs(shapes) do
shape.angle = (math.random() * 100000) % (2 * math.pi) / math.random(1,3)
shape.size = rockSize * math.random(1,3)
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() * math.random() % 1
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 + shape.speed * dt) % arenaW
shape.OY = (shape.OY + shape.speed * dt) % arenaH
shape.angle = shape.angle % (2 * math.pi)
for i = 1, #shape.v, 2 do
shape.erts[i], shape.erts[i + 1] = locXY(shape.v[i], shape.v[i + 1], shape.OX, shape.OY, shape.angle, shape.size)
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, size)
tbx, tby = locXY(shipV.C.BX, shipV.C.BY, shipV.OX, shipV.OY, shipV.theta, size)
tcx, tcy = locXY(shipV.C.CX, shipV.C.CY, shipV.OX, shipV.OY, shipV.theta, size)
tdx, tdy = locXY(shipV.C.DX, shipV.C.DY, shipV.OX, shipV.OY, shipV.theta, size)
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.erts)
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.erts[1] - arenaW / 2,
'triAY: '..(shapes.tri.erts[2] - arenaH / 2) * -1,
'triBX: '..shapes.tri.erts[3] - arenaW / 2,
'triBY: '..(shapes.tri.erts[4] - arenaH / 2) * -1,
'triCX: '..shapes.tri.erts[5] - arenaW / 2,
'triCY: '..(shapes.tri.erts[6] - arenaH / 2) * -1,
-- 'shipSpdX '..shipSpeedX,
-- 'shipSpdY '..shipSpeedY
},'\n')
)
end