-- Глобальные переменные
local audio = love.audio --Вместо love.audio будем писать просто audio
local gr = love.graphics 	-- Вместо love.graphics - gr
local bFont = gr.newFont(18)		-- Шрифт
local m = love.mouse --Вместо love.mouse - m
-- Неигровые сокращения
lo = 255
--Наши переменные
globalourmoney = 30
gom = globalourmoney
globalourbaselevel = 1 -- Уровень Базы
gobl = globalourbaselevel
globalallyspeed = 1 -- Уровень скорости
gas = globalallyspeed
globalallyarmor = 1 -- Уровень брони
gaa = globalallyarmor
globalallyweapon = 1 -- Уровень оружия
gaw = globalallyweapon
globalallyMaxHP = 10 -- Максимальное ХП
gamhp = globalallyMaxHP
globalallynumber = 0 -- Номер
gan = globalallynumber
--Конец наших переменных

--Вражеские переменные
globalenemymoney = 30
gem = globalenemymoney
globalenemybaselevel = 1 -- Уровень Базы
gebl = globalenemybaselevel
globalenemyspeed = 1 -- Уровень скорости
ges = globalenemyspeed
globalenemyarmor = 1 -- Уровень брони
gea = globalenemyarmor
globalenemyweapon = 1 -- Уровень оружия
gew = globalenemyweapon
globalenemyMaxHP = 10 -- Максимальное ХП
gemhp =globalenemyMaxHP
globalenemynumber = 0 -- Номер
gen = globalenemynumber
--Конец вражеских переменных


function love.load()
song = audio.newSource("music.mp3")
audio.play(song)
love.audio.setVolume( 0.1 )
logo = gr.newImage("logo.png")
																	-- Загружаем спрайты
background = gr.newImage("background.png") 							-- Фон
armsprite = gr.newImage("arm.png") 									-- Рука
spawn = gr.newImage("spawn.png") 		
kick = {}
kicksprite = gr.newImage("kick.png")								-- Удар
kill = {}
killsprite = gr.newImage("kill.png")								-- Смерть
armorup = {}
	armorup.x = 20
	armorup.y = 20
	armorup.rgb = 255
	armorup.spr = gr.newImage("armorupgrade.png")
hpup = {}
	hpup.x = 260
	hpup.y = 20
	hpup.rgb = 255
	hpup.spr = gr.newImage("HPupgrade.png")
weaponup = {}
	weaponup.x = 100
	weaponup.y = 20
	weaponup.rgb = 255
	weaponup.spr = gr.newImage("weaponupgrade.png")
baseup = {}
	baseup.x = 340
	baseup.y = 20
	baseup.rgb = 255
	baseup.spr = gr.newImage("baseupgrade.png")
speedup = {}
	speedup.x = 180
	speedup.y = 20
	speedup.rgb = 255
	speedup.spr = gr.newImage("speedupgrade.png")

basesprite = {}														-- Спрайты баз
	for i=1,2 do													-- ...
	basesprite[i] = gr.newImage("base"..i..".png")  						-- ...
	end																-- Спрайты баз (не доделано)
	
bodysprite = {}														-- Спрайты Тел
	for i=1,5 do 													-- ...
	bodysprite[i] = gr.newImage("body"..i..".png")						-- ...
	end																-- Спрайты Тел (не доделано)

bootssprite = {}													-- Спрайты ботинок
	for i=1,5 do 													-- ...
	bootssprite[i] = gr.newImage("leg"..i..".png")					-- ...
	end																-- Спрайты ботинок (готово)
	
weaponsprite = {}													-- Спрайты оружия
	for i=1,5 do 													-- ...
	weaponsprite[i] = gr.newImage("weapon"..i..".png")				-- ...
	end																-- Спрайты оружия (не доделано)
																	-- Закончили загрухку спрайтов
	
ourbase = {} 													-- Переменные нашей базы
	ourbase.maxhp = 150											-- ...
	ourbase.hp = 150											-- ...
	ourbase.x = 10 + 150/2										-- ...
	ourbase.y = gr.getHeight( ) - 10 - 150/2					-- Переменные нашей базы
	
enemybase = {} 													-- Переменные нашей базы
	enemybase.maxhp = 150										-- ...
	enemybase.hp = 150											-- ...
	enemybase.x = gr.getWidth( )- 10 - 150/2					-- ...
	enemybase.y = gr.getHeight( )-10 - 150/2					-- Переменные нашей базы
	
allies = {}														-- Группа Своих
enemies = {}													-- Группа Врагов
remAlly = {}													-- Группа наших, подлежащих удалению
remEnemy = {}													-- Группа врагов, подлежащих удалению
end

function love.update(dt)
if lo > 0 then lo = lo - 1 end
if song:isStopped() then audio.play(song)end
if gom <0 then gom = 0 end
if gem <0 then gem = 0 end
gdt = dt
allyactions()
enemyactions()
for i,v in ipairs(remEnemy) do
	table.remove(enemies, i)
	gen = gen - 1
	for ii,vv in ipairs(enemies) do
		vv.number = vv.number - 1
	end
	gom = gom + 10
end
for i,v in ipairs(remAlly) do
	table.remove(allies, v)
	gan = gan - 1
	for ii,vv in ipairs(allies) do
		vv.number = vv.number - 1
	end
	gem = gem + 10
end
table.remove(remEnemy)
table.remove(remAlly)
for i,v in ipairs(kick) do
	v.o = v.o - 5
	if v.o <= 0 then
		table.remove(kick, i)
	end
end
for i,v in ipairs(kill) do
	v.o = v.o - 5
	if v.o <= 0 then
		table.remove(kill, i)
	end
end

mouseevents()
if gaa == 2 then
	armorup.rgb = 150
end
if gamhp == 50 then
	hpup.rgb = 150
end
if gaw == 3 then
	weaponup.rgb = 150
end
if gas == 5 then
	speedup.rgb = 150
end
if ourbase.maxhp == 300 then
	baseup.rgb = 150
end
end

function love.draw()
gr.setColor(255, 255, 255, 255)
	gr.draw( background, 0, 0 ) 										-- Фон
	gr.draw( basesprite[gobl], ourbase.x-150/2, ourbase.y-150/2 )	-- Наша база
		gr.setFont(bFont)
		gr.print(ourbase.hp, ourbase.x, ourbase.y - 100)
	gr.draw( basesprite[gebl], enemybase.x+150/2, enemybase.y-150/2, 0, -1, 1, 0, 0 ) 	-- Вражеская база
		gr.setFont(bFont)
		gr.print(enemybase.hp, enemybase.x, enemybase.y - 100)
drawinterface()
drawenemy()
drawally()
gr.setColor(255, 255,255, lo)
	gr.draw( logo, 400, 0 ) 
end


function drawinterface()
gr.setFont(bFont)
gr.setColor(255, 255, 255, 255)
	gr.draw( spawn, 440, 20 )
gr.setColor(armorup.rgb, armorup.rgb, armorup.rgb, 255) 
	gr.draw( armorup.spr, armorup.x, armorup.y )
	gr.print(gaa*70, armorup.x+20, armorup.y-20)
gr.setColor(hpup.rgb, hpup.rgb, hpup.rgb, 255) 
	gr.draw( hpup.spr, hpup.x, hpup.y )
	gr.print(gamhp*50/10, hpup.x+20, hpup.y-20)
gr.setColor(weaponup.rgb, weaponup.rgb, weaponup.rgb, 255) 
	gr.draw( weaponup.spr, weaponup.x, weaponup.y )
	gr.print(gaw*70, weaponup.x+20, weaponup.y-20)
gr.setColor(baseup.rgb, baseup.rgb, baseup.rgb, 255) 
	gr.draw( baseup.spr, baseup.x, baseup.y )
	gr.print(200, baseup.x+20, baseup.y-20)
gr.setColor(speedup.rgb, speedup.rgb, speedup.rgb, 255) 
	gr.draw( speedup.spr, speedup.x, speedup.y )
	gr.print(gas*40, speedup.x+20, speedup.y-20)

		gr.setColor(255, 255, 255, 255)
		gr.print("Money: ", 860, 20)
		gr.print(gom, 860 + bFont:getWidth("Money: "), 20)
		gr.print("Armor LvL: ", 860, 40)
		gr.print(gaa, 860 + bFont:getWidth("Armor LvL: "), 40)
		gr.print("Weapon LvL: ", 860, 60)
		gr.print(gaw, 860 + bFont:getWidth("Weapon LvL: "), 60)
		gr.print("Speed LvL: ", 1010, 20)
		gr.print(gas, 1010 + bFont:getWidth("Speed LvL: "), 20)
		gr.print("Unit Count: ", 1010, 60)
		gr.print(gan, 1010 + bFont:getWidth("Unit Count: "), 60)
end


function drawally()																-- Рисуем наших
gr.setColor(255, 255, 255, 255)
	for i,v in ipairs(allies) do
		gr.draw( bodysprite[v.armor], v.x-25, v.y-25, 0 , 1 , 1) 	-- Туловище
		for ii,vv in ipairs(v.boots) do
			gr.setColor(150, 150, 150, 255)
			gr.draw( bootssprite[v.speed], vv.x-vv.dx, vv.y-vv.dy) -- Наши ноги
			gr.setColor(255, 255, 255, 255)
			gr.draw( bootssprite[v.speed], vv.x+vv.dx, vv.y+vv.dy) -- Наши ноги
		end
		for iii,vvv in ipairs(v.arm) do -- Рука
			if v.order == "Attack" then
				gr.draw( weaponsprite[v.weapon], vvv.x, vvv.y, math.rad (vvv.ang), -1, 1, -38, 5  )
				gr.draw( armsprite, vvv.x, vvv.y, math.rad (vvv.ang), 1, 1, 45, 0  ) -- РУКА
			end
			if v.order == "Move" or v.order == "Stop" then
				gr.draw( weaponsprite[v.weapon], vvv.x + vvv.dx+4, vvv.y-3)
				gr.draw( armsprite, vvv.x + vvv.dx, vvv.y)
			end
		end
		gr.setFont(bFont)
		gr.print(v.hp, v.x, v.y - 50)
	end
for i,v in ipairs(kick) do
	gr.setColor(255, 255, 255, v.o)
	gr.draw( kicksprite, v.x-20 , v.y-20)
end
for i,v in ipairs(kill) do
	gr.setColor(255, 255, 255, v.o)
	gr.draw( killsprite, v.x-50 , v.y-50)
end
gr.setColor(255, 255, 255, 255)
end

function drawenemy()															-- Рисуем врагов
gr.setColor(255, 255, 255, 255)
	for i,v in ipairs(enemies) do
		gr.draw( bodysprite[v.armor], v.x+25, v.y-25, 0 , -1 , 1 ) 	-- Туловище
		for ii,vv in ipairs(v.boots) do
			gr.setColor(150, 150, 150, 255)
			gr.draw( bootssprite[v.speed], vv.x+vv.dx, vv.y+vv.dy, 0, -1, 1) -- Наши ноги
			gr.setColor(255, 255, 255, 255)
			gr.draw( bootssprite[v.speed], vv.x-vv.dx, vv.y-vv.dy, 0, -1, 1) -- Наши ноги
		end
		for iii,vvv in ipairs(v.arm) do -- Рука
			if v.order == "Attack" then
				gr.draw( weaponsprite[v.weapon], vvv.x, vvv.y, math.rad (vvv.ang), 1, 1, -38, 12  )
				gr.draw( armsprite, vvv.x, vvv.y, math.rad (vvv.ang), -1, 1, 45, 7  ) -- РУКА
			end
			if v.order == "Move" or v.order == "Stop" then
				gr.draw( weaponsprite[v.weapon], vvv.x - vvv.dx-5, vvv.y-3, 0, -1, 1)
				gr.draw( armsprite, vvv.x - vvv.dx, vvv.y, 0, -1, 1)
			end
		end
		gr.setFont(bFont)
		gr.print(v.hp, v.x, v.y - 50)
	end 
end



function spawnally ()															-- Спавн нашего
gan = gan + 1
   local ally = {}
   ally.t = {}
   ally.x = 110
   ally.y = gr.getHeight( ) - 50
   ally.speed = gas
   ally.armor = gaa
   ally.weapon = gaw
   ally.maxhp = gamhp
   ally.hp = ally.maxhp
   ally.order = "Move"
   ally.number = gan
   ally.boots = {}   
   ally.arm = {}
    local boots = {}
	 boots.x = 110
	 boots.dx = 0
	 boots.num = 10+ally.speed*8
	 boots.y = ally.y + 29
	 boots.dy = 1
	 table.insert(ally.boots, boots)
	local arm = {}
	 arm.x = ally.x
	 arm.y = ally.y + 5
	 arm.ang = 240
	 arm.dx = 0
	 arm.num = 1
	 arm.da = 1
	table.insert(ally.arm, arm)
   table.insert(allies, ally)
end

function spawnenemy()															-- Спавн врага
gen = gen + 1
   local enemy = {}
   enemy.t = {}
   enemy.x = gr.getWidth( ) - 110
   enemy.y = gr.getHeight( ) - 50
   enemy.speed = ges
   enemy.armor = gea
   enemy.weapon = gew
   enemy.maxhp = gemhp
   enemy.hp = enemy.maxhp
   enemy.order = "Move"
   enemy.number = gen
   enemy.boots = {}
   enemy.arm = {}
    local boots = {}
	 boots.x = enemy.x
	 boots.dx = 0
	 boots.num = 10+enemy.speed*8
	 boots.y = enemy.y+29
	 boots.dy = 1
	 table.insert(enemy.boots, boots)
	local arm = {}
	 arm.x = enemy.x
	 arm.y = enemy.y + 5
	 arm.ang = 300
	 arm.dx = 0
	 arm.num = 1
	 arm.da = 1
	table.insert(enemy.arm, arm)
   table.insert(enemies, enemy)
end



function allyactions()															-- Действия наших
	for i,v in ipairs(allies) do
		if v.order == "Move" then
			for o,a in ipairs(allies) do
				if a.number == v.number - 1 then
					v.dist = a.x - v.x
					if v.dist <=70 and v.dist >=0 then
						v.order = "Stop"
					end
				end
			end
			for u,e in ipairs(enemies) do
				if e.number == 1 then
					v.dist = e.x - v.x
					if v.dist <=70 and v.dist >=0 then
						v.order = "Attack"
					end
				end
			end
			v.dist = enemybase.x - v.x
			if v.dist <=120 then
				v.order = "Attack"
			end
			v.x = v.x + gdt * 15 * v.speed
			
			for ii,vv in ipairs(v.boots) do		-- Ноги
				vv.x = v.x-8
				vv.dx = vv.dx + vv.num*gdt
				vv.y = v.y + 25
				if vv.dx >= 17 then 
					vv.num = 0-(10+8*v.speed) 
					vv.dy = 2
				end
				if vv.dx <= -17 then 
					vv.num = 10+8*v.speed
					vv.dy = 0-2
				end
			end									-- Конец ног
			
			for iii,vvv in ipairs(v.arm) do 	-- Руки
				vvv.ang = 60
				vvv.x = v.x - 8
				vvv.dx = vvv.dx + vvv.num
				if vvv.dx >= 12 then vvv.num = (-20)*gdt end
				if vvv.dx <= -12 then vvv.num = 20*gdt end
			end									-- Конец рук
		end -- Конец Move
		
		if v.order == "Stop" then
			if v.number == 1 then
				v.order = "Move"
			end
			for o,a in ipairs(allies) do
				if a.number == v.number - 1 then
					v.dist = a.x - v.x
					if v.dist > 70 then
						v.order = "Move"
					end
				end
			end
			for ii,vv in ipairs(v.boots) do -- Ноги
				vv.x = v.x-8
				vv.dx = 12
				vv.dy = 2
			end
			for iii,vvv in ipairs(v.arm) do -- Руки
				vvv.ang = 60
				vvv.x = v.x - 8
				vvv.dx = 0
			end								-- Конец рук
		end -- Конец Stop
		
		if v.order == "Attack" then
		
		if gen == 0 then
			for iii,vvv in ipairs(v.arm) do -- Руки
			vvv.x = v.x
			vvv.ang = vvv.ang + vvv.da
			if vvv.ang >= 150 then 
				vvv.da = (-100) * gdt
					enemybase.hp = enemybase.hp - v.weapon
					local k = {}
					 k.o = 255
					 k.x = enemybase.x-15
					 k.y = v.y-15
					 table.insert(kick, k)
					if enemybase.hp <= 0 then
						local l = {}
						l.o = 255
						l.x = enemybase.x
						l.y = enemybase.y
						table.insert(kill, l)
						-- Победа
					end
			end
			if vvv.ang <= 90 then 
				vvv.da = 80 * gdt 
			end
			end	-- Конец Рук
		end
				
		if gen >= 1 then
			for ii,vv in ipairs(v.boots) do -- Ноги
				vv.x = v.x-8
				vv.dx = 12
				vv.dy = 2
			end	--Конец ног
			
			for iii,vvv in ipairs(v.arm) do -- Руки
				vvv.x = v.x
				vvv.ang = vvv.ang + vvv.da
				if vvv.ang >= 150 then 
					vvv.da = (-100) * gdt
					for o,e in ipairs(enemies) do
						if e.number == 1 then
							e.hp = e.hp - (v.weapon * 2 - e.armor)
							local k = {}
								k.o = 255
								k.x = e.x-15
								k.y = v.y-15
								table.insert(kick, k)
							if e.hp <= 0 then
								v.order = "Move"
								local l = {}
								l.o = 255
								l.x = e.x
								l.y = e.y
								table.insert(kill, l)
							end
						end
					end
				end	
				if vvv.ang <= 90 then 
					vvv.da = 80 * gdt 
				end	
			end -- Конец Рук
		end
			
		end -- Конец Attack		
	
		if v.hp <=0 then
			table.insert(remAlly, i)
		end
	end
end

function enemyactions()															-- Действия врагов
	for i,v in ipairs(enemies) do
		if v.order == "Move" then
			for o,a in ipairs(enemies) do
				if a.number == v.number - 1 then
					v.dist = v.x - a.x
					if v.dist <=70 and v.dist >=0 then
						v.order = "Stop"
					end
				end
			end
			for u,e in ipairs(allies) do
				if e.number == 1 then
					v.dist = v.x - e.x
					if v.dist <=70 and v.dist >=0 then
						v.order = "Attack"
					end
				end
			end
			v.dist = v.x - ourbase.x
			if v.dist <=120 then
				v.order = "Attack"
			end
			v.x = v.x - gdt * 15 * v.speed
			
			for ii,vv in ipairs(v.boots) do 	-- Ноги
				vv.x = v.x+8
				vv.dx = vv.dx + vv.num*gdt
				vv.y = v.y + 25
				if vv.dx >= 17 then 
					vv.num = 0-(10+8*v.speed) 
					vv.dy = -2
				end
				if vv.dx <= -17 then 
					vv.num = 10+8*v.speed
					vv.dy = 2
				end
			end
			for iii,vvv in ipairs(v.arm) do 	-- Руки
				vvv.ang = 300
				vvv.x = v.x + 8
				vvv.dx = vvv.dx + vvv.num
				if vvv.dx >= 12 then vvv.num = (-20)*gdt end
				if vvv.dx <= -12 then vvv.num = 20*gdt end
			end									-- Конец рук
		end -- Конец Move
		
		if v.order == "Stop" then
			if v.number == 1 then
				v.order = "Move"
			end
			for o,a in ipairs(enemies) do
				if a.number == v.number - 1 then
					v.dist = v.x - a.x
					if v.dist > 70 then
						v.order = "Move"
					end
				end
			end
			for ii,vv in ipairs(v.boots) do -- Ноги
				vv.x = v.x+8
				vv.dx = 12
				vv.dy = -2
			end
			for iii,vvv in ipairs(v.arm) do -- Руки
				vvv.ang = 300
				vvv.x = v.x + 8
				vvv.dx = 0
			end								-- Конец рук
			
		end -- Конец Stop
		
		if v.order == "Attack" then
		if gan == 0 then
			v.dist = v.x - ourbase.x
			if v.dist <=120 then
				for iii,vvv in ipairs(v.arm) do -- Руки
				vvv.x = v.x
				vvv.ang = vvv.ang + vvv.da
				if vvv.ang <= 210 then 
					vvv.da = 100 * gdt
					ourbase.hp = ourbase.hp - v.weapon
					local k = {}
					 k.o = 255
					 k.x = ourbase.x+15
					 k.y = v.y-15
					 table.insert(kick, k)
					if ourbase.hp <= 0 then
						local l = {}
						l.o = 255
						l.x = ourbase.x
						l.y = ourbase.y
						table.insert(kill, l)
						-- Пройгрышь
					end	
				end
				if vvv.ang >= 270 then 
					vvv.da = (-80) * gdt 
				end
				end
			else
				v.order = "Move"
			end
		end -- Конец Рук
		
		if gan >= 1 then
			for o,e in ipairs(allies) do
				if e.number == 1 then
					v.dist = v.x - e.x
					if v.dist >70 then
						v.order = "Move"
					end
				end
			end
			for ii,vv in ipairs(v.boots) do -- Ноги
				vv.x = v.x+8
				vv.dx = 12
				vv.dy = -2
			end	-- Конец ног
			
			for iii,vvv in ipairs(v.arm) do -- Руки
				vvv.x = v.x
				vvv.ang = vvv.ang + vvv.da
				if vvv.ang <= 210 then 
					vvv.da = 100 * gdt
					for o,e in ipairs(allies) do
						if e.number == 1 then
							v.dist = v.x - e.x
							if v.dist <=70 and v.dist >=0 then
								e.hp = e.hp - (v.weapon * 2 - e.armor)
								local k = {}
									k.o = 255
									k.x = e.x+15
									k.y = v.y-15
								table.insert(kick, k)
								if e.hp <= 0 then
									v.order = "Move"
									local l = {}
									l.o = 255
									l.x = e.x
									l.y = e.y
									table.insert(kill, l)
								end
							end

						end
					end
				end	
				if vvv.ang >= 270 then 
					vvv.da = (-80) * gdt 
				end	
			end -- Конец Рук
		end -- Конец Attack	
		end
		
		if v.hp <=0 then
			table.insert(remEnemy, v)
		end
		
		if v.x < ourbase.x + 120 then
			v.x = ourbase.x + 120
		end
	end
end



function love.keypressed( key )                               					-- Клавиши
   if key == "9" then
    spawnally()
   end   
   if key == "0" then
    spawnenemy()
   end 
   if key == "s" then
	if gas < 5 then	
		gas = gas + 1
		ges = ges + 1
	end
   end
   if key == "w" then
	if gaw < 3 then	
		gaw = gaw + 1
		gew = gew + 1
	end
   end
end 

function mouseevents() 
mX, mY = m.getPosition()
if love.mouse.isDown("l") then
	if mY >= 20 and mY <= 80 then
		if mX >= armorup.x and mX <= armorup.x + 60 then -- Armor
			if gaa < 5 then
				if gom >= 70 * gaa then
					gom = gom - 70*gaa
					gaa = gaa + 1
					gea = gea + 1
				end
			end
		end
		if mX >= hpup.x and mX <= hpup.x + 60 then -- Max HP
			if gamhp < 50 then
				if gom >= 50 * gamhp / 10 then
					gom = gom - 50*gamhp
					gamhp = gamhp + 10
					gemhp = gemhp + 10
				end
			end
		end
		if mX >= weaponup.x and mX <= weaponup.x + 60 then -- Weapon
			if gaw < 5 then
				if gom >= 70 * gaw then
					gom = gom - 70*gaw
					gaw = gaw + 1
					gew = gew + 1
				end
			end
		end
		if mX >= speedup.x and mX <= speedup.x + 60 then -- Speed
			if gas < 5 then
				if gom >= 40 * gaw then
					gom = gom - 40*gaw
					gas = gas + 1
					ges = ges + 1
				end
			end
		end
		if mX >= baseup.x and mX <= baseup.x + 60 then -- Base
			if ourbase.maxhp < 300 then
				if gom >= 200 * gaw then
					gobl = gobl + 1
					gom = gom - 200*gaw
					ourbase.maxhp = ourbase.maxhp + 150
					orbase.hp = orurbase.hp + 150
				end
			end
		end
		if mX >= 440 and mX <= 440+180 then -- Spawn
			if gom >= 10 then
				spawnally()
				gom = gom - 10
				local r = math.random (100)
				if r <90 then
					spawnenemy()
				end
			end
		end
		
	end
end
end