Browse Source

enemies follow player

main
Stephanie Gredell 2 years ago
parent
commit
26667292f9
  1. 27
      Chat.lua
  2. 12
      Game.lua
  3. 2
      Player.lua
  4. BIN
      assets/f1065sk1.pdf
  5. 2
      main.lua

27
Chat.lua

@ -4,8 +4,9 @@ local lg = love.graphics @@ -4,8 +4,9 @@ local lg = love.graphics
function Chat(x, y, level)
local CHAT_SPEED = math.random(50) + (level * 2)
local CHAT_SPEED = math.random(30) + (level * 2)
local vel = -1
local life = 5
if math.random() < 0.5 then
vel = 1
@ -18,27 +19,27 @@ function Chat(x, y, level) @@ -18,27 +19,27 @@ function Chat(x, y, level)
x_vel = math.random() * CHAT_SPEED * vel,
y_vel = math.random() * CHAT_SPEED * vel,
radius = 30,
life = 5,
draw = function(self)
lg.draw(self.sprite, self.x, self.y, 0, 1, 1, self.sprite:getWidth() / 2, self.sprite:getHeight() / 2)
lg.circle("line", self.x, self.y, self.radius)
end,
move = function(self, dt)
self.x = self.x + self.x_vel * dt
self.y = self.y + self.y_vel * dt
move = function(self, player_x, player_y, dt)
-- direction is the vector from the enemy to the player
local direction_x = player_x - self.x
local direction_y = player_y - self.y
local distance = math.sqrt(direction_x * direction_x + direction_y * direction_y)
if self.x + self.radius < 0 then
self.x = love.graphics.getWidth() + self.radius
elseif self.x - self.radius > love.graphics.getWidth() then
self.x = -self.radius
if dist ~= 0 then -- avoid division by zero
self.x = self.x + direction_x / distance * CHAT_SPEED * dt
self.y = self.y + direction_y / distance * CHAT_SPEED * dt
end
end,
if self.y + self.radius < 0 then
self.y = love.graphics.getHeight() + self.radius
elseif self.y - self.radius > love.graphics.getHeight() then
self.y = -self.radius
end
decreaseLife = function(self)
self.life = self.life - 1
end,
destroy = function(self, chat_table, index)

12
Game.lua

@ -24,13 +24,11 @@ function Game() @@ -24,13 +24,11 @@ function Game()
chats = {}
local chat_x = math.floor(math.random(love.graphics.getWidth() ))
local chat_y = math.floor(math.random(love.graphics.getHeight()))
table.insert(chats, 1, Chat(chat_x, chat_y, self.level))
table.insert(chats, 2, Chat(chat_x, chat_y, self.level))
table.insert(chats, 3, Chat(chat_x, chat_y, self.level))
table.insert(chats, 4, Chat(chat_x, chat_y, self.level))
for i = 1, 1 do
local chat_x = math.floor(math.random(love.graphics.getWidth()))
local chat_y = math.floor(math.random(love.graphics.getHeight()))
table.insert(chats, i, Chat(chat_x, chat_y, self.level))
end
end
}

2
Player.lua

@ -39,7 +39,6 @@ function Player(debugging) @@ -39,7 +39,6 @@ function Player(debugging)
end
end,
destroyBan = function (self, index)
print(self.ban, index)
table.remove(self.bans, index)
end,
@ -50,7 +49,6 @@ function Player(debugging) @@ -50,7 +49,6 @@ function Player(debugging)
if love.keyboard.isDown('left') then
self.angle = self.angle - self.rotation_speed * dt
self.facing = self.angle
print(self.facing)
end
if love.keyboard.isDown('right') then
self.angle = self.angle + self.rotation_speed * dt

BIN
assets/f1065sk1.pdf

Binary file not shown.

2
main.lua

@ -64,7 +64,7 @@ function love.update(dt) @@ -64,7 +64,7 @@ function love.update(dt)
end
end
chat:move(dt)
chat:move(player.x, player.y, dt)
end
elseif game.state.menu then

Loading…
Cancel
Save