coalesce game table
This commit is contained in:
parent
67b2a620c5
commit
46dc6d16f6
1 changed files with 47 additions and 47 deletions
|
|
@ -1,57 +1,55 @@
|
||||||
local love = require("love")
|
local love = require("love")
|
||||||
|
|
||||||
function love.load()
|
local function initGame()
|
||||||
arenaWidth = 800
|
return {
|
||||||
arenaHeight = 600
|
width = 800,
|
||||||
shipRadius = 30
|
height = 600,
|
||||||
|
ship = {
|
||||||
bulletTimerLimit = 0.5
|
|
||||||
bulletRadius = 5
|
|
||||||
|
|
||||||
asteroidStages = {
|
|
||||||
{
|
|
||||||
speed = 120,
|
|
||||||
radius = 15,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
speed = 70,
|
|
||||||
radius = 30,
|
radius = 30,
|
||||||
|
bullet = {
|
||||||
|
timerLimit = 0.5,
|
||||||
|
radius = 5,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
asteroids = {
|
||||||
speed = 50,
|
stages = {
|
||||||
radius = 50,
|
{ speed = 120, radius = 15 },
|
||||||
|
{ speed = 70, radius = 30 },
|
||||||
|
{ speed = 50, radius = 50 },
|
||||||
|
{ speed = 20, radius = 80 },
|
||||||
},
|
},
|
||||||
{
|
|
||||||
speed = 20,
|
|
||||||
radius = 80,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
local game = initGame()
|
||||||
|
|
||||||
|
function love.load()
|
||||||
function reset()
|
function reset()
|
||||||
shipX = arenaWidth / 2
|
shipX = game.width / 2
|
||||||
shipY = arenaHeight / 2
|
shipY = game.height / 2
|
||||||
shipAngle = 0
|
shipAngle = 0
|
||||||
shipSpeedX = 0
|
shipSpeedX = 0
|
||||||
shipSpeedY = 0
|
shipSpeedY = 0
|
||||||
bullets = {}
|
bullets = {}
|
||||||
bulletTimer = bulletTimerLimit
|
bulletTimer = game.ship.bullet.timerLimit
|
||||||
asteroids = {
|
asteroids = {
|
||||||
{
|
{
|
||||||
x = 100,
|
x = 100,
|
||||||
y = 100,
|
y = 100,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
x = arenaWidth - 100,
|
x = game.width - 100,
|
||||||
y = 100,
|
y = 100,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
x = arenaWidth / 2,
|
x = game.width / 2,
|
||||||
y = arenaHeight - 100,
|
y = game.height - 100,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for asteroidIndex, asteroid in ipairs(asteroids) do
|
for _, asteroid in ipairs(asteroids) do
|
||||||
asteroid.angle = love.math.random() * (2 * math.pi)
|
asteroid.angle = love.math.random() * (2 * math.pi)
|
||||||
asteroid.stage = #asteroidStages
|
asteroid.stage = #game.asteroids.stages
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -74,8 +72,8 @@ function love.update(dt)
|
||||||
shipSpeedY = shipSpeedY + math.sin(shipAngle) * shipSpeed * dt
|
shipSpeedY = shipSpeedY + math.sin(shipAngle) * shipSpeed * dt
|
||||||
end
|
end
|
||||||
|
|
||||||
shipX = (shipX + shipSpeedX * dt) % arenaWidth
|
shipX = (shipX + shipSpeedX * dt) % game.width
|
||||||
shipY = (shipY + shipSpeedY * dt) % arenaHeight
|
shipY = (shipY + shipSpeedY * dt) % game.height
|
||||||
|
|
||||||
local function areCirclesIntersecting(aX, aY, aRadius, bX, bY, bRadius)
|
local function areCirclesIntersecting(aX, aY, aRadius, bX, bY, bRadius)
|
||||||
return (aX - bX) ^ 2 + (aY - bY) ^ 2 <= (aRadius + bRadius) ^ 2
|
return (aX - bX) ^ 2 + (aY - bY) ^ 2 <= (aRadius + bRadius) ^ 2
|
||||||
|
|
@ -88,12 +86,12 @@ function love.update(dt)
|
||||||
table.remove(bullets, bulletIndex)
|
table.remove(bullets, bulletIndex)
|
||||||
else
|
else
|
||||||
local bulletSpeed = 500
|
local bulletSpeed = 500
|
||||||
bullet.x = (bullet.x + math.cos(bullet.angle) * bulletSpeed * dt) % arenaWidth
|
bullet.x = (bullet.x + math.cos(bullet.angle) * bulletSpeed * dt) % game.width
|
||||||
bullet.y = (bullet.y + math.sin(bullet.angle) * bulletSpeed * dt) % arenaHeight
|
bullet.y = (bullet.y + math.sin(bullet.angle) * bulletSpeed * dt) % game.height
|
||||||
end
|
end
|
||||||
for asteroidIndex = #asteroids, 1, -1 do
|
for asteroidIndex = #asteroids, 1, -1 do
|
||||||
local asteroid = asteroids[asteroidIndex]
|
local asteroid = asteroids[asteroidIndex]
|
||||||
if areCirclesIntersecting(bullet.x, bullet.y, bulletRadius, asteroid.x, asteroid.y, asteroidStages[asteroid.stage].radius) then
|
if areCirclesIntersecting(bullet.x, bullet.y, game.ship.bullet.radius, asteroid.x, asteroid.y, game.asteroids.stages[asteroid.stage].radius) then
|
||||||
table.remove(bullets, bulletIndex)
|
table.remove(bullets, bulletIndex)
|
||||||
if asteroid.stage > 1 then
|
if asteroid.stage > 1 then
|
||||||
local angle1 = love.math.random() * (2 * math.pi)
|
local angle1 = love.math.random() * (2 * math.pi)
|
||||||
|
|
@ -119,11 +117,11 @@ function love.update(dt)
|
||||||
|
|
||||||
bulletTimer = bulletTimer + dt
|
bulletTimer = bulletTimer + dt
|
||||||
if love.keyboard.isDown('s') then
|
if love.keyboard.isDown('s') then
|
||||||
if bulletTimer >= bulletTimerLimit then
|
if bulletTimer >= game.ship.bullet.timerLimit then
|
||||||
bulletTimer = 0
|
bulletTimer = 0
|
||||||
table.insert(bullets, {
|
table.insert(bullets, {
|
||||||
x = shipX + math.cos(shipAngle) * shipRadius,
|
x = shipX + math.cos(shipAngle) * game.ship.radius,
|
||||||
y = shipY + math.sin(shipAngle) * shipRadius,
|
y = shipY + math.sin(shipAngle) * game.ship.radius,
|
||||||
angle = shipAngle,
|
angle = shipAngle,
|
||||||
timeLeft = 4,
|
timeLeft = 4,
|
||||||
})
|
})
|
||||||
|
|
@ -131,10 +129,12 @@ function love.update(dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
for asteroidIndex, asteroid in ipairs(asteroids) do
|
for asteroidIndex, asteroid in ipairs(asteroids) do
|
||||||
asteroid.x = (asteroid.x + math.cos(asteroid.angle) * asteroidStages[asteroid.stage].speed * dt) % arenaWidth
|
asteroid.x = (asteroid.x + math.cos(asteroid.angle) * game.asteroids.stages[asteroid.stage].speed * dt) %
|
||||||
asteroid.y = (asteroid.y + math.sin(asteroid.angle) * asteroidStages[asteroid.stage].speed * dt) % arenaHeight
|
game.width
|
||||||
|
asteroid.y = (asteroid.y + math.sin(asteroid.angle) * game.asteroids.stages[asteroid.stage].speed * dt) %
|
||||||
|
game.height
|
||||||
|
|
||||||
if areCirclesIntersecting(shipX, shipY, shipRadius, asteroid.x, asteroid.y, asteroidStages[asteroid.stage].radius) then
|
if areCirclesIntersecting(shipX, shipY, game.ship.radius, asteroid.x, asteroid.y, game.asteroids.stages[asteroid.stage].radius) then
|
||||||
reset()
|
reset()
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
@ -149,9 +149,9 @@ function love.draw()
|
||||||
for y = -1, 1 do
|
for y = -1, 1 do
|
||||||
for x = -1, 1 do
|
for x = -1, 1 do
|
||||||
love.graphics.origin()
|
love.graphics.origin()
|
||||||
love.graphics.translate(x * arenaWidth, y * arenaHeight)
|
love.graphics.translate(x * game.width, y * game.height)
|
||||||
love.graphics.setColor(0, 0, 1)
|
love.graphics.setColor(0, 0, 1)
|
||||||
love.graphics.circle('fill', shipX, shipY, shipRadius)
|
love.graphics.circle('fill', shipX, shipY, game.ship.radius)
|
||||||
local shipCircleDistance = 20
|
local shipCircleDistance = 20
|
||||||
love.graphics.setColor(0, 1, 1)
|
love.graphics.setColor(0, 1, 1)
|
||||||
love.graphics.circle(
|
love.graphics.circle(
|
||||||
|
|
@ -161,14 +161,14 @@ function love.draw()
|
||||||
5
|
5
|
||||||
)
|
)
|
||||||
|
|
||||||
for bulletIndex, bullet in ipairs(bullets) do
|
for _, bullet in ipairs(bullets) do
|
||||||
love.graphics.setColor(0, 1, 0)
|
love.graphics.setColor(0, 1, 0)
|
||||||
love.graphics.circle('fill', bullet.x, bullet.y, bulletRadius)
|
love.graphics.circle('fill', bullet.x, bullet.y, game.ship.bullet.radius)
|
||||||
end
|
end
|
||||||
|
|
||||||
for asteroidIndex, asteroid in ipairs(asteroids) do
|
for _, asteroid in ipairs(asteroids) do
|
||||||
love.graphics.setColor(1, 1, 0)
|
love.graphics.setColor(1, 1, 0)
|
||||||
love.graphics.circle('fill', asteroid.x, asteroid.y, asteroidStages[asteroid.stage].radius)
|
love.graphics.circle('fill', asteroid.x, asteroid.y, game.asteroids.stages[asteroid.stage].radius)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue