ovellas/entidades/pastor.lua

43 lines
1.5 KiB
Lua
Raw Normal View History

2022-05-28 00:05:01 +02:00
Pastor = {}
function Pastor:new(x, y)
self.sprite = love.graphics.newImage("img/pastor.png")
self.width = self.sprite:getWidth()
self.height = self.sprite:getHeight()
self.x = x-self.width/2
self.y = y-self.height/2
self.speed = 2
2022-06-27 23:48:45 +02:00
local width, height, flags = love.window.getMode();
self.body = love.physics.newBody(world, width/2, height/2, "dynamic")
self.shape = love.physics.newCircleShape(self.width/2+5)
self.fixture = love.physics.newFixture(self.body, self.shape, 1)
2022-05-28 00:05:01 +02:00
return self
end
function Pastor:update(dt)
local width, height, flags = love.window.getMode();
width = width - self.width
height = height - self.height
2022-06-27 23:48:45 +02:00
if (love.keyboard.isDown("up") or love.keyboard.isDown("k")) and self.y > 0 then
2022-05-28 00:05:01 +02:00
self.y = self.y - self.speed
end
2022-06-27 23:48:45 +02:00
if (love.keyboard.isDown("down") or love.keyboard.isDown("j")) and self.y < height then
2022-05-28 00:05:01 +02:00
self.y = self.y + self.speed
end
2022-06-27 23:48:45 +02:00
if (love.keyboard.isDown("left") or love.keyboard.isDown("h"))and self.x > 0 then
2022-05-28 00:05:01 +02:00
self.x = self.x - self.speed
end
2022-06-27 23:48:45 +02:00
if (love.keyboard.isDown("right") or love.keyboard.isDown("l")) and self.x < width then
2022-05-28 00:05:01 +02:00
self.x = self.x + self.speed
end
end
function Pastor:draw()
2022-06-27 23:48:45 +02:00
-- love.graphics.circle("fill", self.body:getX(), self.body:getY(), self.shape:getRadius())
love.graphics.circle("fill", self.x+self.width/2, self.y+self.height/2, self.shape:getRadius())
2022-05-28 00:05:01 +02:00
love.graphics.draw(self.sprite, self.x, self.y)
end