added animation for fog. their animation runs out of sync (on purpose). added a portal to draw new 'level'. code refactoring/improvement

overall.
This commit is contained in:
lee2sman 2022-05-13 02:43:33 -04:00
parent b0a7c55e07
commit d709e6f9e9
1 changed files with 84 additions and 17 deletions

101
main.lua
View File

@ -6,7 +6,8 @@ function love.load()
load_sprites()
player_dr = newAnimation(love.graphics.newImage("assets/img/animation/player-test-size.png"),20,30,1)
player = newAnimation(love.graphics.newImage("assets/img/animation/player-test-size.png"),20,30,1)
fog = newAnimation(love.graphics.newImage("assets/img/fog/fog-sheet-resize.png"),20,30,1)
block_width = sprite[1]:getWidth()
block_height = sprite[1]:getHeight()
@ -18,6 +19,7 @@ function love.load()
grid_y = love.graphics.getHeight()/2
create_player()
create_portal()
choose_drone()
@ -27,17 +29,25 @@ function love.update(dt)
local terrain = get_terrain(p_x,p_y)
--play drone corresponding to current space if not playing
if terrain<5 then
stop_old_drone(current_drone)
choose_drone()
end
play_drone(current_drone)
player_dr.currentTime = player_dr.currentTime + dt
if player_dr.currentTime >= player_dr.duration then
player_dr.currentTime = player_dr.currentTime - player_dr.duration
--player animation
player.currentTime = player.currentTime + dt
if player.currentTime >= player.duration then
player.currentTime = player.currentTime - player.duration
end
--fog animation
fog.currentTime = fog.currentTime + dt
if fog.currentTime >= fog.duration then
fog.currentTime = fog.currentTime - fog.duration
end
end
function love.keypressed(key)
@ -63,11 +73,15 @@ function love.keypressed(key)
print('player x: '..p_x..' y: '..p_y)
print('grid piece there: '..grid[p_x][p_y])
check_player_on_portal()
end
function love.draw()
draw_grid()
draw_player()
draw_portal()
end
function load_sounds()
@ -95,13 +109,18 @@ function create_grid()
end
function create_player()
player_x = grid_x
player_y = grid_y - block_depth * 2
p_x = 1
p_y = 1
end
function create_portal()
portal_x = love. math.random(10)
portal_y = love.math.random(10)
print('portal created at '..portal_x,portal_y)
building_base = love.math.random(#building_sprite-6)
end
function draw_grid()
for x = 1,grid_size do
for y = 1,grid_size do
@ -110,33 +129,65 @@ function draw_grid()
grid_x + ((y-x) * (block_width / 2)),
grid_y + ((x+y) * (block_depth / 2)) - (block_depth * (grid_size / 2)) - block_depth)
--draw fog on terrain<5
if grid[x][y]<4 then
draw_fog(x,y)
end
--draw portal
if x == portal_x and y == portal_y then
love.graphics.draw(building_sprite[3],
grid_x + ((y-x) * (block_width / 2)),
grid_y + ((x+y) * (block_depth / 2)) - (block_depth * (grid_size / 2)) - block_depth)
end
end
end
end
function draw_player()
local spriteNum = math.floor(player_dr.currentTime / player_dr.duration * #player_dr.quads) + 1
local spriteNum = math.floor(player.currentTime / player.duration * #player.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/8, 0, 2)
love.graphics.draw(player.spriteSheet, player.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/8, 0, 2)
end
function draw_fog(fog_x,fog_y)
--draw all fog at simultaneous stage of animation
--local spriteNum = math.floor(fog.currentTime / fog.duration * #fog.quads) + 1
-- or: each fog animation sprite starts at different stage
local spriteNum = math.floor(fog.currentTime / fog.duration * grid[fog_x][fog_y]) + 1
love.graphics.draw(fog.spriteSheet, fog.quads[spriteNum], grid_x + ((fog_y-fog_x) * (block_width / 2)) + block_width/4, grid_y + ((fog_x+fog_y) * (block_depth / 2)) - (block_depth * (grid_size / 2)) - block_depth - block_height/8, 0, 2)
end
function draw_portal()
local spriteNum = math.floor(player.currentTime / player.duration * 2) + building_base
love.graphics.draw(building_sprite[spriteNum], grid_x + ((portal_y-portal_x) * (block_width / 2)) + block_width/4, grid_y + ((portal_x+portal_y) * (block_depth / 2)) - (block_depth * (grid_size / 2)) - block_depth - block_height/8, 0, 1)
end
function newAnimation(image, width, height, duration)
local player_dr = {}
player_dr.spriteSheet = image;
player_dr.quads = {};
local player = {}
player.spriteSheet = image;
player.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()))
table.insert(player.quads, love.graphics.newQuad(x, y, width, height, image:getDimensions()))
end
end
player_dr.duration = duration or 1
player_dr.currentTime = 0
player.duration = duration or 1
player.currentTime = 0
return player_dr
return player
end
function get_terrain(x,y)
@ -185,6 +236,14 @@ function load_sprites()
sprite[j] = love.graphics.newImage(dir..files[love.math.random(#files)])
end
--local portal sprites saved as building_sprites table
building_sprite = {}
local dir = 'assets/img/buildings/'
local files = love.filesystem.getDirectoryItems(dir)
for l=1,#files do
building_sprite[l] = love.graphics.newImage(dir..files[l])
end
end
function choose_drone()
@ -194,3 +253,11 @@ end
function stop_old_drone(old_drone)
love.audio.stop(drone[old_drone])
end
function check_player_on_portal()
if p_x == portal_x and p_y == portal_y then
create_grid()
choose_drone()
create_portal()
end
end