I want to make a topdown shooter thing?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
sladjkf
Prole
Posts: 18
Joined: Mon Mar 02, 2015 7:18 pm

I want to make a topdown shooter thing?

Post by sladjkf »

Hi people. I want to make a top down shooter thing, but I'm not really sure how to go about doing so. I looked at the tutorial about shooting at the mouse, but I don't understand all the trig in it. Could someone explain it to me? Also, how would you go about implementing a melee attack? As in, an attack in the direction of the mouse, but not ranged.

I'll put in the code that I copied from the "shooting at the mouse" tutorial.

Code: Select all

function love.load()
	player = {}
	player.x = 500
	player.y = 500
	bullets = {}
	bulletSpeed = 1000
end

function love.update(dt)
	if love.keyboard.isDown("a") then
		player.x = player.x - 300*dt
	end
	if love.keyboard.isDown("d") then
		player.x = player.x + 300*dt
	end
	if love.keyboard.isDown("w") then
		player.y = player.y - 300*dt
	end
	if love.keyboard.isDown("s") then
		player.y = player.y + 300*dt
	end
	for index,key in ipairs(bullets) do
        key.x = key.x + (key.dx * dt)
		key.y = key.y + (key.dy * dt)
    end
end

function love.mousepressed(x, y, button)
    if button == "l" then
        local startX = player.x + 40 / 2
        local startY = player.y + 40 / 2
        local mouseX = x
        local mouseY = y

        local angle = math.atan2((mouseY - startY), (mouseX - startX))

        local bulletDx = bulletSpeed * math.cos(angle)
        local bulletDy = bulletSpeed * math.sin(angle)

        table.insert(bullets, {x = startX, y = startY, dx = bulletDx, dy = bulletDy})
    end
end

function love.draw()
	love.graphics.rectangle("fill",player.x,player.y,40,40)
	 for i,v in ipairs(bullets) do
        love.graphics.circle("fill", v.x, v.y, 10)
    end
end
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: I want to make a topdown shooter thing?

Post by s-ol »

http://www.raywenderlich.com/35866/trig ... ing-part-1

I can't find it, but I am sure there was a guide for atan2, which is what you will probably need in order to look at the mouse.

Melee is simply checking whether something that has:

distanceTo(enemy) < attackDistance
rotation - attackAngle < angleTo(enemy) < rotation + attackAngle

if you get what I mean.

angleTo basically is atan2 again, attackAngle is half of the angle a melee swing covers, attackDistance is how far it goes.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
sladjkf
Prole
Posts: 18
Joined: Mon Mar 02, 2015 7:18 pm

Re: I want to make a topdown shooter thing?

Post by sladjkf »

I've got another question: How do you change the player sprite when the mouse is at a certain x and y relative to the x and y of the player?
EDIT:nvm, I think I got it.
User avatar
UndeadWaffles
Prole
Posts: 2
Joined: Mon Mar 02, 2015 11:52 pm

Re: I want to make a topdown shooter thing?

Post by UndeadWaffles »

sladjkf wrote:I've got another question: How do you change the player sprite when the mouse is at a certain x and y relative to the x and y of the player?
EDIT:nvm, I think I got it.
You solved your issue without posting a solution?! Tell us what you did! I would like to know.
User avatar
sladjkf
Prole
Posts: 18
Joined: Mon Mar 02, 2015 7:18 pm

Re: I want to make a topdown shooter thing?

Post by sladjkf »

I put this snippet in love.update().
I was using rectangles, so I had to set colors. I don't have any sprites and I can't art.
Basically, what I did was calculate the angle of the mouse and player and then switch the color based on that.

Code: Select all

	local mousex, mousey = love.mouse.getPosition()
	local playercenter = {x=(player.x + 40/2), y=(player.y + 40/2)}
	local angle = (math.atan2((mousey - playercenter.y), (mousex - playercenter.x)))*(180/math.pi)
	if angle <= 0 and angle >= -90 then --1
		colors.red = 255
		colors.blue = 0
		colors.green = 0
	elseif angle >= 0 and angle <= 90 then --2
		colors.green = 100
		colors.red = 100
		colors.blue =0
	elseif angle <= -90 and angle >= -180 then --3
		colors.blue = 255
		colors.red = 0
		colors.green = 0
	elseif angle <= 180 and angle >= 90 then --4
		colors.green = 255
		colors.red = 0
		colors.blue = 0
	end
User avatar
sladjkf
Prole
Posts: 18
Joined: Mon Mar 02, 2015 7:18 pm

Re: I want to make a topdown shooter thing?

Post by sladjkf »

Speaking of sprites, I really need some. Anyone know where I could get some sort of sprites that aren't quite top down but sort of top down? I wouldn't quite say isometric, but something more like Nuclear Throne or maybe Hammerwatch.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: I want to make a topdown shooter thing?

Post by micha »

You can try your luck on opengameart.org.
User avatar
nfey
Citizen
Posts: 63
Joined: Tue Feb 18, 2014 9:50 am
Location: Romania

Re: I want to make a topdown shooter thing?

Post by nfey »

sladjkf wrote:I wouldn't quite say isometric, but something more like Nuclear Throne or maybe Hammerwatch.
I admire the enthusiasm but you'll soon learn that, when looking for free stuff, you have to settle for what you get :P
If you're looking for a certain art style you're most likely going to need to contact an actual artist and collaborate with him.
Post Reply

Who is online

Users browsing this forum: Google Adsense [Bot] and 4 guests