152 lines
3.3 KiB
Lua
152 lines
3.3 KiB
Lua
function love.load()
|
|
|
|
math.randomseed(os.time())
|
|
|
|
load_sounds()
|
|
|
|
load_sprites()
|
|
|
|
player_dr = newAnimation(love.graphics.newImage("assets/img/animation/player-test-size.png"),20,30,1)
|
|
|
|
block_width = sprite[1]:getWidth()
|
|
block_height = sprite[1]: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)
|
|
|
|
if key == 'down' then
|
|
p_x = p_x + 1
|
|
end
|
|
if key == 'up' then
|
|
p_x = p_x - 1
|
|
end
|
|
if key == 'left' then
|
|
p_y = p_y - 1
|
|
end
|
|
if key == 'right' then
|
|
p_y = p_y + 1
|
|
end
|
|
--keep the player on the board!
|
|
if p_x<1 then p_x = 1 end
|
|
if p_y<1 then p_y = 1 end
|
|
if p_x>10 then p_x = 10 end
|
|
if p_y>10 then p_y = 10 end
|
|
|
|
print('player x: '..p_x..' y: '..p_y)
|
|
print('grid piece there: '..grid[p_x][p_y])
|
|
local terrain_type = get_terrain(p_x,p_y)
|
|
play_drone(terrain_type)
|
|
|
|
end
|
|
|
|
function love.draw()
|
|
|
|
draw_grid()
|
|
draw_player()
|
|
end
|
|
|
|
function load_sounds()
|
|
local dir = 'assets/audio/drones-pack/'
|
|
local files = love.filesystem.getDirectoryItems(dir)
|
|
drone = {}
|
|
for k, file in ipairs(files) do
|
|
print(k .. ". " .. file) --list all soundfiles as loading
|
|
drone[k] = love.audio.newSource(dir..file, "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)
|
|
--make space a random tile
|
|
grid[x][y] = love.math.random(#sprite)
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
function create_player()
|
|
player_x = grid_x
|
|
player_y = grid_y - block_depth * 2
|
|
|
|
p_x = 1
|
|
p_y = 1
|
|
end
|
|
|
|
function draw_grid()
|
|
for x = 1,grid_size do
|
|
for y = 1,grid_size do
|
|
|
|
love.graphics.draw(sprite[grid[x][y]],
|
|
grid_x + ((y-x) * (block_width / 2)),
|
|
grid_y + ((x+y) * (block_depth / 2)) - (block_depth * (grid_size / 2)) - block_depth)
|
|
|
|
end
|
|
end
|
|
|
|
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], grid_x + ((p_y-p_x) * (block_width / 2)) + block_width/4, grid_y + ((p_x+p_y) * (block_depth / 2)) - (block_depth * (grid_size / 2)) - block_depth - block_height/2, 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
|
|
|
|
function get_terrain(x,y)
|
|
return grid[p_x][p_y]
|
|
end
|
|
|
|
function play_drone(terrain)
|
|
drone[terrain]:play()
|
|
end
|
|
|
|
function load_sprites()
|
|
|
|
local dir = 'assets/img/ground/'
|
|
local files = love.filesystem.getDirectoryItems(dir)
|
|
sprite = {}
|
|
for k, file in ipairs(files) do
|
|
print(k .. ". " .. file) --list all sprites as loading
|
|
sprite[k] = love.graphics.newImage(dir..file)
|
|
end
|
|
|
|
end
|