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.
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
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.
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.
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.
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.
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
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.