Code: Select all
local w
local h
local p = {}
p[1] = {}
p[2] = {}
p[1].touch = {}
p[2].touch = {}
local iceSpikeSlot
local airSwipeSlot
local projectiles = {}
local iceSpike
local airSwipe
function love.load()
w, h = love.graphics.getDimensions()
p[1].x = w / 4
p[2].x = 3 * w / 4
for i = 1, 2 do
p[i].img = love.graphics.newImage('p.png')
p[i].y = h / 2
p[i].w = p[i].img:getWidth()
p[i].h = p[i].img:getHeight()
p[i].element = love.math.random(1, 2)
p[i].touch.x = p[i].x
p[i].touch.y = p[i].y
p[i].defaultSpeed = 750
p[i].speed = p[i].defaultSpeed
p[i].cd1 = 0
p[i].cd2 = 0
p[i].cd3 = 0
p[i].cd4 = 0
p[i].defaultSpeedTimer = 0
end
iceSpikeSlot = love.graphics.newImage('iceSpikeSlot.png')
airSwipeSlot = love.graphics.newImage('airSwipeSlot.png')
iceSpike = love.graphics.newImage('iceSpike.png')
airSwipe = love.graphics.newImage('airSwipe.png')
end
function love.update(dt)
local touches = love.touch.getTouches()
for i, id in ipairs(touches) do
local x, y = love.touch.getPosition(id)
if x < w / 2 then
if x > 150 then
p[1].touch.x = x
p[1].touch.y = y
else
if y < 150 then
if p[1].cd1 <= 0 then
if p[1].element == 1 then
local dir = math.atan2(p[1].touch.y - p[1].y, p[1].touch.x - p[1].x)
table.insert(projectiles, {type = 1, owner = 1, img = iceSpike, x = p[1].x, y = p[1].y, w = iceSpike:getWidth(), h = iceSpike:getHeight(), dir = dir, speed = 1000})
p[1].cd1 = 2
elseif p[1].element == 2 then
local dir = math.atan2(p[1].touch.y - p[1].y, p[1].touch.x - p[1].x)
table.insert(projectiles, {type = 2, owner = 1, img = airSwipe, x = p[1].x, y = p[1].y, w = airSwipe:getWidth(), h = airSwipe:getHeight(), dir = dir, speed = 750})
p[1].cd1 = 1
end
end
elseif y < 300 then
if p[1].element == 1 then
elseif p[1].element == 2 then
end
elseif y < 450 then
if p[1].cd3 <= 0 then
if p[1].element == 1 then
elseif p[1].element == 2 then
p[1].x = love.math.random(0, w / 2)
p[1].y = love.math.random(0, h)
p[1].touch.x = p[1].x
p[1].touch.y = p[1].y
p[1].cd3 = 10
end
end
elseif y < 600 then
if p[1].element == 1 then
elseif p[1].element == 2 then
end
end
end
else
if x < w - 150 then
p[2].touch.x = x
p[2].touch.y = y
else
if y > h - 150 then
if p[2].cd1 <= 0 then
if p[2].element == 1 then
local dir = math.atan2(p[2].touch.y - p[2].y, p[2].touch.x - p[2].x)
table.insert(projectiles, {type = 1, owner = 2, img = iceSpike, x = p[2].x, y = p[2].y, w = iceSpike:getWidth(), h = iceSpike:getHeight(), dir = dir, speed = 1000})
p[2].cd1 = 2
elseif p[2].element == 2 then
local dir = math.atan2(p[2].touch.y - p[2].y, p[2].touch.x - p[2].x)
table.insert(projectiles, {type = 2, owner = 2, img = airSwipe, x = p[2].x, y = p[2].y, w = airSwipe:getWidth(), h = airSwipe:getHeight(), dir = dir, speed = 750})
p[2].cd1 = 1
end
end
elseif y > h - 300 then
if p[2].element == 1 then
elseif p[2].element == 2 then
end
elseif y > h - 450 then
if p[2].cd3 <= 0 then
if p[2].element == 1 then
elseif p[2].element == 2 then
p[2].x = love.math.random(w / 2, w)
p[2].y = love.math.random(0, h)
p[2].touch.x = p[2].x
p[2].touch.y = p[2].y
p[2].cd3 = 10
end
end
elseif y > h - 600 then
if p[2].element == 1 then
elseif p[2].element == 2 then
end
end
end
end
end
for i = 1, 2 do
local distance = math.dist(p[i].x, p[i].y, p[i].touch.x, p[i].touch.y)
local stepSize = p[i].speed * dt
if distance > stepSize then
p[i].x = p[i].x + (p[i].touch.x - p[i].x) / distance * stepSize
p[i].y = p[i].y + (p[i].touch.y - p[i].y) / distance * stepSize
end
end
for i, v in ipairs(projectiles) do
v.x = v.x + math.cos(v.dir) * v.speed * dt
v.y = v.y + math.sin(v.dir) * v.speed * dt
if v.x < v.w / 2 or v.x > w - v.w / 2 then
table.remove(projectiles, i)
end
if CheckCollision(v.x, v.y, v.w, v.h, p[1].x, p[1].y, p[1].w, p[1].h) then
if v.owner == 2 then
table.remove(projectiles, i)
if v.type == 1 then
p[1].speed = p[1].speed / 2
p[1].defaultSpeedTimer = 2
else
end
end
end
if CheckCollision(v.x, v.y, v.w, v.h, p[2].x, p[2].y, p[2].w, p[2].h) then
if v.owner == 1 then
table.remove(projectiles, i)
if v.type == 1 then
p[2].speed = p[2].speed / 2
p[2].defaultSpeedTimer = 2
else
end
end
end
end
for i = 1, 2 do
if p[i].cd1 > 0 then
p[i].cd1 = p[i].cd1 - dt
end
if p[i].cd2 > 0 then
p[i].cd2 = p[i].cd2 - dt
end
if p[i].cd3 > 0 then
p[i].cd3 = p[i].cd3 - dt
end
if p[i].cd4 > 0 then
p[i].cd4 = p[i].cd4 - dt
end
end
for i = 1, 2 do
if p[i].defaultSpeedTimer > 0 then
p[i].defaultSpeedTimer = p[i].defaultSpeedTimer - dt
elseif p[i].defaultSpeedTimer < 0 then
p[i].speed = p[i].defaultSpeed
end
end
end
function love.draw()
love.graphics.line(w / 2, 0, w / 2, h)
love.graphics.line(150, 0, 150, h)
love.graphics.line(w - 150, 0, w - 150, h - 150)
if p[1].element == 1 then
love.graphics.draw(iceSpikeSlot, 0, 0)
elseif p[1].element == 2 then
love.graphics.draw(airSwipeSlot, 0, 0)
end
if p[2].element == 1 then
love.graphics.draw(iceSpikeSlot, w - 150, h - 150)
elseif p[2].element == 2 then
love.graphics.draw(airSwipeSlot, w - 150, h - 150)
end
for i = 1, 2 do
love.graphics.draw(p[i].img, p[i].x - p[i].w / 2, p[i].y - p[i].h / 2)
end
for i, v in ipairs(projectiles) do
love.graphics.draw(v.img, v.x, v.y, v.dir, 1, 1, v.w / 2, v.h / 2)
end
end
function math.dist(x1, y1, x2, y2) return ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ 0.5 end
function CheckCollision(x1, y1, w1, h1, x2, y2, w2, h2) return x1 < x2 + w2 and x2 < x1 + w1 and y1 < y2 + h2 and y2 < y1 + h1 end