dronecollector/main.lua

233 lines
5.9 KiB
Lua

function love.load()
load_sounds()
--load graphics
dirt = love.graphics.newImage("assets/img/dirt.png")
grass = love.graphics.newImage("assets/img/grass.png")
player_dr = newAnimation(love.graphics.newImage("assets/img/animation/player-test-size.png"),20,30,1)
block_width = grass:getWidth()
block_height = grass:getHeight()
block_depth = block_height / 2
create_grid()
grid_x = love.graphics.getWidth()/2
grid_y = love.graphics.getHeight()/2
create_player()
end
function love.update(dt)
player_dr.currentTime = player_dr.currentTime + dt
if player_dr.currentTime >= player_dr.duration then
player_dr.currentTime = player_dr.currentTime - player_dr.duration
end
end
function love.keypressed(key)
--test, play a drone sound on any keypress
drone[math.random(#drone)]:play()
if player_dir == 'downright' then
if key == 'down' or key == 'right' then
player_x = player_x + block_width/2
player_y = player_y + block_height/4
elseif key == 'up' then
player_x = player_x + block_width/2
player_y = player_y - block_height/4
player_dir = 'upright'
elseif key == 'left' then
player_x = player_x - block_width/2
player_y = player_y + block_height/4
player_dir = 'downleft'
end
elseif player_dir == 'upright' then
if key == 'down' then
player_x = player_x + block_width/2
player_y = player_y + block_height/4
player_dir = 'downright'
elseif key == 'right' or key == 'up' then
player_x = player_x + block_width/2
player_y = player_y - block_height/4
elseif key == 'left' then
player_x = player_x - block_width/2
player_y = player_y - block_height/4
player_dir = 'upleft'
end
elseif player_dir == 'upleft' then
if key == 'left' or key == 'up' then
player_x = player_x - block_width/2
player_y = player_y - block_height/4
elseif key == 'down' then
player_x = player_x - block_width/2
player_y = player_y + block_height/4
player_dir = 'downleft'
elseif key == 'right' then
player_x = player_x + block_width/2
player_y = player_y - block_height/4
player_dir = 'upright'
end
elseif player_dir == 'downleft' then
if key == 'down' or key =='left' then
player_x = player_x - block_width/2
player_y = player_y + block_height/4
elseif key == 'up' then
player_x = player_x - block_width/2
player_y = player_y - block_height/4
player_dir = 'upleft'
elseif key == 'right' then
player_x = player_x + block_width/2
player_y = player_y + block_height/4
player_dir = 'downright'
end
end
--print(grid[player_x][player_y])
--print(player_x)
print(grid[1][1])
--[[
if key == 'up' or key == 'down' or key == 'left' or key == 'right' then
player_x = playerX + block_width/2
player_y = playerY + block_height/4
for testY, row in ipairs(level) do
for testX, cell in ipairs(row) do
if cell == player or cell == playerOnStorage then
playerX = testX
playerY = testY
end
end
end
local dx = 0
local dy = 0
if key == 'left' then
dx = -1
elseif key == 'right' then
dx = 1
elseif key == 'up' then
dy = -1
elseif key == 'down' then
dy = 1
end
local current = level[playerY][playerX]
local adjacent = level[playerY + dy][playerX + dx]
local nextAdjacent = {
[empty] = player,
[storage] = playerOnStorage,
}
local nextCurrent = {
[player] = empty,
[playerOnStorage] = storage,
}
if nextAdjacent[adjacent] then
level[playerY][playerX] = nextCurrent[current]
level[playerY + dy][playerX + dx] = nextAdjacent[adjacent]
--play a random drone
--drone[1]:play()
drone[math.random(#drone)]:play()
end
end
]]--
end
function love.draw()
draw_grid()
draw_player()
end
function load_sounds()
drone = {}
for i=1,11
do
drone[i] = love.audio.newSource("assets/audio/"..i..".mp3", "stream")
end
end
function create_grid()
grid_size = 10
grid = {}
for x = 1,grid_size do
grid[x] = {}
for y = 1,grid_size do
--make that space grass (1)
grid[x][y] = 1
end
end
-- number of dirt spaces
--local num_dirt = 0
local num_dirt = love.math.random(20,40) --convert random spaces to dirt (2)
for i=1,num_dirt
do
--make that random space dirt (2)
grid[love.math.random(grid_size)][love.math.random(grid_size)] = 2
end
end
function create_player()
player_x = grid_x
player_y = grid_y - block_depth * 2
player_dir = "downright" -- either 'downright','upright','downleft','upleft'
end
function draw_grid()
for x = 1,grid_size do
for y = 1,grid_size do
if grid[x][y] == 1 then
love.graphics.draw(grass,
grid_x + ((y-x) * (block_width / 2)),
grid_y + ((x+y) * (block_depth / 2)) - (block_depth * (grid_size / 2)) - block_depth)
else -- grid[x][y] == 2
love.graphics.draw(dirt,
grid_x + ((y-x) * (block_width / 2)),
grid_y + ((x+y) * (block_depth / 2)) - (block_depth * (grid_size / 2)) - block_depth)
end
end
end
--draw player
--love.graphics.draw(dr, grid_x, grid_y - block_depth * 2)
end
function draw_player()
local spriteNum = math.floor(player_dr.currentTime / player_dr.duration * #player_dr.quads) + 1
love.graphics.draw(player_dr.spriteSheet, player_dr.quads[spriteNum], player_x, player_y, 0, 2)
end
function newAnimation(image, width, height, duration)
local player_dr = {}
player_dr.spriteSheet = image;
player_dr.quads = {};
for y = 0, image:getHeight() - height, height do
for x = 0, image:getWidth() - width, width do
table.insert(player_dr.quads, love.graphics.newQuad(x, y, width, height, image:getDimensions()))
end
end
player_dr.duration = duration or 1
player_dr.currentTime = 0
return player_dr
end