1
0
dIntDesJam/2017-uge3/main.lua

196 lines
5.7 KiB
Lua

--------------------------------------------------------------------------------
-- Colors
local COLOR_PLAYER_1 = {200/255, 255/255, 200/255}
local COLOR_PLAYER_2 = {200/255, 200/255, 255/255}
local COLOR_WHITE = {255/255,255/255,255/255}
local COLOR_LOWER = {230/255, 230/255, 255/255}
local COLOR_BG = {26/255,26/255,26/255}
--------------------------------------------------------------------------------
WINDOW_WIDTH, WINDOW_HEIGTH = 800, 600
local SOMEBODY_ONCE = false
local SOMEBODY_MUSC = love.audio.newSource('/All_Star_NoIntro.mp3', 'stream')
SOMEBODY_MUSC:setLooping(true)
SOMEBODY_MUSC:play()
local SOMEBODIES = {}
local plat = require "plat"
local color = require "color_tools"
local FONT = love.graphics.newFont('star_font.ttf', 60)
love.graphics.setFont(FONT)
local system, player_1, player_2, reset_timer
-- Stars
local Stars = {}
function Stars:generate (nr_stars)
for i = 1, 200 do
self[#self+1] = math.random() * WINDOW_WIDTH
self[#self+1] = math.random() ^ 2 * WINDOW_HEIGTH
end
end
function Stars:update (dt, scores)
local star_speed = dt * (scores[2] - scores[1])
if star_speed == 0 then return end
for i = 1, #self, 2 do
self[i] = (self[i] + star_speed * self[i+1]/WINDOW_HEIGTH) % WINDOW_WIDTH
end
end
function Stars:draw ()
love.graphics.points(self)
end
-- Scores
local SCORES = { 0, 0 }
local function draw_scores ()
love.graphics.printf(SCORES[1], 50, 600 - 80, 700, 'left')
love.graphics.printf(SCORES[2], 50, 600 - 80, 700, 'right')
end
function reset_game ()
system = plat.PlatformSystem.new()
system:addPlatform(50, 400, 100, 50)
system:addPlatform(800-50-100, 400, 100, 50)
system:addPlatform(300, 200, 200, 50)
local lower = system:addPlatform(300, 550, 200, 50)
lower.resistance = -0.4
lower.color = COLOR_LOWER
player_1 = system:addPlayer(100, 50, 50, 50, {jump = 'w', left = 'a', right = 'd'})
player_1.start_t = 0
player_1.controller.last_dir = 1
player_2 = system:addPlayer(800 - 100 - 50, 50, 50, 50, {jump = 'up', left = 'left', right = 'right'})
player_2.start_t = 0
player_2.controller.last_dir = -1
reset_timer = nil
if SOMEBODY_ONCE then
local somb = SOMEBODY_MUSC:clone()
somb:play()
somb:setLooping(true)
SOMEBODIES[#SOMEBODIES+1] = somb
end
end
local function is_player_outside_bounds (player)
return player.x + player.w < -100 or 900 < player.x or player.y > 700
end
--------------------------------------------------------------------------------
function love.load ()
math.randomseed(os.time())
Stars:generate(1000)
reset_game()
end
local PLAYER_1_SHOOT = 's'
local PLAYER_2_SHOOT = 'down'
function love.update (dt)
Stars:update(dt, SCORES)
system:update(dt)
-- Update color
player_1.color = color.interpolate_rgb(COLOR_PLAYER_1, COLOR_WHITE, love.keyboard.isDown(PLAYER_1_SHOOT) and math.max(0, math.min(1, love.timer.getTime() - player_1.start_t)) or 0)
player_2.color = color.interpolate_rgb(COLOR_PLAYER_2, COLOR_WHITE, love.keyboard.isDown(PLAYER_2_SHOOT) and math.max(0, math.min(1, love.timer.getTime() - player_2.start_t)) or 0)
if reset_timer then
reset_timer = reset_timer - dt
if reset_timer < 0 then
reset_game()
end
end
if is_player_outside_bounds(player_1) and player_1.movable then
SCORES[2] = SCORES[2] + 1
player_1.movable = false
reset_timer = 1
end
if is_player_outside_bounds(player_2) and player_2.movable then
SCORES[1] = SCORES[1] + 1
player_2.movable = false
reset_timer = reset_timer or 1
end
end
local function shoot_bullet_at_player (player)
-- TODO: Add Timeout when shooting bullets shorter than 0.2
local time = love.timer.getTime() - player.start_t
local size = math.max(math.min( 1, time ), 0.2)
local dx = player.controller.last_dir * (1.2 - size) * 600 + player.dx
if dx ~= 0 then
local bw, bh = math.floor(40*size + 0.5), math.floor(20*size + 0.5)
local bx, by = player.x + player.w/2 - bw/2, player.y + player.h/2 - bh/2
system:addBullet(bx, by, bw, bh, dx, player)
end
end
function love.keyreleased (key)
if key == PLAYER_1_SHOOT then shoot_bullet_at_player(player_1) end
if key == PLAYER_2_SHOOT then shoot_bullet_at_player(player_2) end
end
function love.keypressed (key)
if key == 'escape' then
love.event.quit()
end
if key == 's' and player_1.start_t < love.timer.getTime() then
player_1.start_t = love.timer.getTime()
end
if key == 'down' and player_2.start_t < love.timer.getTime() then
player_2.start_t = love.timer.getTime()
end
if key == 'f2' then
SOMEBODY_MUSC:stop()
end
if key == 'f1' then
SOMEBODY_ONCE = not SOMEBODY_ONCE
if not SOMEBODY_ONCE then
for _, track in ipairs(SOMEBODIES) do
track:stop()
end
SOMEBODIES = {}
end
end
end
function love.draw ()
love.graphics.setBackgroundColor(COLOR_BG)
love.graphics.setColor(COLOR_WHITE)
-- Draw score
draw_scores()
-- Draw Stars
Stars:draw()
-- Draw System
system:draw()
if is_player_outside_bounds(player_1) then
love.graphics.setColor(COLOR_PLAYER_1)
love.graphics.rectangle('fill', player_1.x-4000, player_1.y, 8000, 50)
love.graphics.rectangle('fill', player_1.x, player_1.y-4000, 50, 8000)
end
if is_player_outside_bounds(player_2) then
love.graphics.setColor(COLOR_PLAYER_2)
love.graphics.rectangle('fill', player_2.x-4000, player_2.y, 8000, 50)
love.graphics.rectangle('fill', player_2.x, player_2.y-4000, 50, 8000)
end
end