dronecollector/main.lua

363 lines
8.9 KiB
Lua

function love.load()
math.randomseed(os.time())
load_sounds()
load_sprites()
load_font()
load_data()
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)
chaser = newAnimation(love.graphics.newImage("assets/img/chaser/dr2.png"),26,33,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()
create_portal()
create_chaser()
choose_drone()
change_title()
end
function love.update(dt)
move_chaser()
local terrain = get_terrain(p_x,p_y)
--play drone corresponding to current space if not playing
if p_x == chaser_x and p_y == chaser_y then
stop_old_drone(current_drone)
change_title()
elseif terrain<5 and sound_switched == false then
stop_old_drone(current_drone)
choose_drone()
sound_switched = true
elseif terrain>=5 then
sound_switched = false
end
play_drone(current_drone)
--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
--chaser animation
chaser.currentTime = chaser.currentTime + dt
if chaser.currentTime >= chaser.duration then
chaser.currentTime = chaser.currentTime - chaser.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])
check_player_on_portal()
end
function love.draw()
draw_grid()
draw_player()
draw_portal()
draw_text()
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 space a random tile
grid[x][y] = love.math.random(#sprite)
end
end
end
function create_player()
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 create_chaser()
chaser_x= love. math.random(10)
chaser_y = love.math.random(10)
--print('chaser created '..chaser_x..','..chaser_y)
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)
--draw fog on terrain<3
if grid[x][y]<3 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
--draw chaser
if x == chaser_x and y == chaser_y then
draw_chaser(x,y)
end
end
end
end
function draw_player()
local spriteNum = math.floor(player.currentTime / player.duration * #player.quads) + 1
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_chaser(chaser_x,chaser_y)
local spriteNum = math.floor(chaser.currentTime / chaser.duration * #chaser.quads) + 1
love.graphics.draw(chaser.spriteSheet, chaser.quads[spriteNum], grid_x + ((chaser_x-chaser_y) * (block_width / 2)) + block_width/4, grid_y + ((chaser_x+chaser_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/4, 0, 1)
end
function move_chaser()
if chaser_x<1 then chaser_x = 1 end
if chaser_y<1 then chaser_y = 1 end
if chaser_x>10 then chaser_x = 10 end
if chaser_y>10 then chaser_y = 10 end
if love.math.random(100)<3 then
chaser_x = chaser_x + love.math.random(-1,1)
chaser_y = chaser_y + love.math.random(-1,1)
end
--print('chaser_x '..chaser_x..' chaser_y '..chaser_y)
end
function newAnimation(image, width, height, duration)
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.quads, love.graphics.newQuad(x, y, width, height, image:getDimensions()))
end
end
player.duration = duration or 1
player.currentTime = 0
return player
end
function get_terrain(x,y)
return grid[p_x][p_y]
end
function play_drone(terrain)
if not drone[terrain]:isPlaying() then
love.audio.setEffect('dronezone', {type = 'reverb'})
love.audio.setEffect('cave', {type = 'echo'})
drone[terrain]:setEffect('cave')
drone[terrain]:setEffect('ensemble')
love.audio.play(drone[terrain])
end
end
function load_sprites()
local dir = 'assets/img/ground/'
local files = love.filesystem.getDirectoryItems(dir)
sprite = {}
--load terrain
for k, file in ipairs(files) do
--print(k .. ". " .. file) --list all sprites as loading
sprite[k] = love.graphics.newImage(dir..file)
end
--sprinkle some plants in
local dir = 'assets/img/plants/'
local files = love.filesystem.getDirectoryItems(dir)
for i=1,5 do
sprite[love.math.random(#sprite)] = love.graphics.newImage(dir..files[love.math.random(#files)])
end
--sprinkle some special objects in
local dir = 'assets/img/objects/'
local files = love.filesystem.getDirectoryItems(dir)
--cycle through all tiles, replace all low number ones <5
for j=1,10 do
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()
current_drone = love.math.random(#drone)
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()
change_title()
end
end
function load_data()
environment = {}
for line in love.filesystem.lines("assets/data/environments.txt") do
table.insert(environment, line)
end
sense = {}
for line in love.filesystem.lines("assets/data/senses.txt") do
table.insert(sense, line)
end
transition = {}
for line in love.filesystem.lines("assets/data/transition.txt") do
table.insert(transition, line)
end
end
function draw_text()
love.graphics.print(title, 100, 100)
end
function change_title()
current_env = environment[love.math.random(#environment)]
current_sense = sense[love.math.random(#sense)]
current_transition = transition[love.math.random(#transition)]
if love.math.random(2)>1 then
title=current_transition..' '..current_env
else
title=current_sense..' of '..current_env
end
end
function load_font()
font = love.graphics.newFont(18)
love.graphics.setFont(font)
end